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 * @title: GstAllocator
25 * @short_description: allocate memory blocks
26 * @see_also: #GstMemory
28 * Memory is usually created by allocators with a gst_allocator_alloc()
29 * method call. When %NULL is used as the allocator, the default allocator will
32 * New allocators can be registered with gst_allocator_register().
33 * Allocators are identified by name and can be retrieved with
34 * gst_allocator_find(). gst_allocator_set_default() can be used to change the
37 * New memory can be created with gst_memory_new_wrapped() that wraps the memory
38 * allocated elsewhere.
45 #include "gst_private.h"
46 #include "gstmemory.h"
48 GST_DEBUG_CATEGORY_STATIC (gst_allocator_debug);
49 #define GST_CAT_DEFAULT gst_allocator_debug
51 #define GST_ALLOCATOR_GET_PRIVATE(obj) \
52 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_ALLOCATOR, GstAllocatorPrivate))
54 struct _GstAllocatorPrivate
59 #if defined(MEMORY_ALIGNMENT_MALLOC)
60 gsize gst_memory_alignment = 7;
61 #elif defined(MEMORY_ALIGNMENT_PAGESIZE)
62 /* we fill this in in the _init method */
63 gsize gst_memory_alignment = 0;
64 #elif defined(MEMORY_ALIGNMENT)
65 gsize gst_memory_alignment = MEMORY_ALIGNMENT - 1;
67 #error "No memory alignment configured"
68 gsize gst_memory_alignment = 0;
71 /* the default allocator */
72 static GstAllocator *_default_allocator;
74 static GstAllocator *_sysmem_allocator;
76 /* registered allocators */
78 static GHashTable *allocators;
80 G_DEFINE_ABSTRACT_TYPE (GstAllocator, gst_allocator, GST_TYPE_OBJECT);
83 gst_allocator_class_init (GstAllocatorClass * klass)
85 g_type_class_add_private (klass, sizeof (GstAllocatorPrivate));
87 GST_DEBUG_CATEGORY_INIT (gst_allocator_debug, "allocator", 0,
92 _fallback_mem_copy (GstMemory * mem, gssize offset, gssize size)
95 GstMapInfo sinfo, dinfo;
96 GstAllocationParams params = { 0, mem->align, 0, 0, };
97 GstAllocator *allocator;
99 if (!gst_memory_map (mem, &sinfo, GST_MAP_READ))
103 size = sinfo.size > offset ? sinfo.size - offset : 0;
105 /* use the same allocator as the memory we copy */
106 allocator = mem->allocator;
107 if (GST_OBJECT_FLAG_IS_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC))
109 copy = gst_allocator_alloc (allocator, size, ¶ms);
111 if (!gst_memory_map (copy, &dinfo, GST_MAP_WRITE)) {
112 GST_CAT_WARNING (GST_CAT_MEMORY, "could not write map memory %p", copy);
113 gst_allocator_free (mem->allocator, copy);
114 gst_memory_unmap (mem, &sinfo);
118 GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
119 "memcpy %" G_GSSIZE_FORMAT " memory %p -> %p", size, mem, copy);
120 memcpy (dinfo.data, sinfo.data + offset, size);
121 gst_memory_unmap (copy, &dinfo);
122 gst_memory_unmap (mem, &sinfo);
128 _fallback_mem_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
134 gst_allocator_init (GstAllocator * allocator)
136 allocator->priv = GST_ALLOCATOR_GET_PRIVATE (allocator);
138 allocator->mem_copy = _fallback_mem_copy;
139 allocator->mem_is_span = _fallback_mem_is_span;
142 G_DEFINE_BOXED_TYPE (GstAllocationParams, gst_allocation_params,
143 (GBoxedCopyFunc) gst_allocation_params_copy,
144 (GBoxedFreeFunc) gst_allocation_params_free);
147 * gst_allocation_params_init:
148 * @params: a #GstAllocationParams
150 * Initialize @params to its default values
153 gst_allocation_params_init (GstAllocationParams * params)
155 g_return_if_fail (params != NULL);
157 memset (params, 0, sizeof (GstAllocationParams));
161 * gst_allocation_params_copy:
162 * @params: (transfer none): a #GstAllocationParams
164 * Create a copy of @params.
166 * Free-function: gst_allocation_params_free
168 * Returns: (transfer full): a new ##GstAllocationParams, free with
169 * gst_allocation_params_free().
171 GstAllocationParams *
172 gst_allocation_params_copy (const GstAllocationParams * params)
174 GstAllocationParams *result = NULL;
178 (GstAllocationParams *) g_slice_copy (sizeof (GstAllocationParams),
185 * gst_allocation_params_free:
186 * @params: (in) (transfer full): a #GstAllocationParams
191 gst_allocation_params_free (GstAllocationParams * params)
193 g_slice_free (GstAllocationParams, params);
197 * gst_allocator_register:
198 * @name: the name of the allocator
199 * @allocator: (transfer full): #GstAllocator
201 * Registers the memory @allocator with @name. This function takes ownership of
205 gst_allocator_register (const gchar * name, GstAllocator * allocator)
207 g_return_if_fail (name != NULL);
208 g_return_if_fail (allocator != NULL);
210 GST_CAT_DEBUG (GST_CAT_MEMORY, "registering allocator %p with name \"%s\"",
213 g_rw_lock_writer_lock (&lock);
214 /* The ref will never be released */
215 GST_OBJECT_FLAG_SET (allocator, GST_OBJECT_FLAG_MAY_BE_LEAKED);
216 g_hash_table_insert (allocators, (gpointer) name, (gpointer) allocator);
217 g_rw_lock_writer_unlock (&lock);
221 * gst_allocator_find:
222 * @name: (allow-none): the name of the allocator
224 * Find a previously registered allocator with @name. When @name is %NULL, the
225 * default allocator will be returned.
227 * Returns: (transfer full) (nullable): a #GstAllocator or %NULL when
228 * the allocator with @name was not registered. Use gst_object_unref()
229 * to release the allocator after usage.
232 gst_allocator_find (const gchar * name)
234 GstAllocator *allocator;
236 g_rw_lock_reader_lock (&lock);
238 allocator = g_hash_table_lookup (allocators, (gconstpointer) name);
240 allocator = _default_allocator;
243 gst_object_ref (allocator);
244 g_rw_lock_reader_unlock (&lock);
250 * gst_allocator_set_default:
251 * @allocator: (transfer full): a #GstAllocator
253 * Set the default allocator. This function takes ownership of @allocator.
256 gst_allocator_set_default (GstAllocator * allocator)
260 g_return_if_fail (GST_IS_ALLOCATOR (allocator));
262 g_rw_lock_writer_lock (&lock);
263 old = _default_allocator;
264 _default_allocator = allocator;
265 g_rw_lock_writer_unlock (&lock);
268 gst_object_unref (old);
272 * gst_allocator_alloc:
273 * @allocator: (transfer none) (allow-none): a #GstAllocator to use
274 * @size: size of the visible memory area
275 * @params: (transfer none) (allow-none): optional parameters
277 * Use @allocator to allocate a new memory block with memory that is at least
280 * The optional @params can specify the prefix and padding for the memory. If
281 * %NULL is passed, no flags, no extra prefix/padding and a default alignment is
284 * The prefix/padding will be filled with 0 if flags contains
285 * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED respectively.
287 * When @allocator is %NULL, the default allocator will be used.
289 * The alignment in @params is given as a bitmask so that @align + 1 equals
290 * the amount of bytes to align to. For example, to align to 8 bytes,
291 * use an alignment of 7.
293 * Returns: (transfer full): a new #GstMemory.
296 gst_allocator_alloc (GstAllocator * allocator, gsize size,
297 GstAllocationParams * params)
300 static GstAllocationParams defparams = { 0, 0, 0, 0, };
301 GstAllocatorClass *aclass;
304 g_return_val_if_fail (((params->align + 1) & params->align) == 0, NULL);
309 if (allocator == NULL)
310 allocator = _default_allocator;
312 aclass = GST_ALLOCATOR_GET_CLASS (allocator);
314 mem = aclass->alloc (allocator, size, params);
322 * gst_allocator_free:
323 * @allocator: (transfer none): a #GstAllocator to use
324 * @memory: (transfer full): the memory to free
326 * Free @memory that was previously allocated with gst_allocator_alloc().
329 gst_allocator_free (GstAllocator * allocator, GstMemory * memory)
331 GstAllocatorClass *aclass;
333 g_return_if_fail (GST_IS_ALLOCATOR (allocator));
334 g_return_if_fail (memory != NULL);
335 g_return_if_fail (memory->allocator == allocator);
337 aclass = GST_ALLOCATOR_GET_CLASS (allocator);
339 aclass->free (allocator, memory);
342 /* default memory implementation */
351 GDestroyNotify notify;
357 } GstAllocatorSysmem;
361 GstAllocatorClass parent_class;
362 } GstAllocatorSysmemClass;
364 GType gst_allocator_sysmem_get_type (void);
365 G_DEFINE_TYPE (GstAllocatorSysmem, gst_allocator_sysmem, GST_TYPE_ALLOCATOR);
367 /* initialize the fields */
369 _sysmem_init (GstMemorySystem * mem, GstMemoryFlags flags,
370 GstMemory * parent, gsize slice_size,
371 gpointer data, gsize maxsize, gsize align, gsize offset, gsize size,
372 gpointer user_data, GDestroyNotify notify)
374 gst_memory_init (GST_MEMORY_CAST (mem),
375 flags, _sysmem_allocator, parent, maxsize, align, offset, size);
377 mem->slice_size = slice_size;
379 mem->user_data = user_data;
380 mem->notify = notify;
383 /* create a new memory block that manages the given memory */
384 static inline GstMemorySystem *
385 _sysmem_new (GstMemoryFlags flags,
386 GstMemory * parent, gpointer data, gsize maxsize, gsize align, gsize offset,
387 gsize size, gpointer user_data, GDestroyNotify notify)
389 GstMemorySystem *mem;
392 slice_size = sizeof (GstMemorySystem);
394 mem = g_slice_alloc (slice_size);
395 _sysmem_init (mem, flags, parent, slice_size,
396 data, maxsize, align, offset, size, user_data, notify);
401 /* allocate the memory and structure in one block */
402 static GstMemorySystem *
403 _sysmem_new_block (GstMemoryFlags flags,
404 gsize maxsize, gsize align, gsize offset, gsize size)
406 GstMemorySystem *mem;
407 gsize aoffset, slice_size, padding;
410 /* ensure configured alignment */
411 align |= gst_memory_alignment;
412 /* allocate more to compensate for alignment */
414 /* alloc header and data in one block */
415 slice_size = sizeof (GstMemorySystem) + maxsize;
417 mem = g_slice_alloc (slice_size);
421 data = (guint8 *) mem + sizeof (GstMemorySystem);
424 if ((aoffset = ((guintptr) data & align))) {
425 aoffset = (align + 1) - aoffset;
430 if (offset && (flags & GST_MEMORY_FLAG_ZERO_PREFIXED))
431 memset (data, 0, offset);
433 padding = maxsize - (offset + size);
434 if (padding && (flags & GST_MEMORY_FLAG_ZERO_PADDED))
435 memset (data + offset + size, 0, padding);
437 _sysmem_init (mem, flags, NULL, slice_size, data, maxsize,
438 align, offset, size, NULL, NULL);
444 _sysmem_map (GstMemorySystem * mem, gsize maxsize, GstMapFlags flags)
450 _sysmem_unmap (GstMemorySystem * mem)
455 static GstMemorySystem *
456 _sysmem_copy (GstMemorySystem * mem, gssize offset, gsize size)
458 GstMemorySystem *copy;
461 size = mem->mem.size > offset ? mem->mem.size - offset : 0;
463 copy = _sysmem_new_block (0, size, mem->mem.align, 0, size);
464 GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
465 "memcpy %" G_GSIZE_FORMAT " memory %p -> %p", size, mem, copy);
466 memcpy (copy->data, mem->data + mem->mem.offset + offset, size);
471 static GstMemorySystem *
472 _sysmem_share (GstMemorySystem * mem, gssize offset, gsize size)
474 GstMemorySystem *sub;
477 /* find the real parent */
478 if ((parent = mem->mem.parent) == NULL)
479 parent = (GstMemory *) mem;
482 size = mem->mem.size - offset;
484 /* the shared memory is always readonly */
486 _sysmem_new (GST_MINI_OBJECT_FLAGS (parent) |
487 GST_MINI_OBJECT_FLAG_LOCK_READONLY, parent, mem->data, mem->mem.maxsize,
488 mem->mem.align, mem->mem.offset + offset, size, NULL, NULL);
494 _sysmem_is_span (GstMemorySystem * mem1, GstMemorySystem * mem2, gsize * offset)
498 GstMemorySystem *parent;
500 parent = (GstMemorySystem *) mem1->mem.parent;
502 *offset = mem1->mem.offset - parent->mem.offset;
505 /* and memory is contiguous */
506 return mem1->data + mem1->mem.offset + mem1->mem.size ==
507 mem2->data + mem2->mem.offset;
511 default_alloc (GstAllocator * allocator, gsize size,
512 GstAllocationParams * params)
514 gsize maxsize = size + params->prefix + params->padding;
516 return (GstMemory *) _sysmem_new_block (params->flags,
517 maxsize, params->align, params->prefix, size);
521 default_free (GstAllocator * allocator, GstMemory * mem)
523 GstMemorySystem *dmem = (GstMemorySystem *) mem;
527 dmem->notify (dmem->user_data);
529 slice_size = dmem->slice_size;
532 /* just poison the structs, not all the data */
533 memset (mem, 0xff, sizeof (GstMemorySystem));
536 g_slice_free1 (slice_size, mem);
540 gst_allocator_sysmem_finalize (GObject * obj)
542 /* Don't raise warnings if we are shutting down */
543 if (_default_allocator)
544 g_warning ("The default memory allocator was freed!");
546 ((GObjectClass *) gst_allocator_sysmem_parent_class)->finalize (obj);
550 gst_allocator_sysmem_class_init (GstAllocatorSysmemClass * klass)
552 GObjectClass *gobject_class;
553 GstAllocatorClass *allocator_class;
555 gobject_class = (GObjectClass *) klass;
556 allocator_class = (GstAllocatorClass *) klass;
558 gobject_class->finalize = gst_allocator_sysmem_finalize;
560 allocator_class->alloc = default_alloc;
561 allocator_class->free = default_free;
565 gst_allocator_sysmem_init (GstAllocatorSysmem * allocator)
567 GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
569 GST_CAT_DEBUG (GST_CAT_MEMORY, "init allocator %p", allocator);
571 alloc->mem_type = GST_ALLOCATOR_SYSMEM;
572 alloc->mem_map = (GstMemoryMapFunction) _sysmem_map;
573 alloc->mem_unmap = (GstMemoryUnmapFunction) _sysmem_unmap;
574 alloc->mem_copy = (GstMemoryCopyFunction) _sysmem_copy;
575 alloc->mem_share = (GstMemoryShareFunction) _sysmem_share;
576 alloc->mem_is_span = (GstMemoryIsSpanFunction) _sysmem_is_span;
580 _priv_gst_allocator_initialize (void)
582 g_rw_lock_init (&lock);
583 allocators = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
586 #ifdef HAVE_GETPAGESIZE
587 #ifdef MEMORY_ALIGNMENT_PAGESIZE
588 gst_memory_alignment = getpagesize () - 1;
592 GST_CAT_DEBUG (GST_CAT_MEMORY, "memory alignment: %" G_GSIZE_FORMAT,
593 gst_memory_alignment);
595 _sysmem_allocator = g_object_new (gst_allocator_sysmem_get_type (), NULL);
597 gst_allocator_register (GST_ALLOCATOR_SYSMEM,
598 gst_object_ref (_sysmem_allocator));
600 _default_allocator = gst_object_ref (_sysmem_allocator);
604 _priv_gst_allocator_cleanup (void)
606 gst_object_unref (_sysmem_allocator);
607 _sysmem_allocator = NULL;
609 gst_object_unref (_default_allocator);
610 _default_allocator = NULL;
612 g_clear_pointer (&allocators, g_hash_table_unref);
616 * gst_memory_new_wrapped:
617 * @flags: #GstMemoryFlags
618 * @data: (array length=size) (element-type guint8) (transfer none): data to
620 * @maxsize: allocated size of @data
621 * @offset: offset in @data
622 * @size: size of valid data
623 * @user_data: (allow-none): user_data
624 * @notify: (allow-none) (scope async) (closure user_data): called with @user_data when the memory is freed
626 * Allocate a new memory block that wraps the given @data.
628 * The prefix/padding must be filled with 0 if @flags contains
629 * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED respectively.
631 * Returns: (transfer full): a new #GstMemory.
634 gst_memory_new_wrapped (GstMemoryFlags flags, gpointer data,
635 gsize maxsize, gsize offset, gsize size, gpointer user_data,
636 GDestroyNotify notify)
638 GstMemorySystem *mem;
640 g_return_val_if_fail (data != NULL, NULL);
641 g_return_val_if_fail (offset + size <= maxsize, NULL);
644 _sysmem_new (flags, NULL, data, maxsize, 0, offset, size, user_data,
647 return (GstMemory *) mem;