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
31 * Memory is usually created by allocators with a gst_allocator_alloc()
32 * method call. When %NULL is used as the allocator, the default allocator will
35 * New allocators can be registered with gst_allocator_register().
36 * Allocators are identified by name and can be retrieved with
37 * gst_allocator_find(). gst_allocator_set_default() can be used to change the
40 * New memory can be created with gst_memory_new_wrapped() that wraps the memory
41 * allocated elsewhere.
48 #include "gst_private.h"
49 #include "gstmemory.h"
51 GST_DEBUG_CATEGORY_STATIC (gst_allocator_debug);
52 #define GST_CAT_DEFAULT gst_allocator_debug
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_WITH_PRIVATE (GstAllocator, gst_allocator,
84 gst_allocator_class_init (GstAllocatorClass * klass)
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_instance_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_new:
148 * Create a new #GstAllocationParams on the heap. This function is for
149 * use in GStreamer language bindings. In your own code, you can just
150 * declare a #GstAllocationParams on the stack or in a struct, and
151 * call gst_allocation_params_init() to initialize it.
153 * You do not need to call gst_allocation_params_init() on the instance
154 * returned by this function.
156 * Returns: (transfer full) (not nullable): a new #GstAllocationParams
160 GstAllocationParams *
161 gst_allocation_params_new (void)
163 /* Call new() and then init(), rather than calling new0(), in case
164 * init() ever changes to something other than a memset(). */
165 GstAllocationParams *result = g_slice_new (GstAllocationParams);
166 gst_allocation_params_init (result);
171 * gst_allocation_params_init:
172 * @params: a #GstAllocationParams
174 * Initialize @params to its default values
177 gst_allocation_params_init (GstAllocationParams * params)
179 g_return_if_fail (params != NULL);
181 memset (params, 0, sizeof (GstAllocationParams));
185 * gst_allocation_params_copy:
186 * @params: (transfer none) (nullable): a #GstAllocationParams
188 * Create a copy of @params.
190 * Returns: (transfer full) (nullable): a new #GstAllocationParams.
192 GstAllocationParams *
193 gst_allocation_params_copy (const GstAllocationParams * params)
195 GstAllocationParams *result = NULL;
199 (GstAllocationParams *) g_slice_copy (sizeof (GstAllocationParams),
206 * gst_allocation_params_free:
207 * @params: (in) (transfer full): a #GstAllocationParams
212 gst_allocation_params_free (GstAllocationParams * params)
214 g_slice_free (GstAllocationParams, params);
218 * gst_allocator_register:
219 * @name: the name of the allocator
220 * @allocator: (transfer full): #GstAllocator
222 * Registers the memory @allocator with @name.
225 gst_allocator_register (const gchar * name, GstAllocator * allocator)
227 g_return_if_fail (name != NULL);
228 g_return_if_fail (allocator != NULL);
230 GST_CAT_DEBUG (GST_CAT_MEMORY, "registering allocator %p with name \"%s\"",
233 g_rw_lock_writer_lock (&lock);
234 /* The ref will never be released */
235 GST_OBJECT_FLAG_SET (allocator, GST_OBJECT_FLAG_MAY_BE_LEAKED);
236 g_hash_table_insert (allocators, (gpointer) g_strdup (name),
237 (gpointer) allocator);
238 g_rw_lock_writer_unlock (&lock);
242 * gst_allocator_find:
243 * @name: (nullable): the name of the allocator
245 * Find a previously registered allocator with @name. When @name is %NULL, the
246 * default allocator will be returned.
248 * Returns: (transfer full) (nullable): a #GstAllocator or %NULL when
249 * the allocator with @name was not registered.
252 gst_allocator_find (const gchar * name)
254 GstAllocator *allocator;
256 g_rw_lock_reader_lock (&lock);
258 allocator = g_hash_table_lookup (allocators, (gconstpointer) name);
260 allocator = _default_allocator;
263 gst_object_ref (allocator);
264 g_rw_lock_reader_unlock (&lock);
270 * gst_allocator_set_default:
271 * @allocator: (transfer full): a #GstAllocator
273 * Set the default allocator.
276 gst_allocator_set_default (GstAllocator * allocator)
280 g_return_if_fail (GST_IS_ALLOCATOR (allocator));
282 g_rw_lock_writer_lock (&lock);
283 old = _default_allocator;
284 _default_allocator = allocator;
285 g_rw_lock_writer_unlock (&lock);
288 gst_object_unref (old);
292 * gst_allocator_alloc:
293 * @allocator: (transfer none) (nullable): a #GstAllocator to use
294 * @size: size of the visible memory area
295 * @params: (transfer none) (nullable): optional parameters
297 * Use @allocator to allocate a new memory block with memory that is at least
300 * The optional @params can specify the prefix and padding for the memory. If
301 * %NULL is passed, no flags, no extra prefix/padding and a default alignment is
304 * The prefix/padding will be filled with 0 if flags contains
305 * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED respectively.
307 * When @allocator is %NULL, the default allocator will be used.
309 * The alignment in @params is given as a bitmask so that @align + 1 equals
310 * the amount of bytes to align to. For example, to align to 8 bytes,
311 * use an alignment of 7.
313 * Returns: (transfer full) (nullable): a new #GstMemory.
316 gst_allocator_alloc (GstAllocator * allocator, gsize size,
317 GstAllocationParams * params)
320 static GstAllocationParams defparams = { 0, 0, 0, 0, };
321 GstAllocatorClass *aclass;
324 g_return_val_if_fail (((params->align + 1) & params->align) == 0, NULL);
329 if (allocator == NULL)
330 allocator = _default_allocator;
332 aclass = GST_ALLOCATOR_GET_CLASS (allocator);
334 mem = aclass->alloc (allocator, size, params);
342 * gst_allocator_free:
343 * @allocator: (transfer none): a #GstAllocator to use
344 * @memory: (transfer full): the memory to free
346 * Free @memory that was previously allocated with gst_allocator_alloc().
349 gst_allocator_free (GstAllocator * allocator, GstMemory * memory)
351 GstAllocatorClass *aclass;
353 g_return_if_fail (GST_IS_ALLOCATOR (allocator));
354 g_return_if_fail (memory != NULL);
355 g_return_if_fail (memory->allocator == allocator);
357 aclass = GST_ALLOCATOR_GET_CLASS (allocator);
359 aclass->free (allocator, memory);
362 /* default memory implementation */
371 GDestroyNotify notify;
377 } GstAllocatorSysmem;
381 GstAllocatorClass parent_class;
382 } GstAllocatorSysmemClass;
384 static GType gst_allocator_sysmem_get_type (void);
385 G_DEFINE_TYPE (GstAllocatorSysmem, gst_allocator_sysmem, GST_TYPE_ALLOCATOR);
387 /* initialize the fields */
389 _sysmem_init (GstMemorySystem * mem, GstMemoryFlags flags,
390 GstMemory * parent, gsize slice_size,
391 gpointer data, gsize maxsize, gsize align, gsize offset, gsize size,
392 gpointer user_data, GDestroyNotify notify)
394 gst_memory_init (GST_MEMORY_CAST (mem),
395 flags, _sysmem_allocator, parent, maxsize, align, offset, size);
397 mem->slice_size = slice_size;
399 mem->user_data = user_data;
400 mem->notify = notify;
403 /* create a new memory block that manages the given memory */
404 static inline GstMemorySystem *
405 _sysmem_new (GstMemoryFlags flags,
406 GstMemory * parent, gpointer data, gsize maxsize, gsize align, gsize offset,
407 gsize size, gpointer user_data, GDestroyNotify notify)
409 GstMemorySystem *mem;
412 slice_size = sizeof (GstMemorySystem);
414 mem = g_slice_alloc (slice_size);
415 _sysmem_init (mem, flags, parent, slice_size,
416 data, maxsize, align, offset, size, user_data, notify);
421 /* allocate the memory and structure in one block */
422 static GstMemorySystem *
423 _sysmem_new_block (GstMemoryFlags flags,
424 gsize maxsize, gsize align, gsize offset, gsize size)
426 GstMemorySystem *mem;
427 gsize aoffset, slice_size, padding;
430 /* ensure configured alignment */
431 align |= gst_memory_alignment;
432 /* allocate more to compensate for alignment */
434 /* alloc header and data in one block */
435 slice_size = sizeof (GstMemorySystem) + maxsize;
437 mem = g_slice_alloc (slice_size);
441 data = (guint8 *) mem + sizeof (GstMemorySystem);
444 if ((aoffset = ((guintptr) data & align))) {
445 aoffset = (align + 1) - aoffset;
450 if (offset && (flags & GST_MEMORY_FLAG_ZERO_PREFIXED))
451 memset (data, 0, offset);
453 padding = maxsize - (offset + size);
454 if (padding && (flags & GST_MEMORY_FLAG_ZERO_PADDED))
455 memset (data + offset + size, 0, padding);
457 _sysmem_init (mem, flags, NULL, slice_size, data, maxsize,
458 align, offset, size, NULL, NULL);
464 _sysmem_map (GstMemorySystem * mem, gsize maxsize, GstMapFlags flags)
470 _sysmem_unmap (GstMemorySystem * mem)
475 static GstMemorySystem *
476 _sysmem_copy (GstMemorySystem * mem, gssize offset, gsize size)
478 GstMemorySystem *copy;
481 size = mem->mem.size > offset ? mem->mem.size - offset : 0;
483 copy = _sysmem_new_block (0, size, mem->mem.align, 0, size);
484 GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
485 "memcpy %" G_GSIZE_FORMAT " memory %p -> %p", size, mem, copy);
486 memcpy (copy->data, mem->data + mem->mem.offset + offset, size);
491 static GstMemorySystem *
492 _sysmem_share (GstMemorySystem * mem, gssize offset, gsize size)
494 GstMemorySystem *sub;
497 /* find the real parent */
498 if ((parent = mem->mem.parent) == NULL)
499 parent = (GstMemory *) mem;
502 size = mem->mem.size - offset;
504 /* the shared memory is always readonly */
506 _sysmem_new (GST_MINI_OBJECT_FLAGS (parent) |
507 GST_MINI_OBJECT_FLAG_LOCK_READONLY, parent, mem->data, mem->mem.maxsize,
508 mem->mem.align, mem->mem.offset + offset, size, NULL, NULL);
514 _sysmem_is_span (GstMemorySystem * mem1, GstMemorySystem * mem2, gsize * offset)
518 GstMemorySystem *parent;
520 parent = (GstMemorySystem *) mem1->mem.parent;
522 *offset = mem1->mem.offset - parent->mem.offset;
525 /* and memory is contiguous */
526 return mem1->data + mem1->mem.offset + mem1->mem.size ==
527 mem2->data + mem2->mem.offset;
531 default_alloc (GstAllocator * allocator, gsize size,
532 GstAllocationParams * params)
534 gsize maxsize = size + params->prefix + params->padding;
536 return (GstMemory *) _sysmem_new_block (params->flags,
537 maxsize, params->align, params->prefix, size);
541 default_free (GstAllocator * allocator, GstMemory * mem)
543 GstMemorySystem *dmem = (GstMemorySystem *) mem;
547 dmem->notify (dmem->user_data);
549 slice_size = dmem->slice_size;
552 /* just poison the structs, not all the data */
553 memset (mem, 0xff, sizeof (GstMemorySystem));
556 g_slice_free1 (slice_size, mem);
560 gst_allocator_sysmem_finalize (GObject * obj)
562 /* Don't raise warnings if we are shutting down */
563 if (_default_allocator)
564 g_warning ("The default memory allocator was freed!");
566 ((GObjectClass *) gst_allocator_sysmem_parent_class)->finalize (obj);
570 gst_allocator_sysmem_class_init (GstAllocatorSysmemClass * klass)
572 GObjectClass *gobject_class;
573 GstAllocatorClass *allocator_class;
575 gobject_class = (GObjectClass *) klass;
576 allocator_class = (GstAllocatorClass *) klass;
578 gobject_class->finalize = gst_allocator_sysmem_finalize;
580 allocator_class->alloc = default_alloc;
581 allocator_class->free = default_free;
585 gst_allocator_sysmem_init (GstAllocatorSysmem * allocator)
587 GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
589 GST_CAT_DEBUG (GST_CAT_MEMORY, "init allocator %p", allocator);
591 alloc->mem_type = GST_ALLOCATOR_SYSMEM;
592 alloc->mem_map = (GstMemoryMapFunction) _sysmem_map;
593 alloc->mem_unmap = (GstMemoryUnmapFunction) _sysmem_unmap;
594 alloc->mem_copy = (GstMemoryCopyFunction) _sysmem_copy;
595 alloc->mem_share = (GstMemoryShareFunction) _sysmem_share;
596 alloc->mem_is_span = (GstMemoryIsSpanFunction) _sysmem_is_span;
600 _priv_gst_allocator_initialize (void)
602 g_rw_lock_init (&lock);
603 allocators = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
606 #ifdef HAVE_GETPAGESIZE
607 #ifdef MEMORY_ALIGNMENT_PAGESIZE
608 gst_memory_alignment = getpagesize () - 1;
612 GST_CAT_DEBUG (GST_CAT_MEMORY, "memory alignment: %" G_GSIZE_FORMAT,
613 gst_memory_alignment);
615 _sysmem_allocator = g_object_new (gst_allocator_sysmem_get_type (), NULL);
617 /* Clear floating flag */
618 gst_object_ref_sink (_sysmem_allocator);
620 gst_allocator_register (GST_ALLOCATOR_SYSMEM,
621 gst_object_ref (_sysmem_allocator));
623 _default_allocator = gst_object_ref (_sysmem_allocator);
627 _priv_gst_allocator_cleanup (void)
629 gst_object_unref (_sysmem_allocator);
630 _sysmem_allocator = NULL;
632 gst_object_unref (_default_allocator);
633 _default_allocator = NULL;
635 g_clear_pointer (&allocators, g_hash_table_unref);
639 * gst_memory_new_wrapped:
640 * @flags: #GstMemoryFlags
641 * @data: (array length=size) (element-type guint8) (transfer none): data to
643 * @maxsize: allocated size of @data
644 * @offset: offset in @data
645 * @size: size of valid data
646 * @user_data: (nullable): user_data
647 * @notify: (nullable) (scope async) (closure user_data): called with @user_data when the memory is freed
649 * Allocate a new memory block that wraps the given @data.
651 * The prefix/padding must be filled with 0 if @flags contains
652 * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED respectively.
654 * Returns: (transfer full) (nullable): a new #GstMemory.
657 gst_memory_new_wrapped (GstMemoryFlags flags, gpointer data,
658 gsize maxsize, gsize offset, gsize size, gpointer user_data,
659 GDestroyNotify notify)
661 GstMemorySystem *mem;
663 g_return_val_if_fail (data != NULL, NULL);
664 g_return_val_if_fail (offset + size <= maxsize, NULL);
667 _sysmem_new (flags, NULL, data, maxsize, 0, offset, size, user_data,
670 return (GstMemory *) mem;