Commit 7b0e4f27 authored by Wim Taymans's avatar Wim Taymans

memory: make _new_wrapped take user_data and notify

Make it possible to configure a GDestroyNotify and user_data for
gst_memory_new_wrapped() this allows for more flexible wrapping of foreign
memory blocks.
parent 654b0744
......@@ -615,7 +615,7 @@ gst_buffer_new_wrapped_full (gpointer data, GFreeFunc free_func, gsize offset,
newbuf = gst_buffer_new ();
gst_buffer_append_memory (newbuf,
gst_memory_new_wrapped (free_func ? 0 : GST_MEMORY_FLAG_READONLY,
data, free_func, offset + size, offset, size));
data, offset + size, offset, size, data, free_func));
return newbuf;
}
......
......@@ -116,7 +116,8 @@ typedef struct
GstMemory mem;
gsize slice_size;
guint8 *data;
GFreeFunc free_func;
gpointer user_data;
GDestroyNotify notify;
} GstMemoryDefault;
/* the default allocator */
......@@ -129,7 +130,8 @@ static GstAllocator *_default_mem_impl;
static void
_default_mem_init (GstMemoryDefault * mem, GstMemoryFlags flags,
GstMemory * parent, gsize slice_size, gpointer data,
GFreeFunc free_func, gsize maxsize, gsize offset, gsize size)
gsize maxsize, gsize offset, gsize size, gpointer user_data,
GDestroyNotify notify)
{
mem->mem.allocator = _default_mem_impl;
mem->mem.flags = flags;
......@@ -141,7 +143,8 @@ _default_mem_init (GstMemoryDefault * mem, GstMemoryFlags flags,
mem->mem.size = size;
mem->slice_size = slice_size;
mem->data = data;
mem->free_func = free_func;
mem->user_data = user_data;
mem->notify = notify;
GST_CAT_DEBUG (GST_CAT_MEMORY, "new memory %p", mem);
}
......@@ -149,7 +152,8 @@ _default_mem_init (GstMemoryDefault * mem, GstMemoryFlags flags,
/* create a new memory block that manages the given memory */
static GstMemoryDefault *
_default_mem_new (GstMemoryFlags flags, GstMemory * parent, gpointer data,
GFreeFunc free_func, gsize maxsize, gsize offset, gsize size)
gsize maxsize, gsize offset, gsize size, gpointer user_data,
GDestroyNotify notify)
{
GstMemoryDefault *mem;
gsize slice_size;
......@@ -158,7 +162,7 @@ _default_mem_new (GstMemoryFlags flags, GstMemory * parent, gpointer data,
mem = g_slice_alloc (slice_size);
_default_mem_init (mem, flags, parent, slice_size,
data, free_func, maxsize, offset, size);
data, maxsize, offset, size, user_data, notify);
return mem;
}
......@@ -187,8 +191,8 @@ _default_mem_new_block (gsize maxsize, gsize align, gsize offset, gsize size)
if ((aoffset = ((guintptr) data & align)))
aoffset = (align + 1) - aoffset;
_default_mem_init (mem, 0, NULL, slice_size, data, NULL, maxsize,
aoffset + offset, size);
_default_mem_init (mem, 0, NULL, slice_size, data, maxsize,
aoffset + offset, size, NULL, NULL);
return mem;
}
......@@ -220,8 +224,8 @@ _default_mem_free (GstMemoryDefault * mem)
if (mem->mem.parent)
gst_memory_unref (mem->mem.parent);
if (mem->free_func)
mem->free_func (mem->data);
if (mem->notify)
mem->notify (mem->user_data);
g_slice_free1 (mem->slice_size, mem);
}
......@@ -257,8 +261,8 @@ _default_mem_share (GstMemoryDefault * mem, gssize offset, gsize size)
size = mem->mem.size - offset;
sub =
_default_mem_new (parent->flags, parent, mem->data, NULL,
mem->mem.maxsize, mem->mem.offset + offset, size);
_default_mem_new (parent->flags, parent, mem->data,
mem->mem.maxsize, mem->mem.offset + offset, size, NULL, NULL);
return sub;
}
......@@ -366,10 +370,11 @@ _priv_gst_memory_initialize (void)
* gst_memory_new_wrapped:
* @flags: #GstMemoryFlags
* @data: data to wrap
* @free_func: function to free @data
* @maxsize: allocated size of @data
* @offset: offset in @data
* @size: size of valid data
* @user_data: user_data
* @notify: called with @user_data when the memory is freed
*
* Allocate a new memory block that wraps the given @data.
*
......@@ -377,14 +382,17 @@ _priv_gst_memory_initialize (void)
*/
GstMemory *
gst_memory_new_wrapped (GstMemoryFlags flags, gpointer data,
GFreeFunc free_func, gsize maxsize, gsize offset, gsize size)
gsize maxsize, gsize offset, gsize size, gpointer user_data,
GDestroyNotify notify)
{
GstMemoryDefault *mem;
g_return_val_if_fail (data != NULL, NULL);
g_return_val_if_fail (offset + size <= maxsize, NULL);
mem = _default_mem_new (flags, NULL, data, free_func, maxsize, offset, size);
mem =
_default_mem_new (flags, NULL, data, maxsize, offset, size, user_data,
notify);
#ifndef GST_DISABLE_TRACE
_gst_alloc_trace_new (_gst_memory_trace, mem);
......
......@@ -304,8 +304,9 @@ void gst_allocator_set_default (GstAllocator * allocator);
GstMemory * gst_allocator_alloc (GstAllocator * allocator,
gsize maxsize, gsize align);
GstMemory * gst_memory_new_wrapped (GstMemoryFlags flags, gpointer data, GFreeFunc free_func,
gsize maxsize, gsize offset, gsize size);
GstMemory * gst_memory_new_wrapped (GstMemoryFlags flags, gpointer data, gsize maxsize,
gsize offset, gsize size, gpointer user_data,
GDestroyNotify notify);
/* refcounting */
GstMemory * gst_memory_ref (GstMemory *mem);
......
......@@ -766,7 +766,7 @@ gst_adapter_take_buffer (GstAdapter * adapter, gsize nbytes)
buffer = gst_buffer_new ();
gst_buffer_take_memory (buffer, -1,
gst_memory_new_wrapped (0, data, g_free, nbytes, 0, nbytes));
gst_memory_new_wrapped (0, data, nbytes, 0, nbytes, data, g_free));
done:
gst_adapter_flush_unchecked (adapter, nbytes);
......
......@@ -2624,7 +2624,7 @@ gst_base_parse_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
tmpbuf = gst_buffer_new ();
gst_buffer_take_memory (tmpbuf, -1,
gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY |
GST_MEMORY_FLAG_NO_SHARE, (gpointer) data, NULL, av, 0, av));
GST_MEMORY_FLAG_NO_SHARE, (gpointer) data, av, 0, av, NULL, NULL));
/* keep the adapter mapped, so keep track of what has to be flushed */
ret = gst_base_parse_handle_buffer (parse, tmpbuf, &skip, &flush);
......
......@@ -261,7 +261,7 @@ gst_byte_writer_reset_and_get_buffer (GstByteWriter * writer)
buffer = gst_buffer_new ();
if (data != NULL) {
gst_buffer_take_memory (buffer, -1,
gst_memory_new_wrapped (0, data, g_free, size, 0, size));
gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
}
return buffer;
......
......@@ -661,7 +661,7 @@ gst_fake_src_alloc_buffer (GstFakeSrc * src, guint size)
gst_fake_src_prepare_buffer (src, data, size);
gst_buffer_take_memory (buf, -1,
gst_memory_new_wrapped (0, data, g_free, size, 0, size));
gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
}
return buf;
......
......@@ -246,8 +246,8 @@ create_read_only_buffer (void)
/* assign some read-only data to the new buffer */
gst_buffer_take_memory (buf, -1,
gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY,
(gpointer) ro_memory, NULL,
sizeof (ro_memory), 0, sizeof (ro_memory)));
(gpointer) ro_memory, sizeof (ro_memory),
0, sizeof (ro_memory), NULL, NULL));
return buf;
}
......
......@@ -132,7 +132,8 @@ create_read_only_memory (void)
/* assign some read-only data to the new memory */
mem = gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY,
(gpointer) ro_memory, NULL, sizeof (ro_memory), 0, sizeof (ro_memory));
(gpointer) ro_memory, sizeof (ro_memory), 0, sizeof (ro_memory), NULL,
NULL);
fail_unless (GST_MEMORY_IS_READONLY (mem));
return mem;
......
......@@ -49,7 +49,8 @@ GST_START_TEST (test_initialization)
GstMapInfo info;
gst_buffer_take_memory (buffer, -1,
gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY, data, NULL, 4, 0, 4));
gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY, data, 4, 0, 4, NULL,
NULL));
fail_unless (gst_bit_reader_get_bits_uint8 (&reader, &x, 8));
fail_unless_equals_int (x, 0x01);
......
......@@ -49,7 +49,8 @@ GST_START_TEST (test_initialization)
GstMapInfo info;
gst_buffer_take_memory (buffer, -1,
gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY, data, NULL, 4, 0, 4));
gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY, data, 4, 0, 4, NULL,
NULL));
fail_unless (gst_byte_reader_get_uint8 (&reader, &x));
fail_unless_equals_int (x, 0x01);
......
......@@ -55,7 +55,7 @@ GST_START_TEST (test_buffer_range)
gst_buffer_take_memory (buf, -1,
gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY,
(gpointer) vorbisid, NULL, 30, 0, 30));
(gpointer) vorbisid, 30, 0, 30, NULL, NULL));
caps = gst_type_find_helper_for_buffer (NULL, buf, NULL);
fail_unless (caps != NULL);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment