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 (GstAllocatorAllocFunction) _default_alloc_alloc,
324 (GstMemoryMapFunction) _default_mem_map,
325 (GstMemoryUnmapFunction) _default_mem_unmap,
326 (GstMemoryFreeFunction) _default_mem_free,
327 (GstMemoryCopyFunction) _default_mem_copy,
328 (GstMemoryShareFunction) _default_mem_share,
329 (GstMemoryIsSpanFunction) _default_mem_is_span,
332 g_rw_lock_init (&lock);
333 allocators = g_hash_table_new (g_str_hash, g_str_equal);
335 #ifdef HAVE_GETPAGESIZE
336 #ifdef MEMORY_ALIGNMENT_PAGESIZE
337 gst_memory_alignment = getpagesize () - 1;
341 GST_DEBUG ("memory alignment: %" G_GSIZE_FORMAT, gst_memory_alignment);
343 _default_mem_impl = gst_allocator_new (&_mem_info, NULL, _priv_sysmem_notify);
345 _default_allocator = gst_allocator_ref (_default_mem_impl);
346 gst_allocator_register (GST_ALLOCATOR_SYSMEM,
347 gst_allocator_ref (_default_mem_impl));
351 * gst_memory_new_wrapped:
352 * @flags: #GstMemoryFlags
353 * @data: data to wrap
354 * @free_func: function to free @data
355 * @maxsize: allocated size of @data
356 * @offset: offset in @data
357 * @size: size of valid data
359 * Allocate a new memory block that wraps the given @data.
361 * Returns: a new #GstMemory.
364 gst_memory_new_wrapped (GstMemoryFlags flags, gpointer data,
365 GFreeFunc free_func, gsize maxsize, gsize offset, gsize size)
367 GstMemoryDefault *mem;
369 g_return_val_if_fail (data != NULL, NULL);
370 g_return_val_if_fail (offset + size <= maxsize, NULL);
372 mem = _default_mem_new (flags, NULL, data, free_func, maxsize, offset, size);
374 return (GstMemory *) mem;
381 * Increases the refcount of @mem.
383 * Returns: @mem with increased refcount
386 gst_memory_ref (GstMemory * mem)
388 g_return_val_if_fail (mem != NULL, NULL);
390 GST_DEBUG ("memory %p, %d->%d", mem, mem->refcount, mem->refcount + 1);
392 g_atomic_int_inc (&mem->refcount);
401 * Decreases the refcount of @mem. When the refcount reaches 0, the free
402 * function of @mem will be called.
405 gst_memory_unref (GstMemory * mem)
407 g_return_if_fail (mem != NULL);
408 g_return_if_fail (mem->allocator != NULL);
410 GST_DEBUG ("memory %p, %d->%d", mem, mem->refcount, mem->refcount - 1);
412 if (g_atomic_int_dec_and_test (&mem->refcount))
413 mem->allocator->info.mem_free (mem);
417 * gst_memory_get_sizes:
419 * @offset: pointer to offset
420 * @maxsize: pointer to maxsize
422 * Get the current @size, @offset and @maxsize of @mem.
424 * Returns: the current sizes of @mem
427 gst_memory_get_sizes (GstMemory * mem, gsize * offset, gsize * maxsize)
429 g_return_val_if_fail (mem != NULL, 0);
432 *offset = mem->offset;
434 *maxsize = mem->maxsize;
442 * @offset: a new offset
445 * Resize the memory region. @mem should be writable and offset + size should be
446 * less than the maxsize of @mem.
449 gst_memory_resize (GstMemory * mem, gssize offset, gsize size)
451 g_return_if_fail (mem != NULL);
452 g_return_if_fail (gst_memory_is_writable (mem));
453 g_return_if_fail (offset >= 0 || mem->offset >= -offset);
454 g_return_if_fail (size + mem->offset + offset <= mem->maxsize);
456 mem->offset += offset;
461 * gst_memory_is_writable:
464 * Check if @mem is writable.
466 * Returns: %TRUE is @mem is writable.
469 gst_memory_is_writable (GstMemory * mem)
471 g_return_val_if_fail (mem != NULL, FALSE);
473 return (mem->refcount == 1) &&
474 ((mem->parent == NULL) || (mem->parent->refcount == 1)) &&
475 ((mem->flags & GST_MEMORY_FLAG_READONLY) == 0);
479 gst_memory_lock (GstMemory * mem, GstMapFlags flags)
481 gint access_mode, state, newstate;
483 access_mode = flags & 3;
486 state = g_atomic_int_get (&mem->state);
488 /* nothing mapped, set access_mode and refcount */
489 newstate = 4 | access_mode;
491 /* access_mode must match */
492 if ((state & access_mode) != access_mode)
494 /* increase refcount */
495 newstate = state + 4;
497 } while (!g_atomic_int_compare_and_exchange (&mem->state, state, newstate));
503 GST_DEBUG ("lock failed %p: state %d, access_mode %d", mem, state,
510 gst_memory_unlock (GstMemory * mem)
512 gint state, newstate;
515 state = g_atomic_int_get (&mem->state);
516 /* decrease the refcount */
517 newstate = state - 4;
518 /* last refcount, unset access_mode */
521 } while (!g_atomic_int_compare_and_exchange (&mem->state, state, newstate));
526 * gst_memory_make_mapped:
527 * @mem: (transfer full): a #GstMemory
528 * @info: (out): pointer for info
529 * @flags: mapping flags
531 * Create a #GstMemory object that is mapped with @flags. If @mem is mappable
532 * with @flags, this function returns the mapped @mem directly. Otherwise a
533 * mapped copy of @mem is returned.
535 * This function takes ownership of old @mem and returns a reference to a new
538 * Returns: (transfer full): a #GstMemory object mapped with @flags or NULL when
539 * a mapping is not possible.
542 gst_memory_make_mapped (GstMemory * mem, GstMapInfo * info, GstMapFlags flags)
546 if (gst_memory_map (mem, info, flags)) {
549 result = gst_memory_copy (mem, 0, -1);
550 gst_memory_unref (mem);
555 if (!gst_memory_map (result, info, flags))
563 GST_DEBUG ("cannot copy memory %p", mem);
568 GST_DEBUG ("cannot map memory %p with flags %d", mem, flags);
569 gst_memory_unref (result);
577 * @info: (out): pointer for info
578 * @flags: mapping flags
580 * Fill @info with the pointer and sizes of the memory in @mem that can be
581 * accessed according to @flags.
583 * This function can return %FALSE for various reasons:
584 * - the memory backed by @mem is not accessible with the given @flags.
585 * - the memory was already mapped with a different mapping.
587 * @info and its contents remains valid for as long as @mem is alive and until
588 * gst_memory_unmap() is called.
590 * For each gst_memory_map() call, a corresponding gst_memory_unmap() call
593 * Returns: %TRUE if the map operation was successful.
596 gst_memory_map (GstMemory * mem, GstMapInfo * info, GstMapFlags flags)
598 g_return_val_if_fail (mem != NULL, FALSE);
599 g_return_val_if_fail (info != NULL, FALSE);
601 if (!gst_memory_lock (mem, flags))
604 info->data = mem->allocator->info.mem_map (mem, mem->maxsize, flags);
606 if (G_UNLIKELY (info->data == NULL))
611 info->size = mem->size;
612 info->maxsize = mem->maxsize - mem->offset;
613 info->data = info->data + mem->offset;
620 GST_DEBUG ("mem %p: lock %d failed", mem, flags);
625 /* something went wrong, restore the orginal state again */
626 GST_ERROR ("mem %p: map failed", mem);
627 gst_memory_unlock (mem);
635 * @info: a #GstMapInfo
637 * Release the memory obtained with gst_memory_map()
640 gst_memory_unmap (GstMemory * mem, GstMapInfo * info)
642 g_return_if_fail (mem != NULL);
643 g_return_if_fail (info != NULL);
644 g_return_if_fail (info->memory == mem);
645 /* there must be a ref */
646 g_return_if_fail (g_atomic_int_get (&mem->state) >= 4);
648 mem->allocator->info.mem_unmap (mem);
649 gst_memory_unlock (mem);
655 * @offset: an offset to copy
656 * @size: size to copy or -1 to copy all bytes from offset
658 * Return a copy of @size bytes from @mem starting from @offset. This copy is
659 * guaranteed to be writable. @size can be set to -1 to return a copy all bytes
662 * Returns: a new #GstMemory.
665 gst_memory_copy (GstMemory * mem, gssize offset, gssize size)
669 g_return_val_if_fail (mem != NULL, NULL);
670 g_return_val_if_fail (gst_memory_lock (mem, GST_MAP_READ), NULL);
672 copy = mem->allocator->info.mem_copy (mem, offset, size);
674 gst_memory_unlock (mem);
682 * @offset: an offset to share
683 * @size: size to share or -1 to share bytes from offset
685 * Return a shared copy of @size bytes from @mem starting from @offset. No
686 * memory copy is performed and the memory region is simply shared. The result
687 * is guaranteed to be not-writable. @size can be set to -1 to return a share
688 * all bytes from @offset.
690 * Returns: a new #GstMemory.
693 gst_memory_share (GstMemory * mem, gssize offset, gssize size)
695 g_return_val_if_fail (mem != NULL, NULL);
697 return mem->allocator->info.mem_share (mem, offset, size);
701 * gst_memory_is_span:
702 * @mem1: a #GstMemory
703 * @mem2: a #GstMemory
704 * @offset: a pointer to a result offset
706 * Check if @mem1 and mem2 share the memory with a common parent memory object
707 * and that the memory is contiguous.
709 * If this is the case, the memory of @mem1 and @mem2 can be merged
710 * efficiently by performing gst_memory_share() on the parent object from
711 * the returned @offset.
713 * Returns: %TRUE if the memory is contiguous and of a common parent.
716 gst_memory_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
718 g_return_val_if_fail (mem1 != NULL, FALSE);
719 g_return_val_if_fail (mem2 != NULL, FALSE);
721 /* need to have the same allocators */
722 if (mem1->allocator != mem2->allocator)
725 /* need to have the same parent */
726 if (mem1->parent == NULL || mem1->parent != mem2->parent)
729 /* and memory is contiguous */
730 if (!mem1->allocator->info.mem_is_span (mem1, mem2, offset))
737 * gst_allocator_register:
738 * @info: a #GstMemoryInfo
739 * @user_data: user data
740 * @notify: a #GDestroyNotify for @user_data
742 * Create a new memory allocator with @info and @user_data.
744 * All functions in @info are mandatory exept the copy and is_span
745 * functions, which will have a default implementation when left NULL.
747 * The @user_data will be passed to all calls of the alloc function and the
750 * Returns: a new #GstAllocator.
753 gst_allocator_new (const GstMemoryInfo * info, gpointer user_data,
754 GDestroyNotify notify)
756 GstAllocator *allocator;
758 #define INSTALL_FALLBACK(_t) \
759 if (allocator->info._t == NULL) allocator->info._t = _fallback_ ##_t;
761 g_return_val_if_fail (info != NULL, NULL);
762 g_return_val_if_fail (info->alloc != NULL, NULL);
763 g_return_val_if_fail (info->mem_map != NULL, NULL);
764 g_return_val_if_fail (info->mem_unmap != NULL, NULL);
765 g_return_val_if_fail (info->mem_free != NULL, NULL);
766 g_return_val_if_fail (info->mem_share != NULL, NULL);
768 allocator = g_slice_new (GstAllocator);
769 allocator->refcount = 1;
770 allocator->info = *info;
771 allocator->user_data = user_data;
772 allocator->notify = notify;
773 INSTALL_FALLBACK (mem_copy);
774 INSTALL_FALLBACK (mem_is_span);
775 #undef INSTALL_FALLBACK
777 GST_DEBUG ("new allocator %p", allocator);
784 * @allocator: a #GstAllocator
786 * Increases the refcount of @allocator.
788 * Returns: @allocator with increased refcount
791 gst_allocator_ref (GstAllocator * allocator)
793 g_return_val_if_fail (allocator != NULL, NULL);
795 GST_DEBUG ("alocator %p, %d->%d", allocator, allocator->refcount,
796 allocator->refcount + 1);
798 g_atomic_int_inc (&allocator->refcount);
804 * gst_allocator_unref:
805 * @allocator: a #GstAllocator
807 * Decreases the refcount of @allocator. When the refcount reaches 0, the free
808 * function of @allocator will be called.
811 gst_allocator_unref (GstAllocator * allocator)
813 g_return_if_fail (allocator != NULL);
815 GST_DEBUG ("allocator %p, %d->%d", allocator, allocator->refcount,
816 allocator->refcount - 1);
818 if (g_atomic_int_dec_and_test (&allocator->refcount)) {
819 if (allocator->notify)
820 allocator->notify (allocator->user_data);
821 g_slice_free1 (sizeof (GstAllocator), allocator);
826 * gst_allocator_register:
827 * @name: the name of the allocator
828 * @allocator: (transfer full): #GstAllocator
830 * Registers the memory @allocator with @name. This function takes ownership of
834 gst_allocator_register (const gchar * name, GstAllocator * allocator)
836 g_return_if_fail (name != NULL);
837 g_return_if_fail (allocator != NULL);
839 GST_DEBUG ("registering allocator %p with name \"%s\"", allocator, name);
841 g_rw_lock_writer_lock (&lock);
842 g_hash_table_insert (allocators, (gpointer) name, (gpointer) allocator);
843 g_rw_lock_writer_unlock (&lock);
847 * gst_allocator_find:
848 * @name: the name of the allocator
850 * Find a previously registered allocator with @name. When @name is NULL, the
851 * default allocator will be returned.
853 * Returns: (transfer full): a #GstAllocator or NULL when the allocator with @name was not
854 * registered. Use gst_allocator_unref() to release the allocator after usage.
857 gst_allocator_find (const gchar * name)
859 GstAllocator *allocator;
861 g_rw_lock_reader_lock (&lock);
863 allocator = g_hash_table_lookup (allocators, (gconstpointer) name);
865 allocator = _default_allocator;
868 gst_allocator_ref (allocator);
869 g_rw_lock_reader_unlock (&lock);
875 * gst_allocator_set_default:
876 * @allocator: (transfer full): a #GstAllocator
878 * Set the default allocator. This function takes ownership of @allocator.
881 gst_allocator_set_default (GstAllocator * allocator)
884 g_return_if_fail (allocator != NULL);
886 g_rw_lock_writer_lock (&lock);
887 old = _default_allocator;
888 _default_allocator = allocator;
889 g_rw_lock_writer_unlock (&lock);
892 gst_allocator_unref (old);
896 * gst_allocator_alloc:
897 * @allocator: (transfer none) (allow-none): a #GstAllocator to use
898 * @maxsize: allocated size of @data
899 * @align: alignment for the data
901 * Use @allocator to allocate a new memory block with memory that is at least
902 * @maxsize big and has the given alignment.
904 * When @allocator is NULL, the default allocator will be used.
906 * @align is given as a bitmask so that @align + 1 equals the amount of bytes to
907 * align to. For example, to align to 8 bytes, use an alignment of 7.
909 * Returns: (transfer full): a new #GstMemory.
912 gst_allocator_alloc (GstAllocator * allocator, gsize maxsize, gsize align)
914 g_return_val_if_fail (((align + 1) & align) == 0, NULL);
916 if (allocator == NULL)
917 allocator = _default_allocator;
919 return allocator->info.alloc (allocator, maxsize, align,
920 allocator->user_data);