diff mbox series

[FFmpeg-devel,1/4] avformat/aviobuf: Avoid allocation when using dynamic buffer

Message ID AM7PR03MB66605A0988E1C731154F51CB8FF19@AM7PR03MB6660.eurprd03.prod.outlook.com
State Superseded
Headers show
Series [FFmpeg-devel,1/4] avformat/aviobuf: Avoid allocation when using dynamic buffer | expand

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished
andriy/PPC64_make warning Make failed

Commit Message

Andreas Rheinhardt Aug. 4, 2021, 4:21 p.m. UTC
This can be achieved by allocating the AVIOContext and
the dynamic buffer's opaque and internal write buffer together.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavformat/aviobuf.c | 22 +++++++++-------------
 1 file changed, 9 insertions(+), 13 deletions(-)

Comments

Nicolas George Aug. 4, 2021, 4:37 p.m. UTC | #1
Andreas Rheinhardt (12021-08-04):
> Putting child_next besides child_class_iterate is actually nicer.
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
> ---
>  libavutil/log.h | 10 +++++-----
>  1 file changed, 5 insertions(+), 5 deletions(-)

Same as for AVCodec: if we break ABI, might as well order fields by
size.

Regards,
diff mbox series

Patch

diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c
index 66fddf1433..e92b3dc610 100644
--- a/libavformat/aviobuf.c
+++ b/libavformat/aviobuf.c
@@ -1339,22 +1339,21 @@  static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
 
 static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
 {
+    struct { AVIOContext pb; DynBuffer d; } *ret;
     DynBuffer *d;
     unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
 
-    if (sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
+    if (sizeof(*ret) + io_buffer_size < io_buffer_size)
         return AVERROR(ERANGE);
-    d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
-    if (!d)
+    ret = av_mallocz(sizeof(*ret) + io_buffer_size);
+    if (!ret)
         return AVERROR(ENOMEM);
+    d = &ret->d;
     d->io_buffer_size = io_buffer_size;
-    *s = avio_alloc_context(d->io_buffer, d->io_buffer_size, 1, d, NULL,
-                            max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
-                            max_packet_size ? NULL : dyn_buf_seek);
-    if(!*s) {
-        av_free(d);
-        return AVERROR(ENOMEM);
-    }
+    ffio_init_context(&ret->pb, d->io_buffer, d->io_buffer_size, 1, d, NULL,
+                      max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
+                      max_packet_size ? NULL : dyn_buf_seek);
+    *s = &ret->pb;
     (*s)->max_packet_size = max_packet_size;
     return 0;
 }
@@ -1427,7 +1426,6 @@  int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
     d = s->opaque;
     *pbuffer = d->buffer;
     size = d->size;
-    av_free(d);
 
     avio_context_free(&s);
 
@@ -1443,7 +1441,6 @@  void ffio_free_dyn_buf(AVIOContext **s)
 
     d = (*s)->opaque;
     av_free(d->buffer);
-    av_free(d);
     avio_context_free(s);
 }
 
@@ -1475,7 +1472,6 @@  int ffio_close_null_buf(AVIOContext *s)
     avio_flush(s);
 
     size = d->size;
-    av_free(d);
 
     avio_context_free(&s);