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().
43 * New memory can be created with gst_memory_new_wrapped() that wraps the memory
44 * allocated elsewhere.
46 * Refcounting of the memory block is performed with gst_memory_ref() and
49 * The size of the memory can be retrieved and changed with
50 * gst_memory_get_sizes() and gst_memory_resize() respectively.
52 * Getting access to the data of the memory is performed with gst_memory_map().
53 * The call will return a pointer to offset bytes into the region of memory.
54 * After the memory access is completed, gst_memory_unmap() should be called.
56 * Memory can be copied with gst_memory_copy(), which will return a writable
57 * copy. gst_memory_share() will create a new memory block that shares the
58 * memory with an existing memory block at a custom offset and with a custom
61 * Memory can be efficiently merged when gst_memory_is_span() returns TRUE.
63 * Last reviewed on 2011-06-08 (0.11.0)
70 #include "gst_private.h"
71 #include "gstmemory.h"
73 #ifndef GST_DISABLE_TRACE
75 static GstAllocTrace *_gst_memory_trace;
76 static GstAllocTrace *_gst_allocator_trace;
79 G_DEFINE_BOXED_TYPE (GstMemory, gst_memory, (GBoxedCopyFunc) gst_memory_ref,
80 (GBoxedFreeFunc) gst_memory_unref);
82 G_DEFINE_BOXED_TYPE (GstAllocator, gst_allocator,
83 (GBoxedCopyFunc) gst_allocator_ref, (GBoxedFreeFunc) gst_allocator_unref);
86 * gst_memory_alignment:
88 * The default memory alignment in bytes - 1
89 * an alignment of 7 would be the same as what malloc() guarantees.
91 #if defined(MEMORY_ALIGNMENT_MALLOC)
92 size_t gst_memory_alignment = 7;
93 #elif defined(MEMORY_ALIGNMENT_PAGESIZE)
94 /* we fill this in in the _init method */
95 size_t gst_memory_alignment = 0;
96 #elif defined(MEMORY_ALIGNMENT)
97 size_t gst_memory_alignment = MEMORY_ALIGNMENT - 1;
99 #error "No memory alignment configured"
100 size_t gst_memory_alignment = 0;
110 GDestroyNotify notify;
113 /* default memory implementation */
120 GDestroyNotify notify;
123 /* the default allocator */
124 static GstAllocator *_default_allocator;
126 /* our predefined allocators */
127 static GstAllocator *_default_mem_impl;
129 /* initialize the fields */
131 _default_mem_init (GstMemoryDefault * mem, GstMemoryFlags flags,
132 GstMemory * parent, gsize slice_size, gpointer data,
133 gsize maxsize, gsize offset, gsize size, gpointer user_data,
134 GDestroyNotify notify)
136 mem->mem.allocator = _default_mem_impl;
137 mem->mem.flags = flags;
138 mem->mem.refcount = 1;
139 mem->mem.parent = parent ? gst_memory_ref (parent) : NULL;
140 mem->mem.state = (flags & GST_MEMORY_FLAG_READONLY ? 0x1 : 0);
141 mem->mem.maxsize = maxsize;
142 mem->mem.offset = offset;
143 mem->mem.size = size;
144 mem->slice_size = slice_size;
146 mem->user_data = user_data;
147 mem->notify = notify;
149 GST_CAT_DEBUG (GST_CAT_MEMORY, "new memory %p", mem);
152 /* create a new memory block that manages the given memory */
153 static GstMemoryDefault *
154 _default_mem_new (GstMemoryFlags flags, GstMemory * parent, gpointer data,
155 gsize maxsize, gsize offset, gsize size, gpointer user_data,
156 GDestroyNotify notify)
158 GstMemoryDefault *mem;
161 slice_size = sizeof (GstMemoryDefault);
163 mem = g_slice_alloc (slice_size);
164 _default_mem_init (mem, flags, parent, slice_size,
165 data, maxsize, offset, size, user_data, notify);
170 /* allocate the memory and structure in one block */
171 static GstMemoryDefault *
172 _default_mem_new_block (gsize maxsize, gsize align, gsize offset, gsize size)
174 GstMemoryDefault *mem;
175 gsize aoffset, slice_size;
178 /* ensure configured alignment */
179 align |= gst_memory_alignment;
180 /* allocate more to compensate for alignment */
182 /* alloc header and data in one block */
183 slice_size = sizeof (GstMemoryDefault) + maxsize;
185 mem = g_slice_alloc (slice_size);
189 data = (guint8 *) mem + sizeof (GstMemoryDefault);
191 if ((aoffset = ((guintptr) data & align)))
192 aoffset = (align + 1) - aoffset;
194 _default_mem_init (mem, 0, NULL, slice_size, data, maxsize,
195 aoffset + offset, size, NULL, NULL);
201 _default_alloc_alloc (GstAllocator * allocator, gsize maxsize, gsize align,
204 return (GstMemory *) _default_mem_new_block (maxsize, align, 0, maxsize);
208 _default_mem_map (GstMemoryDefault * mem, GstMapFlags flags)
214 _default_mem_unmap (GstMemoryDefault * mem)
220 _default_mem_free (GstMemoryDefault * mem)
222 GST_CAT_DEBUG (GST_CAT_MEMORY, "free memory %p", mem);
225 gst_memory_unref (mem->mem.parent);
228 mem->notify (mem->user_data);
230 g_slice_free1 (mem->slice_size, mem);
233 static GstMemoryDefault *
234 _default_mem_copy (GstMemoryDefault * mem, gssize offset, gsize size)
236 GstMemoryDefault *copy;
239 size = mem->mem.size > offset ? mem->mem.size - offset : 0;
242 _default_mem_new_block (mem->mem.maxsize, 0, mem->mem.offset + offset,
244 memcpy (copy->data, mem->data, mem->mem.maxsize);
245 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy memory %p -> %p", mem, copy);
250 static GstMemoryDefault *
251 _default_mem_share (GstMemoryDefault * mem, gssize offset, gsize size)
253 GstMemoryDefault *sub;
256 /* find the real parent */
257 if ((parent = mem->mem.parent) == NULL)
258 parent = (GstMemory *) mem;
261 size = mem->mem.size - offset;
264 _default_mem_new (parent->flags, parent, mem->data,
265 mem->mem.maxsize, mem->mem.offset + offset, size, NULL, NULL);
271 _default_mem_is_span (GstMemoryDefault * mem1, GstMemoryDefault * mem2,
276 GstMemoryDefault *parent;
278 parent = (GstMemoryDefault *) mem1->mem.parent;
280 *offset = mem1->mem.offset - parent->mem.offset;
283 /* and memory is contiguous */
284 return mem1->data + mem1->mem.offset + mem1->mem.size ==
285 mem2->data + mem2->mem.offset;
289 _fallback_mem_copy (GstMemory * mem, gssize offset, gssize size)
292 GstMapInfo sinfo, dinfo;
294 if (!gst_memory_map (mem, &sinfo, GST_MAP_READ))
298 size = sinfo.size > offset ? sinfo.size - offset : 0;
300 /* use the same allocator as the memory we copy */
301 copy = gst_allocator_alloc (mem->allocator, size, mem->align);
302 if (!gst_memory_map (copy, &dinfo, GST_MAP_WRITE)) {
303 GST_CAT_WARNING (GST_CAT_MEMORY, "could not write map memory %p", copy);
304 gst_memory_unmap (mem, &sinfo);
308 memcpy (dinfo.data, sinfo.data + offset, size);
309 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy memory %p -> %p", mem, copy);
310 gst_memory_unmap (copy, &dinfo);
311 gst_memory_unmap (mem, &sinfo);
317 _fallback_mem_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
323 static GHashTable *allocators;
326 _priv_sysmem_notify (gpointer user_data)
328 g_warning ("The default memory allocator was freed!");
332 _priv_gst_memory_initialize (void)
334 static const GstMemoryInfo _mem_info = {
335 GST_ALLOCATOR_SYSMEM,
336 (GstAllocatorAllocFunction) _default_alloc_alloc,
337 (GstMemoryMapFunction) _default_mem_map,
338 (GstMemoryUnmapFunction) _default_mem_unmap,
339 (GstMemoryFreeFunction) _default_mem_free,
340 (GstMemoryCopyFunction) _default_mem_copy,
341 (GstMemoryShareFunction) _default_mem_share,
342 (GstMemoryIsSpanFunction) _default_mem_is_span,
345 #ifndef GST_DISABLE_TRACE
346 _gst_memory_trace = _gst_alloc_trace_register ("GstMemory", -1);
347 _gst_allocator_trace = _gst_alloc_trace_register ("GstAllocator", -1);
350 g_rw_lock_init (&lock);
351 allocators = g_hash_table_new (g_str_hash, g_str_equal);
353 #ifdef HAVE_GETPAGESIZE
354 #ifdef MEMORY_ALIGNMENT_PAGESIZE
355 gst_memory_alignment = getpagesize () - 1;
359 GST_CAT_DEBUG (GST_CAT_MEMORY, "memory alignment: %" G_GSIZE_FORMAT,
360 gst_memory_alignment);
362 _default_mem_impl = gst_allocator_new (&_mem_info, NULL, _priv_sysmem_notify);
364 _default_allocator = gst_allocator_ref (_default_mem_impl);
365 gst_allocator_register (GST_ALLOCATOR_SYSMEM,
366 gst_allocator_ref (_default_mem_impl));
370 * gst_memory_new_wrapped:
371 * @flags: #GstMemoryFlags
372 * @data: data to wrap
373 * @maxsize: allocated size of @data
374 * @offset: offset in @data
375 * @size: size of valid data
376 * @user_data: user_data
377 * @notify: called with @user_data when the memory is freed
379 * Allocate a new memory block that wraps the given @data.
381 * Returns: a new #GstMemory.
384 gst_memory_new_wrapped (GstMemoryFlags flags, gpointer data,
385 gsize maxsize, gsize offset, gsize size, gpointer user_data,
386 GDestroyNotify notify)
388 GstMemoryDefault *mem;
390 g_return_val_if_fail (data != NULL, NULL);
391 g_return_val_if_fail (offset + size <= maxsize, NULL);
394 _default_mem_new (flags, NULL, data, maxsize, offset, size, user_data,
397 #ifndef GST_DISABLE_TRACE
398 _gst_alloc_trace_new (_gst_memory_trace, mem);
401 return (GstMemory *) mem;
408 * Increases the refcount of @mem.
410 * Returns: @mem with increased refcount
413 gst_memory_ref (GstMemory * mem)
415 g_return_val_if_fail (mem != NULL, NULL);
417 GST_CAT_TRACE (GST_CAT_MEMORY, "memory %p, %d->%d", mem, mem->refcount,
420 g_atomic_int_inc (&mem->refcount);
429 * Decreases the refcount of @mem. When the refcount reaches 0, the free
430 * function of @mem will be called.
433 gst_memory_unref (GstMemory * mem)
435 g_return_if_fail (mem != NULL);
436 g_return_if_fail (mem->allocator != NULL);
438 GST_CAT_TRACE (GST_CAT_MEMORY, "memory %p, %d->%d", mem, mem->refcount,
441 if (g_atomic_int_dec_and_test (&mem->refcount)) {
442 g_return_if_fail (g_atomic_int_get (&mem->state) < 4);
443 #ifndef GST_DISABLE_TRACE
444 _gst_alloc_trace_free (_gst_memory_trace, mem);
446 mem->allocator->info.mem_free (mem);
451 * gst_memory_is_exclusive:
454 * Check if the current ref to @mem is exclusive, this means that no other
455 * references exist other than @mem.
458 gst_memory_is_exclusive (GstMemory * mem)
460 g_return_val_if_fail (mem != NULL, FALSE);
462 return (g_atomic_int_get (&mem->refcount) == 1);
466 * gst_memory_get_sizes:
468 * @offset: pointer to offset
469 * @maxsize: pointer to maxsize
471 * Get the current @size, @offset and @maxsize of @mem.
473 * Returns: the current sizes of @mem
476 gst_memory_get_sizes (GstMemory * mem, gsize * offset, gsize * maxsize)
478 g_return_val_if_fail (mem != NULL, 0);
481 *offset = mem->offset;
483 *maxsize = mem->maxsize;
491 * @offset: a new offset
494 * Resize the memory region. @mem should be writable and offset + size should be
495 * less than the maxsize of @mem.
498 gst_memory_resize (GstMemory * mem, gssize offset, gsize size)
500 g_return_if_fail (mem != NULL);
501 g_return_if_fail (offset >= 0 || mem->offset >= -offset);
502 g_return_if_fail (size + mem->offset + offset <= mem->maxsize);
504 mem->offset += offset;
509 gst_memory_lock (GstMemory * mem, GstMapFlags flags)
511 gint access_mode, state, newstate;
513 access_mode = flags & 3;
516 state = g_atomic_int_get (&mem->state);
518 /* nothing mapped, set access_mode and refcount */
519 newstate = 4 | access_mode;
521 /* access_mode must match */
522 if ((state & access_mode) != access_mode)
524 /* increase refcount */
525 newstate = state + 4;
527 } while (!g_atomic_int_compare_and_exchange (&mem->state, state, newstate));
533 GST_CAT_DEBUG (GST_CAT_MEMORY, "lock failed %p: state %d, access_mode %d",
534 mem, state, access_mode);
540 gst_memory_unlock (GstMemory * mem)
542 gint state, newstate;
545 state = g_atomic_int_get (&mem->state);
546 /* decrease the refcount */
547 newstate = state - 4;
548 /* last refcount, unset access_mode */
551 } while (!g_atomic_int_compare_and_exchange (&mem->state, state, newstate));
556 * gst_memory_make_mapped:
557 * @mem: (transfer full): a #GstMemory
558 * @info: (out): pointer for info
559 * @flags: mapping flags
561 * Create a #GstMemory object that is mapped with @flags. If @mem is mappable
562 * with @flags, this function returns the mapped @mem directly. Otherwise a
563 * mapped copy of @mem is returned.
565 * This function takes ownership of old @mem and returns a reference to a new
568 * Returns: (transfer full): a #GstMemory object mapped with @flags or NULL when
569 * a mapping is not possible.
572 gst_memory_make_mapped (GstMemory * mem, GstMapInfo * info, GstMapFlags flags)
576 if (gst_memory_map (mem, info, flags)) {
579 result = gst_memory_copy (mem, 0, -1);
580 gst_memory_unref (mem);
585 if (!gst_memory_map (result, info, flags))
593 GST_CAT_DEBUG (GST_CAT_MEMORY, "cannot copy memory %p", mem);
598 GST_CAT_DEBUG (GST_CAT_MEMORY, "cannot map memory %p with flags %d", mem,
600 gst_memory_unref (result);
608 * @info: (out): pointer for info
609 * @flags: mapping flags
611 * Fill @info with the pointer and sizes of the memory in @mem that can be
612 * accessed according to @flags.
614 * This function can return %FALSE for various reasons:
615 * - the memory backed by @mem is not accessible with the given @flags.
616 * - the memory was already mapped with a different mapping.
618 * @info and its contents remain valid for as long as @mem is valid and
619 * until gst_memory_unmap() is called.
621 * For each gst_memory_map() call, a corresponding gst_memory_unmap() call
624 * Returns: %TRUE if the map operation was successful.
627 gst_memory_map (GstMemory * mem, GstMapInfo * info, GstMapFlags flags)
629 g_return_val_if_fail (mem != NULL, FALSE);
630 g_return_val_if_fail (info != NULL, FALSE);
632 if (!gst_memory_lock (mem, flags))
635 info->data = mem->allocator->info.mem_map (mem, mem->maxsize, flags);
637 if (G_UNLIKELY (info->data == NULL))
642 info->size = mem->size;
643 info->maxsize = mem->maxsize - mem->offset;
644 info->data = info->data + mem->offset;
651 GST_CAT_DEBUG (GST_CAT_MEMORY, "mem %p: lock %d failed", mem, flags);
656 /* something went wrong, restore the orginal state again */
657 GST_CAT_ERROR (GST_CAT_MEMORY, "mem %p: map failed", mem);
658 gst_memory_unlock (mem);
666 * @info: a #GstMapInfo
668 * Release the memory obtained with gst_memory_map()
671 gst_memory_unmap (GstMemory * mem, GstMapInfo * info)
673 g_return_if_fail (mem != NULL);
674 g_return_if_fail (info != NULL);
675 g_return_if_fail (info->memory == mem);
676 /* there must be a ref */
677 g_return_if_fail (g_atomic_int_get (&mem->state) >= 4);
679 mem->allocator->info.mem_unmap (mem);
680 gst_memory_unlock (mem);
686 * @offset: an offset to copy
687 * @size: size to copy or -1 to copy all bytes from offset
689 * Return a copy of @size bytes from @mem starting from @offset. This copy is
690 * guaranteed to be writable. @size can be set to -1 to return a copy all bytes
693 * Returns: a new #GstMemory.
696 gst_memory_copy (GstMemory * mem, gssize offset, gssize size)
700 g_return_val_if_fail (mem != NULL, NULL);
702 copy = mem->allocator->info.mem_copy (mem, offset, size);
704 #ifndef GST_DISABLE_TRACE
705 _gst_alloc_trace_new (_gst_memory_trace, copy);
714 * @offset: an offset to share
715 * @size: size to share or -1 to share bytes from offset
717 * Return a shared copy of @size bytes from @mem starting from @offset. No
718 * memory copy is performed and the memory region is simply shared. The result
719 * is guaranteed to be not-writable. @size can be set to -1 to return a share
720 * all bytes from @offset.
722 * Returns: a new #GstMemory.
725 gst_memory_share (GstMemory * mem, gssize offset, gssize size)
729 g_return_val_if_fail (mem != NULL, NULL);
730 g_return_val_if_fail (!GST_MEMORY_FLAG_IS_SET (mem, GST_MEMORY_FLAG_NO_SHARE),
733 shared = mem->allocator->info.mem_share (mem, offset, size);
735 #ifndef GST_DISABLE_TRACE
736 _gst_alloc_trace_new (_gst_memory_trace, shared);
743 * gst_memory_is_span:
744 * @mem1: a #GstMemory
745 * @mem2: a #GstMemory
746 * @offset: a pointer to a result offset
748 * Check if @mem1 and mem2 share the memory with a common parent memory object
749 * and that the memory is contiguous.
751 * If this is the case, the memory of @mem1 and @mem2 can be merged
752 * efficiently by performing gst_memory_share() on the parent object from
753 * the returned @offset.
755 * Returns: %TRUE if the memory is contiguous and of a common parent.
758 gst_memory_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
760 g_return_val_if_fail (mem1 != NULL, FALSE);
761 g_return_val_if_fail (mem2 != NULL, FALSE);
763 /* need to have the same allocators */
764 if (mem1->allocator != mem2->allocator)
767 /* need to have the same parent */
768 if (mem1->parent == NULL || mem1->parent != mem2->parent)
771 /* and memory is contiguous */
772 if (!mem1->allocator->info.mem_is_span (mem1, mem2, offset))
779 * gst_allocator_register:
780 * @info: a #GstMemoryInfo
781 * @user_data: user data
782 * @notify: a #GDestroyNotify for @user_data
784 * Create a new memory allocator with @info and @user_data.
786 * All functions in @info are mandatory exept the copy and is_span
787 * functions, which will have a default implementation when left NULL.
789 * The @user_data will be passed to all calls of the alloc function. @notify
790 * will be called with @user_data when the allocator is freed.
792 * Returns: a new #GstAllocator.
795 gst_allocator_new (const GstMemoryInfo * info, gpointer user_data,
796 GDestroyNotify notify)
798 GstAllocator *allocator;
800 #define INSTALL_FALLBACK(_t) \
801 if (allocator->info._t == NULL) allocator->info._t = _fallback_ ##_t;
803 g_return_val_if_fail (info != NULL, NULL);
804 g_return_val_if_fail (info->alloc != NULL, NULL);
805 g_return_val_if_fail (info->mem_map != NULL, NULL);
806 g_return_val_if_fail (info->mem_unmap != NULL, NULL);
807 g_return_val_if_fail (info->mem_free != NULL, NULL);
808 g_return_val_if_fail (info->mem_share != NULL, NULL);
810 allocator = g_slice_new (GstAllocator);
811 allocator->refcount = 1;
812 allocator->info = *info;
813 allocator->user_data = user_data;
814 allocator->notify = notify;
815 INSTALL_FALLBACK (mem_copy);
816 INSTALL_FALLBACK (mem_is_span);
817 #undef INSTALL_FALLBACK
819 GST_CAT_DEBUG (GST_CAT_MEMORY, "new allocator %p", allocator);
821 #ifndef GST_DISABLE_TRACE
822 _gst_alloc_trace_new (_gst_allocator_trace, allocator);
829 * gst_alocator_get_memory_type:
830 * @allocator: a #GstAllocator
832 * Get the memory type allocated by this allocator
834 * Returns: the memory type provided by @allocator
837 gst_allocator_get_memory_type (GstAllocator * allocator)
839 g_return_val_if_fail (allocator != NULL, NULL);
841 return allocator->info.mem_type;
846 * @allocator: a #GstAllocator
848 * Increases the refcount of @allocator.
850 * Returns: @allocator with increased refcount
853 gst_allocator_ref (GstAllocator * allocator)
855 g_return_val_if_fail (allocator != NULL, NULL);
857 GST_CAT_TRACE (GST_CAT_MEMORY, "alocator %p, %d->%d", allocator,
858 allocator->refcount, allocator->refcount + 1);
860 g_atomic_int_inc (&allocator->refcount);
866 * gst_allocator_unref:
867 * @allocator: a #GstAllocator
869 * Decreases the refcount of @allocator. When the refcount reaches 0, the notify
870 * function of @allocator will be called and the allocator will be freed.
873 gst_allocator_unref (GstAllocator * allocator)
875 g_return_if_fail (allocator != NULL);
877 GST_CAT_TRACE (GST_CAT_MEMORY, "allocator %p, %d->%d", allocator,
878 allocator->refcount, allocator->refcount - 1);
880 if (g_atomic_int_dec_and_test (&allocator->refcount)) {
881 if (allocator->notify)
882 allocator->notify (allocator->user_data);
883 #ifndef GST_DISABLE_TRACE
884 _gst_alloc_trace_free (_gst_allocator_trace, allocator);
886 g_slice_free1 (sizeof (GstAllocator), allocator);
891 * gst_allocator_register:
892 * @name: the name of the allocator
893 * @allocator: (transfer full): #GstAllocator
895 * Registers the memory @allocator with @name. This function takes ownership of
899 gst_allocator_register (const gchar * name, GstAllocator * allocator)
901 g_return_if_fail (name != NULL);
902 g_return_if_fail (allocator != NULL);
904 GST_CAT_DEBUG (GST_CAT_MEMORY, "registering allocator %p with name \"%s\"",
907 g_rw_lock_writer_lock (&lock);
908 g_hash_table_insert (allocators, (gpointer) name, (gpointer) allocator);
909 g_rw_lock_writer_unlock (&lock);
913 * gst_allocator_find:
914 * @name: the name of the allocator
916 * Find a previously registered allocator with @name. When @name is NULL, the
917 * default allocator will be returned.
919 * Returns: (transfer full): a #GstAllocator or NULL when the allocator with @name was not
920 * registered. Use gst_allocator_unref() to release the allocator after usage.
923 gst_allocator_find (const gchar * name)
925 GstAllocator *allocator;
927 g_rw_lock_reader_lock (&lock);
929 allocator = g_hash_table_lookup (allocators, (gconstpointer) name);
931 allocator = _default_allocator;
934 gst_allocator_ref (allocator);
935 g_rw_lock_reader_unlock (&lock);
941 * gst_allocator_set_default:
942 * @allocator: (transfer full): a #GstAllocator
944 * Set the default allocator. This function takes ownership of @allocator.
947 gst_allocator_set_default (GstAllocator * allocator)
950 g_return_if_fail (allocator != NULL);
952 g_rw_lock_writer_lock (&lock);
953 old = _default_allocator;
954 _default_allocator = allocator;
955 g_rw_lock_writer_unlock (&lock);
958 gst_allocator_unref (old);
962 * gst_allocator_alloc:
963 * @allocator: (transfer none) (allow-none): a #GstAllocator to use
964 * @maxsize: allocated size of @data
965 * @align: alignment for the data
967 * Use @allocator to allocate a new memory block with memory that is at least
968 * @maxsize big and has the given alignment.
970 * When @allocator is NULL, the default allocator will be used.
972 * @align is given as a bitmask so that @align + 1 equals the amount of bytes to
973 * align to. For example, to align to 8 bytes, use an alignment of 7.
975 * Returns: (transfer full): a new #GstMemory.
978 gst_allocator_alloc (GstAllocator * allocator, gsize maxsize, gsize align)
982 g_return_val_if_fail (((align + 1) & align) == 0, NULL);
984 if (allocator == NULL)
985 allocator = _default_allocator;
987 mem = allocator->info.alloc (allocator, maxsize, align, allocator->user_data);
988 #ifndef GST_DISABLE_TRACE
989 _gst_alloc_trace_new (_gst_memory_trace, mem);