buffer: add gst_buffer_new_copy() convenience function

More convenient and discoverable variant of the fairly widely-used
gst_buffer_new_wrapped(g_memdup(data,size),size).

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/826>
This commit is contained in:
Tim-Philipp Müller 2021-05-22 19:32:55 +01:00 committed by GStreamer Marge Bot
parent caa608e5c0
commit 04fe2233f0
3 changed files with 45 additions and 0 deletions

View file

@ -1039,6 +1039,25 @@ gst_buffer_new_wrapped_bytes (GBytes * bytes)
size, 0, size, g_bytes_ref (bytes), (GDestroyNotify) g_bytes_unref);
}
/**
* gst_buffer_new_copy:
* @data: (array length=size) (element-type guint8) (transfer none): data to copy into new buffer
* @size: size of @data in bytes
*
* Creates a new buffer of size @size and fills it with a copy of @data.
*
* Returns: (transfer full): a new #GstBuffer
*
* Since: 1.20
*/
GstBuffer *
gst_buffer_new_copy (gconstpointer data, gsize size)
{
gpointer data2 = g_memdup (data, size);
return gst_buffer_new_wrapped_full (0, data2, size, 0, size, data2, g_free);
}
/**
* gst_buffer_n_memory:
* @buffer: a #GstBuffer.

View file

@ -314,9 +314,13 @@ GstBuffer * gst_buffer_new_wrapped_full (GstMemoryFlags flags, gpointer data,
GDestroyNotify notify);
GST_API
GstBuffer * gst_buffer_new_wrapped (gpointer data, gsize size);
GST_API
GstBuffer * gst_buffer_new_wrapped_bytes (GBytes * bytes);
GST_API
GstBuffer * gst_buffer_new_copy (gconstpointer data, gsize size);
/* memory blocks */
GST_API

View file

@ -923,6 +923,27 @@ GST_START_TEST (test_wrapped_bytes)
GST_END_TEST;
GST_START_TEST (test_new_copy)
{
GstBuffer *buf;
GstMemory *mem;
buf = gst_buffer_new_copy (ro_memory, sizeof (ro_memory));
fail_if (gst_buffer_memcmp (buf, 0, ro_memory, sizeof (ro_memory)));
fail_unless_equals_int (gst_buffer_get_size (buf), sizeof (ro_memory));
/* the memory should be writable */
mem = gst_buffer_peek_memory (buf, 0);
fail_unless (gst_memory_is_writable (mem));
fail_unless (gst_buffer_is_writable (buf));
gst_buffer_memset (buf, 0, 0xaa, sizeof (ro_memory));
gst_buffer_unref (buf);
}
GST_END_TEST;
static Suite *
gst_buffer_suite (void)
{
@ -948,6 +969,7 @@ gst_buffer_suite (void)
tcase_add_test (tc_chain, test_parent_buffer_meta);
tcase_add_test (tc_chain, test_writable_memory);
tcase_add_test (tc_chain, test_wrapped_bytes);
tcase_add_test (tc_chain, test_new_copy);
return s;
}