From 659785d896914f5e659af5ea2f24efe56ed36099 Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Thu, 27 Feb 2014 16:39:50 +0100 Subject: [PATCH] buffer: add function to check writability of memory Check if memory is writable in a buffer and thus is exclusively owned by this buffer. --- gst/gstbuffer.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ gst/gstbuffer.h | 3 +++ 2 files changed, 62 insertions(+) diff --git a/gst/gstbuffer.c b/gst/gstbuffer.c index 60a1c90..b35fd7c 100644 --- a/gst/gstbuffer.c +++ b/gst/gstbuffer.c @@ -1175,6 +1175,65 @@ gst_buffer_find_memory (GstBuffer * buffer, gsize offset, gsize size, } /** + * gst_buffer_is_memory_range_writable: + * @buffer: a #GstBuffer. + * @idx: an index + * @length: a length should not be 0 + * + * Check if @length memory blocks in @buffer starting from @idx are writable. + * + * @length can be -1 to check all the memory blocks after @idx. + * + * Note that this function does not check if @buffer is writable, use + * gst_buffer_is_writable() to check that if needed. + * + * Returns: %TRUE if the memory range is writable + * + * Since: 1.4 + */ +gboolean +gst_buffer_is_memory_range_writable (GstBuffer * buffer, guint idx, gint length) +{ + guint i, len; + + g_return_if_fail (GST_IS_BUFFER (buffer)); + + GST_CAT_DEBUG (GST_CAT_BUFFER, "idx %u, length %d", idx, length); + + len = GST_BUFFER_MEM_LEN (buffer); + g_return_if_fail ((len == 0 && idx == 0 && length == -1) || + (length == -1 && idx < len) || (length > 0 && length + idx <= len)); + + if (length == -1) + length = len - idx; + + for (i = 0; i < len; i++) { + if (!gst_memory_is_writable (GST_BUFFER_MEM_PTR (buffer, i))) + return FALSE; + } + return TRUE; +} + +/** + * gst_buffer_is_all_memory_writable: + * @buffer: a #GstBuffer. + * + * Check if all memory blocks in @buffer are writable. + * + * Note that this function does not check if @buffer is writable, use + * gst_buffer_is_writable() to check that if needed. + * + * Returns: %TRUE if all memory blocks in @buffer are writable + * + * Since: 1.4 + */ +gboolean +gst_buffer_is_all_memory_writable (GstBuffer * buffer) +{ + return gst_buffer_is_memory_range_writable (buffer, 0, -1); +} + +/** * gst_buffer_get_sizes: * @buffer: a #GstBuffer. * @offset: (out): a pointer to the offset diff --git a/gst/gstbuffer.h b/gst/gstbuffer.h index 914cd73..b5f9a14 100644 --- a/gst/gstbuffer.h +++ b/gst/gstbuffer.h @@ -287,6 +287,9 @@ void gst_buffer_remove_all_memory (GstBuffer *buffer); gboolean gst_buffer_find_memory (GstBuffer *buffer, gsize offset, gsize size, guint *idx, guint *length, gsize *skip); +gboolean gst_buffer_is_memory_range_writable (GstBuffer *buffer, guint idx, gint length); +gboolean gst_buffer_is_all_memory_writable (GstBuffer *buffer); + gsize gst_buffer_fill (GstBuffer *buffer, gsize offset, gconstpointer src, gsize size); gsize gst_buffer_extract (GstBuffer *buffer, gsize offset, -- 2.7.4