summaryrefslogtreecommitdiff
path: root/libavutil/buffer.c
diff options
context:
space:
mode:
authorAnton Khirnov <anton@khirnov.net>2020-06-05 11:17:49 +0200
committerAnton Khirnov <anton@khirnov.net>2020-09-28 11:33:35 +0200
commit68918d3b7fda9e75d436a27705c54b77dcb41ba9 (patch)
tree66fea07125274cf4107ebc07b9dacfeb517f2359 /libavutil/buffer.c
parent5bbf58ab876279ca1a5a2f30563f271c99b93e62 (diff)
lavu/buffer: add a convenience function for replacing buffers
A common pattern e.g. in libavcodec is replacing/updating buffer references: unref old one, ref new one. This function allows simplifying such code and avoiding unnecessary refs+unrefs if the references are already equivalent.
Diffstat (limited to 'libavutil/buffer.c')
-rw-r--r--libavutil/buffer.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/libavutil/buffer.c b/libavutil/buffer.c
index 08e6079139..d67b4bbdaf 100644
--- a/libavutil/buffer.c
+++ b/libavutil/buffer.c
@@ -216,6 +216,32 @@ int av_buffer_realloc(AVBufferRef **pbuf, int size)
return 0;
}
+int av_buffer_replace(AVBufferRef **pdst, AVBufferRef *src)
+{
+ AVBufferRef *dst = *pdst;
+ AVBufferRef *tmp;
+
+ if (!src) {
+ av_buffer_unref(pdst);
+ return 0;
+ }
+
+ if (dst && dst->buffer == src->buffer) {
+ /* make sure the data pointers match */
+ dst->data = src->data;
+ dst->size = src->size;
+ return 0;
+ }
+
+ tmp = av_buffer_ref(src);
+ if (!tmp)
+ return AVERROR(ENOMEM);
+
+ av_buffer_unref(pdst);
+ *pdst = tmp;
+ return 0;
+}
+
AVBufferPool *av_buffer_pool_init2(int size, void *opaque,
AVBufferRef* (*alloc)(void *opaque, int size),
void (*pool_free)(void *opaque))