2012-07-09 14:02:50 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 2011 Wim Taymans <wim.taymans@gmail.be>
|
|
|
|
*
|
|
|
|
* gstallocator.c: memory block allocator
|
|
|
|
*
|
|
|
|
* 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.
|
2012-07-09 14:02:50 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION:gstallocator
|
2017-01-16 14:26:16 +00:00
|
|
|
* @title: GstAllocator
|
2012-07-09 14:02:50 +00:00
|
|
|
* @short_description: allocate memory blocks
|
|
|
|
* @see_also: #GstMemory
|
|
|
|
*
|
|
|
|
* 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
|
2012-07-09 14:02:50 +00:00
|
|
|
* be used.
|
|
|
|
*
|
|
|
|
* New allocators can be registered with gst_allocator_register().
|
|
|
|
* Allocators are identified by name and can be retrieved with
|
|
|
|
* gst_allocator_find(). gst_allocator_set_default() can be used to change the
|
|
|
|
* default allocator.
|
|
|
|
*
|
|
|
|
* New memory can be created with gst_memory_new_wrapped() that wraps the memory
|
|
|
|
* allocated elsewhere.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "gst_private.h"
|
|
|
|
#include "gstmemory.h"
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (gst_allocator_debug);
|
|
|
|
#define GST_CAT_DEFAULT gst_allocator_debug
|
|
|
|
|
|
|
|
struct _GstAllocatorPrivate
|
|
|
|
{
|
|
|
|
gpointer dummy;
|
|
|
|
};
|
|
|
|
|
|
|
|
#if defined(MEMORY_ALIGNMENT_MALLOC)
|
2013-07-08 14:26:38 +00:00
|
|
|
gsize gst_memory_alignment = 7;
|
2012-07-09 14:02:50 +00:00
|
|
|
#elif defined(MEMORY_ALIGNMENT_PAGESIZE)
|
|
|
|
/* we fill this in in the _init method */
|
2013-07-08 14:26:38 +00:00
|
|
|
gsize gst_memory_alignment = 0;
|
2012-07-09 14:02:50 +00:00
|
|
|
#elif defined(MEMORY_ALIGNMENT)
|
2013-07-08 14:26:38 +00:00
|
|
|
gsize gst_memory_alignment = MEMORY_ALIGNMENT - 1;
|
2012-07-09 14:02:50 +00:00
|
|
|
#else
|
|
|
|
#error "No memory alignment configured"
|
2013-07-08 14:26:38 +00:00
|
|
|
gsize gst_memory_alignment = 0;
|
2012-07-09 14:02:50 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* the default allocator */
|
|
|
|
static GstAllocator *_default_allocator;
|
|
|
|
|
|
|
|
static GstAllocator *_sysmem_allocator;
|
|
|
|
|
|
|
|
/* registered allocators */
|
|
|
|
static GRWLock lock;
|
|
|
|
static GHashTable *allocators;
|
|
|
|
|
2018-06-23 15:01:09 +00:00
|
|
|
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GstAllocator, gst_allocator,
|
|
|
|
GST_TYPE_OBJECT);
|
2012-07-09 14:02:50 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
gst_allocator_class_init (GstAllocatorClass * klass)
|
|
|
|
{
|
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_allocator_debug, "allocator", 0,
|
|
|
|
"allocator debug");
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstMemory *
|
|
|
|
_fallback_mem_copy (GstMemory * mem, gssize offset, gssize size)
|
|
|
|
{
|
|
|
|
GstMemory *copy;
|
|
|
|
GstMapInfo sinfo, dinfo;
|
2012-08-08 14:08:44 +00:00
|
|
|
GstAllocationParams params = { 0, mem->align, 0, 0, };
|
2013-02-15 12:08:18 +00:00
|
|
|
GstAllocator *allocator;
|
2012-07-09 14:02:50 +00:00
|
|
|
|
|
|
|
if (!gst_memory_map (mem, &sinfo, GST_MAP_READ))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (size == -1)
|
|
|
|
size = sinfo.size > offset ? sinfo.size - offset : 0;
|
|
|
|
|
|
|
|
/* use the same allocator as the memory we copy */
|
2013-02-15 12:08:18 +00:00
|
|
|
allocator = mem->allocator;
|
|
|
|
if (GST_OBJECT_FLAG_IS_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC))
|
|
|
|
allocator = NULL;
|
|
|
|
copy = gst_allocator_alloc (allocator, size, ¶ms);
|
|
|
|
|
2012-07-09 14:02:50 +00:00
|
|
|
if (!gst_memory_map (copy, &dinfo, GST_MAP_WRITE)) {
|
|
|
|
GST_CAT_WARNING (GST_CAT_MEMORY, "could not write map memory %p", copy);
|
2012-10-22 18:25:43 +00:00
|
|
|
gst_allocator_free (mem->allocator, copy);
|
2012-07-09 14:02:50 +00:00
|
|
|
gst_memory_unmap (mem, &sinfo);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
|
|
|
|
"memcpy %" G_GSSIZE_FORMAT " memory %p -> %p", size, mem, copy);
|
|
|
|
memcpy (dinfo.data, sinfo.data + offset, size);
|
|
|
|
gst_memory_unmap (copy, &dinfo);
|
|
|
|
gst_memory_unmap (mem, &sinfo);
|
|
|
|
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
_fallback_mem_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_allocator_init (GstAllocator * allocator)
|
|
|
|
{
|
2018-06-23 15:01:09 +00:00
|
|
|
allocator->priv = gst_allocator_get_instance_private (allocator);
|
2012-07-09 14:02:50 +00:00
|
|
|
|
|
|
|
allocator->mem_copy = _fallback_mem_copy;
|
|
|
|
allocator->mem_is_span = _fallback_mem_is_span;
|
|
|
|
}
|
|
|
|
|
|
|
|
G_DEFINE_BOXED_TYPE (GstAllocationParams, gst_allocation_params,
|
|
|
|
(GBoxedCopyFunc) gst_allocation_params_copy,
|
|
|
|
(GBoxedFreeFunc) gst_allocation_params_free);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_allocation_params_init:
|
|
|
|
* @params: a #GstAllocationParams
|
|
|
|
*
|
|
|
|
* Initialize @params to its default values
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_allocation_params_init (GstAllocationParams * params)
|
|
|
|
{
|
|
|
|
g_return_if_fail (params != NULL);
|
|
|
|
|
|
|
|
memset (params, 0, sizeof (GstAllocationParams));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_allocation_params_copy:
|
2017-10-22 12:35:30 +00:00
|
|
|
* @params: (transfer none) (nullable): a #GstAllocationParams
|
2012-07-09 14:02:50 +00:00
|
|
|
*
|
|
|
|
* Create a copy of @params.
|
|
|
|
*
|
|
|
|
* Free-function: gst_allocation_params_free
|
|
|
|
*
|
2017-10-22 12:35:30 +00:00
|
|
|
* Returns: (transfer full) (nullable): a new ##GstAllocationParams, free with
|
2012-07-09 14:02:50 +00:00
|
|
|
* gst_allocation_params_free().
|
|
|
|
*/
|
|
|
|
GstAllocationParams *
|
|
|
|
gst_allocation_params_copy (const GstAllocationParams * params)
|
|
|
|
{
|
|
|
|
GstAllocationParams *result = NULL;
|
|
|
|
|
|
|
|
if (params) {
|
|
|
|
result =
|
|
|
|
(GstAllocationParams *) g_slice_copy (sizeof (GstAllocationParams),
|
|
|
|
params);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_allocation_params_free:
|
|
|
|
* @params: (in) (transfer full): a #GstAllocationParams
|
|
|
|
*
|
|
|
|
* Free @params
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_allocation_params_free (GstAllocationParams * params)
|
|
|
|
{
|
|
|
|
g_slice_free (GstAllocationParams, params);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_allocator_register:
|
|
|
|
* @name: the name of the allocator
|
|
|
|
* @allocator: (transfer full): #GstAllocator
|
|
|
|
*
|
|
|
|
* Registers the memory @allocator with @name. This function takes ownership of
|
|
|
|
* @allocator.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_allocator_register (const gchar * name, GstAllocator * allocator)
|
|
|
|
{
|
|
|
|
g_return_if_fail (name != NULL);
|
|
|
|
g_return_if_fail (allocator != NULL);
|
|
|
|
|
|
|
|
GST_CAT_DEBUG (GST_CAT_MEMORY, "registering allocator %p with name \"%s\"",
|
|
|
|
allocator, name);
|
|
|
|
|
|
|
|
g_rw_lock_writer_lock (&lock);
|
2016-10-18 15:59:25 +00:00
|
|
|
/* The ref will never be released */
|
|
|
|
GST_OBJECT_FLAG_SET (allocator, GST_OBJECT_FLAG_MAY_BE_LEAKED);
|
2012-07-09 14:02:50 +00:00
|
|
|
g_hash_table_insert (allocators, (gpointer) name, (gpointer) allocator);
|
|
|
|
g_rw_lock_writer_unlock (&lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_allocator_find:
|
2013-07-03 17:03:49 +00:00
|
|
|
* @name: (allow-none): the name of the allocator
|
2012-07-09 14:02:50 +00:00
|
|
|
*
|
2014-05-29 21:54:34 +00:00
|
|
|
* Find a previously registered allocator with @name. When @name is %NULL, the
|
2012-07-09 14:02:50 +00:00
|
|
|
* default allocator will be returned.
|
|
|
|
*
|
2014-06-11 22:21:34 +00:00
|
|
|
* Returns: (transfer full) (nullable): a #GstAllocator or %NULL when
|
|
|
|
* the allocator with @name was not registered. Use gst_object_unref()
|
|
|
|
* to release the allocator after usage.
|
2012-07-09 14:02:50 +00:00
|
|
|
*/
|
|
|
|
GstAllocator *
|
|
|
|
gst_allocator_find (const gchar * name)
|
|
|
|
{
|
|
|
|
GstAllocator *allocator;
|
|
|
|
|
|
|
|
g_rw_lock_reader_lock (&lock);
|
|
|
|
if (name) {
|
|
|
|
allocator = g_hash_table_lookup (allocators, (gconstpointer) name);
|
|
|
|
} else {
|
|
|
|
allocator = _default_allocator;
|
|
|
|
}
|
|
|
|
if (allocator)
|
|
|
|
gst_object_ref (allocator);
|
|
|
|
g_rw_lock_reader_unlock (&lock);
|
|
|
|
|
|
|
|
return allocator;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_allocator_set_default:
|
|
|
|
* @allocator: (transfer full): a #GstAllocator
|
|
|
|
*
|
|
|
|
* Set the default allocator. This function takes ownership of @allocator.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_allocator_set_default (GstAllocator * allocator)
|
|
|
|
{
|
|
|
|
GstAllocator *old;
|
|
|
|
|
|
|
|
g_return_if_fail (GST_IS_ALLOCATOR (allocator));
|
|
|
|
|
|
|
|
g_rw_lock_writer_lock (&lock);
|
|
|
|
old = _default_allocator;
|
|
|
|
_default_allocator = allocator;
|
|
|
|
g_rw_lock_writer_unlock (&lock);
|
|
|
|
|
|
|
|
if (old)
|
|
|
|
gst_object_unref (old);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_allocator_alloc:
|
|
|
|
* @allocator: (transfer none) (allow-none): a #GstAllocator to use
|
|
|
|
* @size: size of the visible memory area
|
|
|
|
* @params: (transfer none) (allow-none): optional parameters
|
|
|
|
*
|
|
|
|
* Use @allocator to allocate a new memory block with memory that is at least
|
|
|
|
* @size big.
|
|
|
|
*
|
|
|
|
* The optional @params can specify the prefix and padding for the memory. If
|
2014-05-29 21:54:34 +00:00
|
|
|
* %NULL is passed, no flags, no extra prefix/padding and a default alignment is
|
2012-07-09 14:02:50 +00:00
|
|
|
* used.
|
|
|
|
*
|
|
|
|
* The prefix/padding will be filled with 0 if flags contains
|
|
|
|
* #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED respectively.
|
|
|
|
*
|
2014-05-29 21:54:34 +00:00
|
|
|
* When @allocator is %NULL, the default allocator will be used.
|
2012-07-09 14:02:50 +00:00
|
|
|
*
|
|
|
|
* The alignment in @params is given as a bitmask so that @align + 1 equals
|
|
|
|
* the amount of bytes to align to. For example, to align to 8 bytes,
|
|
|
|
* use an alignment of 7.
|
|
|
|
*
|
2017-10-22 12:35:30 +00:00
|
|
|
* Returns: (transfer full) (nullable): a new #GstMemory.
|
2012-07-09 14:02:50 +00:00
|
|
|
*/
|
|
|
|
GstMemory *
|
|
|
|
gst_allocator_alloc (GstAllocator * allocator, gsize size,
|
|
|
|
GstAllocationParams * params)
|
|
|
|
{
|
|
|
|
GstMemory *mem;
|
|
|
|
static GstAllocationParams defparams = { 0, 0, 0, 0, };
|
|
|
|
GstAllocatorClass *aclass;
|
|
|
|
|
|
|
|
if (params) {
|
|
|
|
g_return_val_if_fail (((params->align + 1) & params->align) == 0, NULL);
|
|
|
|
} else {
|
|
|
|
params = &defparams;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (allocator == NULL)
|
|
|
|
allocator = _default_allocator;
|
|
|
|
|
|
|
|
aclass = GST_ALLOCATOR_GET_CLASS (allocator);
|
|
|
|
if (aclass->alloc)
|
2012-07-27 10:12:37 +00:00
|
|
|
mem = aclass->alloc (allocator, size, params);
|
2012-07-09 14:02:50 +00:00
|
|
|
else
|
|
|
|
mem = NULL;
|
|
|
|
|
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_allocator_free:
|
|
|
|
* @allocator: (transfer none): a #GstAllocator to use
|
|
|
|
* @memory: (transfer full): the memory to free
|
|
|
|
*
|
|
|
|
* Free @memory that was previously allocated with gst_allocator_alloc().
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_allocator_free (GstAllocator * allocator, GstMemory * memory)
|
|
|
|
{
|
|
|
|
GstAllocatorClass *aclass;
|
|
|
|
|
|
|
|
g_return_if_fail (GST_IS_ALLOCATOR (allocator));
|
|
|
|
g_return_if_fail (memory != NULL);
|
|
|
|
g_return_if_fail (memory->allocator == allocator);
|
|
|
|
|
|
|
|
aclass = GST_ALLOCATOR_GET_CLASS (allocator);
|
|
|
|
if (aclass->free)
|
|
|
|
aclass->free (allocator, memory);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* default memory implementation */
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GstMemory mem;
|
|
|
|
|
|
|
|
gsize slice_size;
|
|
|
|
guint8 *data;
|
|
|
|
|
|
|
|
gpointer user_data;
|
|
|
|
GDestroyNotify notify;
|
2013-02-26 16:33:30 +00:00
|
|
|
} GstMemorySystem;
|
2012-07-09 14:02:50 +00:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GstAllocator parent;
|
2013-02-26 16:33:30 +00:00
|
|
|
} GstAllocatorSysmem;
|
2012-07-09 14:02:50 +00:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GstAllocatorClass parent_class;
|
2013-02-26 16:33:30 +00:00
|
|
|
} GstAllocatorSysmemClass;
|
2012-07-09 14:02:50 +00:00
|
|
|
|
2017-08-14 10:01:19 +00:00
|
|
|
static GType gst_allocator_sysmem_get_type (void);
|
2013-02-26 16:33:30 +00:00
|
|
|
G_DEFINE_TYPE (GstAllocatorSysmem, gst_allocator_sysmem, GST_TYPE_ALLOCATOR);
|
2012-07-09 14:02:50 +00:00
|
|
|
|
|
|
|
/* initialize the fields */
|
|
|
|
static inline void
|
2013-02-26 16:33:30 +00:00
|
|
|
_sysmem_init (GstMemorySystem * mem, GstMemoryFlags flags,
|
2012-07-09 14:02:50 +00:00
|
|
|
GstMemory * parent, gsize slice_size,
|
|
|
|
gpointer data, gsize maxsize, gsize align, gsize offset, gsize size,
|
|
|
|
gpointer user_data, GDestroyNotify notify)
|
|
|
|
{
|
|
|
|
gst_memory_init (GST_MEMORY_CAST (mem),
|
|
|
|
flags, _sysmem_allocator, parent, maxsize, align, offset, size);
|
|
|
|
|
|
|
|
mem->slice_size = slice_size;
|
|
|
|
mem->data = data;
|
|
|
|
mem->user_data = user_data;
|
|
|
|
mem->notify = notify;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create a new memory block that manages the given memory */
|
2013-02-26 16:33:30 +00:00
|
|
|
static inline GstMemorySystem *
|
|
|
|
_sysmem_new (GstMemoryFlags flags,
|
2012-07-09 14:02:50 +00:00
|
|
|
GstMemory * parent, gpointer data, gsize maxsize, gsize align, gsize offset,
|
|
|
|
gsize size, gpointer user_data, GDestroyNotify notify)
|
|
|
|
{
|
2013-02-26 16:33:30 +00:00
|
|
|
GstMemorySystem *mem;
|
2012-07-09 14:02:50 +00:00
|
|
|
gsize slice_size;
|
|
|
|
|
2013-02-26 16:33:30 +00:00
|
|
|
slice_size = sizeof (GstMemorySystem);
|
2012-07-09 14:02:50 +00:00
|
|
|
|
|
|
|
mem = g_slice_alloc (slice_size);
|
2013-02-26 16:33:30 +00:00
|
|
|
_sysmem_init (mem, flags, parent, slice_size,
|
2012-07-09 14:02:50 +00:00
|
|
|
data, maxsize, align, offset, size, user_data, notify);
|
|
|
|
|
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate the memory and structure in one block */
|
2013-02-26 16:33:30 +00:00
|
|
|
static GstMemorySystem *
|
|
|
|
_sysmem_new_block (GstMemoryFlags flags,
|
2012-07-09 14:02:50 +00:00
|
|
|
gsize maxsize, gsize align, gsize offset, gsize size)
|
|
|
|
{
|
2013-02-26 16:33:30 +00:00
|
|
|
GstMemorySystem *mem;
|
2012-07-09 14:02:50 +00:00
|
|
|
gsize aoffset, slice_size, padding;
|
|
|
|
guint8 *data;
|
|
|
|
|
|
|
|
/* ensure configured alignment */
|
|
|
|
align |= gst_memory_alignment;
|
|
|
|
/* allocate more to compensate for alignment */
|
|
|
|
maxsize += align;
|
|
|
|
/* alloc header and data in one block */
|
2013-02-26 16:33:30 +00:00
|
|
|
slice_size = sizeof (GstMemorySystem) + maxsize;
|
2012-07-09 14:02:50 +00:00
|
|
|
|
|
|
|
mem = g_slice_alloc (slice_size);
|
|
|
|
if (mem == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2013-02-26 16:33:30 +00:00
|
|
|
data = (guint8 *) mem + sizeof (GstMemorySystem);
|
2012-07-09 14:02:50 +00:00
|
|
|
|
|
|
|
/* do alignment */
|
|
|
|
if ((aoffset = ((guintptr) data & align))) {
|
|
|
|
aoffset = (align + 1) - aoffset;
|
|
|
|
data += aoffset;
|
|
|
|
maxsize -= aoffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (offset && (flags & GST_MEMORY_FLAG_ZERO_PREFIXED))
|
|
|
|
memset (data, 0, offset);
|
|
|
|
|
|
|
|
padding = maxsize - (offset + size);
|
|
|
|
if (padding && (flags & GST_MEMORY_FLAG_ZERO_PADDED))
|
|
|
|
memset (data + offset + size, 0, padding);
|
|
|
|
|
2013-02-26 16:33:30 +00:00
|
|
|
_sysmem_init (mem, flags, NULL, slice_size, data, maxsize,
|
2012-07-09 14:02:50 +00:00
|
|
|
align, offset, size, NULL, NULL);
|
|
|
|
|
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gpointer
|
2013-02-26 16:33:30 +00:00
|
|
|
_sysmem_map (GstMemorySystem * mem, gsize maxsize, GstMapFlags flags)
|
2012-07-09 14:02:50 +00:00
|
|
|
{
|
|
|
|
return mem->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2013-02-26 16:33:30 +00:00
|
|
|
_sysmem_unmap (GstMemorySystem * mem)
|
2012-07-09 14:02:50 +00:00
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-02-26 16:33:30 +00:00
|
|
|
static GstMemorySystem *
|
|
|
|
_sysmem_copy (GstMemorySystem * mem, gssize offset, gsize size)
|
2012-07-09 14:02:50 +00:00
|
|
|
{
|
2013-02-26 16:33:30 +00:00
|
|
|
GstMemorySystem *copy;
|
2012-07-09 14:02:50 +00:00
|
|
|
|
|
|
|
if (size == -1)
|
|
|
|
size = mem->mem.size > offset ? mem->mem.size - offset : 0;
|
|
|
|
|
2013-08-13 11:06:50 +00:00
|
|
|
copy = _sysmem_new_block (0, size, mem->mem.align, 0, size);
|
2012-07-09 14:02:50 +00:00
|
|
|
GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
|
2013-08-13 11:06:50 +00:00
|
|
|
"memcpy %" G_GSIZE_FORMAT " memory %p -> %p", size, mem, copy);
|
|
|
|
memcpy (copy->data, mem->data + mem->mem.offset + offset, size);
|
2012-07-09 14:02:50 +00:00
|
|
|
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
2013-02-26 16:33:30 +00:00
|
|
|
static GstMemorySystem *
|
|
|
|
_sysmem_share (GstMemorySystem * mem, gssize offset, gsize size)
|
2012-07-09 14:02:50 +00:00
|
|
|
{
|
2013-02-26 16:33:30 +00:00
|
|
|
GstMemorySystem *sub;
|
2012-07-09 14:02:50 +00:00
|
|
|
GstMemory *parent;
|
|
|
|
|
|
|
|
/* find the real parent */
|
|
|
|
if ((parent = mem->mem.parent) == NULL)
|
|
|
|
parent = (GstMemory *) mem;
|
|
|
|
|
|
|
|
if (size == -1)
|
|
|
|
size = mem->mem.size - offset;
|
|
|
|
|
|
|
|
/* the shared memory is always readonly */
|
|
|
|
sub =
|
2013-02-26 16:33:30 +00:00
|
|
|
_sysmem_new (GST_MINI_OBJECT_FLAGS (parent) |
|
2012-07-09 14:02:50 +00:00
|
|
|
GST_MINI_OBJECT_FLAG_LOCK_READONLY, parent, mem->data, mem->mem.maxsize,
|
|
|
|
mem->mem.align, mem->mem.offset + offset, size, NULL, NULL);
|
|
|
|
|
|
|
|
return sub;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2013-02-26 16:33:30 +00:00
|
|
|
_sysmem_is_span (GstMemorySystem * mem1, GstMemorySystem * mem2, gsize * offset)
|
2012-07-09 14:02:50 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
if (offset) {
|
2013-02-26 16:33:30 +00:00
|
|
|
GstMemorySystem *parent;
|
2012-07-09 14:02:50 +00:00
|
|
|
|
2013-02-26 16:33:30 +00:00
|
|
|
parent = (GstMemorySystem *) mem1->mem.parent;
|
2012-07-09 14:02:50 +00:00
|
|
|
|
|
|
|
*offset = mem1->mem.offset - parent->mem.offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* and memory is contiguous */
|
|
|
|
return mem1->data + mem1->mem.offset + mem1->mem.size ==
|
|
|
|
mem2->data + mem2->mem.offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstMemory *
|
|
|
|
default_alloc (GstAllocator * allocator, gsize size,
|
2012-07-27 10:12:37 +00:00
|
|
|
GstAllocationParams * params)
|
2012-07-09 14:02:50 +00:00
|
|
|
{
|
|
|
|
gsize maxsize = size + params->prefix + params->padding;
|
|
|
|
|
2013-02-26 16:33:30 +00:00
|
|
|
return (GstMemory *) _sysmem_new_block (params->flags,
|
2012-07-09 14:02:50 +00:00
|
|
|
maxsize, params->align, params->prefix, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
default_free (GstAllocator * allocator, GstMemory * mem)
|
|
|
|
{
|
2013-02-26 16:33:30 +00:00
|
|
|
GstMemorySystem *dmem = (GstMemorySystem *) mem;
|
2012-08-04 12:37:32 +00:00
|
|
|
gsize slice_size;
|
2012-07-09 14:02:50 +00:00
|
|
|
|
|
|
|
if (dmem->notify)
|
|
|
|
dmem->notify (dmem->user_data);
|
|
|
|
|
2012-08-04 12:37:32 +00:00
|
|
|
slice_size = dmem->slice_size;
|
|
|
|
|
|
|
|
#ifdef USE_POISONING
|
|
|
|
/* just poison the structs, not all the data */
|
2013-02-26 16:33:30 +00:00
|
|
|
memset (mem, 0xff, sizeof (GstMemorySystem));
|
2012-08-04 12:37:32 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
g_slice_free1 (slice_size, mem);
|
2012-07-09 14:02:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-02-26 16:33:30 +00:00
|
|
|
gst_allocator_sysmem_finalize (GObject * obj)
|
2012-07-09 14:02:50 +00:00
|
|
|
{
|
2016-04-18 10:05:40 +00:00
|
|
|
/* Don't raise warnings if we are shutting down */
|
|
|
|
if (_default_allocator)
|
|
|
|
g_warning ("The default memory allocator was freed!");
|
|
|
|
|
|
|
|
((GObjectClass *) gst_allocator_sysmem_parent_class)->finalize (obj);
|
2012-07-09 14:02:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-02-26 16:33:30 +00:00
|
|
|
gst_allocator_sysmem_class_init (GstAllocatorSysmemClass * klass)
|
2012-07-09 14:02:50 +00:00
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
GstAllocatorClass *allocator_class;
|
|
|
|
|
|
|
|
gobject_class = (GObjectClass *) klass;
|
|
|
|
allocator_class = (GstAllocatorClass *) klass;
|
|
|
|
|
2013-02-26 16:33:30 +00:00
|
|
|
gobject_class->finalize = gst_allocator_sysmem_finalize;
|
2012-07-09 14:02:50 +00:00
|
|
|
|
|
|
|
allocator_class->alloc = default_alloc;
|
|
|
|
allocator_class->free = default_free;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-02-26 16:33:30 +00:00
|
|
|
gst_allocator_sysmem_init (GstAllocatorSysmem * allocator)
|
2012-07-09 14:02:50 +00:00
|
|
|
{
|
|
|
|
GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
|
|
|
|
|
|
|
|
GST_CAT_DEBUG (GST_CAT_MEMORY, "init allocator %p", allocator);
|
|
|
|
|
|
|
|
alloc->mem_type = GST_ALLOCATOR_SYSMEM;
|
2013-02-26 16:33:30 +00:00
|
|
|
alloc->mem_map = (GstMemoryMapFunction) _sysmem_map;
|
|
|
|
alloc->mem_unmap = (GstMemoryUnmapFunction) _sysmem_unmap;
|
|
|
|
alloc->mem_copy = (GstMemoryCopyFunction) _sysmem_copy;
|
|
|
|
alloc->mem_share = (GstMemoryShareFunction) _sysmem_share;
|
|
|
|
alloc->mem_is_span = (GstMemoryIsSpanFunction) _sysmem_is_span;
|
2012-07-09 14:02:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-06-19 06:05:03 +00:00
|
|
|
_priv_gst_allocator_initialize (void)
|
2012-07-09 14:02:50 +00:00
|
|
|
{
|
|
|
|
g_rw_lock_init (&lock);
|
2016-04-18 10:05:40 +00:00
|
|
|
allocators = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
|
|
|
|
gst_object_unref);
|
2012-07-09 14:02:50 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_GETPAGESIZE
|
|
|
|
#ifdef MEMORY_ALIGNMENT_PAGESIZE
|
|
|
|
gst_memory_alignment = getpagesize () - 1;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
GST_CAT_DEBUG (GST_CAT_MEMORY, "memory alignment: %" G_GSIZE_FORMAT,
|
|
|
|
gst_memory_alignment);
|
|
|
|
|
2013-02-26 16:33:30 +00:00
|
|
|
_sysmem_allocator = g_object_new (gst_allocator_sysmem_get_type (), NULL);
|
2012-07-09 14:02:50 +00:00
|
|
|
|
2017-05-15 11:34:57 +00:00
|
|
|
/* Clear floating flag */
|
|
|
|
gst_object_ref_sink (_sysmem_allocator);
|
|
|
|
|
2012-07-09 14:02:50 +00:00
|
|
|
gst_allocator_register (GST_ALLOCATOR_SYSMEM,
|
|
|
|
gst_object_ref (_sysmem_allocator));
|
|
|
|
|
|
|
|
_default_allocator = gst_object_ref (_sysmem_allocator);
|
|
|
|
}
|
|
|
|
|
2016-04-18 10:05:40 +00:00
|
|
|
void
|
|
|
|
_priv_gst_allocator_cleanup (void)
|
|
|
|
{
|
|
|
|
gst_object_unref (_sysmem_allocator);
|
|
|
|
_sysmem_allocator = NULL;
|
|
|
|
|
|
|
|
gst_object_unref (_default_allocator);
|
|
|
|
_default_allocator = NULL;
|
|
|
|
|
|
|
|
g_clear_pointer (&allocators, g_hash_table_unref);
|
|
|
|
}
|
|
|
|
|
2012-07-09 14:02:50 +00:00
|
|
|
/**
|
|
|
|
* gst_memory_new_wrapped:
|
|
|
|
* @flags: #GstMemoryFlags
|
2013-03-26 23:22:18 +00:00
|
|
|
* @data: (array length=size) (element-type guint8) (transfer none): data to
|
|
|
|
* wrap
|
2012-07-09 14:02:50 +00:00
|
|
|
* @maxsize: allocated size of @data
|
|
|
|
* @offset: offset in @data
|
|
|
|
* @size: size of valid data
|
2013-03-26 23:22:18 +00:00
|
|
|
* @user_data: (allow-none): user_data
|
|
|
|
* @notify: (allow-none) (scope async) (closure user_data): called with @user_data when the memory is freed
|
2012-07-09 14:02:50 +00:00
|
|
|
*
|
|
|
|
* Allocate a new memory block that wraps the given @data.
|
|
|
|
*
|
|
|
|
* The prefix/padding must be filled with 0 if @flags contains
|
|
|
|
* #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED respectively.
|
|
|
|
*
|
2017-10-22 12:35:30 +00:00
|
|
|
* Returns: (transfer full) (nullable): a new #GstMemory.
|
2012-07-09 14:02:50 +00:00
|
|
|
*/
|
|
|
|
GstMemory *
|
|
|
|
gst_memory_new_wrapped (GstMemoryFlags flags, gpointer data,
|
|
|
|
gsize maxsize, gsize offset, gsize size, gpointer user_data,
|
|
|
|
GDestroyNotify notify)
|
|
|
|
{
|
2013-02-26 16:33:30 +00:00
|
|
|
GstMemorySystem *mem;
|
2012-07-09 14:02:50 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (data != NULL, NULL);
|
|
|
|
g_return_val_if_fail (offset + size <= maxsize, NULL);
|
|
|
|
|
|
|
|
mem =
|
2013-02-26 16:33:30 +00:00
|
|
|
_sysmem_new (flags, NULL, data, maxsize, 0, offset, size, user_data,
|
2012-07-09 14:02:50 +00:00
|
|
|
notify);
|
|
|
|
|
|
|
|
return (GstMemory *) mem;
|
|
|
|
}
|