Commit 93074b89 authored by Wim Taymans's avatar Wim Taymans

memory: add method to create mapped memory

Add a method to create a GstMemory with the desired mapping. Makes a copy of the
memory if it is currently in use.
parent a5212528
......@@ -963,23 +963,13 @@ gst_buffer_map (GstBuffer * buffer, GstMapInfo * info, GstMapFlags flags)
goto not_writable;
mem = gst_buffer_get_merged_memory (buffer);
if (mem == NULL)
if (G_UNLIKELY (mem == NULL))
goto no_memory;
/* now try to map */
if (!gst_memory_map (mem, info, flags) && write) {
GstMemory *copy;
/* make a (writable) copy */
copy = gst_memory_copy (mem, 0, -1);
gst_memory_unref (mem);
mem = copy;
if (G_UNLIKELY (mem == NULL))
goto cannot_map;
/* try again */
if (!gst_memory_map (mem, info, flags))
goto cannot_map;
}
mem = gst_memory_make_mapped (mem, info, flags);
if (G_UNLIKELY (mem == NULL))
goto cannot_map;
/* if the buffer is writable, replace the memory */
if (writable)
......
......@@ -499,6 +499,53 @@ gst_memory_unlock (GstMemory * mem)
} while (!g_atomic_int_compare_and_exchange (&mem->state, state, newstate));
}
/**
* gst_memory_make_mapped:
* @mem: (transfer full): a #GstMemory
* @info: (out): pointer for info
* @flags: mapping flags
*
* Create a #GstMemory object that is mapped with @flags. If @mem is mappable
* with @flags, this function returns the mapped @mem directly. Otherwise a
* mapped copy of @mem is returned.
*
* This function takes ownership of old @mem and returns a reference to a new
* #GstMemory.
*
* Returns: (transfer full): a #GstMemory object mapped with @flags or NULL when
* a mapping is not possible.
*/
GstMemory *
gst_memory_make_mapped (GstMemory * mem, GstMapInfo * info, GstMapFlags flags)
{
GstMemory *result;
if (gst_memory_map (mem, info, flags)) {
result = mem;
} else {
result = gst_memory_copy (mem, 0, -1);
if (result == NULL)
goto cannot_copy;
if (!gst_memory_map (result, info, flags))
goto cannot_map;
}
return result;
/* ERRORS */
cannot_copy:
{
GST_DEBUG ("cannot copy memory %p", mem);
return NULL;
}
cannot_map:
{
GST_DEBUG ("cannot map memory %p with flags %d", mem, flags);
return NULL;
}
}
/**
* gst_memory_map:
* @mem: a #GstMemory
......
......@@ -277,6 +277,7 @@ void gst_memory_resize (GstMemory *mem, gssize offset, gsize size);
/* retrieving data */
gboolean gst_memory_is_writable (GstMemory *mem);
GstMemory * gst_memory_make_mapped (GstMemory *mem, GstMapInfo *info, GstMapFlags flags);
gboolean gst_memory_map (GstMemory *mem, GstMapInfo *info, GstMapFlags flags);
void gst_memory_unmap (GstMemory *mem, GstMapInfo *info);
......
......@@ -510,6 +510,7 @@ EXPORTS
gst_memory_get_type
gst_memory_is_span
gst_memory_is_writable
gst_memory_make_mapped
gst_memory_map
gst_memory_new_wrapped
gst_memory_ref
......
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