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);
77 * gst_memory_alignment:
79 * The default memory alignment in bytes - 1
80 * an alignment of 7 would be the same as what malloc() guarantees.
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;
101 GDestroyNotify notify;
104 /* default memory implementation */
113 /* the default allocator */
114 static GstAllocator *_default_allocator;
116 /* our predefined allocators */
117 static GstAllocator *_default_mem_impl;
119 /* initialize the fields */
121 _default_mem_init (GstMemoryDefault * mem, GstMemoryFlags flags,
122 GstMemory * parent, gsize slice_size, gpointer data,
123 GFreeFunc free_func, gsize maxsize, gsize offset, gsize size)
125 mem->mem.allocator = _default_mem_impl;
126 mem->mem.flags = flags;
127 mem->mem.refcount = 1;
128 mem->mem.parent = parent ? gst_memory_ref (parent) : NULL;
129 mem->mem.state = (flags & GST_MEMORY_FLAG_READONLY ? 0x5 : 0);
130 mem->mem.maxsize = maxsize;
131 mem->mem.offset = offset;
132 mem->mem.size = size;
133 mem->slice_size = slice_size;
135 mem->free_func = free_func;
137 GST_DEBUG ("new memory %p", mem);
140 /* create a new memory block that manages the given memory */
141 static GstMemoryDefault *
142 _default_mem_new (GstMemoryFlags flags, GstMemory * parent, gpointer data,
143 GFreeFunc free_func, gsize maxsize, gsize offset, gsize size)
145 GstMemoryDefault *mem;
148 slice_size = sizeof (GstMemoryDefault);
150 mem = g_slice_alloc (slice_size);
151 _default_mem_init (mem, flags, parent, slice_size,
152 data, free_func, maxsize, offset, size);
157 /* allocate the memory and structure in one block */
158 static GstMemoryDefault *
159 _default_mem_new_block (gsize maxsize, gsize align, gsize offset, gsize size)
161 GstMemoryDefault *mem;
162 gsize aoffset, slice_size;
165 /* ensure configured alignment */
166 align |= gst_memory_alignment;
167 /* allocate more to compensate for alignment */
169 /* alloc header and data in one block */
170 slice_size = sizeof (GstMemoryDefault) + maxsize;
172 mem = g_slice_alloc (slice_size);
176 data = (guint8 *) mem + sizeof (GstMemoryDefault);
178 if ((aoffset = ((guintptr) data & align)))
179 aoffset = (align + 1) - aoffset;
181 _default_mem_init (mem, 0, NULL, slice_size, data, NULL, maxsize,
182 aoffset + offset, size);
188 _default_alloc_alloc (GstAllocator * allocator, gsize maxsize, gsize align,
191 return (GstMemory *) _default_mem_new_block (maxsize, align, 0, maxsize);
195 _default_mem_map (GstMemoryDefault * mem, GstMapFlags flags)
201 _default_mem_unmap (GstMemoryDefault * mem)
207 _default_mem_free (GstMemoryDefault * mem)
209 GST_DEBUG ("free memory %p", mem);
212 gst_memory_unref (mem->mem.parent);
215 mem->free_func (mem->data);
217 g_slice_free1 (mem->slice_size, mem);
220 static GstMemoryDefault *
221 _default_mem_copy (GstMemoryDefault * mem, gssize offset, gsize size)
223 GstMemoryDefault *copy;
226 size = mem->mem.size > offset ? mem->mem.size - offset : 0;
229 _default_mem_new_block (mem->mem.maxsize, 0, mem->mem.offset + offset,
231 memcpy (copy->data, mem->data, mem->mem.maxsize);
236 static GstMemoryDefault *
237 _default_mem_share (GstMemoryDefault * mem, gssize offset, gsize size)
239 GstMemoryDefault *sub;
242 /* find the real parent */
243 if ((parent = mem->mem.parent) == NULL)
244 parent = (GstMemory *) mem;
247 size = mem->mem.size - offset;
250 _default_mem_new (parent->flags, parent, mem->data, NULL,
251 mem->mem.maxsize, mem->mem.offset + offset, size);
257 _default_mem_is_span (GstMemoryDefault * mem1, GstMemoryDefault * mem2,
262 GstMemoryDefault *parent;
264 parent = (GstMemoryDefault *) mem1->mem.parent;
266 *offset = mem1->mem.offset - parent->mem.offset;
269 /* and memory is contiguous */
270 return mem1->data + mem1->mem.offset + mem1->mem.size ==
271 mem2->data + mem2->mem.offset;
275 _fallback_mem_copy (GstMemory * mem, gssize offset, gssize size)
278 GstMapInfo sinfo, dinfo;
280 if (!gst_memory_map (mem, &sinfo, GST_MAP_READ))
284 size = sinfo.size > offset ? sinfo.size - offset : 0;
286 /* use the same allocator as the memory we copy */
287 copy = gst_allocator_alloc (mem->allocator, size, mem->align);
288 if (!gst_memory_map (copy, &dinfo, GST_MAP_WRITE)) {
289 GST_WARNING ("could not write map memory %p", copy);
290 gst_memory_unmap (mem, &sinfo);
294 memcpy (dinfo.data, sinfo.data + offset, size);
295 gst_memory_unmap (copy, &dinfo);
296 gst_memory_unmap (mem, &sinfo);
302 _fallback_mem_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
308 static GHashTable *allocators;
311 _priv_sysmem_notify (gpointer user_data)
313 g_warning ("The default memory allocator was freed!");
317 _priv_gst_memory_initialize (void)
319 static const GstMemoryInfo _mem_info = {
320 (GstAllocatorAllocFunction) _default_alloc_alloc,
321 (GstMemoryMapFunction) _default_mem_map,
322 (GstMemoryUnmapFunction) _default_mem_unmap,
323 (GstMemoryFreeFunction) _default_mem_free,
324 (GstMemoryCopyFunction) _default_mem_copy,
325 (GstMemoryShareFunction) _default_mem_share,
326 (GstMemoryIsSpanFunction) _default_mem_is_span,
329 g_rw_lock_init (&lock);
330 allocators = g_hash_table_new (g_str_hash, g_str_equal);
332 #ifdef HAVE_GETPAGESIZE
333 #ifdef MEMORY_ALIGNMENT_PAGESIZE
334 gst_memory_alignment = getpagesize () - 1;
338 GST_DEBUG ("memory alignment: %" G_GSIZE_FORMAT, gst_memory_alignment);
340 _default_mem_impl = gst_allocator_new (&_mem_info, NULL, _priv_sysmem_notify);
342 _default_allocator = gst_allocator_ref (_default_mem_impl);
343 gst_allocator_register (GST_ALLOCATOR_SYSMEM,
344 gst_allocator_ref (_default_mem_impl));
348 * gst_memory_new_wrapped:
349 * @flags: #GstMemoryFlags
350 * @data: data to wrap
351 * @free_func: function to free @data
352 * @maxsize: allocated size of @data
353 * @offset: offset in @data
354 * @size: size of valid data
356 * Allocate a new memory block that wraps the given @data.
358 * Returns: a new #GstMemory.
361 gst_memory_new_wrapped (GstMemoryFlags flags, gpointer data,
362 GFreeFunc free_func, gsize maxsize, gsize offset, gsize size)
364 GstMemoryDefault *mem;
366 g_return_val_if_fail (data != NULL, NULL);
367 g_return_val_if_fail (offset + size <= maxsize, NULL);
369 mem = _default_mem_new (flags, NULL, data, free_func, maxsize, offset, size);
371 return (GstMemory *) mem;
378 * Increases the refcount of @mem.
380 * Returns: @mem with increased refcount
383 gst_memory_ref (GstMemory * mem)
385 g_return_val_if_fail (mem != NULL, NULL);
387 GST_DEBUG ("memory %p, %d->%d", mem, mem->refcount, mem->refcount + 1);
389 g_atomic_int_inc (&mem->refcount);
398 * Decreases the refcount of @mem. When the refcount reaches 0, the free
399 * function of @mem will be called.
402 gst_memory_unref (GstMemory * mem)
404 g_return_if_fail (mem != NULL);
405 g_return_if_fail (mem->allocator != NULL);
407 GST_DEBUG ("memory %p, %d->%d", mem, mem->refcount, mem->refcount - 1);
409 if (g_atomic_int_dec_and_test (&mem->refcount))
410 mem->allocator->info.mem_free (mem);
414 * gst_memory_get_sizes:
416 * @offset: pointer to offset
417 * @maxsize: pointer to maxsize
419 * Get the current @size, @offset and @maxsize of @mem.
421 * Returns: the current sizes of @mem
424 gst_memory_get_sizes (GstMemory * mem, gsize * offset, gsize * maxsize)
426 g_return_val_if_fail (mem != NULL, 0);
429 *offset = mem->offset;
431 *maxsize = mem->maxsize;
439 * @offset: a new offset
442 * Resize the memory region. @mem should be writable and offset + size should be
443 * less than the maxsize of @mem.
446 gst_memory_resize (GstMemory * mem, gssize offset, gsize size)
448 g_return_if_fail (mem != NULL);
449 g_return_if_fail (gst_memory_is_writable (mem));
450 g_return_if_fail (offset >= 0 || mem->offset >= -offset);
451 g_return_if_fail (size + mem->offset + offset <= mem->maxsize);
453 mem->offset += offset;
458 * gst_memory_is_writable:
461 * Check if @mem is writable.
463 * Returns: %TRUE is @mem is writable.
466 gst_memory_is_writable (GstMemory * mem)
468 g_return_val_if_fail (mem != NULL, FALSE);
470 return (mem->refcount == 1) &&
471 ((mem->parent == NULL) || (mem->parent->refcount == 1)) &&
472 ((mem->flags & GST_MEMORY_FLAG_READONLY) == 0);
476 gst_memory_lock (GstMemory * mem, GstMapFlags flags)
478 gint access_mode, state, newstate;
480 access_mode = flags & 3;
483 state = g_atomic_int_get (&mem->state);
485 /* nothing mapped, set access_mode and refcount */
486 newstate = 4 | access_mode;
488 /* access_mode must match */
489 if ((state & access_mode) != access_mode)
491 /* increase refcount */
492 newstate = state + 4;
494 } while (!g_atomic_int_compare_and_exchange (&mem->state, state, newstate));
500 GST_DEBUG ("lock failed %p: state %d, access_mode %d", mem, state,
507 gst_memory_unlock (GstMemory * mem)
509 gint state, newstate;
512 state = g_atomic_int_get (&mem->state);
513 /* decrease the refcount */
514 newstate = state - 4;
515 /* last refcount, unset access_mode */
518 } while (!g_atomic_int_compare_and_exchange (&mem->state, state, newstate));
523 * gst_memory_make_mapped:
524 * @mem: (transfer full): a #GstMemory
525 * @info: (out): pointer for info
526 * @flags: mapping flags
528 * Create a #GstMemory object that is mapped with @flags. If @mem is mappable
529 * with @flags, this function returns the mapped @mem directly. Otherwise a
530 * mapped copy of @mem is returned.
532 * This function takes ownership of old @mem and returns a reference to a new
535 * Returns: (transfer full): a #GstMemory object mapped with @flags or NULL when
536 * a mapping is not possible.
539 gst_memory_make_mapped (GstMemory * mem, GstMapInfo * info, GstMapFlags flags)
543 if (gst_memory_map (mem, info, flags)) {
546 result = gst_memory_copy (mem, 0, -1);
547 gst_memory_unref (mem);
552 if (!gst_memory_map (result, info, flags))
560 GST_DEBUG ("cannot copy memory %p", mem);
565 GST_DEBUG ("cannot map memory %p with flags %d", mem, flags);
566 gst_memory_unref (result);
574 * @info: (out): pointer for info
575 * @flags: mapping flags
577 * Fill @info with the pointer and sizes of the memory in @mem that can be
578 * accessed according to @flags.
580 * This function can return %FALSE for various reasons:
581 * - the memory backed by @mem is not accessible with the given @flags.
582 * - the memory was already mapped with a different mapping.
584 * @info and its contents remains valid for as long as @mem is alive and until
585 * gst_memory_unmap() is called.
587 * For each gst_memory_map() call, a corresponding gst_memory_unmap() call
590 * Returns: %TRUE if the map operation was successful.
593 gst_memory_map (GstMemory * mem, GstMapInfo * info, GstMapFlags flags)
595 g_return_val_if_fail (mem != NULL, FALSE);
596 g_return_val_if_fail (info != NULL, FALSE);
598 if (!gst_memory_lock (mem, flags))
601 info->data = mem->allocator->info.mem_map (mem, mem->maxsize, flags);
603 if (G_UNLIKELY (info->data == NULL))
608 info->size = mem->size;
609 info->maxsize = mem->maxsize - mem->offset;
610 info->data = info->data + mem->offset;
617 GST_DEBUG ("mem %p: lock %d failed", mem, flags);
622 /* something went wrong, restore the orginal state again */
623 GST_ERROR ("mem %p: map failed", mem);
624 gst_memory_unlock (mem);
632 * @info: a #GstMapInfo
634 * Release the memory obtained with gst_memory_map()
637 gst_memory_unmap (GstMemory * mem, GstMapInfo * info)
639 g_return_if_fail (mem != NULL);
640 g_return_if_fail (info != NULL);
641 g_return_if_fail (info->memory == mem);
642 /* there must be a ref */
643 g_return_if_fail (g_atomic_int_get (&mem->state) >= 4);
645 mem->allocator->info.mem_unmap (mem);
646 gst_memory_unlock (mem);
652 * @offset: an offset to copy
653 * @size: size to copy or -1 to copy all bytes from offset
655 * Return a copy of @size bytes from @mem starting from @offset. This copy is
656 * guaranteed to be writable. @size can be set to -1 to return a copy all bytes
659 * Returns: a new #GstMemory.
662 gst_memory_copy (GstMemory * mem, gssize offset, gssize size)
666 g_return_val_if_fail (mem != NULL, NULL);
667 g_return_val_if_fail (gst_memory_lock (mem, GST_MAP_READ), NULL);
669 copy = mem->allocator->info.mem_copy (mem, offset, size);
671 gst_memory_unlock (mem);
679 * @offset: an offset to share
680 * @size: size to share or -1 to share bytes from offset
682 * Return a shared copy of @size bytes from @mem starting from @offset. No
683 * memory copy is performed and the memory region is simply shared. The result
684 * is guaranteed to be not-writable. @size can be set to -1 to return a share
685 * all bytes from @offset.
687 * Returns: a new #GstMemory.
690 gst_memory_share (GstMemory * mem, gssize offset, gssize size)
692 g_return_val_if_fail (mem != NULL, NULL);
694 return mem->allocator->info.mem_share (mem, offset, size);
698 * gst_memory_is_span:
699 * @mem1: a #GstMemory
700 * @mem2: a #GstMemory
701 * @offset: a pointer to a result offset
703 * Check if @mem1 and mem2 share the memory with a common parent memory object
704 * and that the memory is contiguous.
706 * If this is the case, the memory of @mem1 and @mem2 can be merged
707 * efficiently by performing gst_memory_share() on the parent object from
708 * the returned @offset.
710 * Returns: %TRUE if the memory is contiguous and of a common parent.
713 gst_memory_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
715 g_return_val_if_fail (mem1 != NULL, FALSE);
716 g_return_val_if_fail (mem2 != NULL, FALSE);
718 /* need to have the same allocators */
719 if (mem1->allocator != mem2->allocator)
722 /* need to have the same parent */
723 if (mem1->parent == NULL || mem1->parent != mem2->parent)
726 /* and memory is contiguous */
727 if (!mem1->allocator->info.mem_is_span (mem1, mem2, offset))
734 * gst_allocator_register:
735 * @info: a #GstMemoryInfo
736 * @user_data: user data
737 * @notify: a #GDestroyNotify for @user_data
739 * Create a new memory allocator with @info and @user_data.
741 * All functions in @info are mandatory exept the copy and is_span
742 * functions, which will have a default implementation when left NULL.
744 * The @user_data will be passed to all calls of the alloc function and the
747 * Returns: a new #GstAllocator.
750 gst_allocator_new (const GstMemoryInfo * info, gpointer user_data,
751 GDestroyNotify notify)
753 GstAllocator *allocator;
755 #define INSTALL_FALLBACK(_t) \
756 if (allocator->info._t == NULL) allocator->info._t = _fallback_ ##_t;
758 g_return_val_if_fail (info != NULL, NULL);
759 g_return_val_if_fail (info->alloc != NULL, NULL);
760 g_return_val_if_fail (info->mem_map != NULL, NULL);
761 g_return_val_if_fail (info->mem_unmap != NULL, NULL);
762 g_return_val_if_fail (info->mem_free != NULL, NULL);
763 g_return_val_if_fail (info->mem_share != NULL, NULL);
765 allocator = g_slice_new (GstAllocator);
766 allocator->refcount = 1;
767 allocator->info = *info;
768 allocator->user_data = user_data;
769 allocator->notify = notify;
770 INSTALL_FALLBACK (mem_copy);
771 INSTALL_FALLBACK (mem_is_span);
772 #undef INSTALL_FALLBACK
774 GST_DEBUG ("new allocator %p", allocator);
781 * @allocator: a #GstAllocator
783 * Increases the refcount of @allocator.
785 * Returns: @allocator with increased refcount
788 gst_allocator_ref (GstAllocator * allocator)
790 g_return_val_if_fail (allocator != NULL, NULL);
792 GST_DEBUG ("alocator %p, %d->%d", allocator, allocator->refcount,
793 allocator->refcount + 1);
795 g_atomic_int_inc (&allocator->refcount);
801 * gst_allocator_unref:
802 * @allocator: a #GstAllocator
804 * Decreases the refcount of @allocator. When the refcount reaches 0, the free
805 * function of @allocator will be called.
808 gst_allocator_unref (GstAllocator * allocator)
810 g_return_if_fail (allocator != NULL);
812 GST_DEBUG ("allocator %p, %d->%d", allocator, allocator->refcount,
813 allocator->refcount - 1);
815 if (g_atomic_int_dec_and_test (&allocator->refcount)) {
816 if (allocator->notify)
817 allocator->notify (allocator->user_data);
818 g_slice_free1 (sizeof (GstAllocator), allocator);
823 * gst_allocator_register:
824 * @name: the name of the allocator
825 * @allocator: (transfer full): #GstAllocator
827 * Registers the memory @allocator with @name. This function takes ownership of
831 gst_allocator_register (const gchar * name, GstAllocator * allocator)
833 g_return_if_fail (name != NULL);
834 g_return_if_fail (allocator != NULL);
836 GST_DEBUG ("registering allocator %p with name \"%s\"", allocator, name);
838 g_rw_lock_writer_lock (&lock);
839 g_hash_table_insert (allocators, (gpointer) name, (gpointer) allocator);
840 g_rw_lock_writer_unlock (&lock);
844 * gst_allocator_find:
845 * @name: the name of the allocator
847 * Find a previously registered allocator with @name. When @name is NULL, the
848 * default allocator will be returned.
850 * Returns: (transfer full): a #GstAllocator or NULL when the allocator with @name was not
851 * registered. Use gst_allocator_unref() to release the allocator after usage.
854 gst_allocator_find (const gchar * name)
856 GstAllocator *allocator;
858 g_rw_lock_reader_lock (&lock);
860 allocator = g_hash_table_lookup (allocators, (gconstpointer) name);
862 allocator = _default_allocator;
865 gst_allocator_ref (allocator);
866 g_rw_lock_reader_unlock (&lock);
872 * gst_allocator_set_default:
873 * @allocator: (transfer full): a #GstAllocator
875 * Set the default allocator. This function takes ownership of @allocator.
878 gst_allocator_set_default (GstAllocator * allocator)
881 g_return_if_fail (allocator != NULL);
883 g_rw_lock_writer_lock (&lock);
884 old = _default_allocator;
885 _default_allocator = allocator;
886 g_rw_lock_writer_unlock (&lock);
889 gst_allocator_unref (old);
893 * gst_allocator_alloc:
894 * @allocator: (transfer none) (allow-none): a #GstAllocator to use
895 * @maxsize: allocated size of @data
896 * @align: alignment for the data
898 * Use @allocator to allocate a new memory block with memory that is at least
899 * @maxsize big and has the given alignment.
901 * When @allocator is NULL, the default allocator will be used.
903 * @align is given as a bitmask so that @align + 1 equals the amount of bytes to
904 * align to. For example, to align to 8 bytes, use an alignment of 7.
906 * Returns: (transfer full): a new #GstMemory.
909 gst_allocator_alloc (GstAllocator * allocator, gsize maxsize, gsize align)
911 g_return_val_if_fail (((align + 1) & align) == 0, NULL);
913 if (allocator == NULL)
914 allocator = _default_allocator;
916 return allocator->info.alloc (allocator, maxsize, align,
917 allocator->user_data);