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;
94 /* default memory implementation */
101 GDestroyNotify notify;
104 /* the default allocator */
105 static GstAllocator *_default_allocator;
107 /* our predefined allocators */
108 static GstAllocator *_default_mem_impl;
111 _gst_memory_copy (GstMemory * mem)
113 GST_CAT_DEBUG (GST_CAT_MEMORY, "copy memory %p", mem);
114 return gst_memory_copy (mem, 0, -1);
118 _gst_memory_free (GstMemory * mem)
120 GST_CAT_DEBUG (GST_CAT_MEMORY, "free memory %p", mem);
123 gst_memory_unlock (mem->parent, GST_LOCK_FLAG_EXCLUSIVE);
124 gst_memory_unref (mem->parent);
127 mem->allocator->info.mem_free (mem);
131 * gst_memory_init: (skip)
133 * @flags: #GstMemoryFlags
134 * @allocator: the #GstAllocator
135 * @parent: the parent of @mem
136 * @maxsize: the total size of the memory
137 * @align: the alignment of the memory
138 * @offset: The offset in the memory
139 * @size: the size of valid data in the memory
141 * Initializes a newly allocated @mem with the given parameters. This function
142 * will call gst_mini_object_init() with the default memory parameters.
145 gst_memory_init (GstMemory * mem, GstMemoryFlags flags,
146 GstAllocator * allocator, GstMemory * parent, gsize maxsize, gsize align,
147 gsize offset, gsize size)
149 gst_mini_object_init (GST_MINI_OBJECT_CAST (mem),
150 flags | GST_MINI_OBJECT_FLAG_LOCKABLE, GST_TYPE_MEMORY,
151 (GstMiniObjectCopyFunction) _gst_memory_copy, NULL,
152 (GstMiniObjectFreeFunction) _gst_memory_free);
154 mem->allocator = allocator;
156 gst_memory_lock (parent, GST_LOCK_FLAG_EXCLUSIVE);
157 gst_memory_ref (parent);
159 mem->parent = parent;
160 mem->maxsize = maxsize;
162 mem->offset = offset;
165 GST_CAT_DEBUG (GST_CAT_MEMORY, "new memory %p, maxsize:%" G_GSIZE_FORMAT
166 " offset:%" G_GSIZE_FORMAT " size:%" G_GSIZE_FORMAT, mem, maxsize,
170 /* initialize the fields */
172 _default_mem_init (GstMemoryDefault * mem, GstMemoryFlags flags,
173 GstMemory * parent, gsize slice_size, gpointer data,
174 gsize maxsize, gsize align, gsize offset, gsize size,
175 gpointer user_data, GDestroyNotify notify)
177 gst_memory_init (GST_MEMORY_CAST (mem),
178 flags, _default_mem_impl, parent, maxsize, align, offset, size);
180 mem->slice_size = slice_size;
182 mem->user_data = user_data;
183 mem->notify = notify;
186 /* create a new memory block that manages the given memory */
187 static inline GstMemoryDefault *
188 _default_mem_new (GstMemoryFlags flags, GstMemory * parent, gpointer data,
189 gsize maxsize, gsize align, gsize offset, gsize size, gpointer user_data,
190 GDestroyNotify notify)
192 GstMemoryDefault *mem;
195 slice_size = sizeof (GstMemoryDefault);
197 mem = g_slice_alloc (slice_size);
198 _default_mem_init (mem, flags, parent, slice_size,
199 data, maxsize, align, offset, size, user_data, notify);
204 /* allocate the memory and structure in one block */
205 static GstMemoryDefault *
206 _default_mem_new_block (GstMemoryFlags flags, gsize maxsize, gsize align,
207 gsize offset, gsize size)
209 GstMemoryDefault *mem;
210 gsize aoffset, slice_size, padding;
213 /* ensure configured alignment */
214 align |= gst_memory_alignment;
215 /* allocate more to compensate for alignment */
217 /* alloc header and data in one block */
218 slice_size = sizeof (GstMemoryDefault) + maxsize;
220 mem = g_slice_alloc (slice_size);
224 data = (guint8 *) mem + sizeof (GstMemoryDefault);
227 if ((aoffset = ((guintptr) data & align))) {
228 aoffset = (align + 1) - aoffset;
233 if (offset && (flags & GST_MEMORY_FLAG_ZERO_PREFIXED))
234 memset (data, 0, offset);
236 padding = maxsize - (offset + size);
237 if (padding && (flags & GST_MEMORY_FLAG_ZERO_PADDED))
238 memset (data + offset + size, 0, padding);
240 _default_mem_init (mem, flags, NULL, slice_size, data, maxsize,
241 align, offset, size, NULL, NULL);
247 _default_alloc_alloc (GstAllocator * allocator, gsize size,
248 GstAllocationParams * params, gpointer user_data)
250 gsize maxsize = size + params->prefix + params->padding;
252 return (GstMemory *) _default_mem_new_block (params->flags,
253 maxsize, params->align, params->prefix, size);
257 _default_mem_map (GstMemoryDefault * mem, gsize maxsize, GstMapFlags flags)
263 _default_mem_unmap (GstMemoryDefault * mem)
269 _default_mem_free (GstMemoryDefault * mem)
272 mem->notify (mem->user_data);
274 g_slice_free1 (mem->slice_size, mem);
277 static GstMemoryDefault *
278 _default_mem_copy (GstMemoryDefault * mem, gssize offset, gsize size)
280 GstMemoryDefault *copy;
283 size = mem->mem.size > offset ? mem->mem.size - offset : 0;
286 _default_mem_new_block (0, mem->mem.maxsize, 0, mem->mem.offset + offset,
288 GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
289 "memcpy %" G_GSIZE_FORMAT " memory %p -> %p", mem->mem.maxsize, mem,
291 memcpy (copy->data, mem->data, mem->mem.maxsize);
296 static GstMemoryDefault *
297 _default_mem_share (GstMemoryDefault * mem, gssize offset, gsize size)
299 GstMemoryDefault *sub;
302 /* find the real parent */
303 if ((parent = mem->mem.parent) == NULL)
304 parent = (GstMemory *) mem;
307 size = mem->mem.size - offset;
309 /* the shared memory is always readonly */
311 _default_mem_new (GST_MINI_OBJECT_FLAGS (parent) |
312 GST_MINI_OBJECT_FLAG_LOCK_READONLY, parent, mem->data, mem->mem.maxsize,
313 mem->mem.align, mem->mem.offset + offset, size, NULL, NULL);
319 _default_mem_is_span (GstMemoryDefault * mem1, GstMemoryDefault * mem2,
324 GstMemoryDefault *parent;
326 parent = (GstMemoryDefault *) mem1->mem.parent;
328 *offset = mem1->mem.offset - parent->mem.offset;
331 /* and memory is contiguous */
332 return mem1->data + mem1->mem.offset + mem1->mem.size ==
333 mem2->data + mem2->mem.offset;
337 _fallback_mem_copy (GstMemory * mem, gssize offset, gssize size)
340 GstMapInfo sinfo, dinfo;
341 GstAllocationParams params = { 0, 0, 0, mem->align, };
343 if (!gst_memory_map (mem, &sinfo, GST_MAP_READ))
347 size = sinfo.size > offset ? sinfo.size - offset : 0;
349 /* use the same allocator as the memory we copy */
350 copy = gst_allocator_alloc (mem->allocator, size, ¶ms);
351 if (!gst_memory_map (copy, &dinfo, GST_MAP_WRITE)) {
352 GST_CAT_WARNING (GST_CAT_MEMORY, "could not write map memory %p", copy);
353 gst_memory_unmap (mem, &sinfo);
357 GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
358 "memcpy %" G_GSSIZE_FORMAT " memory %p -> %p", size, mem, copy);
359 memcpy (dinfo.data, sinfo.data + offset, size);
360 gst_memory_unmap (copy, &dinfo);
361 gst_memory_unmap (mem, &sinfo);
367 _fallback_mem_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
373 static GHashTable *allocators;
376 _priv_sysmem_free (GstMiniObject * obj)
378 g_warning ("The default memory allocator was freed!");
382 _priv_gst_memory_initialize (void)
384 static const GstMemoryInfo _mem_info = {
385 GST_ALLOCATOR_SYSMEM,
386 (GstAllocatorAllocFunction) _default_alloc_alloc,
387 (GstMemoryMapFunction) _default_mem_map,
388 (GstMemoryUnmapFunction) _default_mem_unmap,
389 (GstMemoryFreeFunction) _default_mem_free,
390 (GstMemoryCopyFunction) _default_mem_copy,
391 (GstMemoryShareFunction) _default_mem_share,
392 (GstMemoryIsSpanFunction) _default_mem_is_span,
395 g_rw_lock_init (&lock);
396 allocators = g_hash_table_new (g_str_hash, g_str_equal);
398 #ifdef HAVE_GETPAGESIZE
399 #ifdef MEMORY_ALIGNMENT_PAGESIZE
400 gst_memory_alignment = getpagesize () - 1;
404 GST_CAT_DEBUG (GST_CAT_MEMORY, "memory alignment: %" G_GSIZE_FORMAT,
405 gst_memory_alignment);
407 _default_mem_impl = g_slice_new (GstAllocator);
408 gst_allocator_init (_default_mem_impl, 0, &_mem_info, _priv_sysmem_free);
410 _default_allocator = gst_allocator_ref (_default_mem_impl);
411 gst_allocator_register (GST_ALLOCATOR_SYSMEM,
412 gst_allocator_ref (_default_mem_impl));
416 * gst_memory_new_wrapped:
417 * @flags: #GstMemoryFlags
418 * @data: data to wrap
419 * @maxsize: allocated size of @data
420 * @offset: offset in @data
421 * @size: size of valid data
422 * @user_data: user_data
423 * @notify: called with @user_data when the memory is freed
425 * Allocate a new memory block that wraps the given @data.
427 * The prefix/padding must be filled with 0 if @flags contains
428 * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED respectively.
430 * Returns: a new #GstMemory.
433 gst_memory_new_wrapped (GstMemoryFlags flags, gpointer data,
434 gsize maxsize, gsize offset, gsize size, gpointer user_data,
435 GDestroyNotify notify)
437 GstMemoryDefault *mem;
439 g_return_val_if_fail (data != NULL, NULL);
440 g_return_val_if_fail (offset + size <= maxsize, NULL);
443 _default_mem_new (flags, NULL, data, maxsize, 0, offset, size, user_data,
446 return (GstMemory *) mem;
450 * gst_memory_get_sizes:
452 * @offset: pointer to offset
453 * @maxsize: pointer to maxsize
455 * Get the current @size, @offset and @maxsize of @mem.
457 * Returns: the current sizes of @mem
460 gst_memory_get_sizes (GstMemory * mem, gsize * offset, gsize * maxsize)
462 g_return_val_if_fail (mem != NULL, 0);
465 *offset = mem->offset;
467 *maxsize = mem->maxsize;
475 * @offset: a new offset
478 * Resize the memory region. @mem should be writable and offset + size should be
479 * less than the maxsize of @mem.
481 * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED will be
482 * cleared when offset or padding is increased respectively.
485 gst_memory_resize (GstMemory * mem, gssize offset, gsize size)
487 g_return_if_fail (mem != NULL);
488 g_return_if_fail (offset >= 0 || mem->offset >= -offset);
489 g_return_if_fail (size + mem->offset + offset <= mem->maxsize);
491 /* if we increase the prefix, we can't guarantee it is still 0 filled */
492 if ((offset > 0) && GST_MEMORY_IS_ZERO_PREFIXED (mem))
493 GST_MEMORY_FLAG_UNSET (mem, GST_MEMORY_FLAG_ZERO_PREFIXED);
495 /* if we increase the padding, we can't guarantee it is still 0 filled */
496 if ((offset + size < mem->size) && GST_MEMORY_IS_ZERO_PADDED (mem))
497 GST_MEMORY_FLAG_UNSET (mem, GST_MEMORY_FLAG_ZERO_PADDED);
499 mem->offset += offset;
504 * gst_memory_make_mapped:
505 * @mem: (transfer full): a #GstMemory
506 * @info: (out): pointer for info
507 * @flags: mapping flags
509 * Create a #GstMemory object that is mapped with @flags. If @mem is mappable
510 * with @flags, this function returns the mapped @mem directly. Otherwise a
511 * mapped copy of @mem is returned.
513 * This function takes ownership of old @mem and returns a reference to a new
516 * Returns: (transfer full): a #GstMemory object mapped with @flags or NULL when
517 * a mapping is not possible.
520 gst_memory_make_mapped (GstMemory * mem, GstMapInfo * info, GstMapFlags flags)
524 if (gst_memory_map (mem, info, flags)) {
527 result = gst_memory_copy (mem, 0, -1);
528 gst_memory_unref (mem);
533 if (!gst_memory_map (result, info, flags))
541 GST_CAT_DEBUG (GST_CAT_MEMORY, "cannot copy memory %p", mem);
546 GST_CAT_DEBUG (GST_CAT_MEMORY, "cannot map memory %p with flags %d", mem,
548 gst_memory_unref (result);
556 * @info: (out): pointer for info
557 * @flags: mapping flags
559 * Fill @info with the pointer and sizes of the memory in @mem that can be
560 * accessed according to @flags.
562 * This function can return %FALSE for various reasons:
563 * - the memory backed by @mem is not accessible with the given @flags.
564 * - the memory was already mapped with a different mapping.
566 * @info and its contents remain valid for as long as @mem is valid and
567 * until gst_memory_unmap() is called.
569 * For each gst_memory_map() call, a corresponding gst_memory_unmap() call
572 * Returns: %TRUE if the map operation was successful.
575 gst_memory_map (GstMemory * mem, GstMapInfo * info, GstMapFlags flags)
577 g_return_val_if_fail (mem != NULL, FALSE);
578 g_return_val_if_fail (info != NULL, FALSE);
580 if (!gst_memory_lock (mem, flags))
583 info->data = mem->allocator->info.mem_map (mem, mem->maxsize, flags);
585 if (G_UNLIKELY (info->data == NULL))
590 info->size = mem->size;
591 info->maxsize = mem->maxsize - mem->offset;
592 info->data = info->data + mem->offset;
599 GST_CAT_DEBUG (GST_CAT_MEMORY, "mem %p: lock %d failed", mem, flags);
604 /* something went wrong, restore the orginal state again */
605 GST_CAT_ERROR (GST_CAT_MEMORY, "mem %p: subclass map failed", mem);
606 gst_memory_unlock (mem, flags);
614 * @info: a #GstMapInfo
616 * Release the memory obtained with gst_memory_map()
619 gst_memory_unmap (GstMemory * mem, GstMapInfo * info)
621 g_return_if_fail (mem != NULL);
622 g_return_if_fail (info != NULL);
623 g_return_if_fail (info->memory == mem);
625 mem->allocator->info.mem_unmap (mem);
626 gst_memory_unlock (mem, info->flags);
632 * @offset: an offset to copy
633 * @size: size to copy or -1 to copy all bytes from offset
635 * Return a copy of @size bytes from @mem starting from @offset. This copy is
636 * guaranteed to be writable. @size can be set to -1 to return a copy all bytes
639 * Returns: a new #GstMemory.
642 gst_memory_copy (GstMemory * mem, gssize offset, gssize size)
646 g_return_val_if_fail (mem != NULL, NULL);
648 copy = mem->allocator->info.mem_copy (mem, offset, size);
656 * @offset: an offset to share
657 * @size: size to share or -1 to share bytes from offset
659 * Return a shared copy of @size bytes from @mem starting from @offset. No
660 * memory copy is performed and the memory region is simply shared. The result
661 * is guaranteed to be not-writable. @size can be set to -1 to return a share
662 * all bytes from @offset.
664 * Returns: a new #GstMemory.
667 gst_memory_share (GstMemory * mem, gssize offset, gssize size)
671 g_return_val_if_fail (mem != NULL, NULL);
672 g_return_val_if_fail (!GST_MEMORY_FLAG_IS_SET (mem, GST_MEMORY_FLAG_NO_SHARE),
675 shared = mem->allocator->info.mem_share (mem, offset, size);
681 * gst_memory_is_span:
682 * @mem1: a #GstMemory
683 * @mem2: a #GstMemory
684 * @offset: a pointer to a result offset
686 * Check if @mem1 and mem2 share the memory with a common parent memory object
687 * and that the memory is contiguous.
689 * If this is the case, the memory of @mem1 and @mem2 can be merged
690 * efficiently by performing gst_memory_share() on the parent object from
691 * the returned @offset.
693 * Returns: %TRUE if the memory is contiguous and of a common parent.
696 gst_memory_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
698 g_return_val_if_fail (mem1 != NULL, FALSE);
699 g_return_val_if_fail (mem2 != NULL, FALSE);
701 /* need to have the same allocators */
702 if (mem1->allocator != mem2->allocator)
705 /* need to have the same parent */
706 if (mem1->parent == NULL || mem1->parent != mem2->parent)
709 /* and memory is contiguous */
710 if (!mem1->allocator->info.mem_is_span (mem1, mem2, offset))
716 static GstAllocator *
717 _gst_allocator_copy (GstAllocator * allocator)
719 return gst_allocator_ref (allocator);
723 * gst_allocator_init:
724 * @allocator: a #GstAllocator
725 * @flags: #GstAllocatorFlags
726 * @info: a #GstMemoryInfo
727 * @free_func: a function to free @allocator
729 * Initialize a new memory allocator. The caller should have allocated the
730 * memory to hold at least sizeof (GstAllocator) bytes.
732 * All functions in @info are mandatory exept the copy and is_span
733 * functions, which will have a default implementation when left NULL.
735 * Returns: a new #GstAllocator.
738 gst_allocator_init (GstAllocator * allocator, GstAllocatorFlags flags,
739 const GstMemoryInfo * info, GstMiniObjectFreeFunction free_func)
741 g_return_if_fail (allocator != NULL);
742 g_return_if_fail (info != NULL);
743 g_return_if_fail (info->alloc != NULL);
744 g_return_if_fail (info->mem_map != NULL);
745 g_return_if_fail (info->mem_unmap != NULL);
746 g_return_if_fail (info->mem_free != NULL);
747 g_return_if_fail (info->mem_share != NULL);
749 gst_mini_object_init (GST_MINI_OBJECT_CAST (allocator), flags,
750 GST_TYPE_ALLOCATOR, (GstMiniObjectCopyFunction) _gst_allocator_copy, NULL,
751 (GstMiniObjectFreeFunction) free_func);
753 allocator->info = *info;
755 #define INSTALL_FALLBACK(_t) \
756 if (allocator->info._t == NULL) allocator->info._t = _fallback_ ##_t;
757 INSTALL_FALLBACK (mem_copy);
758 INSTALL_FALLBACK (mem_is_span);
759 #undef INSTALL_FALLBACK
761 GST_CAT_DEBUG (GST_CAT_MEMORY, "init allocator %p", allocator);
765 * gst_allocator_register:
766 * @name: the name of the allocator
767 * @allocator: (transfer full): #GstAllocator
769 * Registers the memory @allocator with @name. This function takes ownership of
773 gst_allocator_register (const gchar * name, GstAllocator * allocator)
775 g_return_if_fail (name != NULL);
776 g_return_if_fail (allocator != NULL);
778 GST_CAT_DEBUG (GST_CAT_MEMORY, "registering allocator %p with name \"%s\"",
781 g_rw_lock_writer_lock (&lock);
782 g_hash_table_insert (allocators, (gpointer) name, (gpointer) allocator);
783 g_rw_lock_writer_unlock (&lock);
787 * gst_allocator_find:
788 * @name: the name of the allocator
790 * Find a previously registered allocator with @name. When @name is NULL, the
791 * default allocator will be returned.
793 * Returns: (transfer full): a #GstAllocator or NULL when the allocator with @name was not
794 * registered. Use gst_allocator_unref() to release the allocator after usage.
797 gst_allocator_find (const gchar * name)
799 GstAllocator *allocator;
801 g_rw_lock_reader_lock (&lock);
803 allocator = g_hash_table_lookup (allocators, (gconstpointer) name);
805 allocator = _default_allocator;
808 gst_allocator_ref (allocator);
809 g_rw_lock_reader_unlock (&lock);
815 * gst_allocator_set_default:
816 * @allocator: (transfer full): a #GstAllocator
818 * Set the default allocator. This function takes ownership of @allocator.
821 gst_allocator_set_default (GstAllocator * allocator)
824 g_return_if_fail (allocator != NULL);
826 g_rw_lock_writer_lock (&lock);
827 old = _default_allocator;
828 _default_allocator = allocator;
829 g_rw_lock_writer_unlock (&lock);
832 gst_allocator_unref (old);
836 * gst_allocation_params_init:
837 * @params: a #GstAllocationParams
839 * Initialize @params to its default values
842 gst_allocation_params_init (GstAllocationParams * params)
844 g_return_if_fail (params != NULL);
846 memset (params, 0, sizeof (GstAllocationParams));
850 * gst_allocation_params_copy:
851 * @params: (transfer none): a #GstAllocationParams
853 * Create a copy of @params.
855 * Free-function: gst_allocation_params_free
857 * Returns: (transfer full): a new ##GstAllocationParams, free with
858 * gst_allocation_params_free().
860 GstAllocationParams *
861 gst_allocation_params_copy (const GstAllocationParams * params)
863 GstAllocationParams *result = NULL;
867 (GstAllocationParams *) g_slice_copy (sizeof (GstAllocationParams),
874 * gst_allocation_params_free:
875 * @params: (in) (transfer full): a #GstAllocationParams
880 gst_allocation_params_free (GstAllocationParams * params)
882 g_slice_free (GstAllocationParams, params);
886 * gst_allocator_alloc:
887 * @allocator: (transfer none) (allow-none): a #GstAllocator to use
888 * @size: size of the visible memory area
889 * @params: (transfer none) (allow-none): optional parameters
891 * Use @allocator to allocate a new memory block with memory that is at least
894 * The optional @params can specify the prefix and padding for the memory. If
895 * NULL is passed, no flags, no extra prefix/padding and a default alignment is
898 * The prefix/padding will be filled with 0 if flags contains
899 * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED respectively.
901 * When @allocator is NULL, the default allocator will be used.
903 * The alignment in @params is given as a bitmask so that @align + 1 equals
904 * the amount of bytes to align to. For example, to align to 8 bytes,
905 * use an alignment of 7.
907 * Returns: (transfer full): a new #GstMemory.
910 gst_allocator_alloc (GstAllocator * allocator, gsize size,
911 GstAllocationParams * params)
914 static GstAllocationParams defparams = { 0, 0, 0, 0, };
917 g_return_val_if_fail (((params->align + 1) & params->align) == 0, NULL);
922 if (allocator == NULL)
923 allocator = _default_allocator;
925 mem = allocator->info.alloc (allocator, size, params, NULL);