[0.11] gstreamer: memory: improve semantics of unmap

Wim Taymans wtay at kemper.freedesktop.org
Thu Jan 5 21:47:10 PST 2012


Module: gstreamer
Branch: 0.11
Commit: e4a58ec71e4cf92995cdc7621f49d3c2f2f30379
URL:    http://cgit.freedesktop.org/gstreamer/gstreamer/commit/?id=e4a58ec71e4cf92995cdc7621f49d3c2f2f30379

Author: Wim Taymans <wim.taymans at collabora.co.uk>
Date:   Fri Jan  6 06:43:08 2012 +0100

memory: improve semantics of unmap

Make an unmap call with a different data pointer than the map call update the
offset field. This allows for both offset and size adjustements in the unmap
call.

---

 gst/gstmemory.c             |   12 ++++++------
 tests/check/gst/gstmemory.c |   40 ++++++++++++++++++++++++++++++++++++----
 2 files changed, 42 insertions(+), 10 deletions(-)

diff --git a/gst/gstmemory.c b/gst/gstmemory.c
index 1cdbf26..923bd2f 100644
--- a/gst/gstmemory.c
+++ b/gst/gstmemory.c
@@ -217,13 +217,10 @@ _default_mem_unmap (GstMemoryDefault * mem, gpointer data, gsize size)
   g_return_val_if_fail ((guint8 *) data >= mem->data
       && (guint8 *) data < mem->data + mem->maxsize, FALSE);
 
-  if (size != -1) {
-    /* check if resize happened or unmap was called with different data */
-    if (mem->data + mem->offset != data) {
-      /* adjust the size */
-      size = (guint8 *) data - mem->data + size - mem->offset;
-    }
+  if (mem->data + mem->offset != data)
+    mem->offset = (guint8 *) data - mem->data;
 
+  if (size != -1) {
     g_return_val_if_fail (mem->offset + size <= mem->maxsize, FALSE);
     mem->size = size;
   }
@@ -487,6 +484,9 @@ gst_memory_map (GstMemory * mem, gsize * size, gsize * maxsize,
  * the memory to @size. @size can be set to -1 when the size should not be
  * updated.
  *
+ * It is possible to pass a different @data than that obtained from
+ * gst_memory_map() in which case the offset of @mem will be updated.
+ *
  * Returns: TRUE when the memory was release successfully.
  */
 gboolean
diff --git a/tests/check/gst/gstmemory.c b/tests/check/gst/gstmemory.c
index 79e27c2..1cc4f9b 100644
--- a/tests/check/gst/gstmemory.c
+++ b/tests/check/gst/gstmemory.c
@@ -479,7 +479,7 @@ GST_END_TEST;
 GST_START_TEST (test_map_resize)
 {
   GstMemory *mem;
-  gsize size, maxsize;
+  gsize size, maxsize, maxalloc, offset;
   gpointer data;
 
   mem = gst_allocator_alloc (NULL, 100, 0);
@@ -490,10 +490,42 @@ GST_START_TEST (test_map_resize)
   fail_unless (size == 100);
 
   /* resize the buffer */
-  gst_memory_resize (mem, 1, maxsize - 1);
+  gst_memory_resize (mem, 1, size - 1);
+  size = gst_memory_get_sizes (mem, &offset, &maxalloc);
+  fail_unless (size == 99);
+  fail_unless (offset == 1);
+  fail_unless (maxalloc >= 100);
 
-  /* unmap the buffer with original pointer and size */
-  gst_memory_unmap (mem, data, maxsize);
+  /* unmap the buffer with original pointer and size, should restore the offset
+   * and size */
+  gst_memory_unmap (mem, data, 100);
+
+  size = gst_memory_get_sizes (mem, &offset, &maxalloc);
+  fail_unless (size == 100);
+  fail_unless (offset == 0);
+  fail_unless (maxalloc >= 100);
+
+  data = gst_memory_map (mem, &size, &maxsize, GST_MAP_READ);
+  fail_unless (data != NULL);
+  fail_unless (size == 100);
+  fail_unless (maxsize >= 100);
+
+  /* resize the buffer with unmap */
+  gst_memory_unmap (mem, (guint8 *) data + 1, 99);
+
+  size = gst_memory_get_sizes (mem, &offset, &maxalloc);
+  fail_unless (size == 99);
+  fail_unless (offset == 1);
+  fail_unless (maxalloc >= 100);
+
+  /* and larger */
+  data = gst_memory_map (mem, &size, &maxsize, GST_MAP_READ);
+  gst_memory_unmap (mem, (guint8 *) data - 1, 100);
+
+  size = gst_memory_get_sizes (mem, &offset, &maxalloc);
+  fail_unless (size == 100);
+  fail_unless (offset == 0);
+  fail_unless (maxalloc >= 100);
 
   gst_memory_unref (mem);
 }



More information about the gstreamer-commits mailing list