2 * Copyright (C) 2011 Wim Taymans <wim.taymans@gmail.be>
4 * gstmemory.c: memory block handling
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.
24 * @short_description: refcounted wrapper for memory blocks
25 * @see_also: #GstBuffer
27 * GstMemory is a lightweight refcounted object that wraps a region of memory.
28 * They are typically used to manage the data of a #GstBuffer.
30 * A GstMemory object has an allocated region of memory of maxsize. The maximum
31 * size does not change during the lifetime of the memory object. The memory
32 * also has an offset and size property that specifies the valid range of memory
33 * in the allocated region.
35 * Memory is usually created by allocators with a gst_allocator_alloc()
36 * method call. When NULL is used as the allocator, the default allocator will
39 * New allocators can be registered with gst_allocator_register().
40 * Allocators are identified by name and can be retrieved with
41 * gst_allocator_find(). gst_allocator_set_default() can be used to change the
44 * New memory can be created with gst_memory_new_wrapped() that wraps the memory
45 * allocated elsewhere.
47 * Refcounting of the memory block is performed with gst_memory_ref() and
50 * The size of the memory can be retrieved and changed with
51 * gst_memory_get_sizes() and gst_memory_resize() respectively.
53 * Getting access to the data of the memory is performed with gst_memory_map().
54 * The call will return a pointer to offset bytes into the region of memory.
55 * After the memory access is completed, gst_memory_unmap() should be called.
57 * Memory can be copied with gst_memory_copy(), which will return a writable
58 * copy. gst_memory_share() will create a new memory block that shares the
59 * memory with an existing memory block at a custom offset and with a custom
62 * Memory can be efficiently merged when gst_memory_is_span() returns TRUE.
64 * Last reviewed on 2012-03-28 (0.11.3)
71 #include "gst_private.h"
72 #include "gstmemory.h"
74 GST_DEFINE_MINI_OBJECT_TYPE (GstMemory, gst_memory);
76 GST_DEFINE_MINI_OBJECT_TYPE (GstAllocator, gst_allocator);
78 G_DEFINE_BOXED_TYPE (GstAllocationParams, gst_allocation_params,
79 (GBoxedCopyFunc) gst_allocation_params_copy,
80 (GBoxedFreeFunc) gst_allocation_params_free);
82 #if defined(MEMORY_ALIGNMENT_MALLOC)
83 size_t gst_memory_alignment = 7;
84 #elif defined(MEMORY_ALIGNMENT_PAGESIZE)
85 /* we fill this in in the _init method */
86 size_t gst_memory_alignment = 0;
87 #elif defined(MEMORY_ALIGNMENT)
88 size_t gst_memory_alignment = MEMORY_ALIGNMENT - 1;
90 #error "No memory alignment configured"
91 size_t gst_memory_alignment = 0;
96 GstMiniObject mini_object;
101 GDestroyNotify notify;
104 /* default memory implementation */
111 GDestroyNotify notify;
114 /* the default allocator */
115 static GstAllocator *_default_allocator;
117 /* our predefined allocators */
118 static GstAllocator *_default_mem_impl;
121 _gst_memory_copy (GstMemory * mem)
123 return gst_memory_copy (mem, 0, -1);
127 _gst_memory_free (GstMemory * mem)
129 /* there should be no outstanding mappings */
130 g_return_if_fail (g_atomic_int_get (&mem->state) < 4);
131 mem->allocator->info.mem_free (mem);
134 /* initialize the fields */
136 _default_mem_init (GstMemoryDefault * mem, GstMemoryFlags flags,
137 GstMemory * parent, gsize slice_size, gpointer data,
138 gsize maxsize, gsize offset, gsize size, gsize align,
139 gpointer user_data, GDestroyNotify notify)
141 gst_mini_object_init (GST_MINI_OBJECT_CAST (mem), GST_TYPE_MEMORY,
142 (GstMiniObjectCopyFunction) _gst_memory_copy, NULL,
143 (GstMiniObjectFreeFunction) _gst_memory_free);
145 mem->mem.mini_object.flags = flags;
147 mem->mem.allocator = _default_mem_impl;
148 mem->mem.parent = parent ? gst_memory_ref (parent) : NULL;
149 mem->mem.state = (flags & GST_MEMORY_FLAG_READONLY ? 0x1 : 0);
150 mem->mem.maxsize = maxsize;
151 mem->mem.align = align;
152 mem->mem.offset = offset;
153 mem->mem.size = size;
154 mem->slice_size = slice_size;
156 mem->user_data = user_data;
157 mem->notify = notify;
159 GST_CAT_DEBUG (GST_CAT_MEMORY, "new memory %p, maxsize:%" G_GSIZE_FORMAT
160 " offset:%" G_GSIZE_FORMAT " size:%" G_GSIZE_FORMAT, mem, maxsize,
164 /* create a new memory block that manages the given memory */
165 static GstMemoryDefault *
166 _default_mem_new (GstMemoryFlags flags, GstMemory * parent, gpointer data,
167 gsize maxsize, gsize offset, gsize size, gsize align, gpointer user_data,
168 GDestroyNotify notify)
170 GstMemoryDefault *mem;
173 slice_size = sizeof (GstMemoryDefault);
175 mem = g_slice_alloc (slice_size);
176 _default_mem_init (mem, flags, parent, slice_size,
177 data, maxsize, offset, size, align, user_data, notify);
182 /* allocate the memory and structure in one block */
183 static GstMemoryDefault *
184 _default_mem_new_block (GstMemoryFlags flags, gsize maxsize, gsize align,
185 gsize offset, gsize size)
187 GstMemoryDefault *mem;
188 gsize aoffset, slice_size, padding;
191 /* ensure configured alignment */
192 align |= gst_memory_alignment;
193 /* allocate more to compensate for alignment */
195 /* alloc header and data in one block */
196 slice_size = sizeof (GstMemoryDefault) + maxsize;
198 mem = g_slice_alloc (slice_size);
202 data = (guint8 *) mem + sizeof (GstMemoryDefault);
205 if ((aoffset = ((guintptr) data & align))) {
206 aoffset = (align + 1) - aoffset;
211 if (offset && (flags & GST_MEMORY_FLAG_ZERO_PREFIXED))
212 memset (data, 0, offset);
214 padding = maxsize - (offset + size);
215 if (padding && (flags & GST_MEMORY_FLAG_ZERO_PADDED))
216 memset (data + offset + size, 0, padding);
218 _default_mem_init (mem, flags, NULL, slice_size, data, maxsize,
219 offset, size, align, NULL, NULL);
225 _default_alloc_alloc (GstAllocator * allocator, gsize size,
226 GstAllocationParams * params, gpointer user_data)
228 gsize maxsize = size + params->prefix + params->padding;
230 return (GstMemory *) _default_mem_new_block (params->flags,
231 maxsize, params->align, params->prefix, size);
235 _default_mem_map (GstMemoryDefault * mem, gsize maxsize, GstMapFlags flags)
241 _default_mem_unmap (GstMemoryDefault * mem)
247 _default_mem_free (GstMemoryDefault * mem)
249 GST_CAT_DEBUG (GST_CAT_MEMORY, "free memory %p", mem);
252 gst_memory_unref (mem->mem.parent);
255 mem->notify (mem->user_data);
257 g_slice_free1 (mem->slice_size, mem);
260 static GstMemoryDefault *
261 _default_mem_copy (GstMemoryDefault * mem, gssize offset, gsize size)
263 GstMemoryDefault *copy;
266 size = mem->mem.size > offset ? mem->mem.size - offset : 0;
269 _default_mem_new_block (0, mem->mem.maxsize, 0, mem->mem.offset + offset,
271 GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
272 "memcpy %" G_GSIZE_FORMAT " memory %p -> %p", mem->mem.maxsize, mem,
274 memcpy (copy->data, mem->data, mem->mem.maxsize);
279 static GstMemoryDefault *
280 _default_mem_share (GstMemoryDefault * mem, gssize offset, gsize size)
282 GstMemoryDefault *sub;
285 /* find the real parent */
286 if ((parent = mem->mem.parent) == NULL)
287 parent = (GstMemory *) mem;
290 size = mem->mem.size - offset;
293 _default_mem_new (GST_MINI_OBJECT_FLAGS (parent), parent, mem->data,
294 mem->mem.maxsize, mem->mem.offset + offset, size, mem->mem.align, NULL,
301 _default_mem_is_span (GstMemoryDefault * mem1, GstMemoryDefault * mem2,
306 GstMemoryDefault *parent;
308 parent = (GstMemoryDefault *) mem1->mem.parent;
310 *offset = mem1->mem.offset - parent->mem.offset;
313 /* and memory is contiguous */
314 return mem1->data + mem1->mem.offset + mem1->mem.size ==
315 mem2->data + mem2->mem.offset;
319 _fallback_mem_copy (GstMemory * mem, gssize offset, gssize size)
322 GstMapInfo sinfo, dinfo;
323 GstAllocationParams params = { 0, 0, 0, mem->align, };
325 if (!gst_memory_map (mem, &sinfo, GST_MAP_READ))
329 size = sinfo.size > offset ? sinfo.size - offset : 0;
331 /* use the same allocator as the memory we copy */
332 copy = gst_allocator_alloc (mem->allocator, size, ¶ms);
333 if (!gst_memory_map (copy, &dinfo, GST_MAP_WRITE)) {
334 GST_CAT_WARNING (GST_CAT_MEMORY, "could not write map memory %p", copy);
335 gst_memory_unmap (mem, &sinfo);
339 GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
340 "memcpy %" G_GSSIZE_FORMAT " memory %p -> %p", size, mem, copy);
341 memcpy (dinfo.data, sinfo.data + offset, size);
342 gst_memory_unmap (copy, &dinfo);
343 gst_memory_unmap (mem, &sinfo);
349 _fallback_mem_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
355 static GHashTable *allocators;
358 _priv_sysmem_notify (gpointer user_data)
360 g_warning ("The default memory allocator was freed!");
364 _priv_gst_memory_initialize (void)
366 static const GstMemoryInfo _mem_info = {
367 GST_ALLOCATOR_SYSMEM,
368 (GstAllocatorAllocFunction) _default_alloc_alloc,
369 (GstMemoryMapFunction) _default_mem_map,
370 (GstMemoryUnmapFunction) _default_mem_unmap,
371 (GstMemoryFreeFunction) _default_mem_free,
372 (GstMemoryCopyFunction) _default_mem_copy,
373 (GstMemoryShareFunction) _default_mem_share,
374 (GstMemoryIsSpanFunction) _default_mem_is_span,
377 g_rw_lock_init (&lock);
378 allocators = g_hash_table_new (g_str_hash, g_str_equal);
380 #ifdef HAVE_GETPAGESIZE
381 #ifdef MEMORY_ALIGNMENT_PAGESIZE
382 gst_memory_alignment = getpagesize () - 1;
386 GST_CAT_DEBUG (GST_CAT_MEMORY, "memory alignment: %" G_GSIZE_FORMAT,
387 gst_memory_alignment);
389 _default_mem_impl = gst_allocator_new (&_mem_info, NULL, _priv_sysmem_notify);
391 _default_allocator = gst_allocator_ref (_default_mem_impl);
392 gst_allocator_register (GST_ALLOCATOR_SYSMEM,
393 gst_allocator_ref (_default_mem_impl));
397 * gst_memory_new_wrapped:
398 * @flags: #GstMemoryFlags
399 * @data: data to wrap
400 * @maxsize: allocated size of @data
401 * @offset: offset in @data
402 * @size: size of valid data
403 * @user_data: user_data
404 * @notify: called with @user_data when the memory is freed
406 * Allocate a new memory block that wraps the given @data.
408 * The prefix/padding must be filled with 0 if @flags contains
409 * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED respectively.
411 * Returns: a new #GstMemory.
414 gst_memory_new_wrapped (GstMemoryFlags flags, gpointer data,
415 gsize maxsize, gsize offset, gsize size, gpointer user_data,
416 GDestroyNotify notify)
418 GstMemoryDefault *mem;
420 g_return_val_if_fail (data != NULL, NULL);
421 g_return_val_if_fail (offset + size <= maxsize, NULL);
424 _default_mem_new (flags, NULL, data, maxsize, offset, size, 0, user_data,
427 return (GstMemory *) mem;
431 * gst_memory_is_exclusive:
434 * Check if the current ref to @mem is exclusive, this means that no other
435 * references exist other than @mem.
438 gst_memory_is_exclusive (GstMemory * mem)
440 g_return_val_if_fail (mem != NULL, FALSE);
442 return GST_MINI_OBJECT_REFCOUNT_VALUE (mem) == 1;
446 * gst_memory_get_sizes:
448 * @offset: pointer to offset
449 * @maxsize: pointer to maxsize
451 * Get the current @size, @offset and @maxsize of @mem.
453 * Returns: the current sizes of @mem
456 gst_memory_get_sizes (GstMemory * mem, gsize * offset, gsize * maxsize)
458 g_return_val_if_fail (mem != NULL, 0);
461 *offset = mem->offset;
463 *maxsize = mem->maxsize;
471 * @offset: a new offset
474 * Resize the memory region. @mem should be writable and offset + size should be
475 * less than the maxsize of @mem.
477 * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED will be
478 * cleared when offset or padding is increased respectively.
481 gst_memory_resize (GstMemory * mem, gssize offset, gsize size)
483 g_return_if_fail (mem != NULL);
484 g_return_if_fail (offset >= 0 || mem->offset >= -offset);
485 g_return_if_fail (size + mem->offset + offset <= mem->maxsize);
487 /* if we increase the prefix, we can't guarantee it is still 0 filled */
488 if ((offset > 0) && GST_MEMORY_IS_ZERO_PREFIXED (mem))
489 GST_MEMORY_FLAG_UNSET (mem, GST_MEMORY_FLAG_ZERO_PREFIXED);
491 /* if we increase the padding, we can't guarantee it is still 0 filled */
492 if ((offset + size < mem->size) && GST_MEMORY_IS_ZERO_PADDED (mem))
493 GST_MEMORY_FLAG_UNSET (mem, GST_MEMORY_FLAG_ZERO_PADDED);
495 mem->offset += offset;
500 gst_memory_lock (GstMemory * mem, GstMapFlags flags)
502 gint access_mode, state, newstate;
504 access_mode = flags & 3;
507 state = g_atomic_int_get (&mem->state);
509 /* nothing mapped, set access_mode and refcount */
510 newstate = 4 | access_mode;
512 /* access_mode must match */
513 if ((state & access_mode) != access_mode)
515 /* increase refcount */
516 newstate = state + 4;
518 } while (!g_atomic_int_compare_and_exchange (&mem->state, state, newstate));
524 GST_CAT_DEBUG (GST_CAT_MEMORY, "lock failed %p: state %d, access_mode %d",
525 mem, state, access_mode);
531 gst_memory_unlock (GstMemory * mem)
533 gint state, newstate;
536 state = g_atomic_int_get (&mem->state);
537 /* decrease the refcount */
538 newstate = state - 4;
539 /* last refcount, unset access_mode */
542 } while (!g_atomic_int_compare_and_exchange (&mem->state, state, newstate));
547 * gst_memory_make_mapped:
548 * @mem: (transfer full): a #GstMemory
549 * @info: (out): pointer for info
550 * @flags: mapping flags
552 * Create a #GstMemory object that is mapped with @flags. If @mem is mappable
553 * with @flags, this function returns the mapped @mem directly. Otherwise a
554 * mapped copy of @mem is returned.
556 * This function takes ownership of old @mem and returns a reference to a new
559 * Returns: (transfer full): a #GstMemory object mapped with @flags or NULL when
560 * a mapping is not possible.
563 gst_memory_make_mapped (GstMemory * mem, GstMapInfo * info, GstMapFlags flags)
567 if (gst_memory_map (mem, info, flags)) {
570 result = gst_memory_copy (mem, 0, -1);
571 gst_memory_unref (mem);
576 if (!gst_memory_map (result, info, flags))
584 GST_CAT_DEBUG (GST_CAT_MEMORY, "cannot copy memory %p", mem);
589 GST_CAT_DEBUG (GST_CAT_MEMORY, "cannot map memory %p with flags %d", mem,
591 gst_memory_unref (result);
599 * @info: (out): pointer for info
600 * @flags: mapping flags
602 * Fill @info with the pointer and sizes of the memory in @mem that can be
603 * accessed according to @flags.
605 * This function can return %FALSE for various reasons:
606 * - the memory backed by @mem is not accessible with the given @flags.
607 * - the memory was already mapped with a different mapping.
609 * @info and its contents remain valid for as long as @mem is valid and
610 * until gst_memory_unmap() is called.
612 * For each gst_memory_map() call, a corresponding gst_memory_unmap() call
615 * Returns: %TRUE if the map operation was successful.
618 gst_memory_map (GstMemory * mem, GstMapInfo * info, GstMapFlags flags)
620 g_return_val_if_fail (mem != NULL, FALSE);
621 g_return_val_if_fail (info != NULL, FALSE);
623 if (!gst_memory_lock (mem, flags))
626 info->data = mem->allocator->info.mem_map (mem, mem->maxsize, flags);
628 if (G_UNLIKELY (info->data == NULL))
633 info->size = mem->size;
634 info->maxsize = mem->maxsize - mem->offset;
635 info->data = info->data + mem->offset;
642 GST_CAT_DEBUG (GST_CAT_MEMORY, "mem %p: lock %d failed", mem, flags);
647 /* something went wrong, restore the orginal state again */
648 GST_CAT_ERROR (GST_CAT_MEMORY, "mem %p: map failed", mem);
649 gst_memory_unlock (mem);
657 * @info: a #GstMapInfo
659 * Release the memory obtained with gst_memory_map()
662 gst_memory_unmap (GstMemory * mem, GstMapInfo * info)
664 g_return_if_fail (mem != NULL);
665 g_return_if_fail (info != NULL);
666 g_return_if_fail (info->memory == mem);
667 /* there must be a ref */
668 g_return_if_fail (g_atomic_int_get (&mem->state) >= 4);
670 mem->allocator->info.mem_unmap (mem);
671 gst_memory_unlock (mem);
677 * @offset: an offset to copy
678 * @size: size to copy or -1 to copy all bytes from offset
680 * Return a copy of @size bytes from @mem starting from @offset. This copy is
681 * guaranteed to be writable. @size can be set to -1 to return a copy all bytes
684 * Returns: a new #GstMemory.
687 gst_memory_copy (GstMemory * mem, gssize offset, gssize size)
691 g_return_val_if_fail (mem != NULL, NULL);
693 copy = mem->allocator->info.mem_copy (mem, offset, size);
701 * @offset: an offset to share
702 * @size: size to share or -1 to share bytes from offset
704 * Return a shared copy of @size bytes from @mem starting from @offset. No
705 * memory copy is performed and the memory region is simply shared. The result
706 * is guaranteed to be not-writable. @size can be set to -1 to return a share
707 * all bytes from @offset.
709 * Returns: a new #GstMemory.
712 gst_memory_share (GstMemory * mem, gssize offset, gssize size)
716 g_return_val_if_fail (mem != NULL, NULL);
717 g_return_val_if_fail (!GST_MEMORY_FLAG_IS_SET (mem, GST_MEMORY_FLAG_NO_SHARE),
720 shared = mem->allocator->info.mem_share (mem, offset, size);
726 * gst_memory_is_span:
727 * @mem1: a #GstMemory
728 * @mem2: a #GstMemory
729 * @offset: a pointer to a result offset
731 * Check if @mem1 and mem2 share the memory with a common parent memory object
732 * and that the memory is contiguous.
734 * If this is the case, the memory of @mem1 and @mem2 can be merged
735 * efficiently by performing gst_memory_share() on the parent object from
736 * the returned @offset.
738 * Returns: %TRUE if the memory is contiguous and of a common parent.
741 gst_memory_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
743 g_return_val_if_fail (mem1 != NULL, FALSE);
744 g_return_val_if_fail (mem2 != NULL, FALSE);
746 /* need to have the same allocators */
747 if (mem1->allocator != mem2->allocator)
750 /* need to have the same parent */
751 if (mem1->parent == NULL || mem1->parent != mem2->parent)
754 /* and memory is contiguous */
755 if (!mem1->allocator->info.mem_is_span (mem1, mem2, offset))
762 _gst_allocator_free (GstAllocator * allocator)
764 if (allocator->notify)
765 allocator->notify (allocator->user_data);
767 g_slice_free1 (sizeof (GstAllocator), allocator);
770 static GstAllocator *
771 _gst_allocator_copy (GstAllocator * allocator)
773 return gst_allocator_ref (allocator);
778 * @info: a #GstMemoryInfo
779 * @user_data: user data
780 * @notify: a #GDestroyNotify for @user_data
782 * Create a new memory allocator with @info and @user_data.
784 * All functions in @info are mandatory exept the copy and is_span
785 * functions, which will have a default implementation when left NULL.
787 * The @user_data will be passed to all calls of the alloc function. @notify
788 * will be called with @user_data when the allocator is freed.
790 * Returns: a new #GstAllocator.
793 gst_allocator_new (const GstMemoryInfo * info, gpointer user_data,
794 GDestroyNotify notify)
796 GstAllocator *allocator;
798 g_return_val_if_fail (info != NULL, NULL);
799 g_return_val_if_fail (info->alloc != NULL, NULL);
800 g_return_val_if_fail (info->mem_map != NULL, NULL);
801 g_return_val_if_fail (info->mem_unmap != NULL, NULL);
802 g_return_val_if_fail (info->mem_free != NULL, NULL);
803 g_return_val_if_fail (info->mem_share != NULL, NULL);
805 allocator = g_slice_new0 (GstAllocator);
807 gst_mini_object_init (GST_MINI_OBJECT_CAST (allocator), GST_TYPE_ALLOCATOR,
808 (GstMiniObjectCopyFunction) _gst_allocator_copy, NULL,
809 (GstMiniObjectFreeFunction) _gst_allocator_free);
811 allocator->info = *info;
812 allocator->user_data = user_data;
813 allocator->notify = notify;
815 #define INSTALL_FALLBACK(_t) \
816 if (allocator->info._t == NULL) allocator->info._t = _fallback_ ##_t;
817 INSTALL_FALLBACK (mem_copy);
818 INSTALL_FALLBACK (mem_is_span);
819 #undef INSTALL_FALLBACK
821 GST_CAT_DEBUG (GST_CAT_MEMORY, "new allocator %p", allocator);
827 * gst_allocator_get_memory_type:
828 * @allocator: a #GstAllocator
830 * Get the memory type allocated by this allocator
832 * Returns: the memory type provided by @allocator
835 gst_allocator_get_memory_type (GstAllocator * allocator)
837 g_return_val_if_fail (allocator != NULL, NULL);
839 return allocator->info.mem_type;
843 * gst_allocator_register:
844 * @name: the name of the allocator
845 * @allocator: (transfer full): #GstAllocator
847 * Registers the memory @allocator with @name. This function takes ownership of
851 gst_allocator_register (const gchar * name, GstAllocator * allocator)
853 g_return_if_fail (name != NULL);
854 g_return_if_fail (allocator != NULL);
856 GST_CAT_DEBUG (GST_CAT_MEMORY, "registering allocator %p with name \"%s\"",
859 g_rw_lock_writer_lock (&lock);
860 g_hash_table_insert (allocators, (gpointer) name, (gpointer) allocator);
861 g_rw_lock_writer_unlock (&lock);
865 * gst_allocator_find:
866 * @name: the name of the allocator
868 * Find a previously registered allocator with @name. When @name is NULL, the
869 * default allocator will be returned.
871 * Returns: (transfer full): a #GstAllocator or NULL when the allocator with @name was not
872 * registered. Use gst_allocator_unref() to release the allocator after usage.
875 gst_allocator_find (const gchar * name)
877 GstAllocator *allocator;
879 g_rw_lock_reader_lock (&lock);
881 allocator = g_hash_table_lookup (allocators, (gconstpointer) name);
883 allocator = _default_allocator;
886 gst_allocator_ref (allocator);
887 g_rw_lock_reader_unlock (&lock);
893 * gst_allocator_set_default:
894 * @allocator: (transfer full): a #GstAllocator
896 * Set the default allocator. This function takes ownership of @allocator.
899 gst_allocator_set_default (GstAllocator * allocator)
902 g_return_if_fail (allocator != NULL);
904 g_rw_lock_writer_lock (&lock);
905 old = _default_allocator;
906 _default_allocator = allocator;
907 g_rw_lock_writer_unlock (&lock);
910 gst_allocator_unref (old);
914 * gst_allocation_params_init:
915 * @params: a #GstAllocationParams
917 * Initialize @params to its default values
920 gst_allocation_params_init (GstAllocationParams * params)
922 g_return_if_fail (params != NULL);
924 memset (params, 0, sizeof (GstAllocationParams));
928 * gst_allocation_params_copy:
929 * @params: (transfer none): a #GstAllocationParams
931 * Create a copy of @params.
933 * Free-function: gst_allocation_params_free
935 * Returns: (transfer full): a new ##GstAllocationParams, free with
936 * gst_allocation_params_free().
938 GstAllocationParams *
939 gst_allocation_params_copy (const GstAllocationParams * params)
941 GstAllocationParams *result = NULL;
945 (GstAllocationParams *) g_slice_copy (sizeof (GstAllocationParams),
952 * gst_allocation_params_free:
953 * @params: (in) (transfer full): a #GstAllocationParams
958 gst_allocation_params_free (GstAllocationParams * params)
960 g_slice_free (GstAllocationParams, params);
964 * gst_allocator_alloc:
965 * @allocator: (transfer none) (allow-none): a #GstAllocator to use
966 * @size: size of the visible memory area
967 * @params: (transfer none) (allow-none): optional parameters
969 * Use @allocator to allocate a new memory block with memory that is at least
972 * The optional @params can specify the prefix and padding for the memory. If
973 * NULL is passed, no flags, no extra prefix/padding and a default alignment is
976 * The prefix/padding will be filled with 0 if flags contains
977 * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED respectively.
979 * When @allocator is NULL, the default allocator will be used.
981 * The alignment in @params is given as a bitmask so that @align + 1 equals
982 * the amount of bytes to align to. For example, to align to 8 bytes,
983 * use an alignment of 7.
985 * Returns: (transfer full): a new #GstMemory.
988 gst_allocator_alloc (GstAllocator * allocator, gsize size,
989 GstAllocationParams * params)
992 static GstAllocationParams defparams = { 0, 0, 0, 0, };
995 g_return_val_if_fail (((params->align + 1) & params->align) == 0, NULL);
1000 if (allocator == NULL)
1001 allocator = _default_allocator;
1003 mem = allocator->info.alloc (allocator, size, params, allocator->user_data);