Blame gst/gstallocator.c

Packit f546b1
/* GStreamer
Packit f546b1
 * Copyright (C) 2011 Wim Taymans <wim.taymans@gmail.be>
Packit f546b1
 *
Packit f546b1
 * gstallocator.c: memory block allocator
Packit f546b1
 *
Packit f546b1
 * This library is free software; you can redistribute it and/or
Packit f546b1
 * modify it under the terms of the GNU Library General Public
Packit f546b1
 * License as published by the Free Software Foundation; either
Packit f546b1
 * version 2 of the License, or (at your option) any later version.
Packit f546b1
 *
Packit f546b1
 * This library is distributed in the hope that it will be useful,
Packit f546b1
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit f546b1
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit f546b1
 * Library General Public License for more details.
Packit f546b1
 *
Packit f546b1
 * You should have received a copy of the GNU Library General Public
Packit f546b1
 * License along with this library; if not, write to the
Packit f546b1
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit f546b1
 * Boston, MA 02110-1301, USA.
Packit f546b1
 */
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * SECTION:gstallocator
Packit f546b1
 * @title: GstAllocator
Packit f546b1
 * @short_description: allocate memory blocks
Packit f546b1
 * @see_also: #GstMemory
Packit f546b1
 *
Packit f546b1
 * Memory is usually created by allocators with a gst_allocator_alloc()
Packit f546b1
 * method call. When %NULL is used as the allocator, the default allocator will
Packit f546b1
 * be used.
Packit f546b1
 *
Packit f546b1
 * New allocators can be registered with gst_allocator_register().
Packit f546b1
 * Allocators are identified by name and can be retrieved with
Packit f546b1
 * gst_allocator_find(). gst_allocator_set_default() can be used to change the
Packit f546b1
 * default allocator.
Packit f546b1
 *
Packit f546b1
 * New memory can be created with gst_memory_new_wrapped() that wraps the memory
Packit f546b1
 * allocated elsewhere.
Packit f546b1
 */
Packit f546b1
Packit f546b1
#ifdef HAVE_CONFIG_H
Packit f546b1
#include "config.h"
Packit f546b1
#endif
Packit f546b1
Packit f546b1
#include "gst_private.h"
Packit f546b1
#include "gstmemory.h"
Packit f546b1
Packit f546b1
GST_DEBUG_CATEGORY_STATIC (gst_allocator_debug);
Packit f546b1
#define GST_CAT_DEFAULT gst_allocator_debug
Packit f546b1
Packit f546b1
#define GST_ALLOCATOR_GET_PRIVATE(obj)  \
Packit f546b1
     (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_ALLOCATOR, GstAllocatorPrivate))
Packit f546b1
Packit f546b1
struct _GstAllocatorPrivate
Packit f546b1
{
Packit f546b1
  gpointer dummy;
Packit f546b1
};
Packit f546b1
Packit f546b1
#if defined(MEMORY_ALIGNMENT_MALLOC)
Packit f546b1
gsize gst_memory_alignment = 7;
Packit f546b1
#elif defined(MEMORY_ALIGNMENT_PAGESIZE)
Packit f546b1
/* we fill this in in the _init method */
Packit f546b1
gsize gst_memory_alignment = 0;
Packit f546b1
#elif defined(MEMORY_ALIGNMENT)
Packit f546b1
gsize gst_memory_alignment = MEMORY_ALIGNMENT - 1;
Packit f546b1
#else
Packit f546b1
#error "No memory alignment configured"
Packit f546b1
gsize gst_memory_alignment = 0;
Packit f546b1
#endif
Packit f546b1
Packit f546b1
/* the default allocator */
Packit f546b1
static GstAllocator *_default_allocator;
Packit f546b1
Packit f546b1
static GstAllocator *_sysmem_allocator;
Packit f546b1
Packit f546b1
/* registered allocators */
Packit f546b1
static GRWLock lock;
Packit f546b1
static GHashTable *allocators;
Packit f546b1
Packit f546b1
G_DEFINE_ABSTRACT_TYPE (GstAllocator, gst_allocator, GST_TYPE_OBJECT);
Packit f546b1
Packit f546b1
static void
Packit f546b1
gst_allocator_class_init (GstAllocatorClass * klass)
Packit f546b1
{
Packit f546b1
  g_type_class_add_private (klass, sizeof (GstAllocatorPrivate));
Packit f546b1
Packit f546b1
  GST_DEBUG_CATEGORY_INIT (gst_allocator_debug, "allocator", 0,
Packit f546b1
      "allocator debug");
Packit f546b1
}
Packit f546b1
Packit f546b1
static GstMemory *
Packit f546b1
_fallback_mem_copy (GstMemory * mem, gssize offset, gssize size)
Packit f546b1
{
Packit f546b1
  GstMemory *copy;
Packit f546b1
  GstMapInfo sinfo, dinfo;
Packit f546b1
  GstAllocationParams params = { 0, mem->align, 0, 0, };
Packit f546b1
  GstAllocator *allocator;
Packit f546b1
Packit f546b1
  if (!gst_memory_map (mem, &sinfo, GST_MAP_READ))
Packit f546b1
    return NULL;
Packit f546b1
Packit f546b1
  if (size == -1)
Packit f546b1
    size = sinfo.size > offset ? sinfo.size - offset : 0;
Packit f546b1
Packit f546b1
  /* use the same allocator as the memory we copy  */
Packit f546b1
  allocator = mem->allocator;
Packit f546b1
  if (GST_OBJECT_FLAG_IS_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC))
Packit f546b1
    allocator = NULL;
Packit f546b1
  copy = gst_allocator_alloc (allocator, size, &params);
Packit f546b1
Packit f546b1
  if (!gst_memory_map (copy, &dinfo, GST_MAP_WRITE)) {
Packit f546b1
    GST_CAT_WARNING (GST_CAT_MEMORY, "could not write map memory %p", copy);
Packit f546b1
    gst_allocator_free (mem->allocator, copy);
Packit f546b1
    gst_memory_unmap (mem, &sinfo);
Packit f546b1
    return NULL;
Packit f546b1
  }
Packit f546b1
Packit f546b1
  GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
Packit f546b1
      "memcpy %" G_GSSIZE_FORMAT " memory %p -> %p", size, mem, copy);
Packit f546b1
  memcpy (dinfo.data, sinfo.data + offset, size);
Packit f546b1
  gst_memory_unmap (copy, &dinfo);
Packit f546b1
  gst_memory_unmap (mem, &sinfo);
Packit f546b1
Packit f546b1
  return copy;
Packit f546b1
}
Packit f546b1
Packit f546b1
static gboolean
Packit f546b1
_fallback_mem_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
Packit f546b1
{
Packit f546b1
  return FALSE;
Packit f546b1
}
Packit f546b1
Packit f546b1
static void
Packit f546b1
gst_allocator_init (GstAllocator * allocator)
Packit f546b1
{
Packit f546b1
  allocator->priv = GST_ALLOCATOR_GET_PRIVATE (allocator);
Packit f546b1
Packit f546b1
  allocator->mem_copy = _fallback_mem_copy;
Packit f546b1
  allocator->mem_is_span = _fallback_mem_is_span;
Packit f546b1
}
Packit f546b1
Packit f546b1
G_DEFINE_BOXED_TYPE (GstAllocationParams, gst_allocation_params,
Packit f546b1
    (GBoxedCopyFunc) gst_allocation_params_copy,
Packit f546b1
    (GBoxedFreeFunc) gst_allocation_params_free);
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * gst_allocation_params_init:
Packit f546b1
 * @params: a #GstAllocationParams
Packit f546b1
 *
Packit f546b1
 * Initialize @params to its default values
Packit f546b1
 */
Packit f546b1
void
Packit f546b1
gst_allocation_params_init (GstAllocationParams * params)
Packit f546b1
{
Packit f546b1
  g_return_if_fail (params != NULL);
Packit f546b1
Packit f546b1
  memset (params, 0, sizeof (GstAllocationParams));
Packit f546b1
}
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * gst_allocation_params_copy:
Packit f546b1
 * @params: (transfer none) (nullable): a #GstAllocationParams
Packit f546b1
 *
Packit f546b1
 * Create a copy of @params.
Packit f546b1
 *
Packit f546b1
 * Free-function: gst_allocation_params_free
Packit f546b1
 *
Packit f546b1
 * Returns: (transfer full) (nullable): a new ##GstAllocationParams, free with
Packit f546b1
 * gst_allocation_params_free().
Packit f546b1
 */
Packit f546b1
GstAllocationParams *
Packit f546b1
gst_allocation_params_copy (const GstAllocationParams * params)
Packit f546b1
{
Packit f546b1
  GstAllocationParams *result = NULL;
Packit f546b1
Packit f546b1
  if (params) {
Packit f546b1
    result =
Packit f546b1
        (GstAllocationParams *) g_slice_copy (sizeof (GstAllocationParams),
Packit f546b1
        params);
Packit f546b1
  }
Packit f546b1
  return result;
Packit f546b1
}
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * gst_allocation_params_free:
Packit f546b1
 * @params: (in) (transfer full): a #GstAllocationParams
Packit f546b1
 *
Packit f546b1
 * Free @params
Packit f546b1
 */
Packit f546b1
void
Packit f546b1
gst_allocation_params_free (GstAllocationParams * params)
Packit f546b1
{
Packit f546b1
  g_slice_free (GstAllocationParams, params);
Packit f546b1
}
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * gst_allocator_register:
Packit f546b1
 * @name: the name of the allocator
Packit f546b1
 * @allocator: (transfer full): #GstAllocator
Packit f546b1
 *
Packit f546b1
 * Registers the memory @allocator with @name. This function takes ownership of
Packit f546b1
 * @allocator.
Packit f546b1
 */
Packit f546b1
void
Packit f546b1
gst_allocator_register (const gchar * name, GstAllocator * allocator)
Packit f546b1
{
Packit f546b1
  g_return_if_fail (name != NULL);
Packit f546b1
  g_return_if_fail (allocator != NULL);
Packit f546b1
Packit f546b1
  GST_CAT_DEBUG (GST_CAT_MEMORY, "registering allocator %p with name \"%s\"",
Packit f546b1
      allocator, name);
Packit f546b1
Packit f546b1
  g_rw_lock_writer_lock (&lock);
Packit f546b1
  /* The ref will never be released */
Packit f546b1
  GST_OBJECT_FLAG_SET (allocator, GST_OBJECT_FLAG_MAY_BE_LEAKED);
Packit f546b1
  g_hash_table_insert (allocators, (gpointer) name, (gpointer) allocator);
Packit f546b1
  g_rw_lock_writer_unlock (&lock);
Packit f546b1
}
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * gst_allocator_find:
Packit f546b1
 * @name: (allow-none): the name of the allocator
Packit f546b1
 *
Packit f546b1
 * Find a previously registered allocator with @name. When @name is %NULL, the
Packit f546b1
 * default allocator will be returned.
Packit f546b1
 *
Packit f546b1
 * Returns: (transfer full) (nullable): a #GstAllocator or %NULL when
Packit f546b1
 * the allocator with @name was not registered. Use gst_object_unref()
Packit f546b1
 * to release the allocator after usage.
Packit f546b1
 */
Packit f546b1
GstAllocator *
Packit f546b1
gst_allocator_find (const gchar * name)
Packit f546b1
{
Packit f546b1
  GstAllocator *allocator;
Packit f546b1
Packit f546b1
  g_rw_lock_reader_lock (&lock);
Packit f546b1
  if (name) {
Packit f546b1
    allocator = g_hash_table_lookup (allocators, (gconstpointer) name);
Packit f546b1
  } else {
Packit f546b1
    allocator = _default_allocator;
Packit f546b1
  }
Packit f546b1
  if (allocator)
Packit f546b1
    gst_object_ref (allocator);
Packit f546b1
  g_rw_lock_reader_unlock (&lock);
Packit f546b1
Packit f546b1
  return allocator;
Packit f546b1
}
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * gst_allocator_set_default:
Packit f546b1
 * @allocator: (transfer full): a #GstAllocator
Packit f546b1
 *
Packit f546b1
 * Set the default allocator. This function takes ownership of @allocator.
Packit f546b1
 */
Packit f546b1
void
Packit f546b1
gst_allocator_set_default (GstAllocator * allocator)
Packit f546b1
{
Packit f546b1
  GstAllocator *old;
Packit f546b1
Packit f546b1
  g_return_if_fail (GST_IS_ALLOCATOR (allocator));
Packit f546b1
Packit f546b1
  g_rw_lock_writer_lock (&lock);
Packit f546b1
  old = _default_allocator;
Packit f546b1
  _default_allocator = allocator;
Packit f546b1
  g_rw_lock_writer_unlock (&lock);
Packit f546b1
Packit f546b1
  if (old)
Packit f546b1
    gst_object_unref (old);
Packit f546b1
}
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * gst_allocator_alloc:
Packit f546b1
 * @allocator: (transfer none) (allow-none): a #GstAllocator to use
Packit f546b1
 * @size: size of the visible memory area
Packit f546b1
 * @params: (transfer none) (allow-none): optional parameters
Packit f546b1
 *
Packit f546b1
 * Use @allocator to allocate a new memory block with memory that is at least
Packit f546b1
 * @size big.
Packit f546b1
 *
Packit f546b1
 * The optional @params can specify the prefix and padding for the memory. If
Packit f546b1
 * %NULL is passed, no flags, no extra prefix/padding and a default alignment is
Packit f546b1
 * used.
Packit f546b1
 *
Packit f546b1
 * The prefix/padding will be filled with 0 if flags contains
Packit f546b1
 * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED respectively.
Packit f546b1
 *
Packit f546b1
 * When @allocator is %NULL, the default allocator will be used.
Packit f546b1
 *
Packit f546b1
 * The alignment in @params is given as a bitmask so that @align + 1 equals
Packit f546b1
 * the amount of bytes to align to. For example, to align to 8 bytes,
Packit f546b1
 * use an alignment of 7.
Packit f546b1
 *
Packit f546b1
 * Returns: (transfer full) (nullable): a new #GstMemory.
Packit f546b1
 */
Packit f546b1
GstMemory *
Packit f546b1
gst_allocator_alloc (GstAllocator * allocator, gsize size,
Packit f546b1
    GstAllocationParams * params)
Packit f546b1
{
Packit f546b1
  GstMemory *mem;
Packit f546b1
  static GstAllocationParams defparams = { 0, 0, 0, 0, };
Packit f546b1
  GstAllocatorClass *aclass;
Packit f546b1
Packit f546b1
  if (params) {
Packit f546b1
    g_return_val_if_fail (((params->align + 1) & params->align) == 0, NULL);
Packit f546b1
  } else {
Packit f546b1
    params = &defparams;
Packit f546b1
  }
Packit f546b1
Packit f546b1
  if (allocator == NULL)
Packit f546b1
    allocator = _default_allocator;
Packit f546b1
Packit f546b1
  aclass = GST_ALLOCATOR_GET_CLASS (allocator);
Packit f546b1
  if (aclass->alloc)
Packit f546b1
    mem = aclass->alloc (allocator, size, params);
Packit f546b1
  else
Packit f546b1
    mem = NULL;
Packit f546b1
Packit f546b1
  return mem;
Packit f546b1
}
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * gst_allocator_free:
Packit f546b1
 * @allocator: (transfer none): a #GstAllocator to use
Packit f546b1
 * @memory: (transfer full): the memory to free
Packit f546b1
 *
Packit f546b1
 * Free @memory that was previously allocated with gst_allocator_alloc().
Packit f546b1
 */
Packit f546b1
void
Packit f546b1
gst_allocator_free (GstAllocator * allocator, GstMemory * memory)
Packit f546b1
{
Packit f546b1
  GstAllocatorClass *aclass;
Packit f546b1
Packit f546b1
  g_return_if_fail (GST_IS_ALLOCATOR (allocator));
Packit f546b1
  g_return_if_fail (memory != NULL);
Packit f546b1
  g_return_if_fail (memory->allocator == allocator);
Packit f546b1
Packit f546b1
  aclass = GST_ALLOCATOR_GET_CLASS (allocator);
Packit f546b1
  if (aclass->free)
Packit f546b1
    aclass->free (allocator, memory);
Packit f546b1
}
Packit f546b1
Packit f546b1
/* default memory implementation */
Packit f546b1
typedef struct
Packit f546b1
{
Packit f546b1
  GstMemory mem;
Packit f546b1
Packit f546b1
  gsize slice_size;
Packit f546b1
  guint8 *data;
Packit f546b1
Packit f546b1
  gpointer user_data;
Packit f546b1
  GDestroyNotify notify;
Packit f546b1
} GstMemorySystem;
Packit f546b1
Packit f546b1
typedef struct
Packit f546b1
{
Packit f546b1
  GstAllocator parent;
Packit f546b1
} GstAllocatorSysmem;
Packit f546b1
Packit f546b1
typedef struct
Packit f546b1
{
Packit f546b1
  GstAllocatorClass parent_class;
Packit f546b1
} GstAllocatorSysmemClass;
Packit f546b1
Packit f546b1
static GType gst_allocator_sysmem_get_type (void);
Packit f546b1
G_DEFINE_TYPE (GstAllocatorSysmem, gst_allocator_sysmem, GST_TYPE_ALLOCATOR);
Packit f546b1
Packit f546b1
/* initialize the fields */
Packit f546b1
static inline void
Packit f546b1
_sysmem_init (GstMemorySystem * mem, GstMemoryFlags flags,
Packit f546b1
    GstMemory * parent, gsize slice_size,
Packit f546b1
    gpointer data, gsize maxsize, gsize align, gsize offset, gsize size,
Packit f546b1
    gpointer user_data, GDestroyNotify notify)
Packit f546b1
{
Packit f546b1
  gst_memory_init (GST_MEMORY_CAST (mem),
Packit f546b1
      flags, _sysmem_allocator, parent, maxsize, align, offset, size);
Packit f546b1
Packit f546b1
  mem->slice_size = slice_size;
Packit f546b1
  mem->data = data;
Packit f546b1
  mem->user_data = user_data;
Packit f546b1
  mem->notify = notify;
Packit f546b1
}
Packit f546b1
Packit f546b1
/* create a new memory block that manages the given memory */
Packit f546b1
static inline GstMemorySystem *
Packit f546b1
_sysmem_new (GstMemoryFlags flags,
Packit f546b1
    GstMemory * parent, gpointer data, gsize maxsize, gsize align, gsize offset,
Packit f546b1
    gsize size, gpointer user_data, GDestroyNotify notify)
Packit f546b1
{
Packit f546b1
  GstMemorySystem *mem;
Packit f546b1
  gsize slice_size;
Packit f546b1
Packit f546b1
  slice_size = sizeof (GstMemorySystem);
Packit f546b1
Packit f546b1
  mem = g_slice_alloc (slice_size);
Packit f546b1
  _sysmem_init (mem, flags, parent, slice_size,
Packit f546b1
      data, maxsize, align, offset, size, user_data, notify);
Packit f546b1
Packit f546b1
  return mem;
Packit f546b1
}
Packit f546b1
Packit f546b1
/* allocate the memory and structure in one block */
Packit f546b1
static GstMemorySystem *
Packit f546b1
_sysmem_new_block (GstMemoryFlags flags,
Packit f546b1
    gsize maxsize, gsize align, gsize offset, gsize size)
Packit f546b1
{
Packit f546b1
  GstMemorySystem *mem;
Packit f546b1
  gsize aoffset, slice_size, padding;
Packit f546b1
  guint8 *data;
Packit f546b1
Packit f546b1
  /* ensure configured alignment */
Packit f546b1
  align |= gst_memory_alignment;
Packit f546b1
  /* allocate more to compensate for alignment */
Packit f546b1
  maxsize += align;
Packit f546b1
  /* alloc header and data in one block */
Packit f546b1
  slice_size = sizeof (GstMemorySystem) + maxsize;
Packit f546b1
Packit f546b1
  mem = g_slice_alloc (slice_size);
Packit f546b1
  if (mem == NULL)
Packit f546b1
    return NULL;
Packit f546b1
Packit f546b1
  data = (guint8 *) mem + sizeof (GstMemorySystem);
Packit f546b1
Packit f546b1
  /* do alignment */
Packit f546b1
  if ((aoffset = ((guintptr) data & align))) {
Packit f546b1
    aoffset = (align + 1) - aoffset;
Packit f546b1
    data += aoffset;
Packit f546b1
    maxsize -= aoffset;
Packit f546b1
  }
Packit f546b1
Packit f546b1
  if (offset && (flags & GST_MEMORY_FLAG_ZERO_PREFIXED))
Packit f546b1
    memset (data, 0, offset);
Packit f546b1
Packit f546b1
  padding = maxsize - (offset + size);
Packit f546b1
  if (padding && (flags & GST_MEMORY_FLAG_ZERO_PADDED))
Packit f546b1
    memset (data + offset + size, 0, padding);
Packit f546b1
Packit f546b1
  _sysmem_init (mem, flags, NULL, slice_size, data, maxsize,
Packit f546b1
      align, offset, size, NULL, NULL);
Packit f546b1
Packit f546b1
  return mem;
Packit f546b1
}
Packit f546b1
Packit f546b1
static gpointer
Packit f546b1
_sysmem_map (GstMemorySystem * mem, gsize maxsize, GstMapFlags flags)
Packit f546b1
{
Packit f546b1
  return mem->data;
Packit f546b1
}
Packit f546b1
Packit f546b1
static gboolean
Packit f546b1
_sysmem_unmap (GstMemorySystem * mem)
Packit f546b1
{
Packit f546b1
  return TRUE;
Packit f546b1
}
Packit f546b1
Packit f546b1
static GstMemorySystem *
Packit f546b1
_sysmem_copy (GstMemorySystem * mem, gssize offset, gsize size)
Packit f546b1
{
Packit f546b1
  GstMemorySystem *copy;
Packit f546b1
Packit f546b1
  if (size == -1)
Packit f546b1
    size = mem->mem.size > offset ? mem->mem.size - offset : 0;
Packit f546b1
Packit f546b1
  copy = _sysmem_new_block (0, size, mem->mem.align, 0, size);
Packit f546b1
  GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
Packit f546b1
      "memcpy %" G_GSIZE_FORMAT " memory %p -> %p", size, mem, copy);
Packit f546b1
  memcpy (copy->data, mem->data + mem->mem.offset + offset, size);
Packit f546b1
Packit f546b1
  return copy;
Packit f546b1
}
Packit f546b1
Packit f546b1
static GstMemorySystem *
Packit f546b1
_sysmem_share (GstMemorySystem * mem, gssize offset, gsize size)
Packit f546b1
{
Packit f546b1
  GstMemorySystem *sub;
Packit f546b1
  GstMemory *parent;
Packit f546b1
Packit f546b1
  /* find the real parent */
Packit f546b1
  if ((parent = mem->mem.parent) == NULL)
Packit f546b1
    parent = (GstMemory *) mem;
Packit f546b1
Packit f546b1
  if (size == -1)
Packit f546b1
    size = mem->mem.size - offset;
Packit f546b1
Packit f546b1
  /* the shared memory is always readonly */
Packit f546b1
  sub =
Packit f546b1
      _sysmem_new (GST_MINI_OBJECT_FLAGS (parent) |
Packit f546b1
      GST_MINI_OBJECT_FLAG_LOCK_READONLY, parent, mem->data, mem->mem.maxsize,
Packit f546b1
      mem->mem.align, mem->mem.offset + offset, size, NULL, NULL);
Packit f546b1
Packit f546b1
  return sub;
Packit f546b1
}
Packit f546b1
Packit f546b1
static gboolean
Packit f546b1
_sysmem_is_span (GstMemorySystem * mem1, GstMemorySystem * mem2, gsize * offset)
Packit f546b1
{
Packit f546b1
Packit f546b1
  if (offset) {
Packit f546b1
    GstMemorySystem *parent;
Packit f546b1
Packit f546b1
    parent = (GstMemorySystem *) mem1->mem.parent;
Packit f546b1
Packit f546b1
    *offset = mem1->mem.offset - parent->mem.offset;
Packit f546b1
  }
Packit f546b1
Packit f546b1
  /* and memory is contiguous */
Packit f546b1
  return mem1->data + mem1->mem.offset + mem1->mem.size ==
Packit f546b1
      mem2->data + mem2->mem.offset;
Packit f546b1
}
Packit f546b1
Packit f546b1
static GstMemory *
Packit f546b1
default_alloc (GstAllocator * allocator, gsize size,
Packit f546b1
    GstAllocationParams * params)
Packit f546b1
{
Packit f546b1
  gsize maxsize = size + params->prefix + params->padding;
Packit f546b1
Packit f546b1
  return (GstMemory *) _sysmem_new_block (params->flags,
Packit f546b1
      maxsize, params->align, params->prefix, size);
Packit f546b1
}
Packit f546b1
Packit f546b1
static void
Packit f546b1
default_free (GstAllocator * allocator, GstMemory * mem)
Packit f546b1
{
Packit f546b1
  GstMemorySystem *dmem = (GstMemorySystem *) mem;
Packit f546b1
  gsize slice_size;
Packit f546b1
Packit f546b1
  if (dmem->notify)
Packit f546b1
    dmem->notify (dmem->user_data);
Packit f546b1
Packit f546b1
  slice_size = dmem->slice_size;
Packit f546b1
Packit f546b1
#ifdef USE_POISONING
Packit f546b1
  /* just poison the structs, not all the data */
Packit f546b1
  memset (mem, 0xff, sizeof (GstMemorySystem));
Packit f546b1
#endif
Packit f546b1
Packit f546b1
  g_slice_free1 (slice_size, mem);
Packit f546b1
}
Packit f546b1
Packit f546b1
static void
Packit f546b1
gst_allocator_sysmem_finalize (GObject * obj)
Packit f546b1
{
Packit f546b1
  /* Don't raise warnings if we are shutting down */
Packit f546b1
  if (_default_allocator)
Packit f546b1
    g_warning ("The default memory allocator was freed!");
Packit f546b1
Packit f546b1
  ((GObjectClass *) gst_allocator_sysmem_parent_class)->finalize (obj);
Packit f546b1
}
Packit f546b1
Packit f546b1
static void
Packit f546b1
gst_allocator_sysmem_class_init (GstAllocatorSysmemClass * klass)
Packit f546b1
{
Packit f546b1
  GObjectClass *gobject_class;
Packit f546b1
  GstAllocatorClass *allocator_class;
Packit f546b1
Packit f546b1
  gobject_class = (GObjectClass *) klass;
Packit f546b1
  allocator_class = (GstAllocatorClass *) klass;
Packit f546b1
Packit f546b1
  gobject_class->finalize = gst_allocator_sysmem_finalize;
Packit f546b1
Packit f546b1
  allocator_class->alloc = default_alloc;
Packit f546b1
  allocator_class->free = default_free;
Packit f546b1
}
Packit f546b1
Packit f546b1
static void
Packit f546b1
gst_allocator_sysmem_init (GstAllocatorSysmem * allocator)
Packit f546b1
{
Packit f546b1
  GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
Packit f546b1
Packit f546b1
  GST_CAT_DEBUG (GST_CAT_MEMORY, "init allocator %p", allocator);
Packit f546b1
Packit f546b1
  alloc->mem_type = GST_ALLOCATOR_SYSMEM;
Packit f546b1
  alloc->mem_map = (GstMemoryMapFunction) _sysmem_map;
Packit f546b1
  alloc->mem_unmap = (GstMemoryUnmapFunction) _sysmem_unmap;
Packit f546b1
  alloc->mem_copy = (GstMemoryCopyFunction) _sysmem_copy;
Packit f546b1
  alloc->mem_share = (GstMemoryShareFunction) _sysmem_share;
Packit f546b1
  alloc->mem_is_span = (GstMemoryIsSpanFunction) _sysmem_is_span;
Packit f546b1
}
Packit f546b1
Packit f546b1
void
Packit f546b1
_priv_gst_allocator_initialize (void)
Packit f546b1
{
Packit f546b1
  g_rw_lock_init (&lock);
Packit f546b1
  allocators = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
Packit f546b1
      gst_object_unref);
Packit f546b1
Packit f546b1
#ifdef HAVE_GETPAGESIZE
Packit f546b1
#ifdef MEMORY_ALIGNMENT_PAGESIZE
Packit f546b1
  gst_memory_alignment = getpagesize () - 1;
Packit f546b1
#endif
Packit f546b1
#endif
Packit f546b1
Packit f546b1
  GST_CAT_DEBUG (GST_CAT_MEMORY, "memory alignment: %" G_GSIZE_FORMAT,
Packit f546b1
      gst_memory_alignment);
Packit f546b1
Packit f546b1
  _sysmem_allocator = g_object_new (gst_allocator_sysmem_get_type (), NULL);
Packit f546b1
Packit f546b1
  /* Clear floating flag */
Packit f546b1
  gst_object_ref_sink (_sysmem_allocator);
Packit f546b1
Packit f546b1
  gst_allocator_register (GST_ALLOCATOR_SYSMEM,
Packit f546b1
      gst_object_ref (_sysmem_allocator));
Packit f546b1
Packit f546b1
  _default_allocator = gst_object_ref (_sysmem_allocator);
Packit f546b1
}
Packit f546b1
Packit f546b1
void
Packit f546b1
_priv_gst_allocator_cleanup (void)
Packit f546b1
{
Packit f546b1
  gst_object_unref (_sysmem_allocator);
Packit f546b1
  _sysmem_allocator = NULL;
Packit f546b1
Packit f546b1
  gst_object_unref (_default_allocator);
Packit f546b1
  _default_allocator = NULL;
Packit f546b1
Packit f546b1
  g_clear_pointer (&allocators, g_hash_table_unref);
Packit f546b1
}
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * gst_memory_new_wrapped:
Packit f546b1
 * @flags: #GstMemoryFlags
Packit f546b1
 * @data: (array length=size) (element-type guint8) (transfer none): data to
Packit f546b1
 *   wrap
Packit f546b1
 * @maxsize: allocated size of @data
Packit f546b1
 * @offset: offset in @data
Packit f546b1
 * @size: size of valid data
Packit f546b1
 * @user_data: (allow-none): user_data
Packit f546b1
 * @notify: (allow-none) (scope async) (closure user_data): called with @user_data when the memory is freed
Packit f546b1
 *
Packit f546b1
 * Allocate a new memory block that wraps the given @data.
Packit f546b1
 *
Packit f546b1
 * The prefix/padding must be filled with 0 if @flags contains
Packit f546b1
 * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED respectively.
Packit f546b1
 *
Packit f546b1
 * Returns: (transfer full) (nullable): a new #GstMemory.
Packit f546b1
 */
Packit f546b1
GstMemory *
Packit f546b1
gst_memory_new_wrapped (GstMemoryFlags flags, gpointer data,
Packit f546b1
    gsize maxsize, gsize offset, gsize size, gpointer user_data,
Packit f546b1
    GDestroyNotify notify)
Packit f546b1
{
Packit f546b1
  GstMemorySystem *mem;
Packit f546b1
Packit f546b1
  g_return_val_if_fail (data != NULL, NULL);
Packit f546b1
  g_return_val_if_fail (offset + size <= maxsize, NULL);
Packit f546b1
Packit f546b1
  mem =
Packit f546b1
      _sysmem_new (flags, NULL, data, maxsize, 0, offset, size, user_data,
Packit f546b1
      notify);
Packit f546b1
Packit f546b1
  return (GstMemory *) mem;
Packit f546b1
}