avutil/buffer: factor buffer_replace() out
authorMichael Niedermayer <michaelni@gmx.at>
Wed, 14 Jan 2015 23:40:35 +0000 (00:40 +0100)
committerMichael Niedermayer <michaelni@gmx.at>
Fri, 23 Jan 2015 03:45:05 +0000 (04:45 +0100)
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
libavutil/buffer.c

index c881d1a..ca102fd 100644 (file)
@@ -103,14 +103,17 @@ AVBufferRef *av_buffer_ref(AVBufferRef *buf)
     return ret;
 }
 
-void av_buffer_unref(AVBufferRef **buf)
+static void buffer_replace(AVBufferRef **dst, AVBufferRef **src)
 {
     AVBuffer *b;
 
-    if (!buf || !*buf)
-        return;
-    b = (*buf)->buffer;
-    av_freep(buf);
+    b = (*dst)->buffer;
+
+    if (src) {
+        **dst = **src;
+        av_freep(src);
+    } else
+        av_freep(dst);
 
     if (!avpriv_atomic_int_add_and_fetch(&b->refcount, -1)) {
         b->free(b->opaque, b->data);
@@ -118,6 +121,14 @@ void av_buffer_unref(AVBufferRef **buf)
     }
 }
 
+void av_buffer_unref(AVBufferRef **buf)
+{
+    if (!buf || !*buf)
+        return;
+
+    buffer_replace(buf, NULL);
+}
+
 int av_buffer_is_writable(const AVBufferRef *buf)
 {
     if (buf->buffer->flags & AV_BUFFER_FLAG_READONLY)