2 * Copyright (C) 2011 Wim Taymans <wim.taymans@gmail.be>
4 * gstallocator.c: memory block allocator
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 * SECTION:gstallocator
24 * @short_description: allocate memory blocks
25 * @see_also: #GstMemory
27 * Memory is usually created by allocators with a gst_allocator_alloc()
28 * method call. When %NULL is used as the allocator, the default allocator will
31 * New allocators can be registered with gst_allocator_register().
32 * Allocators are identified by name and can be retrieved with
33 * gst_allocator_find(). gst_allocator_set_default() can be used to change the
36 * New memory can be created with gst_memory_new_wrapped() that wraps the memory
37 * allocated elsewhere.
44 #include "gst_private.h"
45 #include "gstmemory.h"
47 GST_DEBUG_CATEGORY_STATIC (gst_allocator_debug);
48 #define GST_CAT_DEFAULT gst_allocator_debug
50 #define GST_ALLOCATOR_GET_PRIVATE(obj) \
51 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_ALLOCATOR, GstAllocatorPrivate))
53 struct _GstAllocatorPrivate
58 #if defined(MEMORY_ALIGNMENT_MALLOC)
59 gsize gst_memory_alignment = 7;
60 #elif defined(MEMORY_ALIGNMENT_PAGESIZE)
61 /* we fill this in in the _init method */
62 gsize gst_memory_alignment = 0;
63 #elif defined(MEMORY_ALIGNMENT)
64 gsize gst_memory_alignment = MEMORY_ALIGNMENT - 1;
66 #error "No memory alignment configured"
67 gsize gst_memory_alignment = 0;
70 /* the default allocator */
71 static GstAllocator *_default_allocator;
73 static GstAllocator *_sysmem_allocator;
75 /* registered allocators */
77 static GHashTable *allocators;
79 G_DEFINE_ABSTRACT_TYPE (GstAllocator, gst_allocator, GST_TYPE_OBJECT);
82 gst_allocator_class_init (GstAllocatorClass * klass)
84 g_type_class_add_private (klass, sizeof (GstAllocatorPrivate));
86 GST_DEBUG_CATEGORY_INIT (gst_allocator_debug, "allocator", 0,
91 _fallback_mem_copy (GstMemory * mem, gssize offset, gssize size)
94 GstMapInfo sinfo, dinfo;
95 GstAllocationParams params = { 0, mem->align, 0, 0, };
96 GstAllocator *allocator;
98 if (!gst_memory_map (mem, &sinfo, GST_MAP_READ))
102 size = sinfo.size > offset ? sinfo.size - offset : 0;
104 /* use the same allocator as the memory we copy */
105 allocator = mem->allocator;
106 if (GST_OBJECT_FLAG_IS_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC))
108 copy = gst_allocator_alloc (allocator, size, ¶ms);
110 if (!gst_memory_map (copy, &dinfo, GST_MAP_WRITE)) {
111 GST_CAT_WARNING (GST_CAT_MEMORY, "could not write map memory %p", copy);
112 gst_allocator_free (mem->allocator, copy);
113 gst_memory_unmap (mem, &sinfo);
117 GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
118 "memcpy %" G_GSSIZE_FORMAT " memory %p -> %p", size, mem, copy);
119 memcpy (dinfo.data, sinfo.data + offset, size);
120 gst_memory_unmap (copy, &dinfo);
121 gst_memory_unmap (mem, &sinfo);
127 _fallback_mem_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
133 gst_allocator_init (GstAllocator * allocator)
135 allocator->priv = GST_ALLOCATOR_GET_PRIVATE (allocator);
137 allocator->mem_copy = _fallback_mem_copy;
138 allocator->mem_is_span = _fallback_mem_is_span;
141 G_DEFINE_BOXED_TYPE (GstAllocationParams, gst_allocation_params,
142 (GBoxedCopyFunc) gst_allocation_params_copy,
143 (GBoxedFreeFunc) gst_allocation_params_free);
146 * gst_allocation_params_init:
147 * @params: a #GstAllocationParams
149 * Initialize @params to its default values
152 gst_allocation_params_init (GstAllocationParams * params)
154 g_return_if_fail (params != NULL);
156 memset (params, 0, sizeof (GstAllocationParams));
160 * gst_allocation_params_copy:
161 * @params: (transfer none): a #GstAllocationParams
163 * Create a copy of @params.
165 * Free-function: gst_allocation_params_free
167 * Returns: (transfer full): a new ##GstAllocationParams, free with
168 * gst_allocation_params_free().
170 GstAllocationParams *
171 gst_allocation_params_copy (const GstAllocationParams * params)
173 GstAllocationParams *result = NULL;
177 (GstAllocationParams *) g_slice_copy (sizeof (GstAllocationParams),
184 * gst_allocation_params_free:
185 * @params: (in) (transfer full): a #GstAllocationParams
190 gst_allocation_params_free (GstAllocationParams * params)
192 g_slice_free (GstAllocationParams, params);
196 * gst_allocator_register:
197 * @name: the name of the allocator
198 * @allocator: (transfer full): #GstAllocator
200 * Registers the memory @allocator with @name. This function takes ownership of
204 gst_allocator_register (const gchar * name, GstAllocator * allocator)
206 g_return_if_fail (name != NULL);
207 g_return_if_fail (allocator != NULL);
209 GST_CAT_DEBUG (GST_CAT_MEMORY, "registering allocator %p with name \"%s\"",
212 g_rw_lock_writer_lock (&lock);
213 g_hash_table_insert (allocators, (gpointer) name, (gpointer) allocator);
214 g_rw_lock_writer_unlock (&lock);
218 * gst_allocator_find:
219 * @name: (allow-none): the name of the allocator
221 * Find a previously registered allocator with @name. When @name is %NULL, the
222 * default allocator will be returned.
224 * Returns: (transfer full) (nullable): a #GstAllocator or %NULL when
225 * the allocator with @name was not registered. Use gst_object_unref()
226 * to release the allocator after usage.
229 gst_allocator_find (const gchar * name)
231 GstAllocator *allocator;
233 g_rw_lock_reader_lock (&lock);
235 allocator = g_hash_table_lookup (allocators, (gconstpointer) name);
237 allocator = _default_allocator;
240 gst_object_ref (allocator);
241 g_rw_lock_reader_unlock (&lock);
247 * gst_allocator_set_default:
248 * @allocator: (transfer full): a #GstAllocator
250 * Set the default allocator. This function takes ownership of @allocator.
253 gst_allocator_set_default (GstAllocator * allocator)
257 g_return_if_fail (GST_IS_ALLOCATOR (allocator));
259 g_rw_lock_writer_lock (&lock);
260 old = _default_allocator;
261 _default_allocator = allocator;
262 g_rw_lock_writer_unlock (&lock);
265 gst_object_unref (old);
269 * gst_allocator_alloc:
270 * @allocator: (transfer none) (allow-none): a #GstAllocator to use
271 * @size: size of the visible memory area
272 * @params: (transfer none) (allow-none): optional parameters
274 * Use @allocator to allocate a new memory block with memory that is at least
277 * The optional @params can specify the prefix and padding for the memory. If
278 * %NULL is passed, no flags, no extra prefix/padding and a default alignment is
281 * The prefix/padding will be filled with 0 if flags contains
282 * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED respectively.
284 * When @allocator is %NULL, the default allocator will be used.
286 * The alignment in @params is given as a bitmask so that @align + 1 equals
287 * the amount of bytes to align to. For example, to align to 8 bytes,
288 * use an alignment of 7.
290 * Returns: (transfer full): a new #GstMemory.
293 gst_allocator_alloc (GstAllocator * allocator, gsize size,
294 GstAllocationParams * params)
297 static GstAllocationParams defparams = { 0, 0, 0, 0, };
298 GstAllocatorClass *aclass;
301 g_return_val_if_fail (((params->align + 1) & params->align) == 0, NULL);
306 if (allocator == NULL)
307 allocator = _default_allocator;
309 aclass = GST_ALLOCATOR_GET_CLASS (allocator);
311 mem = aclass->alloc (allocator, size, params);
319 * gst_allocator_free:
320 * @allocator: (transfer none): a #GstAllocator to use
321 * @memory: (transfer full): the memory to free
323 * Free @memory that was previously allocated with gst_allocator_alloc().
326 gst_allocator_free (GstAllocator * allocator, GstMemory * memory)
328 GstAllocatorClass *aclass;
330 g_return_if_fail (GST_IS_ALLOCATOR (allocator));
331 g_return_if_fail (memory != NULL);
332 g_return_if_fail (memory->allocator == allocator);
334 aclass = GST_ALLOCATOR_GET_CLASS (allocator);
336 aclass->free (allocator, memory);
339 /* default memory implementation */
348 GDestroyNotify notify;
354 } GstAllocatorSysmem;
358 GstAllocatorClass parent_class;
359 } GstAllocatorSysmemClass;
361 GType gst_allocator_sysmem_get_type (void);
362 G_DEFINE_TYPE (GstAllocatorSysmem, gst_allocator_sysmem, GST_TYPE_ALLOCATOR);
364 /* initialize the fields */
366 _sysmem_init (GstMemorySystem * mem, GstMemoryFlags flags,
367 GstMemory * parent, gsize slice_size,
368 gpointer data, gsize maxsize, gsize align, gsize offset, gsize size,
369 gpointer user_data, GDestroyNotify notify)
371 gst_memory_init (GST_MEMORY_CAST (mem),
372 flags, _sysmem_allocator, parent, maxsize, align, offset, size);
374 mem->slice_size = slice_size;
376 mem->user_data = user_data;
377 mem->notify = notify;
380 /* create a new memory block that manages the given memory */
381 static inline GstMemorySystem *
382 _sysmem_new (GstMemoryFlags flags,
383 GstMemory * parent, gpointer data, gsize maxsize, gsize align, gsize offset,
384 gsize size, gpointer user_data, GDestroyNotify notify)
386 GstMemorySystem *mem;
389 slice_size = sizeof (GstMemorySystem);
391 mem = g_slice_alloc (slice_size);
392 _sysmem_init (mem, flags, parent, slice_size,
393 data, maxsize, align, offset, size, user_data, notify);
398 /* allocate the memory and structure in one block */
399 static GstMemorySystem *
400 _sysmem_new_block (GstMemoryFlags flags,
401 gsize maxsize, gsize align, gsize offset, gsize size)
403 GstMemorySystem *mem;
404 gsize aoffset, slice_size, padding;
407 /* ensure configured alignment */
408 align |= gst_memory_alignment;
409 /* allocate more to compensate for alignment */
411 /* alloc header and data in one block */
412 slice_size = sizeof (GstMemorySystem) + maxsize;
414 mem = g_slice_alloc (slice_size);
418 data = (guint8 *) mem + sizeof (GstMemorySystem);
421 if ((aoffset = ((guintptr) data & align))) {
422 aoffset = (align + 1) - aoffset;
427 if (offset && (flags & GST_MEMORY_FLAG_ZERO_PREFIXED))
428 memset (data, 0, offset);
430 padding = maxsize - (offset + size);
431 if (padding && (flags & GST_MEMORY_FLAG_ZERO_PADDED))
432 memset (data + offset + size, 0, padding);
434 _sysmem_init (mem, flags, NULL, slice_size, data, maxsize,
435 align, offset, size, NULL, NULL);
441 _sysmem_map (GstMemorySystem * mem, gsize maxsize, GstMapFlags flags)
447 _sysmem_unmap (GstMemorySystem * mem)
452 static GstMemorySystem *
453 _sysmem_copy (GstMemorySystem * mem, gssize offset, gsize size)
455 GstMemorySystem *copy;
458 size = mem->mem.size > offset ? mem->mem.size - offset : 0;
460 copy = _sysmem_new_block (0, size, mem->mem.align, 0, size);
461 GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
462 "memcpy %" G_GSIZE_FORMAT " memory %p -> %p", size, mem, copy);
463 memcpy (copy->data, mem->data + mem->mem.offset + offset, size);
468 static GstMemorySystem *
469 _sysmem_share (GstMemorySystem * mem, gssize offset, gsize size)
471 GstMemorySystem *sub;
474 /* find the real parent */
475 if ((parent = mem->mem.parent) == NULL)
476 parent = (GstMemory *) mem;
479 size = mem->mem.size - offset;
481 /* the shared memory is always readonly */
483 _sysmem_new (GST_MINI_OBJECT_FLAGS (parent) |
484 GST_MINI_OBJECT_FLAG_LOCK_READONLY, parent, mem->data, mem->mem.maxsize,
485 mem->mem.align, mem->mem.offset + offset, size, NULL, NULL);
491 _sysmem_is_span (GstMemorySystem * mem1, GstMemorySystem * mem2, gsize * offset)
495 GstMemorySystem *parent;
497 parent = (GstMemorySystem *) mem1->mem.parent;
499 *offset = mem1->mem.offset - parent->mem.offset;
502 /* and memory is contiguous */
503 return mem1->data + mem1->mem.offset + mem1->mem.size ==
504 mem2->data + mem2->mem.offset;
508 default_alloc (GstAllocator * allocator, gsize size,
509 GstAllocationParams * params)
511 gsize maxsize = size + params->prefix + params->padding;
513 return (GstMemory *) _sysmem_new_block (params->flags,
514 maxsize, params->align, params->prefix, size);
518 default_free (GstAllocator * allocator, GstMemory * mem)
520 GstMemorySystem *dmem = (GstMemorySystem *) mem;
524 dmem->notify (dmem->user_data);
526 slice_size = dmem->slice_size;
529 /* just poison the structs, not all the data */
530 memset (mem, 0xff, sizeof (GstMemorySystem));
533 g_slice_free1 (slice_size, mem);
537 gst_allocator_sysmem_finalize (GObject * obj)
539 g_warning ("The default memory allocator was freed!");
543 gst_allocator_sysmem_class_init (GstAllocatorSysmemClass * klass)
545 GObjectClass *gobject_class;
546 GstAllocatorClass *allocator_class;
548 gobject_class = (GObjectClass *) klass;
549 allocator_class = (GstAllocatorClass *) klass;
551 gobject_class->finalize = gst_allocator_sysmem_finalize;
553 allocator_class->alloc = default_alloc;
554 allocator_class->free = default_free;
558 gst_allocator_sysmem_init (GstAllocatorSysmem * allocator)
560 GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
562 GST_CAT_DEBUG (GST_CAT_MEMORY, "init allocator %p", allocator);
564 alloc->mem_type = GST_ALLOCATOR_SYSMEM;
565 alloc->mem_map = (GstMemoryMapFunction) _sysmem_map;
566 alloc->mem_unmap = (GstMemoryUnmapFunction) _sysmem_unmap;
567 alloc->mem_copy = (GstMemoryCopyFunction) _sysmem_copy;
568 alloc->mem_share = (GstMemoryShareFunction) _sysmem_share;
569 alloc->mem_is_span = (GstMemoryIsSpanFunction) _sysmem_is_span;
573 _priv_gst_allocator_initialize (void)
575 g_rw_lock_init (&lock);
576 allocators = g_hash_table_new (g_str_hash, g_str_equal);
578 #ifdef HAVE_GETPAGESIZE
579 #ifdef MEMORY_ALIGNMENT_PAGESIZE
580 gst_memory_alignment = getpagesize () - 1;
584 GST_CAT_DEBUG (GST_CAT_MEMORY, "memory alignment: %" G_GSIZE_FORMAT,
585 gst_memory_alignment);
587 _sysmem_allocator = g_object_new (gst_allocator_sysmem_get_type (), NULL);
589 gst_allocator_register (GST_ALLOCATOR_SYSMEM,
590 gst_object_ref (_sysmem_allocator));
592 _default_allocator = gst_object_ref (_sysmem_allocator);
596 * gst_memory_new_wrapped:
597 * @flags: #GstMemoryFlags
598 * @data: (array length=size) (element-type guint8) (transfer none): data to
600 * @maxsize: allocated size of @data
601 * @offset: offset in @data
602 * @size: size of valid data
603 * @user_data: (allow-none): user_data
604 * @notify: (allow-none) (scope async) (closure user_data): called with @user_data when the memory is freed
606 * Allocate a new memory block that wraps the given @data.
608 * The prefix/padding must be filled with 0 if @flags contains
609 * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED respectively.
611 * Returns: (transfer full): a new #GstMemory.
614 gst_memory_new_wrapped (GstMemoryFlags flags, gpointer data,
615 gsize maxsize, gsize offset, gsize size, gpointer user_data,
616 GDestroyNotify notify)
618 GstMemorySystem *mem;
620 g_return_val_if_fail (data != NULL, NULL);
621 g_return_val_if_fail (offset + size <= maxsize, NULL);
624 _sysmem_new (flags, NULL, data, maxsize, 0, offset, size, user_data,
627 return (GstMemory *) mem;