-/* initialize the fields */
-static inline void
-_default_mem_init (GstMemoryDefault * mem, GstMemoryFlags flags,
- 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, _default_mem_impl, 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 */
-static inline GstMemoryDefault *
-_default_mem_new (GstMemoryFlags flags, GstMemory * parent, gpointer data,
- gsize maxsize, gsize align, gsize offset, gsize size, gpointer user_data,
- GDestroyNotify notify)
-{
- GstMemoryDefault *mem;
- gsize slice_size;
-
- slice_size = sizeof (GstMemoryDefault);
-
- mem = g_slice_alloc (slice_size);
- _default_mem_init (mem, flags, parent, slice_size,
- data, maxsize, align, offset, size, user_data, notify);
-
- return mem;
-}
-
-/* allocate the memory and structure in one block */
-static GstMemoryDefault *
-_default_mem_new_block (GstMemoryFlags flags, gsize maxsize, gsize align,
- gsize offset, gsize size)
-{
- GstMemoryDefault *mem;
- 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 */
- slice_size = sizeof (GstMemoryDefault) + maxsize;
-
- mem = g_slice_alloc (slice_size);
- if (mem == NULL)
- return NULL;
-
- data = (guint8 *) mem + sizeof (GstMemoryDefault);
-
- /* 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);
-
- _default_mem_init (mem, flags, NULL, slice_size, data, maxsize,
- align, offset, size, NULL, NULL);
-
- return mem;
-}
-
-static GstMemory *
-_default_alloc_alloc (GstAllocator * allocator, gsize size,
- GstAllocationParams * params, gpointer user_data)
-{
- gsize maxsize = size + params->prefix + params->padding;
-
- return (GstMemory *) _default_mem_new_block (params->flags,
- maxsize, params->align, params->prefix, size);
-}
-
-static gpointer
-_default_mem_map (GstMemoryDefault * mem, gsize maxsize, GstMapFlags flags)
-{
- return mem->data;
-}
-
-static gboolean
-_default_mem_unmap (GstMemoryDefault * mem)
-{
- return TRUE;
-}
-
-static void
-_default_mem_free (GstMemoryDefault * mem)
-{
- if (mem->notify)
- mem->notify (mem->user_data);
-
- g_slice_free1 (mem->slice_size, mem);
-}
-
-static GstMemoryDefault *
-_default_mem_copy (GstMemoryDefault * mem, gssize offset, gsize size)
-{
- GstMemoryDefault *copy;
-
- if (size == -1)
- size = mem->mem.size > offset ? mem->mem.size - offset : 0;
-
- copy =
- _default_mem_new_block (0, mem->mem.maxsize, 0, mem->mem.offset + offset,
- size);
- GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
- "memcpy %" G_GSIZE_FORMAT " memory %p -> %p", mem->mem.maxsize, mem,
- copy);
- memcpy (copy->data, mem->data, mem->mem.maxsize);
-
- return copy;
-}
-
-static GstMemoryDefault *
-_default_mem_share (GstMemoryDefault * mem, gssize offset, gsize size)
-{
- GstMemoryDefault *sub;
- 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 =
- _default_mem_new (GST_MINI_OBJECT_FLAGS (parent) |
- 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
-_default_mem_is_span (GstMemoryDefault * mem1, GstMemoryDefault * mem2,
- gsize * offset)
-{
-
- if (offset) {
- GstMemoryDefault *parent;
-
- parent = (GstMemoryDefault *) mem1->mem.parent;
-
- *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 *
-_fallback_mem_copy (GstMemory * mem, gssize offset, gssize size)
-{
- GstMemory *copy;
- GstMapInfo sinfo, dinfo;
- GstAllocationParams params = { 0, 0, 0, mem->align, };
-
- 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 */
- copy = gst_allocator_alloc (mem->allocator, size, ¶ms);
- if (!gst_memory_map (copy, &dinfo, GST_MAP_WRITE)) {
- GST_CAT_WARNING (GST_CAT_MEMORY, "could not write map memory %p", copy);
- 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 GRWLock lock;
-static GHashTable *allocators;
-
-static void
-_priv_sysmem_free (GstMiniObject * obj)
-{
- g_warning ("The default memory allocator was freed!");
-}
-
-void
-_priv_gst_memory_initialize (void)
-{
- static const GstMemoryInfo _mem_info = {
- GST_ALLOCATOR_SYSMEM,
- (GstAllocatorAllocFunction) _default_alloc_alloc,
- (GstMemoryMapFunction) _default_mem_map,
- (GstMemoryUnmapFunction) _default_mem_unmap,
- (GstMemoryFreeFunction) _default_mem_free,
- (GstMemoryCopyFunction) _default_mem_copy,
- (GstMemoryShareFunction) _default_mem_share,
- (GstMemoryIsSpanFunction) _default_mem_is_span,
- };
-
- g_rw_lock_init (&lock);
- allocators = g_hash_table_new (g_str_hash, g_str_equal);
-
-#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);
-
- _default_mem_impl = g_slice_new (GstAllocator);
- gst_allocator_init (_default_mem_impl, 0, &_mem_info, _priv_sysmem_free);
-
- _default_allocator = gst_allocator_ref (_default_mem_impl);
- gst_allocator_register (GST_ALLOCATOR_SYSMEM,
- gst_allocator_ref (_default_mem_impl));
-}
-