2006-07-05 Wim Taymans <wim@fluendo.com>
+ * gst/gstbuffer.c: (_gst_buffer_copy), (gst_buffer_create_sub):
+ * tests/check/gst/gstbuffer.c: (GST_START_TEST),
+ (gst_buffer_suite):
+ No point in checking if the size of the subbuffer > 0, the
+ code handles it correclty as demonstrated by unit test.
+ Also add a unit test for the zero sized _new_and_alloc and
+ _copy. Fixes #346663.
+
+2006-07-05 Wim Taymans <wim@fluendo.com>
+
* libs/gst/base/gstbasetransform.c:
(gst_base_transform_prepare_output_buffer),
(gst_base_transform_buffer_alloc),
/* we simply copy everything from our parent */
copy->data = g_memdup (buffer->data, buffer->size);
/* make sure it gets freed (even if the parent is subclassed, we return a
- normal buffer */
+ normal buffer) */
copy->malloc_data = copy->data;
copy->size = buffer->size;
* Creates a newly allocated buffer with data of the given size.
* The buffer memory is not cleared.
*
+ * Note that when @size == 0, the buffer data pointer will be NULL.
+ *
* MT safe.
* Returns: the new #GstBuffer.
*/
* @parent: a #GstBuffer.
* @offset: the offset into parent #GstBuffer at which the new sub-buffer
* begins.
- * @size: the size of the new #GstBuffer sub-buffer, in bytes (with size > 0).
+ * @size: the size of the new #GstBuffer sub-buffer, in bytes.
*
* Creates a sub-buffer from @parent at @offset and @size.
* This sub-buffer uses the actual memory space of the parent buffer.
g_return_val_if_fail (buffer != NULL, NULL);
g_return_val_if_fail (buffer->mini_object.refcount > 0, NULL);
- g_return_val_if_fail (size > 0, NULL);
g_return_val_if_fail (buffer->size >= offset + size, NULL);
/* find real parent */
ASSERT_BUFFER_REFCOUNT (buffer, "parent", 2);
ASSERT_BUFFER_REFCOUNT (sub, "subbuffer", 1);
+ gst_buffer_unref (sub);
+
+ /* create a subbuffer of size 0 */
+ sub = gst_buffer_create_sub (buffer, 1, 0);
+ fail_if (sub == NULL, "create_sub of buffer returned NULL");
+ fail_unless (GST_BUFFER_SIZE (sub) == 0, "subbuffer has wrong size");
+ fail_unless (memcmp (GST_BUFFER_DATA (buffer) + 1, GST_BUFFER_DATA (sub),
+ 0) == 0, "subbuffer contains the wrong data");
+ ASSERT_BUFFER_REFCOUNT (buffer, "parent", 2);
+ ASSERT_BUFFER_REFCOUNT (sub, "subbuffer", 1);
+
/* clean up */
gst_buffer_unref (sub);
gst_buffer_unref (buffer);
gst_buffer_unref (span);
ASSERT_BUFFER_REFCOUNT (buffer, "parent", 3);
-/* clean up */
+ /* clean up */
gst_buffer_unref (sub1);
gst_buffer_unref (sub2);
gst_buffer_unref (buffer);
GST_END_TEST;
+GST_START_TEST (test_copy)
+{
+ GstBuffer *buffer, *copy;
+
+ buffer = gst_buffer_new_and_alloc (4);
+ ASSERT_BUFFER_REFCOUNT (buffer, "buffer", 1);
+
+ copy = gst_buffer_copy (buffer);
+ ASSERT_BUFFER_REFCOUNT (buffer, "buffer", 1);
+ ASSERT_BUFFER_REFCOUNT (copy, "copy", 1);
+ /* data must be copied and thus point to different memory */
+ fail_if (GST_BUFFER_DATA (buffer) == GST_BUFFER_DATA (copy));
+
+ gst_buffer_unref (copy);
+ gst_buffer_unref (buffer);
+
+ /* a 0-sized buffer has NULL data as per docs */
+ buffer = gst_buffer_new_and_alloc (0);
+ fail_unless (GST_BUFFER_DATA (buffer) == NULL);
+ fail_unless (GST_BUFFER_SIZE (buffer) == 0);
+
+ /* copying a 0-sized buffer should not crash and also set
+ * the data member NULL. */
+ copy = gst_buffer_copy (buffer);
+ fail_unless (GST_BUFFER_DATA (copy) == NULL);
+ fail_unless (GST_BUFFER_SIZE (copy) == 0);
+
+ gst_buffer_unref (copy);
+ gst_buffer_unref (buffer);
+}
+
+GST_END_TEST;
+
Suite *
gst_buffer_suite (void)
{
tcase_add_test (tc_chain, test_is_span_fast);
tcase_add_test (tc_chain, test_span);
tcase_add_test (tc_chain, test_metadata_writable);
+ tcase_add_test (tc_chain, test_copy);
+
return s;
}