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);
235 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy memory %p -> %p", mem, copy);
240 static GstMemoryDefault *
241 _default_mem_share (GstMemoryDefault * mem, gssize offset, gsize size)
243 GstMemoryDefault *sub;
246 /* find the real parent */
247 if ((parent = mem->mem.parent) == NULL)
248 parent = (GstMemory *) mem;
251 size = mem->mem.size - offset;
254 _default_mem_new (parent->flags, parent, mem->data, NULL,
255 mem->mem.maxsize, mem->mem.offset + offset, size);
261 _default_mem_is_span (GstMemoryDefault * mem1, GstMemoryDefault * mem2,
266 GstMemoryDefault *parent;
268 parent = (GstMemoryDefault *) mem1->mem.parent;
270 *offset = mem1->mem.offset - parent->mem.offset;
273 /* and memory is contiguous */
274 return mem1->data + mem1->mem.offset + mem1->mem.size ==
275 mem2->data + mem2->mem.offset;
279 _fallback_mem_copy (GstMemory * mem, gssize offset, gssize size)
282 GstMapInfo sinfo, dinfo;
284 if (!gst_memory_map (mem, &sinfo, GST_MAP_READ))
288 size = sinfo.size > offset ? sinfo.size - offset : 0;
290 /* use the same allocator as the memory we copy */
291 copy = gst_allocator_alloc (mem->allocator, size, mem->align);
292 if (!gst_memory_map (copy, &dinfo, GST_MAP_WRITE)) {
293 GST_WARNING ("could not write map memory %p", copy);
294 gst_memory_unmap (mem, &sinfo);
298 memcpy (dinfo.data, sinfo.data + offset, size);
299 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy memory %p -> %p", mem, copy);
300 gst_memory_unmap (copy, &dinfo);
301 gst_memory_unmap (mem, &sinfo);
307 _fallback_mem_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
313 static GHashTable *allocators;
316 _priv_sysmem_notify (gpointer user_data)
318 g_warning ("The default memory allocator was freed!");
322 _priv_gst_memory_initialize (void)
324 static const GstMemoryInfo _mem_info = {
325 GST_ALLOCATOR_SYSMEM,
326 (GstAllocatorAllocFunction) _default_alloc_alloc,
327 (GstMemoryMapFunction) _default_mem_map,
328 (GstMemoryUnmapFunction) _default_mem_unmap,
329 (GstMemoryFreeFunction) _default_mem_free,
330 (GstMemoryCopyFunction) _default_mem_copy,
331 (GstMemoryShareFunction) _default_mem_share,
332 (GstMemoryIsSpanFunction) _default_mem_is_span,
335 g_rw_lock_init (&lock);
336 allocators = g_hash_table_new (g_str_hash, g_str_equal);
338 #ifdef HAVE_GETPAGESIZE
339 #ifdef MEMORY_ALIGNMENT_PAGESIZE
340 gst_memory_alignment = getpagesize () - 1;
344 GST_DEBUG ("memory alignment: %" G_GSIZE_FORMAT, gst_memory_alignment);
346 _default_mem_impl = gst_allocator_new (&_mem_info, NULL, _priv_sysmem_notify);
348 _default_allocator = gst_allocator_ref (_default_mem_impl);
349 gst_allocator_register (GST_ALLOCATOR_SYSMEM,
350 gst_allocator_ref (_default_mem_impl));
354 * gst_memory_new_wrapped:
355 * @flags: #GstMemoryFlags
356 * @data: data to wrap
357 * @free_func: function to free @data
358 * @maxsize: allocated size of @data
359 * @offset: offset in @data
360 * @size: size of valid data
362 * Allocate a new memory block that wraps the given @data.
364 * Returns: a new #GstMemory.
367 gst_memory_new_wrapped (GstMemoryFlags flags, gpointer data,
368 GFreeFunc free_func, gsize maxsize, gsize offset, gsize size)
370 GstMemoryDefault *mem;
372 g_return_val_if_fail (data != NULL, NULL);
373 g_return_val_if_fail (offset + size <= maxsize, NULL);
375 mem = _default_mem_new (flags, NULL, data, free_func, maxsize, offset, size);
377 return (GstMemory *) mem;
384 * Increases the refcount of @mem.
386 * Returns: @mem with increased refcount
389 gst_memory_ref (GstMemory * mem)
391 g_return_val_if_fail (mem != NULL, NULL);
393 GST_DEBUG ("memory %p, %d->%d", mem, mem->refcount, mem->refcount + 1);
395 g_atomic_int_inc (&mem->refcount);
404 * Decreases the refcount of @mem. When the refcount reaches 0, the free
405 * function of @mem will be called.
408 gst_memory_unref (GstMemory * mem)
410 g_return_if_fail (mem != NULL);
411 g_return_if_fail (mem->allocator != NULL);
413 GST_DEBUG ("memory %p, %d->%d", mem, mem->refcount, mem->refcount - 1);
415 if (g_atomic_int_dec_and_test (&mem->refcount))
416 mem->allocator->info.mem_free (mem);
420 * gst_memory_get_sizes:
422 * @offset: pointer to offset
423 * @maxsize: pointer to maxsize
425 * Get the current @size, @offset and @maxsize of @mem.
427 * Returns: the current sizes of @mem
430 gst_memory_get_sizes (GstMemory * mem, gsize * offset, gsize * maxsize)
432 g_return_val_if_fail (mem != NULL, 0);
435 *offset = mem->offset;
437 *maxsize = mem->maxsize;
445 * @offset: a new offset
448 * Resize the memory region. @mem should be writable and offset + size should be
449 * less than the maxsize of @mem.
452 gst_memory_resize (GstMemory * mem, gssize offset, gsize size)
454 g_return_if_fail (mem != NULL);
455 g_return_if_fail (gst_memory_is_writable (mem));
456 g_return_if_fail (offset >= 0 || mem->offset >= -offset);
457 g_return_if_fail (size + mem->offset + offset <= mem->maxsize);
459 mem->offset += offset;
464 * gst_memory_is_writable:
467 * Check if @mem is writable.
469 * Returns: %TRUE is @mem is writable.
472 gst_memory_is_writable (GstMemory * mem)
474 g_return_val_if_fail (mem != NULL, FALSE);
476 return (mem->refcount == 1) &&
477 ((mem->parent == NULL) || (mem->parent->refcount == 1)) &&
478 ((mem->flags & GST_MEMORY_FLAG_READONLY) == 0);
482 gst_memory_lock (GstMemory * mem, GstMapFlags flags)
484 gint access_mode, state, newstate;
486 access_mode = flags & 3;
489 state = g_atomic_int_get (&mem->state);
491 /* nothing mapped, set access_mode and refcount */
492 newstate = 4 | access_mode;
494 /* access_mode must match */
495 if ((state & access_mode) != access_mode)
497 /* increase refcount */
498 newstate = state + 4;
500 } while (!g_atomic_int_compare_and_exchange (&mem->state, state, newstate));
506 GST_DEBUG ("lock failed %p: state %d, access_mode %d", mem, state,
513 gst_memory_unlock (GstMemory * mem)
515 gint state, newstate;
518 state = g_atomic_int_get (&mem->state);
519 /* decrease the refcount */
520 newstate = state - 4;
521 /* last refcount, unset access_mode */
524 } while (!g_atomic_int_compare_and_exchange (&mem->state, state, newstate));
529 * gst_memory_make_mapped:
530 * @mem: (transfer full): a #GstMemory
531 * @info: (out): pointer for info
532 * @flags: mapping flags
534 * Create a #GstMemory object that is mapped with @flags. If @mem is mappable
535 * with @flags, this function returns the mapped @mem directly. Otherwise a
536 * mapped copy of @mem is returned.
538 * This function takes ownership of old @mem and returns a reference to a new
541 * Returns: (transfer full): a #GstMemory object mapped with @flags or NULL when
542 * a mapping is not possible.
545 gst_memory_make_mapped (GstMemory * mem, GstMapInfo * info, GstMapFlags flags)
549 if (gst_memory_map (mem, info, flags)) {
552 result = gst_memory_copy (mem, 0, -1);
553 gst_memory_unref (mem);
558 if (!gst_memory_map (result, info, flags))
566 GST_DEBUG ("cannot copy memory %p", mem);
571 GST_DEBUG ("cannot map memory %p with flags %d", mem, flags);
572 gst_memory_unref (result);
580 * @info: (out): pointer for info
581 * @flags: mapping flags
583 * Fill @info with the pointer and sizes of the memory in @mem that can be
584 * accessed according to @flags.
586 * This function can return %FALSE for various reasons:
587 * - the memory backed by @mem is not accessible with the given @flags.
588 * - the memory was already mapped with a different mapping.
590 * @info and its contents remains valid for as long as @mem is alive and until
591 * gst_memory_unmap() is called.
593 * For each gst_memory_map() call, a corresponding gst_memory_unmap() call
596 * Returns: %TRUE if the map operation was successful.
599 gst_memory_map (GstMemory * mem, GstMapInfo * info, GstMapFlags flags)
601 g_return_val_if_fail (mem != NULL, FALSE);
602 g_return_val_if_fail (info != NULL, FALSE);
604 if (!gst_memory_lock (mem, flags))
607 info->data = mem->allocator->info.mem_map (mem, mem->maxsize, flags);
609 if (G_UNLIKELY (info->data == NULL))
614 info->size = mem->size;
615 info->maxsize = mem->maxsize - mem->offset;
616 info->data = info->data + mem->offset;
623 GST_DEBUG ("mem %p: lock %d failed", mem, flags);
628 /* something went wrong, restore the orginal state again */
629 GST_ERROR ("mem %p: map failed", mem);
630 gst_memory_unlock (mem);
638 * @info: a #GstMapInfo
640 * Release the memory obtained with gst_memory_map()
643 gst_memory_unmap (GstMemory * mem, GstMapInfo * info)
645 g_return_if_fail (mem != NULL);
646 g_return_if_fail (info != NULL);
647 g_return_if_fail (info->memory == mem);
648 /* there must be a ref */
649 g_return_if_fail (g_atomic_int_get (&mem->state) >= 4);
651 mem->allocator->info.mem_unmap (mem);
652 gst_memory_unlock (mem);
658 * @offset: an offset to copy
659 * @size: size to copy or -1 to copy all bytes from offset
661 * Return a copy of @size bytes from @mem starting from @offset. This copy is
662 * guaranteed to be writable. @size can be set to -1 to return a copy all bytes
665 * Returns: a new #GstMemory.
668 gst_memory_copy (GstMemory * mem, gssize offset, gssize size)
672 g_return_val_if_fail (mem != NULL, NULL);
673 g_return_val_if_fail (gst_memory_lock (mem, GST_MAP_READ), NULL);
675 copy = mem->allocator->info.mem_copy (mem, offset, size);
677 gst_memory_unlock (mem);
685 * @offset: an offset to share
686 * @size: size to share or -1 to share bytes from offset
688 * Return a shared copy of @size bytes from @mem starting from @offset. No
689 * memory copy is performed and the memory region is simply shared. The result
690 * is guaranteed to be not-writable. @size can be set to -1 to return a share
691 * all bytes from @offset.
693 * Returns: a new #GstMemory.
696 gst_memory_share (GstMemory * mem, gssize offset, gssize size)
698 g_return_val_if_fail (mem != NULL, NULL);
700 return mem->allocator->info.mem_share (mem, offset, size);
704 * gst_memory_is_span:
705 * @mem1: a #GstMemory
706 * @mem2: a #GstMemory
707 * @offset: a pointer to a result offset
709 * Check if @mem1 and mem2 share the memory with a common parent memory object
710 * and that the memory is contiguous.
712 * If this is the case, the memory of @mem1 and @mem2 can be merged
713 * efficiently by performing gst_memory_share() on the parent object from
714 * the returned @offset.
716 * Returns: %TRUE if the memory is contiguous and of a common parent.
719 gst_memory_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
721 g_return_val_if_fail (mem1 != NULL, FALSE);
722 g_return_val_if_fail (mem2 != NULL, FALSE);
724 /* need to have the same allocators */
725 if (mem1->allocator != mem2->allocator)
728 /* need to have the same parent */
729 if (mem1->parent == NULL || mem1->parent != mem2->parent)
732 /* and memory is contiguous */
733 if (!mem1->allocator->info.mem_is_span (mem1, mem2, offset))
740 * gst_allocator_register:
741 * @info: a #GstMemoryInfo
742 * @user_data: user data
743 * @notify: a #GDestroyNotify for @user_data
745 * Create a new memory allocator with @info and @user_data.
747 * All functions in @info are mandatory exept the copy and is_span
748 * functions, which will have a default implementation when left NULL.
750 * The @user_data will be passed to all calls of the alloc function and the
753 * Returns: a new #GstAllocator.
756 gst_allocator_new (const GstMemoryInfo * info, gpointer user_data,
757 GDestroyNotify notify)
759 GstAllocator *allocator;
761 #define INSTALL_FALLBACK(_t) \
762 if (allocator->info._t == NULL) allocator->info._t = _fallback_ ##_t;
764 g_return_val_if_fail (info != NULL, NULL);
765 g_return_val_if_fail (info->alloc != NULL, NULL);
766 g_return_val_if_fail (info->mem_map != NULL, NULL);
767 g_return_val_if_fail (info->mem_unmap != NULL, NULL);
768 g_return_val_if_fail (info->mem_free != NULL, NULL);
769 g_return_val_if_fail (info->mem_share != NULL, NULL);
771 allocator = g_slice_new (GstAllocator);
772 allocator->refcount = 1;
773 allocator->info = *info;
774 allocator->user_data = user_data;
775 allocator->notify = notify;
776 INSTALL_FALLBACK (mem_copy);
777 INSTALL_FALLBACK (mem_is_span);
778 #undef INSTALL_FALLBACK
780 GST_DEBUG ("new allocator %p", allocator);
786 * gst_alocator_get_memory_type:
787 * @allocator: a #GstAllocator
789 * Get the memory type allocated by this allocator
791 * Returns: @allocator with increased refcount
794 gst_allocator_get_memory_type (GstAllocator * allocator)
796 g_return_val_if_fail (allocator != NULL, NULL);
798 return allocator->info.mem_type;
803 * @allocator: a #GstAllocator
805 * Increases the refcount of @allocator.
807 * Returns: @allocator with increased refcount
810 gst_allocator_ref (GstAllocator * allocator)
812 g_return_val_if_fail (allocator != NULL, NULL);
814 GST_DEBUG ("alocator %p, %d->%d", allocator, allocator->refcount,
815 allocator->refcount + 1);
817 g_atomic_int_inc (&allocator->refcount);
823 * gst_allocator_unref:
824 * @allocator: a #GstAllocator
826 * Decreases the refcount of @allocator. When the refcount reaches 0, the free
827 * function of @allocator will be called.
830 gst_allocator_unref (GstAllocator * allocator)
832 g_return_if_fail (allocator != NULL);
834 GST_DEBUG ("allocator %p, %d->%d", allocator, allocator->refcount,
835 allocator->refcount - 1);
837 if (g_atomic_int_dec_and_test (&allocator->refcount)) {
838 if (allocator->notify)
839 allocator->notify (allocator->user_data);
840 g_slice_free1 (sizeof (GstAllocator), allocator);
845 * gst_allocator_register:
846 * @name: the name of the allocator
847 * @allocator: (transfer full): #GstAllocator
849 * Registers the memory @allocator with @name. This function takes ownership of
853 gst_allocator_register (const gchar * name, GstAllocator * allocator)
855 g_return_if_fail (name != NULL);
856 g_return_if_fail (allocator != NULL);
858 GST_DEBUG ("registering allocator %p with name \"%s\"", allocator, name);
860 g_rw_lock_writer_lock (&lock);
861 g_hash_table_insert (allocators, (gpointer) name, (gpointer) allocator);
862 g_rw_lock_writer_unlock (&lock);
866 * gst_allocator_find:
867 * @name: the name of the allocator
869 * Find a previously registered allocator with @name. When @name is NULL, the
870 * default allocator will be returned.
872 * Returns: (transfer full): a #GstAllocator or NULL when the allocator with @name was not
873 * registered. Use gst_allocator_unref() to release the allocator after usage.
876 gst_allocator_find (const gchar * name)
878 GstAllocator *allocator;
880 g_rw_lock_reader_lock (&lock);
882 allocator = g_hash_table_lookup (allocators, (gconstpointer) name);
884 allocator = _default_allocator;
887 gst_allocator_ref (allocator);
888 g_rw_lock_reader_unlock (&lock);
894 * gst_allocator_set_default:
895 * @allocator: (transfer full): a #GstAllocator
897 * Set the default allocator. This function takes ownership of @allocator.
900 gst_allocator_set_default (GstAllocator * allocator)
903 g_return_if_fail (allocator != NULL);
905 g_rw_lock_writer_lock (&lock);
906 old = _default_allocator;
907 _default_allocator = allocator;
908 g_rw_lock_writer_unlock (&lock);
911 gst_allocator_unref (old);
915 * gst_allocator_alloc:
916 * @allocator: (transfer none) (allow-none): a #GstAllocator to use
917 * @maxsize: allocated size of @data
918 * @align: alignment for the data
920 * Use @allocator to allocate a new memory block with memory that is at least
921 * @maxsize big and has the given alignment.
923 * When @allocator is NULL, the default allocator will be used.
925 * @align is given as a bitmask so that @align + 1 equals the amount of bytes to
926 * align to. For example, to align to 8 bytes, use an alignment of 7.
928 * Returns: (transfer full): a new #GstMemory.
931 gst_allocator_alloc (GstAllocator * allocator, gsize maxsize, gsize align)
933 g_return_val_if_fail (((align + 1) & align) == 0, NULL);
935 if (allocator == NULL)
936 allocator = _default_allocator;
938 return allocator->info.alloc (allocator, maxsize, align,
939 allocator->user_data);