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;
135 /* create a new memory block that manages the given memory */
136 static GstMemoryDefault *
137 _default_mem_new (GstMemoryFlags flags, GstMemory * parent, gpointer data,
138 GFreeFunc free_func, gsize maxsize, gsize offset, gsize size)
140 GstMemoryDefault *mem;
143 slice_size = sizeof (GstMemoryDefault);
145 mem = g_slice_alloc (slice_size);
146 _default_mem_init (mem, flags, parent, slice_size,
147 data, free_func, maxsize, offset, size);
152 /* allocate the memory and structure in one block */
153 static GstMemoryDefault *
154 _default_mem_new_block (gsize maxsize, gsize align, gsize offset, gsize size)
156 GstMemoryDefault *mem;
157 gsize aoffset, slice_size;
160 /* ensure configured alignment */
161 align |= gst_memory_alignment;
162 /* allocate more to compensate for alignment */
164 /* alloc header and data in one block */
165 slice_size = sizeof (GstMemoryDefault) + maxsize;
167 mem = g_slice_alloc (slice_size);
171 data = (guint8 *) mem + sizeof (GstMemoryDefault);
173 if ((aoffset = ((guintptr) data & align)))
174 aoffset = (align + 1) - aoffset;
176 _default_mem_init (mem, 0, NULL, slice_size, data, NULL, maxsize,
177 aoffset + offset, size);
183 _default_mem_alloc (const GstAllocator * allocator, gsize maxsize, gsize align)
185 return (GstMemory *) _default_mem_new_block (maxsize, align, 0, maxsize);
189 _default_mem_map (GstMemoryDefault * mem, GstMapFlags flags)
195 _default_mem_unmap (GstMemoryDefault * mem)
201 _default_mem_free (GstMemoryDefault * mem)
204 gst_memory_unref (mem->mem.parent);
207 mem->free_func (mem->data);
209 g_slice_free1 (mem->slice_size, mem);
212 static GstMemoryDefault *
213 _default_mem_copy (GstMemoryDefault * mem, gssize offset, gsize size)
215 GstMemoryDefault *copy;
218 size = mem->mem.size > offset ? mem->mem.size - offset : 0;
221 _default_mem_new_block (mem->mem.maxsize, 0, mem->mem.offset + offset,
223 memcpy (copy->data, mem->data, mem->mem.maxsize);
228 static GstMemoryDefault *
229 _default_mem_share (GstMemoryDefault * mem, gssize offset, gsize size)
231 GstMemoryDefault *sub;
234 /* find the real parent */
235 if ((parent = mem->mem.parent) == NULL)
236 parent = (GstMemory *) mem;
239 size = mem->mem.size - offset;
242 _default_mem_new (parent->flags, parent, mem->data, NULL,
243 mem->mem.maxsize, mem->mem.offset + offset, size);
249 _default_mem_is_span (GstMemoryDefault * mem1, GstMemoryDefault * mem2,
254 GstMemoryDefault *parent;
256 parent = (GstMemoryDefault *) mem1->mem.parent;
258 *offset = mem1->mem.offset - parent->mem.offset;
261 /* and memory is contiguous */
262 return mem1->data + mem1->mem.offset + mem1->mem.size ==
263 mem2->data + mem2->mem.offset;
267 _fallback_copy (GstMemory * mem, gssize offset, gssize size)
270 GstMapInfo sinfo, dinfo;
272 if (!gst_memory_map (mem, &sinfo, GST_MAP_READ))
276 size = sinfo.size > offset ? sinfo.size - offset : 0;
278 /* use the same allocator as the memory we copy */
279 copy = gst_allocator_alloc (mem->allocator, size, mem->align);
280 if (!gst_memory_map (copy, &dinfo, GST_MAP_WRITE)) {
281 GST_WARNING ("could not write map memory %p", copy);
282 gst_memory_unmap (mem, &sinfo);
286 memcpy (dinfo.data, sinfo.data + offset, size);
287 gst_memory_unmap (copy, &dinfo);
288 gst_memory_unmap (mem, &sinfo);
294 _fallback_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
300 static GHashTable *allocators;
303 _priv_gst_memory_initialize (void)
305 static const GstMemoryInfo _mem_info = {
306 (GstMemoryAllocFunction) _default_mem_alloc,
307 (GstMemoryMapFunction) _default_mem_map,
308 (GstMemoryUnmapFunction) _default_mem_unmap,
309 (GstMemoryFreeFunction) _default_mem_free,
310 (GstMemoryCopyFunction) _default_mem_copy,
311 (GstMemoryShareFunction) _default_mem_share,
312 (GstMemoryIsSpanFunction) _default_mem_is_span,
316 g_rw_lock_init (&lock);
317 allocators = g_hash_table_new (g_str_hash, g_str_equal);
319 #ifdef HAVE_GETPAGESIZE
320 #ifdef MEMORY_ALIGNMENT_PAGESIZE
321 gst_memory_alignment = getpagesize () - 1;
325 GST_DEBUG ("memory alignment: %" G_GSIZE_FORMAT, gst_memory_alignment);
327 _default_mem_impl = gst_allocator_register (GST_ALLOCATOR_SYSMEM, &_mem_info);
329 _default_allocator = _default_mem_impl;
333 * gst_memory_new_wrapped:
334 * @flags: #GstMemoryFlags
335 * @data: data to wrap
336 * @free_func: function to free @data
337 * @maxsize: allocated size of @data
338 * @offset: offset in @data
339 * @size: size of valid data
341 * Allocate a new memory block that wraps the given @data.
343 * Returns: a new #GstMemory.
346 gst_memory_new_wrapped (GstMemoryFlags flags, gpointer data,
347 GFreeFunc free_func, gsize maxsize, gsize offset, gsize size)
349 GstMemoryDefault *mem;
351 g_return_val_if_fail (data != NULL, NULL);
352 g_return_val_if_fail (offset + size <= maxsize, NULL);
354 mem = _default_mem_new (flags, NULL, data, free_func, maxsize, offset, size);
356 return (GstMemory *) mem;
363 * Increases the refcount of @mem.
365 * Returns: @mem with increased refcount
368 gst_memory_ref (GstMemory * mem)
370 g_return_val_if_fail (mem != NULL, NULL);
372 g_atomic_int_inc (&mem->refcount);
381 * Decreases the refcount of @mem. When the refcount reaches 0, the free
382 * function of @mem will be called.
385 gst_memory_unref (GstMemory * mem)
387 g_return_if_fail (mem != NULL);
388 g_return_if_fail (mem->allocator != NULL);
390 if (g_atomic_int_dec_and_test (&mem->refcount))
391 mem->allocator->info.free (mem);
395 * gst_memory_get_sizes:
397 * @offset: pointer to offset
398 * @maxsize: pointer to maxsize
400 * Get the current @size, @offset and @maxsize of @mem.
402 * Returns: the current sizes of @mem
405 gst_memory_get_sizes (GstMemory * mem, gsize * offset, gsize * maxsize)
407 g_return_val_if_fail (mem != NULL, 0);
410 *offset = mem->offset;
412 *maxsize = mem->maxsize;
420 * @offset: a new offset
423 * Resize the memory region. @mem should be writable and offset + size should be
424 * less than the maxsize of @mem.
427 gst_memory_resize (GstMemory * mem, gssize offset, gsize size)
429 g_return_if_fail (mem != NULL);
430 g_return_if_fail (gst_memory_is_writable (mem));
431 g_return_if_fail (offset >= 0 || mem->offset >= -offset);
432 g_return_if_fail (size + mem->offset + offset <= mem->maxsize);
434 mem->offset += offset;
439 * gst_memory_is_writable:
442 * Check if @mem is writable.
444 * Returns: %TRUE is @mem is writable.
447 gst_memory_is_writable (GstMemory * mem)
449 g_return_val_if_fail (mem != NULL, FALSE);
451 return (mem->refcount == 1) &&
452 ((mem->parent == NULL) || (mem->parent->refcount == 1)) &&
453 ((mem->flags & GST_MEMORY_FLAG_READONLY) == 0);
457 gst_memory_lock (GstMemory * mem, GstMapFlags flags)
459 gint access_mode, state, newstate;
461 access_mode = flags & 3;
464 state = g_atomic_int_get (&mem->state);
466 /* nothing mapped, set access_mode and refcount */
467 newstate = 4 | access_mode;
469 /* access_mode must match */
470 if ((state & access_mode) != access_mode)
472 /* increase refcount */
473 newstate = state + 4;
475 } while (!g_atomic_int_compare_and_exchange (&mem->state, state, newstate));
481 GST_DEBUG ("lock failed %p: state %d, access_mode %d", mem, state,
488 gst_memory_unlock (GstMemory * mem)
490 gint state, newstate;
493 state = g_atomic_int_get (&mem->state);
494 /* decrease the refcount */
495 newstate = state - 4;
496 /* last refcount, unset access_mode */
499 } while (!g_atomic_int_compare_and_exchange (&mem->state, state, newstate));
504 * gst_memory_make_mapped:
505 * @mem: (transfer full): a #GstMemory
506 * @info: (out): pointer for info
507 * @flags: mapping flags
509 * Create a #GstMemory object that is mapped with @flags. If @mem is mappable
510 * with @flags, this function returns the mapped @mem directly. Otherwise a
511 * mapped copy of @mem is returned.
513 * This function takes ownership of old @mem and returns a reference to a new
516 * Returns: (transfer full): a #GstMemory object mapped with @flags or NULL when
517 * a mapping is not possible.
520 gst_memory_make_mapped (GstMemory * mem, GstMapInfo * info, GstMapFlags flags)
524 if (gst_memory_map (mem, info, flags)) {
527 result = gst_memory_copy (mem, 0, -1);
531 if (!gst_memory_map (result, info, flags))
539 GST_DEBUG ("cannot copy memory %p", mem);
544 GST_DEBUG ("cannot map memory %p with flags %d", mem, flags);
552 * @info: (out): pointer for info
553 * @flags: mapping flags
555 * Fill @info with the pointer and sizes of the memory in @mem that can be
556 * accessed according to @flags.
558 * This function can return %FALSE for various reasons:
559 * - the memory backed by @mem is not accessible with the given @flags.
560 * - the memory was already mapped with a different mapping.
562 * @info and its contents remains valid for as long as @mem is alive and until
563 * gst_memory_unmap() is called.
565 * For each gst_memory_map() call, a corresponding gst_memory_unmap() call
568 * Returns: %TRUE if the map operation was successful.
571 gst_memory_map (GstMemory * mem, GstMapInfo * info, GstMapFlags flags)
573 g_return_val_if_fail (mem != NULL, FALSE);
574 g_return_val_if_fail (info != NULL, FALSE);
576 if (!gst_memory_lock (mem, flags))
579 info->data = mem->allocator->info.map (mem, mem->maxsize, flags);
581 if (G_UNLIKELY (info->data == NULL))
586 info->size = mem->size;
587 info->maxsize = mem->maxsize - mem->offset;
588 info->data = info->data + mem->offset;
595 GST_DEBUG ("mem %p: lock %d failed", flags);
600 /* something went wrong, restore the orginal state again */
601 GST_ERROR ("mem %p: map failed", mem);
602 gst_memory_unlock (mem);
610 * @info: a #GstMapInfo
612 * Release the memory obtained with gst_memory_map()
615 gst_memory_unmap (GstMemory * mem, GstMapInfo * info)
617 g_return_if_fail (mem != NULL);
618 g_return_if_fail (info != NULL);
619 g_return_if_fail (info->memory == mem);
620 /* there must be a ref */
621 g_return_if_fail (g_atomic_int_get (&mem->state) >= 4);
623 mem->allocator->info.unmap (mem);
624 gst_memory_unlock (mem);
630 * @offset: an offset to copy
631 * @size: size to copy or -1 to copy all bytes from offset
633 * Return a copy of @size bytes from @mem starting from @offset. This copy is
634 * guaranteed to be writable. @size can be set to -1 to return a copy all bytes
637 * Returns: a new #GstMemory.
640 gst_memory_copy (GstMemory * mem, gssize offset, gssize size)
644 g_return_val_if_fail (mem != NULL, NULL);
645 g_return_val_if_fail (gst_memory_lock (mem, GST_MAP_READ), NULL);
647 copy = mem->allocator->info.copy (mem, offset, size);
649 gst_memory_unlock (mem);
657 * @offset: an offset to share
658 * @size: size to share or -1 to share bytes from offset
660 * Return a shared copy of @size bytes from @mem starting from @offset. No
661 * memory copy is performed and the memory region is simply shared. The result
662 * is guaranteed to be not-writable. @size can be set to -1 to return a share
663 * all bytes from @offset.
665 * Returns: a new #GstMemory.
668 gst_memory_share (GstMemory * mem, gssize offset, gssize size)
670 g_return_val_if_fail (mem != NULL, NULL);
672 return mem->allocator->info.share (mem, offset, size);
676 * gst_memory_is_span:
677 * @mem1: a #GstMemory
678 * @mem2: a #GstMemory
679 * @offset: a pointer to a result offset
681 * Check if @mem1 and mem2 share the memory with a common parent memory object
682 * and that the memory is contiguous.
684 * If this is the case, the memory of @mem1 and @mem2 can be merged
685 * efficiently by performing gst_memory_share() on the parent object from
686 * the returned @offset.
688 * Returns: %TRUE if the memory is contiguous and of a common parent.
691 gst_memory_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
693 g_return_val_if_fail (mem1 != NULL, FALSE);
694 g_return_val_if_fail (mem2 != NULL, FALSE);
696 /* need to have the same allocators */
697 if (mem1->allocator != mem2->allocator)
700 /* need to have the same parent */
701 if (mem1->parent == NULL || mem1->parent != mem2->parent)
704 /* and memory is contiguous */
705 if (!mem1->allocator->info.is_span (mem1, mem2, offset))
712 * gst_allocator_register:
713 * @name: the name of the allocator
714 * @info: #GstMemoryInfo
716 * Registers the memory allocator with @name and implementation functions
719 * All functions in @info are mandatory exept the copy and is_span
720 * functions, which will have a default implementation when left NULL.
722 * The user_data field in @info will be passed to all calls of the alloc
725 * Returns: a new #GstAllocator.
728 gst_allocator_register (const gchar * name, const GstMemoryInfo * info)
730 GstAllocator *allocator;
732 #define INSTALL_FALLBACK(_t) \
733 if (allocator->info._t == NULL) allocator->info._t = _fallback_ ##_t;
735 g_return_val_if_fail (name != NULL, NULL);
736 g_return_val_if_fail (info != NULL, NULL);
737 g_return_val_if_fail (info->alloc != NULL, NULL);
738 g_return_val_if_fail (info->map != NULL, NULL);
739 g_return_val_if_fail (info->unmap != NULL, NULL);
740 g_return_val_if_fail (info->free != NULL, NULL);
741 g_return_val_if_fail (info->share != NULL, NULL);
743 allocator = g_slice_new (GstAllocator);
744 allocator->name = g_quark_from_string (name);
745 allocator->info = *info;
746 INSTALL_FALLBACK (copy);
747 INSTALL_FALLBACK (is_span);
748 #undef INSTALL_FALLBACK
750 GST_DEBUG ("registering allocator \"%s\"", name);
752 g_rw_lock_writer_lock (&lock);
753 g_hash_table_insert (allocators, (gpointer) name, (gpointer) allocator);
754 g_rw_lock_writer_unlock (&lock);
760 * gst_allocator_find:
761 * @name: the name of the allocator
763 * Find a previously registered allocator with @name. When @name is NULL, the
764 * default allocator will be returned.
766 * Returns: a #GstAllocator or NULL when the allocator with @name was not
770 gst_allocator_find (const gchar * name)
772 const GstAllocator *allocator;
774 g_rw_lock_reader_lock (&lock);
776 allocator = g_hash_table_lookup (allocators, (gconstpointer) name);
778 allocator = _default_allocator;
780 g_rw_lock_reader_unlock (&lock);
786 * gst_allocator_set_default:
787 * @allocator: a #GstAllocator
789 * Set the default allocator.
792 gst_allocator_set_default (const GstAllocator * allocator)
794 g_return_if_fail (allocator != NULL);
796 g_rw_lock_writer_lock (&lock);
797 _default_allocator = allocator;
798 g_rw_lock_writer_unlock (&lock);
802 * gst_allocator_alloc:
803 * @allocator: (transfer none) (allow-none): a #GstAllocator to use
804 * @maxsize: allocated size of @data
805 * @align: alignment for the data
807 * Use @allocator to allocate a new memory block with memory that is at least
808 * @maxsize big and has the given alignment.
810 * When @allocator is NULL, the default allocator will be used.
812 * @align is given as a bitmask so that @align + 1 equals the amount of bytes to
813 * align to. For example, to align to 8 bytes, use an alignment of 7.
815 * Returns: (transfer full): a new #GstMemory.
818 gst_allocator_alloc (const GstAllocator * allocator, gsize maxsize, gsize align)
820 g_return_val_if_fail (((align + 1) & align) == 0, NULL);
822 if (allocator == NULL)
823 allocator = _default_allocator;
825 return allocator->info.alloc (allocator, maxsize, align,
826 allocator->info.user_data);