2011-03-18 18:28:17 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 2011 Wim Taymans <wim.taymans@gmail.be>
|
|
|
|
*
|
|
|
|
* gstmemory.c: memory block handling
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
2012-11-03 20:44:48 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2011-03-18 18:28:17 +00:00
|
|
|
*/
|
|
|
|
|
2011-03-30 16:45:08 +00:00
|
|
|
/**
|
|
|
|
* SECTION:gstmemory
|
2017-01-16 14:26:16 +00:00
|
|
|
* @title: GstMemory
|
2011-03-30 16:45:08 +00:00
|
|
|
* @short_description: refcounted wrapper for memory blocks
|
|
|
|
* @see_also: #GstBuffer
|
|
|
|
*
|
|
|
|
* GstMemory is a lightweight refcounted object that wraps a region of memory.
|
|
|
|
* They are typically used to manage the data of a #GstBuffer.
|
|
|
|
*
|
2012-01-06 09:39:46 +00:00
|
|
|
* A GstMemory object has an allocated region of memory of maxsize. The maximum
|
|
|
|
* size does not change during the lifetime of the memory object. The memory
|
|
|
|
* also has an offset and size property that specifies the valid range of memory
|
|
|
|
* in the allocated region.
|
|
|
|
*
|
2011-06-22 09:42:46 +00:00
|
|
|
* Memory is usually created by allocators with a gst_allocator_alloc()
|
2014-05-29 21:54:34 +00:00
|
|
|
* method call. When %NULL is used as the allocator, the default allocator will
|
2011-06-08 09:03:50 +00:00
|
|
|
* be used.
|
|
|
|
*
|
2011-06-22 09:42:46 +00:00
|
|
|
* New allocators can be registered with gst_allocator_register().
|
2011-06-08 09:03:50 +00:00
|
|
|
* Allocators are identified by name and can be retrieved with
|
2012-03-29 11:34:50 +00:00
|
|
|
* gst_allocator_find(). gst_allocator_set_default() can be used to change the
|
|
|
|
* default allocator.
|
2011-06-07 14:14:50 +00:00
|
|
|
*
|
2011-03-30 16:45:08 +00:00
|
|
|
* New memory can be created with gst_memory_new_wrapped() that wraps the memory
|
2011-06-07 14:14:50 +00:00
|
|
|
* allocated elsewhere.
|
2011-03-30 16:45:08 +00:00
|
|
|
*
|
|
|
|
* Refcounting of the memory block is performed with gst_memory_ref() and
|
|
|
|
* gst_memory_unref().
|
|
|
|
*
|
|
|
|
* The size of the memory can be retrieved and changed with
|
|
|
|
* gst_memory_get_sizes() and gst_memory_resize() respectively.
|
|
|
|
*
|
|
|
|
* Getting access to the data of the memory is performed with gst_memory_map().
|
2012-01-06 09:39:46 +00:00
|
|
|
* The call will return a pointer to offset bytes into the region of memory.
|
2011-03-30 16:45:08 +00:00
|
|
|
* After the memory access is completed, gst_memory_unmap() should be called.
|
|
|
|
*
|
2012-02-23 16:48:10 +00:00
|
|
|
* Memory can be copied with gst_memory_copy(), which will return a writable
|
2011-03-30 16:45:08 +00:00
|
|
|
* copy. gst_memory_share() will create a new memory block that shares the
|
|
|
|
* memory with an existing memory block at a custom offset and with a custom
|
|
|
|
* size.
|
|
|
|
*
|
2014-05-29 21:54:34 +00:00
|
|
|
* Memory can be efficiently merged when gst_memory_is_span() returns %TRUE.
|
2011-03-30 16:45:08 +00:00
|
|
|
*/
|
2011-03-18 18:28:17 +00:00
|
|
|
|
2012-01-18 01:31:56 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2011-03-30 16:45:08 +00:00
|
|
|
#include "config.h"
|
2012-01-18 01:31:56 +00:00
|
|
|
#endif
|
|
|
|
|
2011-03-18 18:28:17 +00:00
|
|
|
#include "gst_private.h"
|
|
|
|
#include "gstmemory.h"
|
|
|
|
|
2014-06-19 06:05:03 +00:00
|
|
|
GType _gst_memory_type = 0;
|
2012-06-14 14:30:12 +00:00
|
|
|
GST_DEFINE_MINI_OBJECT_TYPE (GstMemory, gst_memory);
|
2011-03-30 16:45:08 +00:00
|
|
|
|
2012-06-14 14:30:12 +00:00
|
|
|
static GstMemory *
|
|
|
|
_gst_memory_copy (GstMemory * mem)
|
|
|
|
{
|
2012-07-05 15:11:01 +00:00
|
|
|
GST_CAT_DEBUG (GST_CAT_MEMORY, "copy memory %p", mem);
|
2012-06-14 14:30:12 +00:00
|
|
|
return gst_memory_copy (mem, 0, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_gst_memory_free (GstMemory * mem)
|
|
|
|
{
|
2013-02-26 13:40:38 +00:00
|
|
|
GstAllocator *allocator;
|
|
|
|
|
2012-07-05 15:11:01 +00:00
|
|
|
GST_CAT_DEBUG (GST_CAT_MEMORY, "free memory %p", mem);
|
|
|
|
|
|
|
|
if (mem->parent) {
|
|
|
|
gst_memory_unlock (mem->parent, GST_LOCK_FLAG_EXCLUSIVE);
|
|
|
|
gst_memory_unref (mem->parent);
|
|
|
|
}
|
|
|
|
|
2013-02-26 13:40:38 +00:00
|
|
|
allocator = mem->allocator;
|
|
|
|
|
|
|
|
gst_allocator_free (allocator, mem);
|
2018-08-03 10:16:21 +00:00
|
|
|
|
2013-02-26 13:40:38 +00:00
|
|
|
gst_object_unref (allocator);
|
2012-06-14 14:30:12 +00:00
|
|
|
}
|
|
|
|
|
2012-07-05 15:11:01 +00:00
|
|
|
/**
|
|
|
|
* gst_memory_init: (skip)
|
|
|
|
* @mem: a #GstMemory
|
|
|
|
* @flags: #GstMemoryFlags
|
|
|
|
* @allocator: the #GstAllocator
|
|
|
|
* @parent: the parent of @mem
|
|
|
|
* @maxsize: the total size of the memory
|
|
|
|
* @align: the alignment of the memory
|
|
|
|
* @offset: The offset in the memory
|
|
|
|
* @size: the size of valid data in the memory
|
|
|
|
|
|
|
|
* Initializes a newly allocated @mem with the given parameters. This function
|
|
|
|
* will call gst_mini_object_init() with the default memory parameters.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_memory_init (GstMemory * mem, GstMemoryFlags flags,
|
|
|
|
GstAllocator * allocator, GstMemory * parent, gsize maxsize, gsize align,
|
|
|
|
gsize offset, gsize size)
|
2011-03-19 09:45:37 +00:00
|
|
|
{
|
2012-07-04 14:38:15 +00:00
|
|
|
gst_mini_object_init (GST_MINI_OBJECT_CAST (mem),
|
|
|
|
flags | GST_MINI_OBJECT_FLAG_LOCKABLE, GST_TYPE_MEMORY,
|
2012-06-23 18:56:12 +00:00
|
|
|
(GstMiniObjectCopyFunction) _gst_memory_copy, NULL,
|
|
|
|
(GstMiniObjectFreeFunction) _gst_memory_free);
|
2012-06-14 14:30:12 +00:00
|
|
|
|
2013-02-26 13:40:38 +00:00
|
|
|
mem->allocator = gst_object_ref (allocator);
|
2012-07-05 15:11:01 +00:00
|
|
|
if (parent) {
|
2015-06-02 06:14:50 +00:00
|
|
|
/* FIXME 2.0: this can fail if the memory is already write locked */
|
2012-07-05 15:11:01 +00:00
|
|
|
gst_memory_lock (parent, GST_LOCK_FLAG_EXCLUSIVE);
|
|
|
|
gst_memory_ref (parent);
|
|
|
|
}
|
|
|
|
mem->parent = parent;
|
|
|
|
mem->maxsize = maxsize;
|
|
|
|
mem->align = align;
|
|
|
|
mem->offset = offset;
|
|
|
|
mem->size = size;
|
2012-01-27 10:40:37 +00:00
|
|
|
|
2012-03-14 23:23:57 +00:00
|
|
|
GST_CAT_DEBUG (GST_CAT_MEMORY, "new memory %p, maxsize:%" G_GSIZE_FORMAT
|
|
|
|
" offset:%" G_GSIZE_FORMAT " size:%" G_GSIZE_FORMAT, mem, maxsize,
|
|
|
|
offset, size);
|
2011-03-19 09:45:37 +00:00
|
|
|
}
|
|
|
|
|
2013-02-26 14:37:24 +00:00
|
|
|
/**
|
|
|
|
* gst_memory_is_type:
|
|
|
|
* @mem: a #GstMemory
|
|
|
|
* @mem_type: a memory type
|
|
|
|
*
|
|
|
|
* Check if @mem if allocated with an allocator for @mem_type.
|
|
|
|
*
|
|
|
|
* Returns: %TRUE if @mem was allocated from an allocator for @mem_type.
|
|
|
|
*
|
|
|
|
* Since: 1.2
|
|
|
|
*/
|
|
|
|
gboolean
|
|
|
|
gst_memory_is_type (GstMemory * mem, const gchar * mem_type)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (mem != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (mem->allocator != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (mem_type != NULL, FALSE);
|
|
|
|
|
|
|
|
return (g_strcmp0 (mem->allocator->mem_type, mem_type) == 0);
|
|
|
|
}
|
|
|
|
|
2011-03-24 10:49:46 +00:00
|
|
|
/**
|
|
|
|
* gst_memory_get_sizes:
|
|
|
|
* @mem: a #GstMemory
|
2018-02-13 11:38:33 +00:00
|
|
|
* @offset: (out) (allow-none): pointer to offset
|
|
|
|
* @maxsize: (out) (allow-none): pointer to maxsize
|
2011-03-24 10:49:46 +00:00
|
|
|
*
|
2011-07-11 14:17:57 +00:00
|
|
|
* Get the current @size, @offset and @maxsize of @mem.
|
2011-03-30 16:45:08 +00:00
|
|
|
*
|
2019-06-20 12:04:55 +00:00
|
|
|
* Returns: the current size of @mem
|
2011-03-24 10:49:46 +00:00
|
|
|
*/
|
2011-03-18 18:28:17 +00:00
|
|
|
gsize
|
2011-07-11 14:17:57 +00:00
|
|
|
gst_memory_get_sizes (GstMemory * mem, gsize * offset, gsize * maxsize)
|
2011-03-18 18:28:17 +00:00
|
|
|
{
|
|
|
|
g_return_val_if_fail (mem != NULL, 0);
|
|
|
|
|
2012-01-19 08:12:05 +00:00
|
|
|
if (offset)
|
|
|
|
*offset = mem->offset;
|
|
|
|
if (maxsize)
|
|
|
|
*maxsize = mem->maxsize;
|
|
|
|
|
|
|
|
return mem->size;
|
2011-03-18 18:28:17 +00:00
|
|
|
}
|
|
|
|
|
2011-03-30 16:45:08 +00:00
|
|
|
/**
|
|
|
|
* gst_memory_resize:
|
|
|
|
* @mem: a #GstMemory
|
|
|
|
* @offset: a new offset
|
|
|
|
* @size: a new size
|
|
|
|
*
|
|
|
|
* Resize the memory region. @mem should be writable and offset + size should be
|
|
|
|
* less than the maxsize of @mem.
|
2012-03-14 20:32:22 +00:00
|
|
|
*
|
|
|
|
* #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED will be
|
|
|
|
* cleared when offset or padding is increased respectively.
|
2011-03-30 16:45:08 +00:00
|
|
|
*/
|
|
|
|
void
|
2011-07-11 14:17:57 +00:00
|
|
|
gst_memory_resize (GstMemory * mem, gssize offset, gsize size)
|
2011-03-30 16:45:08 +00:00
|
|
|
{
|
|
|
|
g_return_if_fail (mem != NULL);
|
2015-04-06 04:48:15 +00:00
|
|
|
g_return_if_fail (gst_memory_is_writable (mem));
|
2012-01-19 08:12:05 +00:00
|
|
|
g_return_if_fail (offset >= 0 || mem->offset >= -offset);
|
|
|
|
g_return_if_fail (size + mem->offset + offset <= mem->maxsize);
|
2011-03-30 16:45:08 +00:00
|
|
|
|
2012-03-14 20:32:22 +00:00
|
|
|
/* if we increase the prefix, we can't guarantee it is still 0 filled */
|
|
|
|
if ((offset > 0) && GST_MEMORY_IS_ZERO_PREFIXED (mem))
|
|
|
|
GST_MEMORY_FLAG_UNSET (mem, GST_MEMORY_FLAG_ZERO_PREFIXED);
|
|
|
|
|
|
|
|
/* if we increase the padding, we can't guarantee it is still 0 filled */
|
|
|
|
if ((offset + size < mem->size) && GST_MEMORY_IS_ZERO_PADDED (mem))
|
|
|
|
GST_MEMORY_FLAG_UNSET (mem, GST_MEMORY_FLAG_ZERO_PADDED);
|
|
|
|
|
2012-01-19 08:12:05 +00:00
|
|
|
mem->offset += offset;
|
|
|
|
mem->size = size;
|
2011-03-30 16:45:08 +00:00
|
|
|
}
|
|
|
|
|
2012-01-20 13:56:49 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2014-06-11 22:21:34 +00:00
|
|
|
* Returns: (transfer full) (nullable): a #GstMemory object mapped
|
|
|
|
* with @flags or %NULL when a mapping is not possible.
|
2012-01-20 13:56:49 +00:00
|
|
|
*/
|
|
|
|
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);
|
2012-01-27 10:40:37 +00:00
|
|
|
gst_memory_unref (mem);
|
|
|
|
|
2012-01-20 13:56:49 +00:00
|
|
|
if (result == NULL)
|
|
|
|
goto cannot_copy;
|
|
|
|
|
|
|
|
if (!gst_memory_map (result, info, flags))
|
|
|
|
goto cannot_map;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
cannot_copy:
|
|
|
|
{
|
2012-02-02 14:57:48 +00:00
|
|
|
GST_CAT_DEBUG (GST_CAT_MEMORY, "cannot copy memory %p", mem);
|
2012-01-20 13:56:49 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
cannot_map:
|
|
|
|
{
|
2012-02-02 14:57:48 +00:00
|
|
|
GST_CAT_DEBUG (GST_CAT_MEMORY, "cannot map memory %p with flags %d", mem,
|
|
|
|
flags);
|
2012-01-27 10:40:37 +00:00
|
|
|
gst_memory_unref (result);
|
2012-01-20 13:56:49 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-30 16:45:08 +00:00
|
|
|
/**
|
|
|
|
* gst_memory_map:
|
|
|
|
* @mem: a #GstMemory
|
2012-01-20 13:23:57 +00:00
|
|
|
* @info: (out): pointer for info
|
2011-03-30 16:45:08 +00:00
|
|
|
* @flags: mapping flags
|
|
|
|
*
|
2012-01-20 13:23:57 +00:00
|
|
|
* Fill @info with the pointer and sizes of the memory in @mem that can be
|
|
|
|
* accessed according to @flags.
|
2011-03-30 16:45:08 +00:00
|
|
|
*
|
2012-01-20 13:23:57 +00:00
|
|
|
* This function can return %FALSE for various reasons:
|
2012-01-19 08:12:05 +00:00
|
|
|
* - the memory backed by @mem is not accessible with the given @flags.
|
|
|
|
* - the memory was already mapped with a different mapping.
|
|
|
|
*
|
2012-02-13 15:33:00 +00:00
|
|
|
* @info and its contents remain valid for as long as @mem is valid and
|
|
|
|
* until gst_memory_unmap() is called.
|
2012-01-19 08:12:05 +00:00
|
|
|
*
|
|
|
|
* For each gst_memory_map() call, a corresponding gst_memory_unmap() call
|
|
|
|
* should be done.
|
|
|
|
*
|
2012-01-20 13:23:57 +00:00
|
|
|
* Returns: %TRUE if the map operation was successful.
|
2011-03-30 16:45:08 +00:00
|
|
|
*/
|
2012-01-20 13:23:57 +00:00
|
|
|
gboolean
|
|
|
|
gst_memory_map (GstMemory * mem, GstMapInfo * info, GstMapFlags flags)
|
2011-03-18 18:28:17 +00:00
|
|
|
{
|
2012-01-20 13:23:57 +00:00
|
|
|
g_return_val_if_fail (mem != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (info != NULL, FALSE);
|
2012-01-06 12:10:18 +00:00
|
|
|
|
2013-11-04 10:48:47 +00:00
|
|
|
if (!gst_memory_lock (mem, (GstLockFlags) flags))
|
2012-01-19 08:12:05 +00:00
|
|
|
goto lock_failed;
|
|
|
|
|
2015-06-03 14:03:16 +00:00
|
|
|
info->flags = flags;
|
|
|
|
info->memory = mem;
|
|
|
|
info->size = mem->size;
|
|
|
|
info->maxsize = mem->maxsize - mem->offset;
|
|
|
|
|
|
|
|
if (mem->allocator->mem_map_full)
|
|
|
|
info->data = mem->allocator->mem_map_full (mem, info, mem->maxsize);
|
|
|
|
else
|
|
|
|
info->data = mem->allocator->mem_map (mem, mem->maxsize, flags);
|
2012-01-19 08:12:05 +00:00
|
|
|
|
2012-01-20 13:23:57 +00:00
|
|
|
if (G_UNLIKELY (info->data == NULL))
|
2012-01-19 08:12:05 +00:00
|
|
|
goto error;
|
|
|
|
|
2012-01-20 15:10:26 +00:00
|
|
|
info->data = info->data + mem->offset;
|
2012-01-06 12:10:18 +00:00
|
|
|
|
2012-01-20 13:23:57 +00:00
|
|
|
return TRUE;
|
2012-01-06 12:10:18 +00:00
|
|
|
|
2012-01-19 08:12:05 +00:00
|
|
|
/* ERRORS */
|
|
|
|
lock_failed:
|
|
|
|
{
|
2012-02-02 14:57:48 +00:00
|
|
|
GST_CAT_DEBUG (GST_CAT_MEMORY, "mem %p: lock %d failed", mem, flags);
|
2014-04-16 17:49:56 +00:00
|
|
|
memset (info, 0, sizeof (GstMapInfo));
|
2012-01-20 13:23:57 +00:00
|
|
|
return FALSE;
|
2012-01-19 08:12:05 +00:00
|
|
|
}
|
|
|
|
error:
|
|
|
|
{
|
2018-04-27 16:40:31 +00:00
|
|
|
/* something went wrong, restore the original state again
|
2016-10-07 10:39:26 +00:00
|
|
|
* it is up to the subclass to log an error if needed. */
|
|
|
|
GST_CAT_INFO (GST_CAT_MEMORY, "mem %p: subclass map failed", mem);
|
2013-11-04 10:48:47 +00:00
|
|
|
gst_memory_unlock (mem, (GstLockFlags) flags);
|
2014-04-16 17:49:56 +00:00
|
|
|
memset (info, 0, sizeof (GstMapInfo));
|
2012-01-20 13:23:57 +00:00
|
|
|
return FALSE;
|
2012-01-06 12:10:18 +00:00
|
|
|
}
|
2011-03-18 18:28:17 +00:00
|
|
|
}
|
|
|
|
|
2011-03-30 16:45:08 +00:00
|
|
|
/**
|
|
|
|
* gst_memory_unmap:
|
|
|
|
* @mem: a #GstMemory
|
2012-01-20 13:23:57 +00:00
|
|
|
* @info: a #GstMapInfo
|
2011-03-30 16:45:08 +00:00
|
|
|
*
|
2012-01-19 08:12:05 +00:00
|
|
|
* Release the memory obtained with gst_memory_map()
|
2011-03-30 16:45:08 +00:00
|
|
|
*/
|
2012-01-19 08:12:05 +00:00
|
|
|
void
|
2012-01-20 13:23:57 +00:00
|
|
|
gst_memory_unmap (GstMemory * mem, GstMapInfo * info)
|
2011-03-18 18:28:17 +00:00
|
|
|
{
|
2012-01-19 08:12:05 +00:00
|
|
|
g_return_if_fail (mem != NULL);
|
2012-01-20 13:23:57 +00:00
|
|
|
g_return_if_fail (info != NULL);
|
|
|
|
g_return_if_fail (info->memory == mem);
|
2012-01-06 12:10:18 +00:00
|
|
|
|
2015-06-03 08:03:36 +00:00
|
|
|
if (mem->allocator->mem_unmap_full)
|
2015-06-03 14:03:16 +00:00
|
|
|
mem->allocator->mem_unmap_full (mem, info);
|
2015-06-03 08:03:36 +00:00
|
|
|
else
|
|
|
|
mem->allocator->mem_unmap (mem);
|
2013-11-04 10:48:47 +00:00
|
|
|
gst_memory_unlock (mem, (GstLockFlags) info->flags);
|
2011-03-18 18:28:17 +00:00
|
|
|
}
|
|
|
|
|
2011-03-30 16:45:08 +00:00
|
|
|
/**
|
|
|
|
* gst_memory_copy:
|
|
|
|
* @mem: a #GstMemory
|
2015-04-02 16:29:46 +00:00
|
|
|
* @offset: offset to copy from
|
|
|
|
* @size: size to copy, or -1 to copy to the end of the memory region
|
2011-03-30 16:45:08 +00:00
|
|
|
*
|
|
|
|
* Return a copy of @size bytes from @mem starting from @offset. This copy is
|
2015-04-02 16:29:46 +00:00
|
|
|
* guaranteed to be writable. @size can be set to -1 to return a copy
|
|
|
|
* from @offset to the end of the memory region.
|
2011-03-30 16:45:08 +00:00
|
|
|
*
|
|
|
|
* Returns: a new #GstMemory.
|
|
|
|
*/
|
2011-03-18 18:28:17 +00:00
|
|
|
GstMemory *
|
2011-11-11 01:47:30 +00:00
|
|
|
gst_memory_copy (GstMemory * mem, gssize offset, gssize size)
|
2011-03-18 18:28:17 +00:00
|
|
|
{
|
2012-01-19 08:12:05 +00:00
|
|
|
GstMemory *copy;
|
|
|
|
|
2011-03-18 18:28:17 +00:00
|
|
|
g_return_val_if_fail (mem != NULL, NULL);
|
2012-01-19 08:12:05 +00:00
|
|
|
|
2012-07-09 14:02:50 +00:00
|
|
|
copy = mem->allocator->mem_copy (mem, offset, size);
|
2011-03-18 18:28:17 +00:00
|
|
|
|
2012-01-19 08:12:05 +00:00
|
|
|
return copy;
|
2011-03-18 18:28:17 +00:00
|
|
|
}
|
|
|
|
|
2011-03-30 16:45:08 +00:00
|
|
|
/**
|
|
|
|
* gst_memory_share:
|
|
|
|
* @mem: a #GstMemory
|
2015-04-02 16:29:46 +00:00
|
|
|
* @offset: offset to share from
|
|
|
|
* @size: size to share, or -1 to share to the end of the memory region
|
2011-03-30 16:45:08 +00:00
|
|
|
*
|
2011-11-11 01:47:30 +00:00
|
|
|
* Return a shared copy of @size bytes from @mem starting from @offset. No
|
|
|
|
* memory copy is performed and the memory region is simply shared. The result
|
2015-04-02 16:29:46 +00:00
|
|
|
* is guaranteed to be non-writable. @size can be set to -1 to return a shared
|
|
|
|
* copy from @offset to the end of the memory region.
|
2011-03-30 16:45:08 +00:00
|
|
|
*
|
|
|
|
* Returns: a new #GstMemory.
|
|
|
|
*/
|
2011-03-18 20:45:39 +00:00
|
|
|
GstMemory *
|
2011-11-11 01:47:30 +00:00
|
|
|
gst_memory_share (GstMemory * mem, gssize offset, gssize size)
|
2011-03-18 20:45:39 +00:00
|
|
|
{
|
2012-02-02 14:57:48 +00:00
|
|
|
GstMemory *shared;
|
|
|
|
|
2011-03-18 20:45:39 +00:00
|
|
|
g_return_val_if_fail (mem != NULL, NULL);
|
2012-02-13 15:33:00 +00:00
|
|
|
g_return_val_if_fail (!GST_MEMORY_FLAG_IS_SET (mem, GST_MEMORY_FLAG_NO_SHARE),
|
|
|
|
NULL);
|
2011-03-18 20:45:39 +00:00
|
|
|
|
2015-06-02 06:14:50 +00:00
|
|
|
/* whether we can lock the memory exclusively */
|
|
|
|
/* in order to maintain backwards compatibility by not requiring subclasses
|
|
|
|
* to lock the memory themselves and propagate the possible failure in their
|
|
|
|
* mem_share implementation */
|
|
|
|
/* FIXME 2.0: remove and fix gst_memory_init() and/or all memory subclasses
|
|
|
|
* to propagate this failure case */
|
|
|
|
if (!gst_memory_lock (mem, GST_LOCK_FLAG_EXCLUSIVE))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* double lock to ensure we are not mapped writable without an
|
|
|
|
* exclusive lock. */
|
|
|
|
if (!gst_memory_lock (mem, GST_LOCK_FLAG_EXCLUSIVE)) {
|
|
|
|
gst_memory_unlock (mem, GST_LOCK_FLAG_EXCLUSIVE);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-07-09 14:02:50 +00:00
|
|
|
shared = mem->allocator->mem_share (mem, offset, size);
|
2012-02-02 14:57:48 +00:00
|
|
|
|
2015-06-02 06:14:50 +00:00
|
|
|
/* unlocking before calling the subclass would be racy */
|
|
|
|
gst_memory_unlock (mem, GST_LOCK_FLAG_EXCLUSIVE);
|
|
|
|
gst_memory_unlock (mem, GST_LOCK_FLAG_EXCLUSIVE);
|
|
|
|
|
2012-02-02 14:57:48 +00:00
|
|
|
return shared;
|
2011-03-18 20:45:39 +00:00
|
|
|
}
|
|
|
|
|
2011-03-30 16:45:08 +00:00
|
|
|
/**
|
|
|
|
* gst_memory_is_span:
|
|
|
|
* @mem1: a #GstMemory
|
|
|
|
* @mem2: a #GstMemory
|
2018-02-13 11:38:33 +00:00
|
|
|
* @offset: (out): a pointer to a result offset
|
2011-03-30 16:45:08 +00:00
|
|
|
*
|
|
|
|
* Check if @mem1 and mem2 share the memory with a common parent memory object
|
|
|
|
* and that the memory is contiguous.
|
|
|
|
*
|
|
|
|
* If this is the case, the memory of @mem1 and @mem2 can be merged
|
|
|
|
* efficiently by performing gst_memory_share() on the parent object from
|
|
|
|
* the returned @offset.
|
|
|
|
*
|
|
|
|
* Returns: %TRUE if the memory is contiguous and of a common parent.
|
|
|
|
*/
|
2011-03-19 09:45:37 +00:00
|
|
|
gboolean
|
2011-03-29 14:52:21 +00:00
|
|
|
gst_memory_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
|
2011-03-19 09:45:37 +00:00
|
|
|
{
|
|
|
|
g_return_val_if_fail (mem1 != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (mem2 != NULL, FALSE);
|
|
|
|
|
2011-06-07 14:14:50 +00:00
|
|
|
/* need to have the same allocators */
|
|
|
|
if (mem1->allocator != mem2->allocator)
|
2011-03-21 12:07:42 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2011-03-29 14:52:21 +00:00
|
|
|
/* need to have the same parent */
|
|
|
|
if (mem1->parent == NULL || mem1->parent != mem2->parent)
|
|
|
|
return FALSE;
|
2011-03-19 09:45:37 +00:00
|
|
|
|
2011-03-29 14:52:21 +00:00
|
|
|
/* and memory is contiguous */
|
2012-07-09 14:02:50 +00:00
|
|
|
if (!mem1->allocator->mem_is_span (mem1, mem2, offset))
|
2011-03-29 14:52:21 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
2011-03-19 09:45:37 +00:00
|
|
|
}
|
2014-06-19 06:05:03 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
_priv_gst_memory_initialize (void)
|
|
|
|
{
|
|
|
|
_gst_memory_type = gst_memory_get_type ();
|
|
|
|
}
|