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., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, 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, };
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 copy = gst_allocator_alloc (mem->allocator, size, ¶ms);
107 if (!gst_memory_map (copy, &dinfo, GST_MAP_WRITE)) {
108 GST_CAT_WARNING (GST_CAT_MEMORY, "could not write map memory %p", copy);
109 gst_memory_unmap (mem, &sinfo);
113 GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
114 "memcpy %" G_GSSIZE_FORMAT " memory %p -> %p", size, mem, copy);
115 memcpy (dinfo.data, sinfo.data + offset, size);
116 gst_memory_unmap (copy, &dinfo);
117 gst_memory_unmap (mem, &sinfo);
123 _fallback_mem_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
129 gst_allocator_init (GstAllocator * allocator)
131 allocator->priv = GST_ALLOCATOR_GET_PRIVATE (allocator);
133 allocator->mem_copy = _fallback_mem_copy;
134 allocator->mem_is_span = _fallback_mem_is_span;
137 G_DEFINE_BOXED_TYPE (GstAllocationParams, gst_allocation_params,
138 (GBoxedCopyFunc) gst_allocation_params_copy,
139 (GBoxedFreeFunc) gst_allocation_params_free);
142 * gst_allocation_params_init:
143 * @params: a #GstAllocationParams
145 * Initialize @params to its default values
148 gst_allocation_params_init (GstAllocationParams * params)
150 g_return_if_fail (params != NULL);
152 memset (params, 0, sizeof (GstAllocationParams));
156 * gst_allocation_params_copy:
157 * @params: (transfer none): a #GstAllocationParams
159 * Create a copy of @params.
161 * Free-function: gst_allocation_params_free
163 * Returns: (transfer full): a new ##GstAllocationParams, free with
164 * gst_allocation_params_free().
166 GstAllocationParams *
167 gst_allocation_params_copy (const GstAllocationParams * params)
169 GstAllocationParams *result = NULL;
173 (GstAllocationParams *) g_slice_copy (sizeof (GstAllocationParams),
180 * gst_allocation_params_free:
181 * @params: (in) (transfer full): a #GstAllocationParams
186 gst_allocation_params_free (GstAllocationParams * params)
188 g_slice_free (GstAllocationParams, params);
192 * gst_allocator_register:
193 * @name: the name of the allocator
194 * @allocator: (transfer full): #GstAllocator
196 * Registers the memory @allocator with @name. This function takes ownership of
200 gst_allocator_register (const gchar * name, GstAllocator * allocator)
202 g_return_if_fail (name != NULL);
203 g_return_if_fail (allocator != NULL);
205 GST_CAT_DEBUG (GST_CAT_MEMORY, "registering allocator %p with name \"%s\"",
208 g_rw_lock_writer_lock (&lock);
209 g_hash_table_insert (allocators, (gpointer) name, (gpointer) allocator);
210 g_rw_lock_writer_unlock (&lock);
214 * gst_allocator_find:
215 * @name: the name of the allocator
217 * Find a previously registered allocator with @name. When @name is NULL, the
218 * default allocator will be returned.
220 * Returns: (transfer full): a #GstAllocator or NULL when the allocator with @name was not
221 * registered. Use gst_object_unref() to release the allocator after usage.
224 gst_allocator_find (const gchar * name)
226 GstAllocator *allocator;
228 g_rw_lock_reader_lock (&lock);
230 allocator = g_hash_table_lookup (allocators, (gconstpointer) name);
232 allocator = _default_allocator;
235 gst_object_ref (allocator);
236 g_rw_lock_reader_unlock (&lock);
242 * gst_allocator_set_default:
243 * @allocator: (transfer full): a #GstAllocator
245 * Set the default allocator. This function takes ownership of @allocator.
248 gst_allocator_set_default (GstAllocator * allocator)
252 g_return_if_fail (GST_IS_ALLOCATOR (allocator));
254 g_rw_lock_writer_lock (&lock);
255 old = _default_allocator;
256 _default_allocator = allocator;
257 g_rw_lock_writer_unlock (&lock);
260 gst_object_unref (old);
264 * gst_allocator_alloc:
265 * @allocator: (transfer none) (allow-none): a #GstAllocator to use
266 * @size: size of the visible memory area
267 * @params: (transfer none) (allow-none): optional parameters
269 * Use @allocator to allocate a new memory block with memory that is at least
272 * The optional @params can specify the prefix and padding for the memory. If
273 * NULL is passed, no flags, no extra prefix/padding and a default alignment is
276 * The prefix/padding will be filled with 0 if flags contains
277 * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED respectively.
279 * When @allocator is NULL, the default allocator will be used.
281 * The alignment in @params is given as a bitmask so that @align + 1 equals
282 * the amount of bytes to align to. For example, to align to 8 bytes,
283 * use an alignment of 7.
285 * Returns: (transfer full): a new #GstMemory.
288 gst_allocator_alloc (GstAllocator * allocator, gsize size,
289 GstAllocationParams * params)
292 static GstAllocationParams defparams = { 0, 0, 0, 0, };
293 GstAllocatorClass *aclass;
296 g_return_val_if_fail (((params->align + 1) & params->align) == 0, NULL);
301 if (allocator == NULL)
302 allocator = _default_allocator;
304 aclass = GST_ALLOCATOR_GET_CLASS (allocator);
306 mem = aclass->alloc (allocator, size, params);
314 * gst_allocator_free:
315 * @allocator: (transfer none): a #GstAllocator to use
316 * @memory: (transfer full): the memory to free
318 * Free @memory that was previously allocated with gst_allocator_alloc().
321 gst_allocator_free (GstAllocator * allocator, GstMemory * memory)
323 GstAllocatorClass *aclass;
325 g_return_if_fail (GST_IS_ALLOCATOR (allocator));
326 g_return_if_fail (memory != NULL);
327 g_return_if_fail (memory->allocator == allocator);
329 aclass = GST_ALLOCATOR_GET_CLASS (allocator);
331 aclass->free (allocator, memory);
334 /* default memory implementation */
343 GDestroyNotify notify;
349 } GstDefaultAllocator;
353 GstAllocatorClass parent_class;
354 } GstDefaultAllocatorClass;
356 GType gst_default_allocator_get_type (void);
357 G_DEFINE_TYPE (GstDefaultAllocator, gst_default_allocator, GST_TYPE_ALLOCATOR);
359 /* initialize the fields */
361 _default_mem_init (GstMemoryDefault * mem, GstMemoryFlags flags,
362 GstMemory * parent, gsize slice_size,
363 gpointer data, gsize maxsize, gsize align, gsize offset, gsize size,
364 gpointer user_data, GDestroyNotify notify)
366 gst_memory_init (GST_MEMORY_CAST (mem),
367 flags, _sysmem_allocator, parent, maxsize, align, offset, size);
369 mem->slice_size = slice_size;
371 mem->user_data = user_data;
372 mem->notify = notify;
375 /* create a new memory block that manages the given memory */
376 static inline GstMemoryDefault *
377 _default_mem_new (GstMemoryFlags flags,
378 GstMemory * parent, gpointer data, gsize maxsize, gsize align, gsize offset,
379 gsize size, gpointer user_data, GDestroyNotify notify)
381 GstMemoryDefault *mem;
384 slice_size = sizeof (GstMemoryDefault);
386 mem = g_slice_alloc (slice_size);
387 _default_mem_init (mem, flags, parent, slice_size,
388 data, maxsize, align, offset, size, user_data, notify);
393 /* allocate the memory and structure in one block */
394 static GstMemoryDefault *
395 _default_mem_new_block (GstMemoryFlags flags,
396 gsize maxsize, gsize align, gsize offset, gsize size)
398 GstMemoryDefault *mem;
399 gsize aoffset, slice_size, padding;
402 /* ensure configured alignment */
403 align |= gst_memory_alignment;
404 /* allocate more to compensate for alignment */
406 /* alloc header and data in one block */
407 slice_size = sizeof (GstMemoryDefault) + maxsize;
409 mem = g_slice_alloc (slice_size);
413 data = (guint8 *) mem + sizeof (GstMemoryDefault);
416 if ((aoffset = ((guintptr) data & align))) {
417 aoffset = (align + 1) - aoffset;
422 if (offset && (flags & GST_MEMORY_FLAG_ZERO_PREFIXED))
423 memset (data, 0, offset);
425 padding = maxsize - (offset + size);
426 if (padding && (flags & GST_MEMORY_FLAG_ZERO_PADDED))
427 memset (data + offset + size, 0, padding);
429 _default_mem_init (mem, flags, NULL, slice_size, data, maxsize,
430 align, offset, size, NULL, NULL);
436 _default_mem_map (GstMemoryDefault * mem, gsize maxsize, GstMapFlags flags)
442 _default_mem_unmap (GstMemoryDefault * mem)
447 static GstMemoryDefault *
448 _default_mem_copy (GstMemoryDefault * mem, gssize offset, gsize size)
450 GstMemoryDefault *copy;
453 size = mem->mem.size > offset ? mem->mem.size - offset : 0;
456 _default_mem_new_block (0, mem->mem.maxsize, 0, mem->mem.offset + offset,
458 GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
459 "memcpy %" G_GSIZE_FORMAT " memory %p -> %p", mem->mem.maxsize, mem,
461 memcpy (copy->data, mem->data, mem->mem.maxsize);
466 static GstMemoryDefault *
467 _default_mem_share (GstMemoryDefault * mem, gssize offset, gsize size)
469 GstMemoryDefault *sub;
472 /* find the real parent */
473 if ((parent = mem->mem.parent) == NULL)
474 parent = (GstMemory *) mem;
477 size = mem->mem.size - offset;
479 /* the shared memory is always readonly */
481 _default_mem_new (GST_MINI_OBJECT_FLAGS (parent) |
482 GST_MINI_OBJECT_FLAG_LOCK_READONLY, parent, mem->data, mem->mem.maxsize,
483 mem->mem.align, mem->mem.offset + offset, size, NULL, NULL);
489 _default_mem_is_span (GstMemoryDefault * mem1, GstMemoryDefault * mem2,
494 GstMemoryDefault *parent;
496 parent = (GstMemoryDefault *) mem1->mem.parent;
498 *offset = mem1->mem.offset - parent->mem.offset;
501 /* and memory is contiguous */
502 return mem1->data + mem1->mem.offset + mem1->mem.size ==
503 mem2->data + mem2->mem.offset;
507 default_alloc (GstAllocator * allocator, gsize size,
508 GstAllocationParams * params)
510 gsize maxsize = size + params->prefix + params->padding;
512 return (GstMemory *) _default_mem_new_block (params->flags,
513 maxsize, params->align, params->prefix, size);
517 default_free (GstAllocator * allocator, GstMemory * mem)
519 GstMemoryDefault *dmem = (GstMemoryDefault *) mem;
523 dmem->notify (dmem->user_data);
525 slice_size = dmem->slice_size;
528 /* just poison the structs, not all the data */
529 memset (mem, 0xff, sizeof (GstMemoryDefault));
532 g_slice_free1 (slice_size, mem);
536 gst_default_allocator_finalize (GObject * obj)
538 g_warning ("The default memory allocator was freed!");
542 gst_default_allocator_class_init (GstDefaultAllocatorClass * klass)
544 GObjectClass *gobject_class;
545 GstAllocatorClass *allocator_class;
547 gobject_class = (GObjectClass *) klass;
548 allocator_class = (GstAllocatorClass *) klass;
550 gobject_class->finalize = gst_default_allocator_finalize;
552 allocator_class->alloc = default_alloc;
553 allocator_class->free = default_free;
557 gst_default_allocator_init (GstDefaultAllocator * allocator)
559 GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
561 GST_CAT_DEBUG (GST_CAT_MEMORY, "init allocator %p", allocator);
563 alloc->mem_type = GST_ALLOCATOR_SYSMEM;
564 alloc->mem_map = (GstMemoryMapFunction) _default_mem_map;
565 alloc->mem_unmap = (GstMemoryUnmapFunction) _default_mem_unmap;
566 alloc->mem_copy = (GstMemoryCopyFunction) _default_mem_copy;
567 alloc->mem_share = (GstMemoryShareFunction) _default_mem_share;
568 alloc->mem_is_span = (GstMemoryIsSpanFunction) _default_mem_is_span;
572 _priv_gst_memory_initialize (void)
574 g_rw_lock_init (&lock);
575 allocators = g_hash_table_new (g_str_hash, g_str_equal);
577 #ifdef HAVE_GETPAGESIZE
578 #ifdef MEMORY_ALIGNMENT_PAGESIZE
579 gst_memory_alignment = getpagesize () - 1;
583 GST_CAT_DEBUG (GST_CAT_MEMORY, "memory alignment: %" G_GSIZE_FORMAT,
584 gst_memory_alignment);
586 _sysmem_allocator = g_object_new (gst_default_allocator_get_type (), NULL);
588 gst_allocator_register (GST_ALLOCATOR_SYSMEM,
589 gst_object_ref (_sysmem_allocator));
591 _default_allocator = gst_object_ref (_sysmem_allocator);
595 * gst_memory_new_wrapped:
596 * @flags: #GstMemoryFlags
597 * @data: data to wrap
598 * @maxsize: allocated size of @data
599 * @offset: offset in @data
600 * @size: size of valid data
601 * @user_data: user_data
602 * @notify: called with @user_data when the memory is freed
604 * Allocate a new memory block that wraps the given @data.
606 * The prefix/padding must be filled with 0 if @flags contains
607 * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED respectively.
609 * Returns: a new #GstMemory.
612 gst_memory_new_wrapped (GstMemoryFlags flags, gpointer data,
613 gsize maxsize, gsize offset, gsize size, gpointer user_data,
614 GDestroyNotify notify)
616 GstMemoryDefault *mem;
618 g_return_val_if_fail (data != NULL, NULL);
619 g_return_val_if_fail (offset + size <= maxsize, NULL);
622 _default_mem_new (flags, NULL, data, maxsize, 0, offset, size, user_data,
625 return (GstMemory *) mem;