buffer: make new _buffer_allocate method
authorWim Taymans <wim.taymans@collabora.co.uk>
Fri, 10 Jun 2011 11:40:57 +0000 (13:40 +0200)
committerWim Taymans <wim.taymans@collabora.co.uk>
Fri, 10 Jun 2011 11:49:23 +0000 (13:49 +0200)
Make a new method to allocate a buffer + memory that takes the allocator and the
alignment as parameters. Provide a macro for the old method but prefer to use
the new method to encourage plugins to negotiate the allocator properly.

gst/gstbuffer.c
gst/gstbuffer.h
gst/gstcompat.h
gst/gstvalue.c
libs/gst/base/gstbasesrc.c
libs/gst/base/gstbasetransform.c
libs/gst/dataprotocol/dataprotocol.c
plugins/elements/gstfakesrc.c
plugins/elements/gstfdsrc.c
plugins/elements/gstqueue2.c

index 870313f..c1778c7 100644 (file)
@@ -211,7 +211,7 @@ _memory_add (GstBuffer * buffer, GstMemory * mem)
   GST_BUFFER_MEM_LEN (buffer) = len + 1;
 }
 
-#if 1
+#if 0
 /* buffer alignment in bytes - 1
  * an alignment of 7 would be the same as malloc() guarantees
  */
@@ -233,11 +233,13 @@ _gst_buffer_initialize (void)
 {
   if (G_LIKELY (_gst_buffer_type == 0)) {
     _gst_buffer_type = gst_mini_object_register ("GstBuffer");
+#if 0
 #ifdef HAVE_GETPAGESIZE
 #ifdef BUFFER_ALIGNMENT_PAGESIZE
     _gst_buffer_data_alignment = getpagesize () - 1;
 #endif
 #endif
+#endif
   }
 }
 
@@ -421,6 +423,7 @@ _gst_buffer_free (GstBuffer * buffer)
   for (i = 0; i < len; i++)
     gst_memory_unref (GST_BUFFER_MEM_PTR (buffer, i));
 
+  /* we set msize to 0 when the buffer is part of the memory block */
   if (msize)
     g_slice_free1 (msize, buffer);
 }
@@ -470,12 +473,18 @@ gst_buffer_new (void)
 }
 
 /**
- * gst_buffer_new_and_alloc:
+ * gst_buffer_new_allocate:
+ * @allocator: the #GstMemoryAllocator to use
  * @size: the size in bytes of the new buffer's data.
+ * @align: the alignment of the buffer memory
+ *
+ * Tries to create a newly allocated buffer with data of the given size and
+ * alignment from @allocator. If the requested amount of memory can't be
+ * allocated, NULL will be returned. The allocated buffer memory is not cleared.
+ *
+ * When @allocator is NULL, the default memory allocator will be used.
  *
- * Tries to create a newly allocated buffer with data of the given size. If
- * the requested amount of memory can't be allocated, NULL will be returned.
- * The allocated buffer memory is not cleared.
+ * Allocator buffer memory will be aligned to multiples of (@align + 1) bytes.
  *
  * Note that when @size == 0, the buffer will not have memory associated with it.
  *
@@ -485,7 +494,8 @@ gst_buffer_new (void)
  *     be allocated.
  */
 GstBuffer *
-gst_buffer_new_and_alloc (guint size)
+gst_buffer_new_allocate (GstMemoryAllocator * allocator, gsize size,
+    gsize align)
 {
   GstBuffer *newbuf;
   GstMemory *mem;
@@ -496,7 +506,7 @@ gst_buffer_new_and_alloc (guint size)
 
 #if 1
   if (size > 0) {
-    mem = gst_memory_allocator_alloc (NULL, size, _gst_buffer_data_alignment);
+    mem = gst_memory_allocator_alloc (allocator, size, align);
     if (G_UNLIKELY (mem == NULL))
       goto no_memory;
   } else {
index dcd5d2e..c11edd2 100644 (file)
@@ -226,7 +226,7 @@ struct _GstBuffer {
 
 /* allocation */
 GstBuffer * gst_buffer_new                 (void);
-GstBuffer * gst_buffer_new_and_alloc       (guint size);
+GstBuffer * gst_buffer_new_allocate        (GstMemoryAllocator * allocator, gsize maxsize, gsize align);
 
 /* memory blocks */
 guint       gst_buffer_n_memory            (GstBuffer *buffer);
index 8d0c457..cba5fc2 100644 (file)
@@ -49,6 +49,9 @@ G_BEGIN_DECLS
 
 //#define gst_buffer_create_sub(b,o,s)           gst_buffer_copy_region(b,GST_BUFFER_COPY_ALL,o,s)
 
+#define gst_buffer_new_and_alloc(s)            gst_buffer_new_allocate(NULL, s, 0)
+
+
 #ifndef GST_DISABLE_DEPRECATED
 
 #endif /* not GST_DISABLE_DEPRECATED */
index c1fc50a..2fb5088 100644 (file)
@@ -1789,7 +1789,7 @@ gst_value_deserialize_buffer (GValue * dest, const gchar * s)
   if (len & 1)
     goto wrong_length;
 
-  buffer = gst_buffer_new_and_alloc (len / 2);
+  buffer = gst_buffer_new_allocate (NULL, len / 2, 0);
   data = gst_buffer_map (buffer, &size, NULL, GST_MAP_WRITE);
 
   for (i = 0; i < len / 2; i++) {
index d21e7c6..6b0165c 100644 (file)
@@ -1259,7 +1259,7 @@ gst_base_src_default_create (GstBaseSrc * src, guint64 offset,
   if (G_UNLIKELY (!bclass->fill))
     goto no_function;
 
-  buf = gst_buffer_new_and_alloc (size);
+  buf = gst_buffer_new_allocate (NULL, size, 0);
   if (G_UNLIKELY (buf == NULL))
     goto alloc_failed;
 
index 9af756d..b8c9d87 100644 (file)
@@ -1406,7 +1406,7 @@ gst_base_transform_prepare_output_buffer (GstBaseTransform * trans,
       ret = gst_buffer_pool_acquire_buffer (priv->srcpool, out_buf, NULL);
     } else {
       GST_DEBUG_OBJECT (trans, "doing alloc of size %u", outsize);
-      *out_buf = gst_buffer_new_and_alloc (outsize);
+      *out_buf = gst_buffer_new_allocate (NULL, outsize, 0);
     }
   }
 
index 8b32bb8..e924ea5 100644 (file)
@@ -461,7 +461,8 @@ gst_dp_buffer_from_header (guint header_length, const guint8 * header)
       GST_DP_PAYLOAD_BUFFER, NULL);
 
   buffer =
-      gst_buffer_new_and_alloc ((guint) GST_DP_HEADER_PAYLOAD_LENGTH (header));
+      gst_buffer_new_allocate (NULL,
+      (guint) GST_DP_HEADER_PAYLOAD_LENGTH (header), 0);
 
   GST_BUFFER_TIMESTAMP (buffer) = GST_DP_HEADER_TIMESTAMP (header);
   GST_BUFFER_DURATION (buffer) = GST_DP_HEADER_DURATION (header);
index 27d8004..afe05fe 100644 (file)
@@ -460,7 +460,7 @@ gst_fake_src_alloc_parent (GstFakeSrc * src)
 {
   GstBuffer *buf;
 
-  buf = gst_buffer_new_and_alloc (src->parentsize);
+  buf = gst_buffer_new_allocate (NULL, src->parentsize, 0);
 
   src->parent = buf;
   src->parentoffset = 0;
index 0082327..eea0af8 100644 (file)
@@ -442,7 +442,7 @@ gst_fd_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
   blocksize = GST_BASE_SRC (src)->blocksize;
 
   /* create the buffer */
-  buf = gst_buffer_new_and_alloc (blocksize);
+  buf = gst_buffer_new_allocate (NULL, blocksize, 0);
   if (G_UNLIKELY (buf == NULL))
     goto alloc_failed;
 
index 0b7ca8c..798c17a 100644 (file)
@@ -1138,7 +1138,7 @@ gst_queue2_create_read (GstQueue2 * queue, guint64 offset, guint length,
   GstFlowReturn ret = GST_FLOW_OK;
 
   /* allocate the output buffer of the requested size */
-  buf = gst_buffer_new_and_alloc (length);
+  buf = gst_buffer_new_allocate (NULL, length, 0);
   data = gst_buffer_map (buf, NULL, NULL, GST_MAP_WRITE);
 
   GST_DEBUG_OBJECT (queue, "Reading %u bytes from %" G_GUINT64_FORMAT, length,