memory: make writability check into a method

Use a method to check for writability instead of a macro so that we can change
the implementation more easily.
This commit is contained in:
Wim Taymans 2012-01-16 15:57:35 +01:00
parent 4eb0c8ae07
commit c96d5a330c
4 changed files with 40 additions and 30 deletions

View file

@ -652,7 +652,7 @@ _get_memory (GstBuffer * buffer, guint idx, gboolean write)
mem = GST_BUFFER_MEM_PTR (buffer, idx);
if (G_UNLIKELY (write && !GST_MEMORY_IS_WRITABLE (mem))) {
if (G_UNLIKELY (write && !gst_memory_is_writable (mem))) {
GstMemory *copy;
GST_CAT_LOG (GST_CAT_BUFFER,
"making writable copy of memory %p in buffer %p", mem, buffer);
@ -868,7 +868,7 @@ gst_buffer_resize (GstBuffer * buffer, gssize offset, gssize size)
if (offset != 0 || left != bsize) {
/* we need to clip something */
if (GST_MEMORY_IS_WRITABLE (mem)) {
if (gst_memory_is_writable (mem)) {
gst_memory_resize (mem, offset, left);
} else {
GstMemory *tmp;
@ -968,7 +968,7 @@ gst_buffer_map (GstBuffer * buffer, gsize * size, gsize * maxsize,
}
}
if (G_UNLIKELY (write && !GST_MEMORY_IS_WRITABLE (mem))) {
if (G_UNLIKELY (write && !gst_memory_is_writable (mem))) {
GstMemory *copy;
/* replace with a writable copy */
copy = gst_memory_copy (mem, 0, -1);

View file

@ -460,11 +460,29 @@ void
gst_memory_resize (GstMemory * mem, gssize offset, gsize size)
{
g_return_if_fail (mem != NULL);
g_return_if_fail (GST_MEMORY_IS_WRITABLE (mem));
g_return_if_fail (gst_memory_is_writable (mem));
mem->allocator->info.resize (mem, offset, size);
}
/**
* gst_memory_is_writable:
* @mem: a #GstMemory
*
* Check if @mem is writable.
*
* Returns: %TRUE is @mem is writable.
*/
gboolean
gst_memory_is_writable (GstMemory * mem)
{
g_return_val_if_fail (mem != NULL, FALSE);
return (mem->refcount == 1) &&
((mem->parent == NULL) || (mem->parent->refcount == 1)) &&
((mem->flags & GST_MEMORY_FLAG_READONLY) == 0);
}
/**
* gst_memory_map:
* @mem: a #GstMemory
@ -489,7 +507,7 @@ gst_memory_map (GstMemory * mem, gsize * size, gsize * maxsize,
g_return_val_if_fail (mem != NULL, NULL);
access_mode = flags & 3;
g_return_val_if_fail (!(access_mode & GST_MAP_WRITE)
|| GST_MEMORY_IS_WRITABLE (mem), NULL);
|| gst_memory_is_writable (mem), NULL);
do {
state = g_atomic_int_get (&mem->state);

View file

@ -55,16 +55,6 @@ typedef enum {
GST_MEMORY_FLAG_LAST = (1 << 16)
} GstMemoryFlags;
/**
* GST_MEMORY_IS_WRITABLE:
* @mem: a #GstMemory
*
* Check if @mem is writable.
*/
#define GST_MEMORY_IS_WRITABLE(mem) (((mem)->refcount == 1) && \
(((mem)->parent == NULL) || ((mem)->parent->refcount == 1)) && \
(((mem)->flags & GST_MEMORY_FLAG_READONLY) == 0))
/**
* GstMemory:
* @allocator: pointer to the #GstAllocator
@ -289,24 +279,26 @@ GstMemory * gst_memory_new_wrapped (GstMemoryFlags flags, gpointer data, GFr
gsize maxsize, gsize offset, gsize size);
/* refcounting */
GstMemory * gst_memory_ref (GstMemory *mem);
void gst_memory_unref (GstMemory *mem);
GstMemory * gst_memory_ref (GstMemory *mem);
void gst_memory_unref (GstMemory *mem);
/* getting/setting memory properties */
gsize gst_memory_get_sizes (GstMemory *mem, gsize *offset, gsize *maxsize);
void gst_memory_resize (GstMemory *mem, gssize offset, gsize size);
gsize gst_memory_get_sizes (GstMemory *mem, gsize *offset, gsize *maxsize);
void gst_memory_resize (GstMemory *mem, gssize offset, gsize size);
/* retrieving data */
gpointer gst_memory_map (GstMemory *mem, gsize *size, gsize *maxsize,
GstMapFlags flags);
gboolean gst_memory_unmap (GstMemory *mem, gpointer data, gssize size);
gboolean gst_memory_is_writable (GstMemory *mem);
gpointer gst_memory_map (GstMemory *mem, gsize *size, gsize *maxsize,
GstMapFlags flags);
gboolean gst_memory_unmap (GstMemory *mem, gpointer data, gssize size);
/* copy and subregions */
GstMemory * gst_memory_copy (GstMemory *mem, gssize offset, gssize size);
GstMemory * gst_memory_share (GstMemory *mem, gssize offset, gssize size);
GstMemory * gst_memory_copy (GstMemory *mem, gssize offset, gssize size);
GstMemory * gst_memory_share (GstMemory *mem, gssize offset, gssize size);
/* span memory */
gboolean gst_memory_is_span (GstMemory *mem1, GstMemory *mem2, gsize *offset);
gboolean gst_memory_is_span (GstMemory *mem1, GstMemory *mem2, gsize *offset);
G_END_DECLS

View file

@ -134,7 +134,7 @@ 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));
fail_if (GST_MEMORY_IS_WRITABLE (mem));
fail_if (gst_memory_is_writable (mem));
return mem;
}
@ -149,11 +149,11 @@ GST_START_TEST (test_writable)
mem = create_read_only_memory ();
ASSERT_CRITICAL (gst_memory_map (mem, &size, NULL, GST_MAP_WRITE));
fail_if (GST_MEMORY_IS_WRITABLE (mem));
fail_if (gst_memory_is_writable (mem));
mem2 = gst_memory_copy (mem, 0, -1);
fail_if (GST_MEMORY_IS_WRITABLE (mem));
fail_unless (GST_MEMORY_IS_WRITABLE (mem2));
fail_if (gst_memory_is_writable (mem));
fail_unless (gst_memory_is_writable (mem2));
data = gst_memory_map (mem2, &size, NULL, GST_MAP_WRITE);
data[4] = 'a';
@ -183,7 +183,7 @@ GST_START_TEST (test_submemory_writable)
mem = create_read_only_memory ();
sub_mem = gst_memory_share (mem, 0, 8);
fail_if (GST_MEMORY_IS_WRITABLE (sub_mem));
fail_if (gst_memory_is_writable (sub_mem));
ASSERT_CRITICAL (gst_memory_map (mem, &size, NULL, GST_MAP_WRITE));
ASSERT_CRITICAL (gst_memory_map (sub_mem, &size, NULL, GST_MAP_WRITE));