buffer: make new _buffer_allocate method

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.
This commit is contained in:
Wim Taymans 2011-06-10 13:40:57 +02:00
parent 30b3e90117
commit b27ee30a35
10 changed files with 29 additions and 15 deletions

View file

@ -211,7 +211,7 @@ _memory_add (GstBuffer * buffer, GstMemory * mem)
GST_BUFFER_MEM_LEN (buffer) = len + 1; GST_BUFFER_MEM_LEN (buffer) = len + 1;
} }
#if 1 #if 0
/* buffer alignment in bytes - 1 /* buffer alignment in bytes - 1
* an alignment of 7 would be the same as malloc() guarantees * an alignment of 7 would be the same as malloc() guarantees
*/ */
@ -233,10 +233,12 @@ _gst_buffer_initialize (void)
{ {
if (G_LIKELY (_gst_buffer_type == 0)) { if (G_LIKELY (_gst_buffer_type == 0)) {
_gst_buffer_type = gst_mini_object_register ("GstBuffer"); _gst_buffer_type = gst_mini_object_register ("GstBuffer");
#if 0
#ifdef HAVE_GETPAGESIZE #ifdef HAVE_GETPAGESIZE
#ifdef BUFFER_ALIGNMENT_PAGESIZE #ifdef BUFFER_ALIGNMENT_PAGESIZE
_gst_buffer_data_alignment = getpagesize () - 1; _gst_buffer_data_alignment = getpagesize () - 1;
#endif #endif
#endif
#endif #endif
} }
} }
@ -421,6 +423,7 @@ _gst_buffer_free (GstBuffer * buffer)
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
gst_memory_unref (GST_BUFFER_MEM_PTR (buffer, 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) if (msize)
g_slice_free1 (msize, buffer); 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. * @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. If * Tries to create a newly allocated buffer with data of the given size and
* the requested amount of memory can't be allocated, NULL will be returned. * alignment from @allocator. If the requested amount of memory can't be
* The allocated buffer memory is not cleared. * allocated, NULL will be returned. The allocated buffer memory is not cleared.
*
* When @allocator is NULL, the default memory allocator will be used.
*
* 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. * Note that when @size == 0, the buffer will not have memory associated with it.
* *
@ -485,7 +494,8 @@ gst_buffer_new (void)
* be allocated. * be allocated.
*/ */
GstBuffer * GstBuffer *
gst_buffer_new_and_alloc (guint size) gst_buffer_new_allocate (GstMemoryAllocator * allocator, gsize size,
gsize align)
{ {
GstBuffer *newbuf; GstBuffer *newbuf;
GstMemory *mem; GstMemory *mem;
@ -496,7 +506,7 @@ gst_buffer_new_and_alloc (guint size)
#if 1 #if 1
if (size > 0) { 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)) if (G_UNLIKELY (mem == NULL))
goto no_memory; goto no_memory;
} else { } else {

View file

@ -226,7 +226,7 @@ struct _GstBuffer {
/* allocation */ /* allocation */
GstBuffer * gst_buffer_new (void); 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 */ /* memory blocks */
guint gst_buffer_n_memory (GstBuffer *buffer); guint gst_buffer_n_memory (GstBuffer *buffer);

View 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_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 #ifndef GST_DISABLE_DEPRECATED
#endif /* not GST_DISABLE_DEPRECATED */ #endif /* not GST_DISABLE_DEPRECATED */

View file

@ -1789,7 +1789,7 @@ gst_value_deserialize_buffer (GValue * dest, const gchar * s)
if (len & 1) if (len & 1)
goto wrong_length; 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); data = gst_buffer_map (buffer, &size, NULL, GST_MAP_WRITE);
for (i = 0; i < len / 2; i++) { for (i = 0; i < len / 2; i++) {

View file

@ -1259,7 +1259,7 @@ gst_base_src_default_create (GstBaseSrc * src, guint64 offset,
if (G_UNLIKELY (!bclass->fill)) if (G_UNLIKELY (!bclass->fill))
goto no_function; goto no_function;
buf = gst_buffer_new_and_alloc (size); buf = gst_buffer_new_allocate (NULL, size, 0);
if (G_UNLIKELY (buf == NULL)) if (G_UNLIKELY (buf == NULL))
goto alloc_failed; goto alloc_failed;

View file

@ -1406,7 +1406,7 @@ gst_base_transform_prepare_output_buffer (GstBaseTransform * trans,
ret = gst_buffer_pool_acquire_buffer (priv->srcpool, out_buf, NULL); ret = gst_buffer_pool_acquire_buffer (priv->srcpool, out_buf, NULL);
} else { } else {
GST_DEBUG_OBJECT (trans, "doing alloc of size %u", outsize); 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);
} }
} }

View file

@ -461,7 +461,8 @@ gst_dp_buffer_from_header (guint header_length, const guint8 * header)
GST_DP_PAYLOAD_BUFFER, NULL); GST_DP_PAYLOAD_BUFFER, NULL);
buffer = 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_TIMESTAMP (buffer) = GST_DP_HEADER_TIMESTAMP (header);
GST_BUFFER_DURATION (buffer) = GST_DP_HEADER_DURATION (header); GST_BUFFER_DURATION (buffer) = GST_DP_HEADER_DURATION (header);

View file

@ -460,7 +460,7 @@ gst_fake_src_alloc_parent (GstFakeSrc * src)
{ {
GstBuffer *buf; GstBuffer *buf;
buf = gst_buffer_new_and_alloc (src->parentsize); buf = gst_buffer_new_allocate (NULL, src->parentsize, 0);
src->parent = buf; src->parent = buf;
src->parentoffset = 0; src->parentoffset = 0;

View file

@ -442,7 +442,7 @@ gst_fd_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
blocksize = GST_BASE_SRC (src)->blocksize; blocksize = GST_BASE_SRC (src)->blocksize;
/* create the buffer */ /* create the buffer */
buf = gst_buffer_new_and_alloc (blocksize); buf = gst_buffer_new_allocate (NULL, blocksize, 0);
if (G_UNLIKELY (buf == NULL)) if (G_UNLIKELY (buf == NULL))
goto alloc_failed; goto alloc_failed;

View file

@ -1138,7 +1138,7 @@ gst_queue2_create_read (GstQueue2 * queue, guint64 offset, guint length,
GstFlowReturn ret = GST_FLOW_OK; GstFlowReturn ret = GST_FLOW_OK;
/* allocate the output buffer of the requested size */ /* 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); data = gst_buffer_map (buf, NULL, NULL, GST_MAP_WRITE);
GST_DEBUG_OBJECT (queue, "Reading %u bytes from %" G_GUINT64_FORMAT, length, GST_DEBUG_OBJECT (queue, "Reading %u bytes from %" G_GUINT64_FORMAT, length,