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 #ifndef GST_DISABLE_TRACE
76 static GstAllocTrace *_gst_memory_trace;
77 static GstAllocTrace *_gst_allocator_trace;
80 G_DEFINE_BOXED_TYPE (GstMemory, gst_memory, (GBoxedCopyFunc) gst_memory_ref,
81 (GBoxedFreeFunc) gst_memory_unref);
83 G_DEFINE_BOXED_TYPE (GstAllocator, gst_allocator,
84 (GBoxedCopyFunc) gst_allocator_ref, (GBoxedFreeFunc) gst_allocator_unref);
86 G_DEFINE_BOXED_TYPE (GstAllocationParams, gst_allocation_params,
87 (GBoxedCopyFunc) gst_allocation_params_copy,
88 (GBoxedFreeFunc) gst_allocation_params_free);
90 #if defined(MEMORY_ALIGNMENT_MALLOC)
91 size_t gst_memory_alignment = 7;
92 #elif defined(MEMORY_ALIGNMENT_PAGESIZE)
93 /* we fill this in in the _init method */
94 size_t gst_memory_alignment = 0;
95 #elif defined(MEMORY_ALIGNMENT)
96 size_t gst_memory_alignment = MEMORY_ALIGNMENT - 1;
98 #error "No memory alignment configured"
99 size_t gst_memory_alignment = 0;
109 GDestroyNotify notify;
112 /* default memory implementation */
119 GDestroyNotify notify;
122 /* the default allocator */
123 static GstAllocator *_default_allocator;
125 /* our predefined allocators */
126 static GstAllocator *_default_mem_impl;
128 /* initialize the fields */
130 _default_mem_init (GstMemoryDefault * mem, GstMemoryFlags flags,
131 GstMemory * parent, gsize slice_size, gpointer data,
132 gsize maxsize, gsize offset, gsize size, gsize align,
133 gpointer user_data, GDestroyNotify notify)
135 mem->mem.allocator = _default_mem_impl;
136 mem->mem.flags = flags;
137 mem->mem.refcount = 1;
138 mem->mem.parent = parent ? gst_memory_ref (parent) : NULL;
139 mem->mem.state = (flags & GST_MEMORY_FLAG_READONLY ? 0x1 : 0);
140 mem->mem.maxsize = maxsize;
141 mem->mem.align = align;
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, maxsize:%" G_GSIZE_FORMAT
150 " offset:%" G_GSIZE_FORMAT " size:%" G_GSIZE_FORMAT, mem, maxsize,
154 /* create a new memory block that manages the given memory */
155 static GstMemoryDefault *
156 _default_mem_new (GstMemoryFlags flags, GstMemory * parent, gpointer data,
157 gsize maxsize, gsize offset, gsize size, gsize align, gpointer user_data,
158 GDestroyNotify notify)
160 GstMemoryDefault *mem;
163 slice_size = sizeof (GstMemoryDefault);
165 mem = g_slice_alloc (slice_size);
166 _default_mem_init (mem, flags, parent, slice_size,
167 data, maxsize, offset, size, align, user_data, notify);
172 /* allocate the memory and structure in one block */
173 static GstMemoryDefault *
174 _default_mem_new_block (GstMemoryFlags flags, gsize maxsize, gsize align,
175 gsize offset, gsize size)
177 GstMemoryDefault *mem;
178 gsize aoffset, slice_size, padding;
181 /* ensure configured alignment */
182 align |= gst_memory_alignment;
183 /* allocate more to compensate for alignment */
185 /* alloc header and data in one block */
186 slice_size = sizeof (GstMemoryDefault) + maxsize;
188 mem = g_slice_alloc (slice_size);
192 data = (guint8 *) mem + sizeof (GstMemoryDefault);
195 if ((aoffset = ((guintptr) data & align))) {
196 aoffset = (align + 1) - aoffset;
201 if (offset && (flags & GST_MEMORY_FLAG_ZERO_PREFIXED))
202 memset (data, 0, offset);
204 padding = maxsize - (offset + size);
205 if (padding && (flags & GST_MEMORY_FLAG_ZERO_PADDED))
206 memset (data + offset + size, 0, padding);
208 _default_mem_init (mem, flags, NULL, slice_size, data, maxsize,
209 offset, size, align, NULL, NULL);
215 _default_alloc_alloc (GstAllocator * allocator, gsize size,
216 GstAllocationParams * params, gpointer user_data)
218 gsize maxsize = size + params->prefix + params->padding;
220 return (GstMemory *) _default_mem_new_block (params->flags,
221 maxsize, params->align, params->prefix, size);
225 _default_mem_map (GstMemoryDefault * mem, gsize maxsize, GstMapFlags flags)
231 _default_mem_unmap (GstMemoryDefault * mem)
237 _default_mem_free (GstMemoryDefault * mem)
239 GST_CAT_DEBUG (GST_CAT_MEMORY, "free memory %p", mem);
242 gst_memory_unref (mem->mem.parent);
245 mem->notify (mem->user_data);
247 g_slice_free1 (mem->slice_size, mem);
250 static GstMemoryDefault *
251 _default_mem_copy (GstMemoryDefault * mem, gssize offset, gsize size)
253 GstMemoryDefault *copy;
256 size = mem->mem.size > offset ? mem->mem.size - offset : 0;
259 _default_mem_new_block (0, mem->mem.maxsize, 0, mem->mem.offset + offset,
261 GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
262 "memcpy %" G_GSIZE_FORMAT " memory %p -> %p", mem->mem.maxsize, mem,
264 memcpy (copy->data, mem->data, mem->mem.maxsize);
269 static GstMemoryDefault *
270 _default_mem_share (GstMemoryDefault * mem, gssize offset, gsize size)
272 GstMemoryDefault *sub;
275 /* find the real parent */
276 if ((parent = mem->mem.parent) == NULL)
277 parent = (GstMemory *) mem;
280 size = mem->mem.size - offset;
283 _default_mem_new (parent->flags, parent, mem->data,
284 mem->mem.maxsize, mem->mem.offset + offset, size, mem->mem.align, NULL,
291 _default_mem_is_span (GstMemoryDefault * mem1, GstMemoryDefault * mem2,
296 GstMemoryDefault *parent;
298 parent = (GstMemoryDefault *) mem1->mem.parent;
300 *offset = mem1->mem.offset - parent->mem.offset;
303 /* and memory is contiguous */
304 return mem1->data + mem1->mem.offset + mem1->mem.size ==
305 mem2->data + mem2->mem.offset;
309 _fallback_mem_copy (GstMemory * mem, gssize offset, gssize size)
312 GstMapInfo sinfo, dinfo;
313 GstAllocationParams params = { 0, 0, 0, mem->align, };
315 if (!gst_memory_map (mem, &sinfo, GST_MAP_READ))
319 size = sinfo.size > offset ? sinfo.size - offset : 0;
321 /* use the same allocator as the memory we copy */
322 copy = gst_allocator_alloc (mem->allocator, size, ¶ms);
323 if (!gst_memory_map (copy, &dinfo, GST_MAP_WRITE)) {
324 GST_CAT_WARNING (GST_CAT_MEMORY, "could not write map memory %p", copy);
325 gst_memory_unmap (mem, &sinfo);
329 GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
330 "memcpy %" G_GSSIZE_FORMAT " memory %p -> %p", size, mem, copy);
331 memcpy (dinfo.data, sinfo.data + offset, size);
332 gst_memory_unmap (copy, &dinfo);
333 gst_memory_unmap (mem, &sinfo);
339 _fallback_mem_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
345 static GHashTable *allocators;
348 _priv_sysmem_notify (gpointer user_data)
350 g_warning ("The default memory allocator was freed!");
354 _priv_gst_memory_initialize (void)
356 static const GstMemoryInfo _mem_info = {
357 GST_ALLOCATOR_SYSMEM,
358 (GstAllocatorAllocFunction) _default_alloc_alloc,
359 (GstMemoryMapFunction) _default_mem_map,
360 (GstMemoryUnmapFunction) _default_mem_unmap,
361 (GstMemoryFreeFunction) _default_mem_free,
362 (GstMemoryCopyFunction) _default_mem_copy,
363 (GstMemoryShareFunction) _default_mem_share,
364 (GstMemoryIsSpanFunction) _default_mem_is_span,
367 #ifndef GST_DISABLE_TRACE
368 _gst_memory_trace = _gst_alloc_trace_register ("GstMemory", -1);
369 _gst_allocator_trace = _gst_alloc_trace_register ("GstAllocator", -1);
372 g_rw_lock_init (&lock);
373 allocators = g_hash_table_new (g_str_hash, g_str_equal);
375 #ifdef HAVE_GETPAGESIZE
376 #ifdef MEMORY_ALIGNMENT_PAGESIZE
377 gst_memory_alignment = getpagesize () - 1;
381 GST_CAT_DEBUG (GST_CAT_MEMORY, "memory alignment: %" G_GSIZE_FORMAT,
382 gst_memory_alignment);
384 _default_mem_impl = gst_allocator_new (&_mem_info, NULL, _priv_sysmem_notify);
386 _default_allocator = gst_allocator_ref (_default_mem_impl);
387 gst_allocator_register (GST_ALLOCATOR_SYSMEM,
388 gst_allocator_ref (_default_mem_impl));
392 * gst_memory_new_wrapped:
393 * @flags: #GstMemoryFlags
394 * @data: data to wrap
395 * @maxsize: allocated size of @data
396 * @offset: offset in @data
397 * @size: size of valid data
398 * @user_data: user_data
399 * @notify: called with @user_data when the memory is freed
401 * Allocate a new memory block that wraps the given @data.
403 * The prefix/padding must be filled with 0 if @flags contains
404 * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED respectively.
406 * Returns: a new #GstMemory.
409 gst_memory_new_wrapped (GstMemoryFlags flags, gpointer data,
410 gsize maxsize, gsize offset, gsize size, gpointer user_data,
411 GDestroyNotify notify)
413 GstMemoryDefault *mem;
415 g_return_val_if_fail (data != NULL, NULL);
416 g_return_val_if_fail (offset + size <= maxsize, NULL);
419 _default_mem_new (flags, NULL, data, maxsize, offset, size, 0, user_data,
422 #ifndef GST_DISABLE_TRACE
423 _gst_alloc_trace_new (_gst_memory_trace, mem);
426 return (GstMemory *) mem;
433 * Increases the refcount of @mem.
435 * Returns: @mem with increased refcount
438 gst_memory_ref (GstMemory * mem)
440 g_return_val_if_fail (mem != NULL, NULL);
442 GST_CAT_TRACE (GST_CAT_MEMORY, "memory %p, %d->%d", mem, mem->refcount,
445 g_atomic_int_inc (&mem->refcount);
454 * Decreases the refcount of @mem. When the refcount reaches 0, the free
455 * function of @mem will be called.
458 gst_memory_unref (GstMemory * mem)
460 g_return_if_fail (mem != NULL);
461 g_return_if_fail (mem->allocator != NULL);
463 GST_CAT_TRACE (GST_CAT_MEMORY, "memory %p, %d->%d", mem, mem->refcount,
466 if (g_atomic_int_dec_and_test (&mem->refcount)) {
467 /* there should be no outstanding mappings */
468 g_return_if_fail (g_atomic_int_get (&mem->state) < 4);
469 #ifndef GST_DISABLE_TRACE
470 _gst_alloc_trace_free (_gst_memory_trace, mem);
472 mem->allocator->info.mem_free (mem);
477 * gst_memory_is_exclusive:
480 * Check if the current ref to @mem is exclusive, this means that no other
481 * references exist other than @mem.
484 gst_memory_is_exclusive (GstMemory * mem)
486 g_return_val_if_fail (mem != NULL, FALSE);
488 return (g_atomic_int_get (&mem->refcount) == 1);
492 * gst_memory_get_sizes:
494 * @offset: pointer to offset
495 * @maxsize: pointer to maxsize
497 * Get the current @size, @offset and @maxsize of @mem.
499 * Returns: the current sizes of @mem
502 gst_memory_get_sizes (GstMemory * mem, gsize * offset, gsize * maxsize)
504 g_return_val_if_fail (mem != NULL, 0);
507 *offset = mem->offset;
509 *maxsize = mem->maxsize;
517 * @offset: a new offset
520 * Resize the memory region. @mem should be writable and offset + size should be
521 * less than the maxsize of @mem.
523 * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED will be
524 * cleared when offset or padding is increased respectively.
527 gst_memory_resize (GstMemory * mem, gssize offset, gsize size)
529 g_return_if_fail (mem != NULL);
530 g_return_if_fail (offset >= 0 || mem->offset >= -offset);
531 g_return_if_fail (size + mem->offset + offset <= mem->maxsize);
533 /* if we increase the prefix, we can't guarantee it is still 0 filled */
534 if ((offset > 0) && GST_MEMORY_IS_ZERO_PREFIXED (mem))
535 GST_MEMORY_FLAG_UNSET (mem, GST_MEMORY_FLAG_ZERO_PREFIXED);
537 /* if we increase the padding, we can't guarantee it is still 0 filled */
538 if ((offset + size < mem->size) && GST_MEMORY_IS_ZERO_PADDED (mem))
539 GST_MEMORY_FLAG_UNSET (mem, GST_MEMORY_FLAG_ZERO_PADDED);
541 mem->offset += offset;
546 gst_memory_lock (GstMemory * mem, GstMapFlags flags)
548 gint access_mode, state, newstate;
550 access_mode = flags & 3;
553 state = g_atomic_int_get (&mem->state);
555 /* nothing mapped, set access_mode and refcount */
556 newstate = 4 | access_mode;
558 /* access_mode must match */
559 if ((state & access_mode) != access_mode)
561 /* increase refcount */
562 newstate = state + 4;
564 } while (!g_atomic_int_compare_and_exchange (&mem->state, state, newstate));
570 GST_CAT_DEBUG (GST_CAT_MEMORY, "lock failed %p: state %d, access_mode %d",
571 mem, state, access_mode);
577 gst_memory_unlock (GstMemory * mem)
579 gint state, newstate;
582 state = g_atomic_int_get (&mem->state);
583 /* decrease the refcount */
584 newstate = state - 4;
585 /* last refcount, unset access_mode */
588 } while (!g_atomic_int_compare_and_exchange (&mem->state, state, newstate));
593 * gst_memory_make_mapped:
594 * @mem: (transfer full): a #GstMemory
595 * @info: (out): pointer for info
596 * @flags: mapping flags
598 * Create a #GstMemory object that is mapped with @flags. If @mem is mappable
599 * with @flags, this function returns the mapped @mem directly. Otherwise a
600 * mapped copy of @mem is returned.
602 * This function takes ownership of old @mem and returns a reference to a new
605 * Returns: (transfer full): a #GstMemory object mapped with @flags or NULL when
606 * a mapping is not possible.
609 gst_memory_make_mapped (GstMemory * mem, GstMapInfo * info, GstMapFlags flags)
613 if (gst_memory_map (mem, info, flags)) {
616 result = gst_memory_copy (mem, 0, -1);
617 gst_memory_unref (mem);
622 if (!gst_memory_map (result, info, flags))
630 GST_CAT_DEBUG (GST_CAT_MEMORY, "cannot copy memory %p", mem);
635 GST_CAT_DEBUG (GST_CAT_MEMORY, "cannot map memory %p with flags %d", mem,
637 gst_memory_unref (result);
645 * @info: (out): pointer for info
646 * @flags: mapping flags
648 * Fill @info with the pointer and sizes of the memory in @mem that can be
649 * accessed according to @flags.
651 * This function can return %FALSE for various reasons:
652 * - the memory backed by @mem is not accessible with the given @flags.
653 * - the memory was already mapped with a different mapping.
655 * @info and its contents remain valid for as long as @mem is valid and
656 * until gst_memory_unmap() is called.
658 * For each gst_memory_map() call, a corresponding gst_memory_unmap() call
661 * Returns: %TRUE if the map operation was successful.
664 gst_memory_map (GstMemory * mem, GstMapInfo * info, GstMapFlags flags)
666 g_return_val_if_fail (mem != NULL, FALSE);
667 g_return_val_if_fail (info != NULL, FALSE);
669 if (!gst_memory_lock (mem, flags))
672 info->data = mem->allocator->info.mem_map (mem, mem->maxsize, flags);
674 if (G_UNLIKELY (info->data == NULL))
679 info->size = mem->size;
680 info->maxsize = mem->maxsize - mem->offset;
681 info->data = info->data + mem->offset;
688 GST_CAT_DEBUG (GST_CAT_MEMORY, "mem %p: lock %d failed", mem, flags);
693 /* something went wrong, restore the orginal state again */
694 GST_CAT_ERROR (GST_CAT_MEMORY, "mem %p: map failed", mem);
695 gst_memory_unlock (mem);
703 * @info: a #GstMapInfo
705 * Release the memory obtained with gst_memory_map()
708 gst_memory_unmap (GstMemory * mem, GstMapInfo * info)
710 g_return_if_fail (mem != NULL);
711 g_return_if_fail (info != NULL);
712 g_return_if_fail (info->memory == mem);
713 /* there must be a ref */
714 g_return_if_fail (g_atomic_int_get (&mem->state) >= 4);
716 mem->allocator->info.mem_unmap (mem);
717 gst_memory_unlock (mem);
723 * @offset: an offset to copy
724 * @size: size to copy or -1 to copy all bytes from offset
726 * Return a copy of @size bytes from @mem starting from @offset. This copy is
727 * guaranteed to be writable. @size can be set to -1 to return a copy all bytes
730 * Returns: a new #GstMemory.
733 gst_memory_copy (GstMemory * mem, gssize offset, gssize size)
737 g_return_val_if_fail (mem != NULL, NULL);
739 copy = mem->allocator->info.mem_copy (mem, offset, size);
741 #ifndef GST_DISABLE_TRACE
742 _gst_alloc_trace_new (_gst_memory_trace, copy);
751 * @offset: an offset to share
752 * @size: size to share or -1 to share bytes from offset
754 * Return a shared copy of @size bytes from @mem starting from @offset. No
755 * memory copy is performed and the memory region is simply shared. The result
756 * is guaranteed to be not-writable. @size can be set to -1 to return a share
757 * all bytes from @offset.
759 * Returns: a new #GstMemory.
762 gst_memory_share (GstMemory * mem, gssize offset, gssize size)
766 g_return_val_if_fail (mem != NULL, NULL);
767 g_return_val_if_fail (!GST_MEMORY_FLAG_IS_SET (mem, GST_MEMORY_FLAG_NO_SHARE),
770 shared = mem->allocator->info.mem_share (mem, offset, size);
772 #ifndef GST_DISABLE_TRACE
773 _gst_alloc_trace_new (_gst_memory_trace, shared);
780 * gst_memory_is_span:
781 * @mem1: a #GstMemory
782 * @mem2: a #GstMemory
783 * @offset: a pointer to a result offset
785 * Check if @mem1 and mem2 share the memory with a common parent memory object
786 * and that the memory is contiguous.
788 * If this is the case, the memory of @mem1 and @mem2 can be merged
789 * efficiently by performing gst_memory_share() on the parent object from
790 * the returned @offset.
792 * Returns: %TRUE if the memory is contiguous and of a common parent.
795 gst_memory_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
797 g_return_val_if_fail (mem1 != NULL, FALSE);
798 g_return_val_if_fail (mem2 != NULL, FALSE);
800 /* need to have the same allocators */
801 if (mem1->allocator != mem2->allocator)
804 /* need to have the same parent */
805 if (mem1->parent == NULL || mem1->parent != mem2->parent)
808 /* and memory is contiguous */
809 if (!mem1->allocator->info.mem_is_span (mem1, mem2, offset))
817 * @info: a #GstMemoryInfo
818 * @user_data: user data
819 * @notify: a #GDestroyNotify for @user_data
821 * Create a new memory allocator with @info and @user_data.
823 * All functions in @info are mandatory exept the copy and is_span
824 * functions, which will have a default implementation when left NULL.
826 * The @user_data will be passed to all calls of the alloc function. @notify
827 * will be called with @user_data when the allocator is freed.
829 * Returns: a new #GstAllocator.
832 gst_allocator_new (const GstMemoryInfo * info, gpointer user_data,
833 GDestroyNotify notify)
835 GstAllocator *allocator;
837 #define INSTALL_FALLBACK(_t) \
838 if (allocator->info._t == NULL) allocator->info._t = _fallback_ ##_t;
840 g_return_val_if_fail (info != NULL, NULL);
841 g_return_val_if_fail (info->alloc != NULL, NULL);
842 g_return_val_if_fail (info->mem_map != NULL, NULL);
843 g_return_val_if_fail (info->mem_unmap != NULL, NULL);
844 g_return_val_if_fail (info->mem_free != NULL, NULL);
845 g_return_val_if_fail (info->mem_share != NULL, NULL);
847 allocator = g_slice_new (GstAllocator);
848 allocator->refcount = 1;
849 allocator->info = *info;
850 allocator->user_data = user_data;
851 allocator->notify = notify;
852 INSTALL_FALLBACK (mem_copy);
853 INSTALL_FALLBACK (mem_is_span);
854 #undef INSTALL_FALLBACK
856 GST_CAT_DEBUG (GST_CAT_MEMORY, "new allocator %p", allocator);
858 #ifndef GST_DISABLE_TRACE
859 _gst_alloc_trace_new (_gst_allocator_trace, allocator);
866 * gst_allocator_get_memory_type:
867 * @allocator: a #GstAllocator
869 * Get the memory type allocated by this allocator
871 * Returns: the memory type provided by @allocator
874 gst_allocator_get_memory_type (GstAllocator * allocator)
876 g_return_val_if_fail (allocator != NULL, NULL);
878 return allocator->info.mem_type;
883 * @allocator: a #GstAllocator
885 * Increases the refcount of @allocator.
887 * Returns: @allocator with increased refcount
890 gst_allocator_ref (GstAllocator * allocator)
892 g_return_val_if_fail (allocator != NULL, NULL);
894 GST_CAT_TRACE (GST_CAT_MEMORY, "allocator %p, %d->%d", allocator,
895 allocator->refcount, allocator->refcount + 1);
897 g_atomic_int_inc (&allocator->refcount);
903 * gst_allocator_unref:
904 * @allocator: a #GstAllocator
906 * Decreases the refcount of @allocator. When the refcount reaches 0, the notify
907 * function of @allocator will be called and the allocator will be freed.
910 gst_allocator_unref (GstAllocator * allocator)
912 g_return_if_fail (allocator != NULL);
914 GST_CAT_TRACE (GST_CAT_MEMORY, "allocator %p, %d->%d", allocator,
915 allocator->refcount, allocator->refcount - 1);
917 if (g_atomic_int_dec_and_test (&allocator->refcount)) {
918 if (allocator->notify)
919 allocator->notify (allocator->user_data);
920 #ifndef GST_DISABLE_TRACE
921 _gst_alloc_trace_free (_gst_allocator_trace, allocator);
923 g_slice_free1 (sizeof (GstAllocator), allocator);
928 * gst_allocator_register:
929 * @name: the name of the allocator
930 * @allocator: (transfer full): #GstAllocator
932 * Registers the memory @allocator with @name. This function takes ownership of
936 gst_allocator_register (const gchar * name, GstAllocator * allocator)
938 g_return_if_fail (name != NULL);
939 g_return_if_fail (allocator != NULL);
941 GST_CAT_DEBUG (GST_CAT_MEMORY, "registering allocator %p with name \"%s\"",
944 g_rw_lock_writer_lock (&lock);
945 g_hash_table_insert (allocators, (gpointer) name, (gpointer) allocator);
946 g_rw_lock_writer_unlock (&lock);
950 * gst_allocator_find:
951 * @name: the name of the allocator
953 * Find a previously registered allocator with @name. When @name is NULL, the
954 * default allocator will be returned.
956 * Returns: (transfer full): a #GstAllocator or NULL when the allocator with @name was not
957 * registered. Use gst_allocator_unref() to release the allocator after usage.
960 gst_allocator_find (const gchar * name)
962 GstAllocator *allocator;
964 g_rw_lock_reader_lock (&lock);
966 allocator = g_hash_table_lookup (allocators, (gconstpointer) name);
968 allocator = _default_allocator;
971 gst_allocator_ref (allocator);
972 g_rw_lock_reader_unlock (&lock);
978 * gst_allocator_set_default:
979 * @allocator: (transfer full): a #GstAllocator
981 * Set the default allocator. This function takes ownership of @allocator.
984 gst_allocator_set_default (GstAllocator * allocator)
987 g_return_if_fail (allocator != NULL);
989 g_rw_lock_writer_lock (&lock);
990 old = _default_allocator;
991 _default_allocator = allocator;
992 g_rw_lock_writer_unlock (&lock);
995 gst_allocator_unref (old);
999 * gst_allocation_params_init:
1000 * @params: a #GstAllocationParams
1002 * Initialize @params to its default values
1005 gst_allocation_params_init (GstAllocationParams * params)
1007 g_return_if_fail (params != NULL);
1009 memset (params, 0, sizeof (GstAllocationParams));
1013 * gst_allocation_params_copy:
1014 * @params: (transfer none): a #GstAllocationParams
1016 * Create a copy of @params.
1018 * Free-function: gst_allocation_params_free
1020 * Returns: (transfer full): a new ##GstAllocationParams, free with
1021 * gst_allocation_params_free().
1023 GstAllocationParams *
1024 gst_allocation_params_copy (const GstAllocationParams * params)
1026 GstAllocationParams *result = NULL;
1030 (GstAllocationParams *) g_slice_copy (sizeof (GstAllocationParams),
1037 * gst_allocation_params_free:
1038 * @params: (in) (transfer full): a #GstAllocationParams
1043 gst_allocation_params_free (GstAllocationParams * params)
1045 g_slice_free (GstAllocationParams, params);
1049 * gst_allocator_alloc:
1050 * @allocator: (transfer none) (allow-none): a #GstAllocator to use
1051 * @size: size of the visible memory area
1052 * @params: (transfer none) (allow-none): optional parameters
1054 * Use @allocator to allocate a new memory block with memory that is at least
1057 * The optional @params can specify the prefix and padding for the memory. If
1058 * NULL is passed, no flags, no extra prefix/padding and a default alignment is
1061 * The prefix/padding will be filled with 0 if flags contains
1062 * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED respectively.
1064 * When @allocator is NULL, the default allocator will be used.
1066 * The alignment in @params is given as a bitmask so that @align + 1 equals
1067 * the amount of bytes to align to. For example, to align to 8 bytes,
1068 * use an alignment of 7.
1070 * Returns: (transfer full): a new #GstMemory.
1073 gst_allocator_alloc (GstAllocator * allocator, gsize size,
1074 GstAllocationParams * params)
1077 static GstAllocationParams defparams = { 0, 0, 0, 0, };
1080 g_return_val_if_fail (((params->align + 1) & params->align) == 0, NULL);
1082 params = &defparams;
1085 if (allocator == NULL)
1086 allocator = _default_allocator;
1088 mem = allocator->info.alloc (allocator, size, params, allocator->user_data);
1090 #ifndef GST_DISABLE_TRACE
1091 _gst_alloc_trace_new (_gst_memory_trace, mem);