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 /* default memory implementation */
110 /* the default allocator */
111 static const GstAllocator *_default_allocator;
113 /* our predefined allocators */
114 static const GstAllocator *_default_mem_impl;
116 /* initialize the fields */
118 _default_mem_init (GstMemoryDefault * mem, GstMemoryFlags flags,
119 GstMemory * parent, gsize slice_size, gpointer data,
120 GFreeFunc free_func, gsize maxsize, gsize offset, gsize size)
122 mem->mem.allocator = _default_mem_impl;
123 mem->mem.flags = flags;
124 mem->mem.refcount = 1;
125 mem->mem.parent = parent ? gst_memory_ref (parent) : NULL;
126 mem->mem.state = (flags & GST_MEMORY_FLAG_READONLY ? 0x5 : 0);
127 mem->mem.maxsize = maxsize;
128 mem->mem.offset = offset;
129 mem->mem.size = size;
130 mem->slice_size = slice_size;
132 mem->free_func = free_func;
134 GST_DEBUG ("new memory %p", mem);
137 /* create a new memory block that manages the given memory */
138 static GstMemoryDefault *
139 _default_mem_new (GstMemoryFlags flags, GstMemory * parent, gpointer data,
140 GFreeFunc free_func, gsize maxsize, gsize offset, gsize size)
142 GstMemoryDefault *mem;
145 slice_size = sizeof (GstMemoryDefault);
147 mem = g_slice_alloc (slice_size);
148 _default_mem_init (mem, flags, parent, slice_size,
149 data, free_func, maxsize, offset, size);
154 /* allocate the memory and structure in one block */
155 static GstMemoryDefault *
156 _default_mem_new_block (gsize maxsize, gsize align, gsize offset, gsize size)
158 GstMemoryDefault *mem;
159 gsize aoffset, slice_size;
162 /* ensure configured alignment */
163 align |= gst_memory_alignment;
164 /* allocate more to compensate for alignment */
166 /* alloc header and data in one block */
167 slice_size = sizeof (GstMemoryDefault) + maxsize;
169 mem = g_slice_alloc (slice_size);
173 data = (guint8 *) mem + sizeof (GstMemoryDefault);
175 if ((aoffset = ((guintptr) data & align)))
176 aoffset = (align + 1) - aoffset;
178 _default_mem_init (mem, 0, NULL, slice_size, data, NULL, maxsize,
179 aoffset + offset, size);
185 _default_mem_alloc (const GstAllocator * allocator, gsize maxsize, gsize align)
187 return (GstMemory *) _default_mem_new_block (maxsize, align, 0, maxsize);
191 _default_mem_map (GstMemoryDefault * mem, GstMapFlags flags)
197 _default_mem_unmap (GstMemoryDefault * mem)
203 _default_mem_free (GstMemoryDefault * mem)
205 GST_DEBUG ("free memory %p", mem);
208 gst_memory_unref (mem->mem.parent);
211 mem->free_func (mem->data);
213 g_slice_free1 (mem->slice_size, mem);
216 static GstMemoryDefault *
217 _default_mem_copy (GstMemoryDefault * mem, gssize offset, gsize size)
219 GstMemoryDefault *copy;
222 size = mem->mem.size > offset ? mem->mem.size - offset : 0;
225 _default_mem_new_block (mem->mem.maxsize, 0, mem->mem.offset + offset,
227 memcpy (copy->data, mem->data, mem->mem.maxsize);
232 static GstMemoryDefault *
233 _default_mem_share (GstMemoryDefault * mem, gssize offset, gsize size)
235 GstMemoryDefault *sub;
238 /* find the real parent */
239 if ((parent = mem->mem.parent) == NULL)
240 parent = (GstMemory *) mem;
243 size = mem->mem.size - offset;
246 _default_mem_new (parent->flags, parent, mem->data, NULL,
247 mem->mem.maxsize, mem->mem.offset + offset, size);
253 _default_mem_is_span (GstMemoryDefault * mem1, GstMemoryDefault * mem2,
258 GstMemoryDefault *parent;
260 parent = (GstMemoryDefault *) mem1->mem.parent;
262 *offset = mem1->mem.offset - parent->mem.offset;
265 /* and memory is contiguous */
266 return mem1->data + mem1->mem.offset + mem1->mem.size ==
267 mem2->data + mem2->mem.offset;
271 _fallback_copy (GstMemory * mem, gssize offset, gssize size)
274 GstMapInfo sinfo, dinfo;
276 if (!gst_memory_map (mem, &sinfo, GST_MAP_READ))
280 size = sinfo.size > offset ? sinfo.size - offset : 0;
282 /* use the same allocator as the memory we copy */
283 copy = gst_allocator_alloc (mem->allocator, size, mem->align);
284 if (!gst_memory_map (copy, &dinfo, GST_MAP_WRITE)) {
285 GST_WARNING ("could not write map memory %p", copy);
286 gst_memory_unmap (mem, &sinfo);
290 memcpy (dinfo.data, sinfo.data + offset, size);
291 gst_memory_unmap (copy, &dinfo);
292 gst_memory_unmap (mem, &sinfo);
298 _fallback_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
304 static GHashTable *allocators;
307 _priv_gst_memory_initialize (void)
309 static const GstMemoryInfo _mem_info = {
310 (GstMemoryAllocFunction) _default_mem_alloc,
311 (GstMemoryMapFunction) _default_mem_map,
312 (GstMemoryUnmapFunction) _default_mem_unmap,
313 (GstMemoryFreeFunction) _default_mem_free,
314 (GstMemoryCopyFunction) _default_mem_copy,
315 (GstMemoryShareFunction) _default_mem_share,
316 (GstMemoryIsSpanFunction) _default_mem_is_span,
320 g_rw_lock_init (&lock);
321 allocators = g_hash_table_new (g_str_hash, g_str_equal);
323 #ifdef HAVE_GETPAGESIZE
324 #ifdef MEMORY_ALIGNMENT_PAGESIZE
325 gst_memory_alignment = getpagesize () - 1;
329 GST_DEBUG ("memory alignment: %" G_GSIZE_FORMAT, gst_memory_alignment);
331 _default_mem_impl = gst_allocator_register (GST_ALLOCATOR_SYSMEM, &_mem_info);
333 _default_allocator = _default_mem_impl;
337 * gst_memory_new_wrapped:
338 * @flags: #GstMemoryFlags
339 * @data: data to wrap
340 * @free_func: function to free @data
341 * @maxsize: allocated size of @data
342 * @offset: offset in @data
343 * @size: size of valid data
345 * Allocate a new memory block that wraps the given @data.
347 * Returns: a new #GstMemory.
350 gst_memory_new_wrapped (GstMemoryFlags flags, gpointer data,
351 GFreeFunc free_func, gsize maxsize, gsize offset, gsize size)
353 GstMemoryDefault *mem;
355 g_return_val_if_fail (data != NULL, NULL);
356 g_return_val_if_fail (offset + size <= maxsize, NULL);
358 mem = _default_mem_new (flags, NULL, data, free_func, maxsize, offset, size);
360 return (GstMemory *) mem;
367 * Increases the refcount of @mem.
369 * Returns: @mem with increased refcount
372 gst_memory_ref (GstMemory * mem)
374 g_return_val_if_fail (mem != NULL, NULL);
376 GST_DEBUG ("memory %p, %d->%d", mem, mem->refcount, mem->refcount + 1);
378 g_atomic_int_inc (&mem->refcount);
387 * Decreases the refcount of @mem. When the refcount reaches 0, the free
388 * function of @mem will be called.
391 gst_memory_unref (GstMemory * mem)
393 g_return_if_fail (mem != NULL);
394 g_return_if_fail (mem->allocator != NULL);
396 GST_DEBUG ("memory %p, %d->%d", mem, mem->refcount, mem->refcount - 1);
398 if (g_atomic_int_dec_and_test (&mem->refcount))
399 mem->allocator->info.free (mem);
403 * gst_memory_get_sizes:
405 * @offset: pointer to offset
406 * @maxsize: pointer to maxsize
408 * Get the current @size, @offset and @maxsize of @mem.
410 * Returns: the current sizes of @mem
413 gst_memory_get_sizes (GstMemory * mem, gsize * offset, gsize * maxsize)
415 g_return_val_if_fail (mem != NULL, 0);
418 *offset = mem->offset;
420 *maxsize = mem->maxsize;
428 * @offset: a new offset
431 * Resize the memory region. @mem should be writable and offset + size should be
432 * less than the maxsize of @mem.
435 gst_memory_resize (GstMemory * mem, gssize offset, gsize size)
437 g_return_if_fail (mem != NULL);
438 g_return_if_fail (gst_memory_is_writable (mem));
439 g_return_if_fail (offset >= 0 || mem->offset >= -offset);
440 g_return_if_fail (size + mem->offset + offset <= mem->maxsize);
442 mem->offset += offset;
447 * gst_memory_is_writable:
450 * Check if @mem is writable.
452 * Returns: %TRUE is @mem is writable.
455 gst_memory_is_writable (GstMemory * mem)
457 g_return_val_if_fail (mem != NULL, FALSE);
459 return (mem->refcount == 1) &&
460 ((mem->parent == NULL) || (mem->parent->refcount == 1)) &&
461 ((mem->flags & GST_MEMORY_FLAG_READONLY) == 0);
465 gst_memory_lock (GstMemory * mem, GstMapFlags flags)
467 gint access_mode, state, newstate;
469 access_mode = flags & 3;
472 state = g_atomic_int_get (&mem->state);
474 /* nothing mapped, set access_mode and refcount */
475 newstate = 4 | access_mode;
477 /* access_mode must match */
478 if ((state & access_mode) != access_mode)
480 /* increase refcount */
481 newstate = state + 4;
483 } while (!g_atomic_int_compare_and_exchange (&mem->state, state, newstate));
489 GST_DEBUG ("lock failed %p: state %d, access_mode %d", mem, state,
496 gst_memory_unlock (GstMemory * mem)
498 gint state, newstate;
501 state = g_atomic_int_get (&mem->state);
502 /* decrease the refcount */
503 newstate = state - 4;
504 /* last refcount, unset access_mode */
507 } while (!g_atomic_int_compare_and_exchange (&mem->state, state, newstate));
512 * gst_memory_make_mapped:
513 * @mem: (transfer full): a #GstMemory
514 * @info: (out): pointer for info
515 * @flags: mapping flags
517 * Create a #GstMemory object that is mapped with @flags. If @mem is mappable
518 * with @flags, this function returns the mapped @mem directly. Otherwise a
519 * mapped copy of @mem is returned.
521 * This function takes ownership of old @mem and returns a reference to a new
524 * Returns: (transfer full): a #GstMemory object mapped with @flags or NULL when
525 * a mapping is not possible.
528 gst_memory_make_mapped (GstMemory * mem, GstMapInfo * info, GstMapFlags flags)
532 if (gst_memory_map (mem, info, flags)) {
535 result = gst_memory_copy (mem, 0, -1);
536 gst_memory_unref (mem);
541 if (!gst_memory_map (result, info, flags))
549 GST_DEBUG ("cannot copy memory %p", mem);
554 GST_DEBUG ("cannot map memory %p with flags %d", mem, flags);
555 gst_memory_unref (result);
563 * @info: (out): pointer for info
564 * @flags: mapping flags
566 * Fill @info with the pointer and sizes of the memory in @mem that can be
567 * accessed according to @flags.
569 * This function can return %FALSE for various reasons:
570 * - the memory backed by @mem is not accessible with the given @flags.
571 * - the memory was already mapped with a different mapping.
573 * @info and its contents remains valid for as long as @mem is alive and until
574 * gst_memory_unmap() is called.
576 * For each gst_memory_map() call, a corresponding gst_memory_unmap() call
579 * Returns: %TRUE if the map operation was successful.
582 gst_memory_map (GstMemory * mem, GstMapInfo * info, GstMapFlags flags)
584 g_return_val_if_fail (mem != NULL, FALSE);
585 g_return_val_if_fail (info != NULL, FALSE);
587 if (!gst_memory_lock (mem, flags))
590 info->data = mem->allocator->info.map (mem, mem->maxsize, flags);
592 if (G_UNLIKELY (info->data == NULL))
597 info->size = mem->size;
598 info->maxsize = mem->maxsize - mem->offset;
599 info->data = info->data + mem->offset;
606 GST_DEBUG ("mem %p: lock %d failed", mem, flags);
611 /* something went wrong, restore the orginal state again */
612 GST_ERROR ("mem %p: map failed", mem);
613 gst_memory_unlock (mem);
621 * @info: a #GstMapInfo
623 * Release the memory obtained with gst_memory_map()
626 gst_memory_unmap (GstMemory * mem, GstMapInfo * info)
628 g_return_if_fail (mem != NULL);
629 g_return_if_fail (info != NULL);
630 g_return_if_fail (info->memory == mem);
631 /* there must be a ref */
632 g_return_if_fail (g_atomic_int_get (&mem->state) >= 4);
634 mem->allocator->info.unmap (mem);
635 gst_memory_unlock (mem);
641 * @offset: an offset to copy
642 * @size: size to copy or -1 to copy all bytes from offset
644 * Return a copy of @size bytes from @mem starting from @offset. This copy is
645 * guaranteed to be writable. @size can be set to -1 to return a copy all bytes
648 * Returns: a new #GstMemory.
651 gst_memory_copy (GstMemory * mem, gssize offset, gssize size)
655 g_return_val_if_fail (mem != NULL, NULL);
656 g_return_val_if_fail (gst_memory_lock (mem, GST_MAP_READ), NULL);
658 copy = mem->allocator->info.copy (mem, offset, size);
660 gst_memory_unlock (mem);
668 * @offset: an offset to share
669 * @size: size to share or -1 to share bytes from offset
671 * Return a shared copy of @size bytes from @mem starting from @offset. No
672 * memory copy is performed and the memory region is simply shared. The result
673 * is guaranteed to be not-writable. @size can be set to -1 to return a share
674 * all bytes from @offset.
676 * Returns: a new #GstMemory.
679 gst_memory_share (GstMemory * mem, gssize offset, gssize size)
681 g_return_val_if_fail (mem != NULL, NULL);
683 return mem->allocator->info.share (mem, offset, size);
687 * gst_memory_is_span:
688 * @mem1: a #GstMemory
689 * @mem2: a #GstMemory
690 * @offset: a pointer to a result offset
692 * Check if @mem1 and mem2 share the memory with a common parent memory object
693 * and that the memory is contiguous.
695 * If this is the case, the memory of @mem1 and @mem2 can be merged
696 * efficiently by performing gst_memory_share() on the parent object from
697 * the returned @offset.
699 * Returns: %TRUE if the memory is contiguous and of a common parent.
702 gst_memory_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
704 g_return_val_if_fail (mem1 != NULL, FALSE);
705 g_return_val_if_fail (mem2 != NULL, FALSE);
707 /* need to have the same allocators */
708 if (mem1->allocator != mem2->allocator)
711 /* need to have the same parent */
712 if (mem1->parent == NULL || mem1->parent != mem2->parent)
715 /* and memory is contiguous */
716 if (!mem1->allocator->info.is_span (mem1, mem2, offset))
723 * gst_allocator_register:
724 * @name: the name of the allocator
725 * @info: #GstMemoryInfo
727 * Registers the memory allocator with @name and implementation functions
730 * All functions in @info are mandatory exept the copy and is_span
731 * functions, which will have a default implementation when left NULL.
733 * The user_data field in @info will be passed to all calls of the alloc
736 * Returns: a new #GstAllocator.
739 gst_allocator_register (const gchar * name, const GstMemoryInfo * info)
741 GstAllocator *allocator;
743 #define INSTALL_FALLBACK(_t) \
744 if (allocator->info._t == NULL) allocator->info._t = _fallback_ ##_t;
746 g_return_val_if_fail (name != NULL, NULL);
747 g_return_val_if_fail (info != NULL, NULL);
748 g_return_val_if_fail (info->alloc != NULL, NULL);
749 g_return_val_if_fail (info->map != NULL, NULL);
750 g_return_val_if_fail (info->unmap != NULL, NULL);
751 g_return_val_if_fail (info->free != NULL, NULL);
752 g_return_val_if_fail (info->share != NULL, NULL);
754 allocator = g_slice_new (GstAllocator);
755 allocator->name = g_quark_from_string (name);
756 allocator->info = *info;
757 INSTALL_FALLBACK (copy);
758 INSTALL_FALLBACK (is_span);
759 #undef INSTALL_FALLBACK
761 GST_DEBUG ("registering allocator \"%s\"", name);
763 g_rw_lock_writer_lock (&lock);
764 g_hash_table_insert (allocators, (gpointer) name, (gpointer) allocator);
765 g_rw_lock_writer_unlock (&lock);
771 * gst_allocator_find:
772 * @name: the name of the allocator
774 * Find a previously registered allocator with @name. When @name is NULL, the
775 * default allocator will be returned.
777 * Returns: a #GstAllocator or NULL when the allocator with @name was not
781 gst_allocator_find (const gchar * name)
783 const GstAllocator *allocator;
785 g_rw_lock_reader_lock (&lock);
787 allocator = g_hash_table_lookup (allocators, (gconstpointer) name);
789 allocator = _default_allocator;
791 g_rw_lock_reader_unlock (&lock);
797 * gst_allocator_set_default:
798 * @allocator: a #GstAllocator
800 * Set the default allocator.
803 gst_allocator_set_default (const GstAllocator * allocator)
805 g_return_if_fail (allocator != NULL);
807 g_rw_lock_writer_lock (&lock);
808 _default_allocator = allocator;
809 g_rw_lock_writer_unlock (&lock);
813 * gst_allocator_alloc:
814 * @allocator: (transfer none) (allow-none): a #GstAllocator to use
815 * @maxsize: allocated size of @data
816 * @align: alignment for the data
818 * Use @allocator to allocate a new memory block with memory that is at least
819 * @maxsize big and has the given alignment.
821 * When @allocator is NULL, the default allocator will be used.
823 * @align is given as a bitmask so that @align + 1 equals the amount of bytes to
824 * align to. For example, to align to 8 bytes, use an alignment of 7.
826 * Returns: (transfer full): a new #GstMemory.
829 gst_allocator_alloc (const GstAllocator * allocator, gsize maxsize, gsize align)
831 g_return_val_if_fail (((align + 1) & align) == 0, NULL);
833 if (allocator == NULL)
834 allocator = _default_allocator;
836 return allocator->info.alloc (allocator, maxsize, align,
837 allocator->info.user_data);