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.
39 * Last reviewed on 2012-07-09 (0.11.3)
46 #include "gst_private.h"
47 #include "gstmemory.h"
49 GST_DEBUG_CATEGORY_STATIC (gst_allocator_debug);
50 #define GST_CAT_DEFAULT gst_allocator_debug
52 #define GST_ALLOCATOR_GET_PRIVATE(obj) \
53 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_ALLOCATOR, GstAllocatorPrivate))
55 struct _GstAllocatorPrivate
60 #if defined(MEMORY_ALIGNMENT_MALLOC)
61 size_t gst_memory_alignment = 7;
62 #elif defined(MEMORY_ALIGNMENT_PAGESIZE)
63 /* we fill this in in the _init method */
64 size_t gst_memory_alignment = 0;
65 #elif defined(MEMORY_ALIGNMENT)
66 size_t gst_memory_alignment = MEMORY_ALIGNMENT - 1;
68 #error "No memory alignment configured"
69 size_t gst_memory_alignment = 0;
72 /* the default allocator */
73 static GstAllocator *_default_allocator;
75 static GstAllocator *_sysmem_allocator;
77 /* registered allocators */
79 static GHashTable *allocators;
81 G_DEFINE_ABSTRACT_TYPE (GstAllocator, gst_allocator, GST_TYPE_OBJECT);
84 gst_allocator_class_init (GstAllocatorClass * klass)
86 g_type_class_add_private (klass, sizeof (GstAllocatorPrivate));
88 GST_DEBUG_CATEGORY_INIT (gst_allocator_debug, "allocator", 0,
93 _fallback_mem_copy (GstMemory * mem, gssize offset, gssize size)
96 GstMapInfo sinfo, dinfo;
97 GstAllocationParams params = { 0, mem->align, 0, 0, };
98 GstAllocator *allocator;
100 if (!gst_memory_map (mem, &sinfo, GST_MAP_READ))
104 size = sinfo.size > offset ? sinfo.size - offset : 0;
106 /* use the same allocator as the memory we copy */
107 allocator = mem->allocator;
108 if (GST_OBJECT_FLAG_IS_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC))
110 copy = gst_allocator_alloc (allocator, size, ¶ms);
112 if (!gst_memory_map (copy, &dinfo, GST_MAP_WRITE)) {
113 GST_CAT_WARNING (GST_CAT_MEMORY, "could not write map memory %p", copy);
114 gst_allocator_free (mem->allocator, copy);
115 gst_memory_unmap (mem, &sinfo);
119 GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
120 "memcpy %" G_GSSIZE_FORMAT " memory %p -> %p", size, mem, copy);
121 memcpy (dinfo.data, sinfo.data + offset, size);
122 gst_memory_unmap (copy, &dinfo);
123 gst_memory_unmap (mem, &sinfo);
129 _fallback_mem_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
135 gst_allocator_init (GstAllocator * allocator)
137 allocator->priv = GST_ALLOCATOR_GET_PRIVATE (allocator);
139 allocator->mem_copy = _fallback_mem_copy;
140 allocator->mem_is_span = _fallback_mem_is_span;
143 G_DEFINE_BOXED_TYPE (GstAllocationParams, gst_allocation_params,
144 (GBoxedCopyFunc) gst_allocation_params_copy,
145 (GBoxedFreeFunc) gst_allocation_params_free);
148 * gst_allocation_params_init:
149 * @params: a #GstAllocationParams
151 * Initialize @params to its default values
154 gst_allocation_params_init (GstAllocationParams * params)
156 g_return_if_fail (params != NULL);
158 memset (params, 0, sizeof (GstAllocationParams));
162 * gst_allocation_params_copy:
163 * @params: (transfer none): a #GstAllocationParams
165 * Create a copy of @params.
167 * Free-function: gst_allocation_params_free
169 * Returns: (transfer full): a new ##GstAllocationParams, free with
170 * gst_allocation_params_free().
172 GstAllocationParams *
173 gst_allocation_params_copy (const GstAllocationParams * params)
175 GstAllocationParams *result = NULL;
179 (GstAllocationParams *) g_slice_copy (sizeof (GstAllocationParams),
186 * gst_allocation_params_free:
187 * @params: (in) (transfer full): a #GstAllocationParams
192 gst_allocation_params_free (GstAllocationParams * params)
194 g_slice_free (GstAllocationParams, params);
198 * gst_allocator_register:
199 * @name: the name of the allocator
200 * @allocator: (transfer full): #GstAllocator
202 * Registers the memory @allocator with @name. This function takes ownership of
206 gst_allocator_register (const gchar * name, GstAllocator * allocator)
208 g_return_if_fail (name != NULL);
209 g_return_if_fail (allocator != NULL);
211 GST_CAT_DEBUG (GST_CAT_MEMORY, "registering allocator %p with name \"%s\"",
214 g_rw_lock_writer_lock (&lock);
215 g_hash_table_insert (allocators, (gpointer) name, (gpointer) allocator);
216 g_rw_lock_writer_unlock (&lock);
220 * gst_allocator_find:
221 * @name: the name of the allocator
223 * Find a previously registered allocator with @name. When @name is NULL, the
224 * default allocator will be returned.
226 * Returns: (transfer full): a #GstAllocator or NULL when the allocator with @name was not
227 * registered. Use gst_object_unref() to release the allocator after usage.
230 gst_allocator_find (const gchar * name)
232 GstAllocator *allocator;
234 g_rw_lock_reader_lock (&lock);
236 allocator = g_hash_table_lookup (allocators, (gconstpointer) name);
238 allocator = _default_allocator;
241 gst_object_ref (allocator);
242 g_rw_lock_reader_unlock (&lock);
248 * gst_allocator_set_default:
249 * @allocator: (transfer full): a #GstAllocator
251 * Set the default allocator. This function takes ownership of @allocator.
254 gst_allocator_set_default (GstAllocator * allocator)
258 g_return_if_fail (GST_IS_ALLOCATOR (allocator));
260 g_rw_lock_writer_lock (&lock);
261 old = _default_allocator;
262 _default_allocator = allocator;
263 g_rw_lock_writer_unlock (&lock);
266 gst_object_unref (old);
270 * gst_allocator_alloc:
271 * @allocator: (transfer none) (allow-none): a #GstAllocator to use
272 * @size: size of the visible memory area
273 * @params: (transfer none) (allow-none): optional parameters
275 * Use @allocator to allocate a new memory block with memory that is at least
278 * The optional @params can specify the prefix and padding for the memory. If
279 * NULL is passed, no flags, no extra prefix/padding and a default alignment is
282 * The prefix/padding will be filled with 0 if flags contains
283 * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED respectively.
285 * When @allocator is NULL, the default allocator will be used.
287 * The alignment in @params is given as a bitmask so that @align + 1 equals
288 * the amount of bytes to align to. For example, to align to 8 bytes,
289 * use an alignment of 7.
291 * Returns: (transfer full): a new #GstMemory.
294 gst_allocator_alloc (GstAllocator * allocator, gsize size,
295 GstAllocationParams * params)
298 static GstAllocationParams defparams = { 0, 0, 0, 0, };
299 GstAllocatorClass *aclass;
302 g_return_val_if_fail (((params->align + 1) & params->align) == 0, NULL);
307 if (allocator == NULL)
308 allocator = _default_allocator;
310 aclass = GST_ALLOCATOR_GET_CLASS (allocator);
312 mem = aclass->alloc (allocator, size, params);
320 * gst_allocator_free:
321 * @allocator: (transfer none): a #GstAllocator to use
322 * @memory: (transfer full): the memory to free
324 * Free @memory that was previously allocated with gst_allocator_alloc().
327 gst_allocator_free (GstAllocator * allocator, GstMemory * memory)
329 GstAllocatorClass *aclass;
331 g_return_if_fail (GST_IS_ALLOCATOR (allocator));
332 g_return_if_fail (memory != NULL);
333 g_return_if_fail (memory->allocator == allocator);
335 aclass = GST_ALLOCATOR_GET_CLASS (allocator);
337 aclass->free (allocator, memory);
340 /* default memory implementation */
349 GDestroyNotify notify;
355 } GstAllocatorSysmem;
359 GstAllocatorClass parent_class;
360 } GstAllocatorSysmemClass;
362 GType gst_allocator_sysmem_get_type (void);
363 G_DEFINE_TYPE (GstAllocatorSysmem, gst_allocator_sysmem, GST_TYPE_ALLOCATOR);
365 /* initialize the fields */
367 _sysmem_init (GstMemorySystem * mem, GstMemoryFlags flags,
368 GstMemory * parent, gsize slice_size,
369 gpointer data, gsize maxsize, gsize align, gsize offset, gsize size,
370 gpointer user_data, GDestroyNotify notify)
372 gst_memory_init (GST_MEMORY_CAST (mem),
373 flags, _sysmem_allocator, parent, maxsize, align, offset, size);
375 mem->slice_size = slice_size;
377 mem->user_data = user_data;
378 mem->notify = notify;
381 /* create a new memory block that manages the given memory */
382 static inline GstMemorySystem *
383 _sysmem_new (GstMemoryFlags flags,
384 GstMemory * parent, gpointer data, gsize maxsize, gsize align, gsize offset,
385 gsize size, gpointer user_data, GDestroyNotify notify)
387 GstMemorySystem *mem;
390 slice_size = sizeof (GstMemorySystem);
392 mem = g_slice_alloc (slice_size);
393 _sysmem_init (mem, flags, parent, slice_size,
394 data, maxsize, align, offset, size, user_data, notify);
399 /* allocate the memory and structure in one block */
400 static GstMemorySystem *
401 _sysmem_new_block (GstMemoryFlags flags,
402 gsize maxsize, gsize align, gsize offset, gsize size)
404 GstMemorySystem *mem;
405 gsize aoffset, slice_size, padding;
408 /* ensure configured alignment */
409 align |= gst_memory_alignment;
410 /* allocate more to compensate for alignment */
412 /* alloc header and data in one block */
413 slice_size = sizeof (GstMemorySystem) + maxsize;
415 mem = g_slice_alloc (slice_size);
419 data = (guint8 *) mem + sizeof (GstMemorySystem);
422 if ((aoffset = ((guintptr) data & align))) {
423 aoffset = (align + 1) - aoffset;
428 if (offset && (flags & GST_MEMORY_FLAG_ZERO_PREFIXED))
429 memset (data, 0, offset);
431 padding = maxsize - (offset + size);
432 if (padding && (flags & GST_MEMORY_FLAG_ZERO_PADDED))
433 memset (data + offset + size, 0, padding);
435 _sysmem_init (mem, flags, NULL, slice_size, data, maxsize,
436 align, offset, size, NULL, NULL);
442 _sysmem_map (GstMemorySystem * mem, gsize maxsize, GstMapFlags flags)
448 _sysmem_unmap (GstMemorySystem * mem)
453 static GstMemorySystem *
454 _sysmem_copy (GstMemorySystem * mem, gssize offset, gsize size)
456 GstMemorySystem *copy;
459 size = mem->mem.size > offset ? mem->mem.size - offset : 0;
462 _sysmem_new_block (0, mem->mem.maxsize, mem->mem.align,
463 mem->mem.offset + offset, size);
464 GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
465 "memcpy %" G_GSIZE_FORMAT " memory %p -> %p", mem->mem.maxsize, mem,
467 memcpy (copy->data, mem->data, mem->mem.maxsize);
472 static GstMemorySystem *
473 _sysmem_share (GstMemorySystem * mem, gssize offset, gsize size)
475 GstMemorySystem *sub;
478 /* find the real parent */
479 if ((parent = mem->mem.parent) == NULL)
480 parent = (GstMemory *) mem;
483 size = mem->mem.size - offset;
485 /* the shared memory is always readonly */
487 _sysmem_new (GST_MINI_OBJECT_FLAGS (parent) |
488 GST_MINI_OBJECT_FLAG_LOCK_READONLY, parent, mem->data, mem->mem.maxsize,
489 mem->mem.align, mem->mem.offset + offset, size, NULL, NULL);
495 _sysmem_is_span (GstMemorySystem * mem1, GstMemorySystem * mem2, gsize * offset)
499 GstMemorySystem *parent;
501 parent = (GstMemorySystem *) mem1->mem.parent;
503 *offset = mem1->mem.offset - parent->mem.offset;
506 /* and memory is contiguous */
507 return mem1->data + mem1->mem.offset + mem1->mem.size ==
508 mem2->data + mem2->mem.offset;
512 default_alloc (GstAllocator * allocator, gsize size,
513 GstAllocationParams * params)
515 gsize maxsize = size + params->prefix + params->padding;
517 return (GstMemory *) _sysmem_new_block (params->flags,
518 maxsize, params->align, params->prefix, size);
522 default_free (GstAllocator * allocator, GstMemory * mem)
524 GstMemorySystem *dmem = (GstMemorySystem *) mem;
528 dmem->notify (dmem->user_data);
530 slice_size = dmem->slice_size;
533 /* just poison the structs, not all the data */
534 memset (mem, 0xff, sizeof (GstMemorySystem));
537 g_slice_free1 (slice_size, mem);
541 gst_allocator_sysmem_finalize (GObject * obj)
543 g_warning ("The default memory allocator was freed!");
547 gst_allocator_sysmem_class_init (GstAllocatorSysmemClass * klass)
549 GObjectClass *gobject_class;
550 GstAllocatorClass *allocator_class;
552 gobject_class = (GObjectClass *) klass;
553 allocator_class = (GstAllocatorClass *) klass;
555 gobject_class->finalize = gst_allocator_sysmem_finalize;
557 allocator_class->alloc = default_alloc;
558 allocator_class->free = default_free;
562 gst_allocator_sysmem_init (GstAllocatorSysmem * allocator)
564 GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
566 GST_CAT_DEBUG (GST_CAT_MEMORY, "init allocator %p", allocator);
568 alloc->mem_type = GST_ALLOCATOR_SYSMEM;
569 alloc->mem_map = (GstMemoryMapFunction) _sysmem_map;
570 alloc->mem_unmap = (GstMemoryUnmapFunction) _sysmem_unmap;
571 alloc->mem_copy = (GstMemoryCopyFunction) _sysmem_copy;
572 alloc->mem_share = (GstMemoryShareFunction) _sysmem_share;
573 alloc->mem_is_span = (GstMemoryIsSpanFunction) _sysmem_is_span;
577 _priv_gst_memory_initialize (void)
579 g_rw_lock_init (&lock);
580 allocators = g_hash_table_new (g_str_hash, g_str_equal);
582 #ifdef HAVE_GETPAGESIZE
583 #ifdef MEMORY_ALIGNMENT_PAGESIZE
584 gst_memory_alignment = getpagesize () - 1;
588 GST_CAT_DEBUG (GST_CAT_MEMORY, "memory alignment: %" G_GSIZE_FORMAT,
589 gst_memory_alignment);
591 _sysmem_allocator = g_object_new (gst_allocator_sysmem_get_type (), NULL);
593 gst_allocator_register (GST_ALLOCATOR_SYSMEM,
594 gst_object_ref (_sysmem_allocator));
596 _default_allocator = gst_object_ref (_sysmem_allocator);
600 * gst_memory_new_wrapped:
601 * @flags: #GstMemoryFlags
602 * @data: data to wrap
603 * @maxsize: allocated size of @data
604 * @offset: offset in @data
605 * @size: size of valid data
606 * @user_data: user_data
607 * @notify: called with @user_data when the memory is freed
609 * Allocate a new memory block that wraps the given @data.
611 * The prefix/padding must be filled with 0 if @flags contains
612 * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED respectively.
614 * Returns: a new #GstMemory.
617 gst_memory_new_wrapped (GstMemoryFlags flags, gpointer data,
618 gsize maxsize, gsize offset, gsize size, gpointer user_data,
619 GDestroyNotify notify)
621 GstMemorySystem *mem;
623 g_return_val_if_fail (data != NULL, NULL);
624 g_return_val_if_fail (offset + size <= maxsize, NULL);
627 _sysmem_new (flags, NULL, data, maxsize, 0, offset, size, user_data,
630 return (GstMemory *) mem;