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 returnn 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 G_DEFINE_BOXED_TYPE (GstMemory, gst_memory, (GBoxedCopyFunc) gst_memory_ref,
74 (GBoxedFreeFunc) gst_memory_unref);
76 G_DEFINE_BOXED_TYPE (GstAllocator, gst_allocator,
77 (GBoxedCopyFunc) gst_allocator_ref, (GBoxedFreeFunc) gst_allocator_unref);
80 * gst_memory_alignment:
82 * The default memory alignment in bytes - 1
83 * an alignment of 7 would be the same as what malloc() guarantees.
85 #if defined(MEMORY_ALIGNMENT_MALLOC)
86 size_t gst_memory_alignment = 7;
87 #elif defined(MEMORY_ALIGNMENT_PAGESIZE)
88 /* we fill this in in the _init method */
89 size_t gst_memory_alignment = 0;
90 #elif defined(MEMORY_ALIGNMENT)
91 size_t gst_memory_alignment = MEMORY_ALIGNMENT - 1;
93 #error "No memory alignment configured"
94 size_t gst_memory_alignment = 0;
104 GDestroyNotify notify;
107 /* default memory implementation */
116 /* the default allocator */
117 static GstAllocator *_default_allocator;
119 /* our predefined allocators */
120 static GstAllocator *_default_mem_impl;
122 /* initialize the fields */
124 _default_mem_init (GstMemoryDefault * mem, GstMemoryFlags flags,
125 GstMemory * parent, gsize slice_size, gpointer data,
126 GFreeFunc free_func, gsize maxsize, gsize offset, gsize size)
128 mem->mem.allocator = _default_mem_impl;
129 mem->mem.flags = flags;
130 mem->mem.refcount = 1;
131 mem->mem.parent = parent ? gst_memory_ref (parent) : NULL;
132 mem->mem.state = (flags & GST_MEMORY_FLAG_READONLY ? 0x5 : 0);
133 mem->mem.maxsize = maxsize;
134 mem->mem.offset = offset;
135 mem->mem.size = size;
136 mem->slice_size = slice_size;
138 mem->free_func = free_func;
140 GST_DEBUG ("new memory %p", mem);
143 /* create a new memory block that manages the given memory */
144 static GstMemoryDefault *
145 _default_mem_new (GstMemoryFlags flags, GstMemory * parent, gpointer data,
146 GFreeFunc free_func, gsize maxsize, gsize offset, gsize size)
148 GstMemoryDefault *mem;
151 slice_size = sizeof (GstMemoryDefault);
153 mem = g_slice_alloc (slice_size);
154 _default_mem_init (mem, flags, parent, slice_size,
155 data, free_func, maxsize, offset, size);
160 /* allocate the memory and structure in one block */
161 static GstMemoryDefault *
162 _default_mem_new_block (gsize maxsize, gsize align, gsize offset, gsize size)
164 GstMemoryDefault *mem;
165 gsize aoffset, slice_size;
168 /* ensure configured alignment */
169 align |= gst_memory_alignment;
170 /* allocate more to compensate for alignment */
172 /* alloc header and data in one block */
173 slice_size = sizeof (GstMemoryDefault) + maxsize;
175 mem = g_slice_alloc (slice_size);
179 data = (guint8 *) mem + sizeof (GstMemoryDefault);
181 if ((aoffset = ((guintptr) data & align)))
182 aoffset = (align + 1) - aoffset;
184 _default_mem_init (mem, 0, NULL, slice_size, data, NULL, maxsize,
185 aoffset + offset, size);
191 _default_alloc_alloc (GstAllocator * allocator, gsize maxsize, gsize align,
194 return (GstMemory *) _default_mem_new_block (maxsize, align, 0, maxsize);
198 _default_mem_map (GstMemoryDefault * mem, GstMapFlags flags)
204 _default_mem_unmap (GstMemoryDefault * mem)
210 _default_mem_free (GstMemoryDefault * mem)
212 GST_DEBUG ("free memory %p", mem);
215 gst_memory_unref (mem->mem.parent);
218 mem->free_func (mem->data);
220 g_slice_free1 (mem->slice_size, mem);
223 static GstMemoryDefault *
224 _default_mem_copy (GstMemoryDefault * mem, gssize offset, gsize size)
226 GstMemoryDefault *copy;
229 size = mem->mem.size > offset ? mem->mem.size - offset : 0;
232 _default_mem_new_block (mem->mem.maxsize, 0, mem->mem.offset + offset,
234 memcpy (copy->data, mem->data, mem->mem.maxsize);
239 static GstMemoryDefault *
240 _default_mem_share (GstMemoryDefault * mem, gssize offset, gsize size)
242 GstMemoryDefault *sub;
245 /* find the real parent */
246 if ((parent = mem->mem.parent) == NULL)
247 parent = (GstMemory *) mem;
250 size = mem->mem.size - offset;
253 _default_mem_new (parent->flags, parent, mem->data, NULL,
254 mem->mem.maxsize, mem->mem.offset + offset, size);
260 _default_mem_is_span (GstMemoryDefault * mem1, GstMemoryDefault * mem2,
265 GstMemoryDefault *parent;
267 parent = (GstMemoryDefault *) mem1->mem.parent;
269 *offset = mem1->mem.offset - parent->mem.offset;
272 /* and memory is contiguous */
273 return mem1->data + mem1->mem.offset + mem1->mem.size ==
274 mem2->data + mem2->mem.offset;
278 _fallback_mem_copy (GstMemory * mem, gssize offset, gssize size)
281 GstMapInfo sinfo, dinfo;
283 if (!gst_memory_map (mem, &sinfo, GST_MAP_READ))
287 size = sinfo.size > offset ? sinfo.size - offset : 0;
289 /* use the same allocator as the memory we copy */
290 copy = gst_allocator_alloc (mem->allocator, size, mem->align);
291 if (!gst_memory_map (copy, &dinfo, GST_MAP_WRITE)) {
292 GST_WARNING ("could not write map memory %p", copy);
293 gst_memory_unmap (mem, &sinfo);
297 memcpy (dinfo.data, sinfo.data + offset, size);
298 gst_memory_unmap (copy, &dinfo);
299 gst_memory_unmap (mem, &sinfo);
305 _fallback_mem_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
311 static GHashTable *allocators;
314 _priv_sysmem_notify (gpointer user_data)
316 g_warning ("The default memory allocator was freed!");
320 _priv_gst_memory_initialize (void)
322 static const GstMemoryInfo _mem_info = {
323 GST_ALLOCATOR_SYSMEM,
324 (GstAllocatorAllocFunction) _default_alloc_alloc,
325 (GstMemoryMapFunction) _default_mem_map,
326 (GstMemoryUnmapFunction) _default_mem_unmap,
327 (GstMemoryFreeFunction) _default_mem_free,
328 (GstMemoryCopyFunction) _default_mem_copy,
329 (GstMemoryShareFunction) _default_mem_share,
330 (GstMemoryIsSpanFunction) _default_mem_is_span,
333 g_rw_lock_init (&lock);
334 allocators = g_hash_table_new (g_str_hash, g_str_equal);
336 #ifdef HAVE_GETPAGESIZE
337 #ifdef MEMORY_ALIGNMENT_PAGESIZE
338 gst_memory_alignment = getpagesize () - 1;
342 GST_DEBUG ("memory alignment: %" G_GSIZE_FORMAT, gst_memory_alignment);
344 _default_mem_impl = gst_allocator_new (&_mem_info, NULL, _priv_sysmem_notify);
346 _default_allocator = gst_allocator_ref (_default_mem_impl);
347 gst_allocator_register (GST_ALLOCATOR_SYSMEM,
348 gst_allocator_ref (_default_mem_impl));
352 * gst_memory_new_wrapped:
353 * @flags: #GstMemoryFlags
354 * @data: data to wrap
355 * @free_func: function to free @data
356 * @maxsize: allocated size of @data
357 * @offset: offset in @data
358 * @size: size of valid data
360 * Allocate a new memory block that wraps the given @data.
362 * Returns: a new #GstMemory.
365 gst_memory_new_wrapped (GstMemoryFlags flags, gpointer data,
366 GFreeFunc free_func, gsize maxsize, gsize offset, gsize size)
368 GstMemoryDefault *mem;
370 g_return_val_if_fail (data != NULL, NULL);
371 g_return_val_if_fail (offset + size <= maxsize, NULL);
373 mem = _default_mem_new (flags, NULL, data, free_func, maxsize, offset, size);
375 return (GstMemory *) mem;
382 * Increases the refcount of @mem.
384 * Returns: @mem with increased refcount
387 gst_memory_ref (GstMemory * mem)
389 g_return_val_if_fail (mem != NULL, NULL);
391 GST_DEBUG ("memory %p, %d->%d", mem, mem->refcount, mem->refcount + 1);
393 g_atomic_int_inc (&mem->refcount);
402 * Decreases the refcount of @mem. When the refcount reaches 0, the free
403 * function of @mem will be called.
406 gst_memory_unref (GstMemory * mem)
408 g_return_if_fail (mem != NULL);
409 g_return_if_fail (mem->allocator != NULL);
411 GST_DEBUG ("memory %p, %d->%d", mem, mem->refcount, mem->refcount - 1);
413 if (g_atomic_int_dec_and_test (&mem->refcount))
414 mem->allocator->info.mem_free (mem);
418 * gst_memory_get_sizes:
420 * @offset: pointer to offset
421 * @maxsize: pointer to maxsize
423 * Get the current @size, @offset and @maxsize of @mem.
425 * Returns: the current sizes of @mem
428 gst_memory_get_sizes (GstMemory * mem, gsize * offset, gsize * maxsize)
430 g_return_val_if_fail (mem != NULL, 0);
433 *offset = mem->offset;
435 *maxsize = mem->maxsize;
443 * @offset: a new offset
446 * Resize the memory region. @mem should be writable and offset + size should be
447 * less than the maxsize of @mem.
450 gst_memory_resize (GstMemory * mem, gssize offset, gsize size)
452 g_return_if_fail (mem != NULL);
453 g_return_if_fail (gst_memory_is_writable (mem));
454 g_return_if_fail (offset >= 0 || mem->offset >= -offset);
455 g_return_if_fail (size + mem->offset + offset <= mem->maxsize);
457 mem->offset += offset;
462 * gst_memory_is_writable:
465 * Check if @mem is writable.
467 * Returns: %TRUE is @mem is writable.
470 gst_memory_is_writable (GstMemory * mem)
472 g_return_val_if_fail (mem != NULL, FALSE);
474 return (mem->refcount == 1) &&
475 ((mem->parent == NULL) || (mem->parent->refcount == 1)) &&
476 ((mem->flags & GST_MEMORY_FLAG_READONLY) == 0);
480 gst_memory_lock (GstMemory * mem, GstMapFlags flags)
482 gint access_mode, state, newstate;
484 access_mode = flags & 3;
487 state = g_atomic_int_get (&mem->state);
489 /* nothing mapped, set access_mode and refcount */
490 newstate = 4 | access_mode;
492 /* access_mode must match */
493 if ((state & access_mode) != access_mode)
495 /* increase refcount */
496 newstate = state + 4;
498 } while (!g_atomic_int_compare_and_exchange (&mem->state, state, newstate));
504 GST_DEBUG ("lock failed %p: state %d, access_mode %d", mem, state,
511 gst_memory_unlock (GstMemory * mem)
513 gint state, newstate;
516 state = g_atomic_int_get (&mem->state);
517 /* decrease the refcount */
518 newstate = state - 4;
519 /* last refcount, unset access_mode */
522 } while (!g_atomic_int_compare_and_exchange (&mem->state, state, newstate));
527 * gst_memory_make_mapped:
528 * @mem: (transfer full): a #GstMemory
529 * @info: (out): pointer for info
530 * @flags: mapping flags
532 * Create a #GstMemory object that is mapped with @flags. If @mem is mappable
533 * with @flags, this function returns the mapped @mem directly. Otherwise a
534 * mapped copy of @mem is returned.
536 * This function takes ownership of old @mem and returns a reference to a new
539 * Returns: (transfer full): a #GstMemory object mapped with @flags or NULL when
540 * a mapping is not possible.
543 gst_memory_make_mapped (GstMemory * mem, GstMapInfo * info, GstMapFlags flags)
547 if (gst_memory_map (mem, info, flags)) {
550 result = gst_memory_copy (mem, 0, -1);
551 gst_memory_unref (mem);
556 if (!gst_memory_map (result, info, flags))
564 GST_DEBUG ("cannot copy memory %p", mem);
569 GST_DEBUG ("cannot map memory %p with flags %d", mem, flags);
570 gst_memory_unref (result);
578 * @info: (out): pointer for info
579 * @flags: mapping flags
581 * Fill @info with the pointer and sizes of the memory in @mem that can be
582 * accessed according to @flags.
584 * This function can return %FALSE for various reasons:
585 * - the memory backed by @mem is not accessible with the given @flags.
586 * - the memory was already mapped with a different mapping.
588 * @info and its contents remains valid for as long as @mem is alive and until
589 * gst_memory_unmap() is called.
591 * For each gst_memory_map() call, a corresponding gst_memory_unmap() call
594 * Returns: %TRUE if the map operation was successful.
597 gst_memory_map (GstMemory * mem, GstMapInfo * info, GstMapFlags flags)
599 g_return_val_if_fail (mem != NULL, FALSE);
600 g_return_val_if_fail (info != NULL, FALSE);
602 if (!gst_memory_lock (mem, flags))
605 info->data = mem->allocator->info.mem_map (mem, mem->maxsize, flags);
607 if (G_UNLIKELY (info->data == NULL))
612 info->size = mem->size;
613 info->maxsize = mem->maxsize - mem->offset;
614 info->data = info->data + mem->offset;
621 GST_DEBUG ("mem %p: lock %d failed", mem, flags);
626 /* something went wrong, restore the orginal state again */
627 GST_ERROR ("mem %p: map failed", mem);
628 gst_memory_unlock (mem);
636 * @info: a #GstMapInfo
638 * Release the memory obtained with gst_memory_map()
641 gst_memory_unmap (GstMemory * mem, GstMapInfo * info)
643 g_return_if_fail (mem != NULL);
644 g_return_if_fail (info != NULL);
645 g_return_if_fail (info->memory == mem);
646 /* there must be a ref */
647 g_return_if_fail (g_atomic_int_get (&mem->state) >= 4);
649 mem->allocator->info.mem_unmap (mem);
650 gst_memory_unlock (mem);
656 * @offset: an offset to copy
657 * @size: size to copy or -1 to copy all bytes from offset
659 * Return a copy of @size bytes from @mem starting from @offset. This copy is
660 * guaranteed to be writable. @size can be set to -1 to return a copy all bytes
663 * Returns: a new #GstMemory.
666 gst_memory_copy (GstMemory * mem, gssize offset, gssize size)
670 g_return_val_if_fail (mem != NULL, NULL);
671 g_return_val_if_fail (gst_memory_lock (mem, GST_MAP_READ), NULL);
673 copy = mem->allocator->info.mem_copy (mem, offset, size);
675 gst_memory_unlock (mem);
683 * @offset: an offset to share
684 * @size: size to share or -1 to share bytes from offset
686 * Return a shared copy of @size bytes from @mem starting from @offset. No
687 * memory copy is performed and the memory region is simply shared. The result
688 * is guaranteed to be not-writable. @size can be set to -1 to return a share
689 * all bytes from @offset.
691 * Returns: a new #GstMemory.
694 gst_memory_share (GstMemory * mem, gssize offset, gssize size)
696 g_return_val_if_fail (mem != NULL, NULL);
698 return mem->allocator->info.mem_share (mem, offset, size);
702 * gst_memory_is_span:
703 * @mem1: a #GstMemory
704 * @mem2: a #GstMemory
705 * @offset: a pointer to a result offset
707 * Check if @mem1 and mem2 share the memory with a common parent memory object
708 * and that the memory is contiguous.
710 * If this is the case, the memory of @mem1 and @mem2 can be merged
711 * efficiently by performing gst_memory_share() on the parent object from
712 * the returned @offset.
714 * Returns: %TRUE if the memory is contiguous and of a common parent.
717 gst_memory_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
719 g_return_val_if_fail (mem1 != NULL, FALSE);
720 g_return_val_if_fail (mem2 != NULL, FALSE);
722 /* need to have the same allocators */
723 if (mem1->allocator != mem2->allocator)
726 /* need to have the same parent */
727 if (mem1->parent == NULL || mem1->parent != mem2->parent)
730 /* and memory is contiguous */
731 if (!mem1->allocator->info.mem_is_span (mem1, mem2, offset))
738 * gst_allocator_register:
739 * @info: a #GstMemoryInfo
740 * @user_data: user data
741 * @notify: a #GDestroyNotify for @user_data
743 * Create a new memory allocator with @info and @user_data.
745 * All functions in @info are mandatory exept the copy and is_span
746 * functions, which will have a default implementation when left NULL.
748 * The @user_data will be passed to all calls of the alloc function and the
751 * Returns: a new #GstAllocator.
754 gst_allocator_new (const GstMemoryInfo * info, gpointer user_data,
755 GDestroyNotify notify)
757 GstAllocator *allocator;
759 #define INSTALL_FALLBACK(_t) \
760 if (allocator->info._t == NULL) allocator->info._t = _fallback_ ##_t;
762 g_return_val_if_fail (info != NULL, NULL);
763 g_return_val_if_fail (info->alloc != NULL, NULL);
764 g_return_val_if_fail (info->mem_map != NULL, NULL);
765 g_return_val_if_fail (info->mem_unmap != NULL, NULL);
766 g_return_val_if_fail (info->mem_free != NULL, NULL);
767 g_return_val_if_fail (info->mem_share != NULL, NULL);
769 allocator = g_slice_new (GstAllocator);
770 allocator->refcount = 1;
771 allocator->info = *info;
772 allocator->user_data = user_data;
773 allocator->notify = notify;
774 INSTALL_FALLBACK (mem_copy);
775 INSTALL_FALLBACK (mem_is_span);
776 #undef INSTALL_FALLBACK
778 GST_DEBUG ("new allocator %p", allocator);
784 * gst_alocator_get_memory_type:
785 * @allocator: a #GstAllocator
787 * Get the memory type allocated by this allocator
789 * Returns: @allocator with increased refcount
792 gst_allocator_get_memory_type (GstAllocator * allocator)
794 g_return_val_if_fail (allocator != NULL, NULL);
796 return allocator->info.mem_type;
801 * @allocator: a #GstAllocator
803 * Increases the refcount of @allocator.
805 * Returns: @allocator with increased refcount
808 gst_allocator_ref (GstAllocator * allocator)
810 g_return_val_if_fail (allocator != NULL, NULL);
812 GST_DEBUG ("alocator %p, %d->%d", allocator, allocator->refcount,
813 allocator->refcount + 1);
815 g_atomic_int_inc (&allocator->refcount);
821 * gst_allocator_unref:
822 * @allocator: a #GstAllocator
824 * Decreases the refcount of @allocator. When the refcount reaches 0, the free
825 * function of @allocator will be called.
828 gst_allocator_unref (GstAllocator * allocator)
830 g_return_if_fail (allocator != NULL);
832 GST_DEBUG ("allocator %p, %d->%d", allocator, allocator->refcount,
833 allocator->refcount - 1);
835 if (g_atomic_int_dec_and_test (&allocator->refcount)) {
836 if (allocator->notify)
837 allocator->notify (allocator->user_data);
838 g_slice_free1 (sizeof (GstAllocator), allocator);
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_DEBUG ("registering allocator %p with name \"%s\"", allocator, name);
858 g_rw_lock_writer_lock (&lock);
859 g_hash_table_insert (allocators, (gpointer) name, (gpointer) allocator);
860 g_rw_lock_writer_unlock (&lock);
864 * gst_allocator_find:
865 * @name: the name of the allocator
867 * Find a previously registered allocator with @name. When @name is NULL, the
868 * default allocator will be returned.
870 * Returns: (transfer full): a #GstAllocator or NULL when the allocator with @name was not
871 * registered. Use gst_allocator_unref() to release the allocator after usage.
874 gst_allocator_find (const gchar * name)
876 GstAllocator *allocator;
878 g_rw_lock_reader_lock (&lock);
880 allocator = g_hash_table_lookup (allocators, (gconstpointer) name);
882 allocator = _default_allocator;
885 gst_allocator_ref (allocator);
886 g_rw_lock_reader_unlock (&lock);
892 * gst_allocator_set_default:
893 * @allocator: (transfer full): a #GstAllocator
895 * Set the default allocator. This function takes ownership of @allocator.
898 gst_allocator_set_default (GstAllocator * allocator)
901 g_return_if_fail (allocator != NULL);
903 g_rw_lock_writer_lock (&lock);
904 old = _default_allocator;
905 _default_allocator = allocator;
906 g_rw_lock_writer_unlock (&lock);
909 gst_allocator_unref (old);
913 * gst_allocator_alloc:
914 * @allocator: (transfer none) (allow-none): a #GstAllocator to use
915 * @maxsize: allocated size of @data
916 * @align: alignment for the data
918 * Use @allocator to allocate a new memory block with memory that is at least
919 * @maxsize big and has the given alignment.
921 * When @allocator is NULL, the default allocator will be used.
923 * @align is given as a bitmask so that @align + 1 equals the amount of bytes to
924 * align to. For example, to align to 8 bytes, use an alignment of 7.
926 * Returns: (transfer full): a new #GstMemory.
929 gst_allocator_alloc (GstAllocator * allocator, gsize maxsize, gsize align)
931 g_return_val_if_fail (((align + 1) & align) == 0, NULL);
933 if (allocator == NULL)
934 allocator = _default_allocator;
936 return allocator->info.alloc (allocator, maxsize, align,
937 allocator->user_data);