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 * Memory is usually created by allocators with a gst_allocator_alloc()
31 * method call. When NULL is used as the allocator, the default allocator will
34 * New allocators can be registered with gst_allocator_register().
35 * Allocators are identified by name and can be retrieved with
36 * gst_allocator_find().
38 * New memory can be created with gst_memory_new_wrapped() that wraps the memory
39 * allocated elsewhere.
41 * Refcounting of the memory block is performed with gst_memory_ref() and
44 * The size of the memory can be retrieved and changed with
45 * gst_memory_get_sizes() and gst_memory_resize() respectively.
47 * Getting access to the data of the memory is performed with gst_memory_map().
48 * After the memory access is completed, gst_memory_unmap() should be called.
50 * Memory can be copied with gst_memory_copy(), which will returnn a writable
51 * copy. gst_memory_share() will create a new memory block that shares the
52 * memory with an existing memory block at a custom offset and with a custom
55 * Memory can be efficiently merged when gst_memory_is_span() returns TRUE.
57 * Last reviewed on 2011-06-08 (0.11.0)
61 #include "gst_private.h"
62 #include "gstmemory.h"
64 G_DEFINE_BOXED_TYPE (GstMemory, gst_memory, (GBoxedCopyFunc) gst_memory_ref,
65 (GBoxedFreeFunc) gst_memory_unref);
68 * gst_memory_alignment:
70 * The default memory alignment in bytes - 1
71 * an alignment of 7 would be the same as what malloc() guarantees.
73 #if defined(MEMORY_ALIGNMENT_MALLOC)
74 size_t gst_memory_alignment = 7;
75 #elif defined(MEMORY_ALIGNMENT_PAGESIZE)
76 /* we fill this in in the _init method */
77 size_t gst_memory_alignment = 0;
78 #elif defined(MEMORY_ALIGNMENT)
79 size_t gst_memory_alignment = MEMORY_ALIGNMENT - 1;
81 #error "No memory alignment configured"
82 size_t gst_memory_alignment = 0;
92 /* default memory implementation */
104 /* the default allocator */
105 static const GstAllocator *_default_allocator;
107 /* our predefined allocators */
108 static const GstAllocator *_default_mem_impl;
110 /* initialize the fields */
112 _default_mem_init (GstMemoryDefault * mem, GstMemoryFlags flags,
113 GstMemory * parent, gsize slice_size, gpointer data,
114 GFreeFunc free_func, gsize maxsize, gsize offset, gsize size)
116 mem->mem.allocator = _default_mem_impl;
117 mem->mem.flags = flags;
118 mem->mem.refcount = 1;
119 mem->mem.parent = parent ? gst_memory_ref (parent) : NULL;
120 mem->slice_size = slice_size;
122 mem->free_func = free_func;
123 mem->maxsize = maxsize;
124 mem->offset = offset;
128 /* create a new memory block that manages the given memory */
129 static GstMemoryDefault *
130 _default_mem_new (GstMemoryFlags flags, GstMemory * parent, gpointer data,
131 GFreeFunc free_func, gsize maxsize, gsize offset, gsize size)
133 GstMemoryDefault *mem;
136 slice_size = sizeof (GstMemoryDefault);
138 mem = g_slice_alloc (slice_size);
139 _default_mem_init (mem, flags, parent, slice_size,
140 data, free_func, maxsize, offset, size);
145 /* allocate the memory and structure in one block */
146 static GstMemoryDefault *
147 _default_mem_new_block (gsize maxsize, gsize align, gsize offset, gsize size)
149 GstMemoryDefault *mem;
150 gsize aoffset, slice_size;
153 /* ensure configured alignment */
154 align |= gst_memory_alignment;
155 /* allocate more to compensate for alignment */
157 /* alloc header and data in one block */
158 slice_size = sizeof (GstMemoryDefault) + maxsize;
160 mem = g_slice_alloc (slice_size);
164 data = (guint8 *) mem + sizeof (GstMemoryDefault);
166 if ((aoffset = ((guintptr) data & align)))
167 aoffset = (align + 1) - aoffset;
169 _default_mem_init (mem, 0, NULL, slice_size, data, NULL, maxsize,
170 aoffset + offset, size);
176 _default_mem_alloc (const GstAllocator * allocator, gsize maxsize, gsize align)
178 return (GstMemory *) _default_mem_new_block (maxsize, align, 0, maxsize);
182 _default_mem_get_sizes (GstMemoryDefault * mem, gsize * offset, gsize * maxsize)
185 *offset = mem->offset;
187 *maxsize = mem->maxsize;
193 _default_mem_resize (GstMemoryDefault * mem, gssize offset, gsize size)
195 g_return_if_fail (offset >= 0 || mem->offset >= -offset);
196 g_return_if_fail (size + mem->offset + offset <= mem->maxsize);
198 mem->offset += offset;
203 _default_mem_map (GstMemoryDefault * mem, gsize * size, gsize * maxsize,
209 *maxsize = mem->maxsize - mem->offset;
211 return mem->data + mem->offset;
215 _default_mem_unmap (GstMemoryDefault * mem, gpointer data, gsize size)
217 g_return_val_if_fail ((guint8 *) data >= mem->data
218 && (guint8 *) data < mem->data + mem->maxsize, FALSE);
220 if (mem->data + mem->offset != data) {
221 gsize newoffset = (guint8 *) data - mem->data;
223 size = mem->offset + mem->size - newoffset;
224 mem->offset = newoffset;
228 g_return_val_if_fail (mem->offset + size <= mem->maxsize, FALSE);
236 _default_mem_free (GstMemoryDefault * mem)
239 gst_memory_unref (mem->mem.parent);
242 mem->free_func (mem->data);
244 g_slice_free1 (mem->slice_size, mem);
247 static GstMemoryDefault *
248 _default_mem_copy (GstMemoryDefault * mem, gssize offset, gsize size)
250 GstMemoryDefault *copy;
253 size = mem->size > offset ? mem->size - offset : 0;
255 copy = _default_mem_new_block (mem->maxsize, 0, mem->offset + offset, size);
256 memcpy (copy->data, mem->data, mem->maxsize);
261 static GstMemoryDefault *
262 _default_mem_share (GstMemoryDefault * mem, gssize offset, gsize size)
264 GstMemoryDefault *sub;
267 /* find the real parent */
268 if ((parent = mem->mem.parent) == NULL)
269 parent = (GstMemory *) mem;
272 size = mem->size - offset;
274 sub = _default_mem_new (parent->flags, parent, mem->data, NULL, mem->maxsize,
275 mem->offset + offset, size);
281 _default_mem_is_span (GstMemoryDefault * mem1, GstMemoryDefault * mem2,
286 GstMemoryDefault *parent;
288 parent = (GstMemoryDefault *) mem1->mem.parent;
290 *offset = mem1->offset - parent->offset;
293 /* and memory is contiguous */
294 return mem1->data + mem1->offset + mem1->size == mem2->data + mem2->offset;
298 _fallback_copy (GstMemory * mem, gssize offset, gssize size)
304 data = gst_memory_map (mem, &msize, NULL, GST_MAP_READ);
306 size = msize > offset ? msize - offset : 0;
307 /* use the same allocator as the memory we copy, FIXME, alignment? */
308 copy = gst_allocator_alloc (mem->allocator, size, 0);
309 dest = gst_memory_map (copy, NULL, NULL, GST_MAP_WRITE);
310 memcpy (dest, data + offset, size);
311 gst_memory_unmap (copy, dest, size);
313 gst_memory_unmap (mem, data, msize);
315 return (GstMemory *) copy;
319 _fallback_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
324 static GStaticRWLock lock = G_STATIC_RW_LOCK_INIT;
325 static GHashTable *allocators;
328 _priv_gst_memory_initialize (void)
330 static const GstMemoryInfo _mem_info = {
331 (GstMemoryAllocFunction) _default_mem_alloc,
332 (GstMemoryGetSizesFunction) _default_mem_get_sizes,
333 (GstMemoryResizeFunction) _default_mem_resize,
334 (GstMemoryMapFunction) _default_mem_map,
335 (GstMemoryUnmapFunction) _default_mem_unmap,
336 (GstMemoryFreeFunction) _default_mem_free,
337 (GstMemoryCopyFunction) _default_mem_copy,
338 (GstMemoryShareFunction) _default_mem_share,
339 (GstMemoryIsSpanFunction) _default_mem_is_span,
343 allocators = g_hash_table_new (g_str_hash, g_str_equal);
345 #ifdef HAVE_GETPAGESIZE
346 #ifdef MEMORY_ALIGNMENT_PAGESIZE
347 gst_memory_alignment = getpagesize () - 1;
351 GST_DEBUG ("memory alignment: %" G_GSIZE_FORMAT, gst_memory_alignment);
353 _default_mem_impl = gst_allocator_register (GST_ALLOCATOR_SYSMEM, &_mem_info);
355 _default_allocator = _default_mem_impl;
359 * gst_memory_new_wrapped:
360 * @flags: #GstMemoryFlags
361 * @data: data to wrap
362 * @free_func: function to free @data
363 * @maxsize: allocated size of @data
364 * @offset: offset in @data
365 * @size: size of valid data
367 * Allocate a new memory block that wraps the given @data.
369 * Returns: a new #GstMemory.
372 gst_memory_new_wrapped (GstMemoryFlags flags, gpointer data,
373 GFreeFunc free_func, gsize maxsize, gsize offset, gsize size)
375 GstMemoryDefault *mem;
377 g_return_val_if_fail (data != NULL, NULL);
378 g_return_val_if_fail (offset + size <= maxsize, NULL);
380 mem = _default_mem_new (flags, NULL, data, free_func, maxsize, offset, size);
382 return (GstMemory *) mem;
389 * Increases the refcount of @mem.
391 * Returns: @mem with increased refcount
394 gst_memory_ref (GstMemory * mem)
396 g_return_val_if_fail (mem != NULL, NULL);
398 g_atomic_int_inc (&mem->refcount);
407 * Decreases the refcount of @mem. When the refcount reaches 0, the free
408 * function of @mem will be called.
411 gst_memory_unref (GstMemory * mem)
413 g_return_if_fail (mem != NULL);
414 g_return_if_fail (mem->allocator != NULL);
416 if (g_atomic_int_dec_and_test (&mem->refcount))
417 mem->allocator->info.free (mem);
421 * gst_memory_get_sizes:
423 * @offset: pointer to offset
424 * @maxsize: pointer to maxsize
426 * Get the current @size, @offset and @maxsize of @mem.
428 * Returns: the current sizes of @mem
431 gst_memory_get_sizes (GstMemory * mem, gsize * offset, gsize * maxsize)
433 g_return_val_if_fail (mem != NULL, 0);
435 return mem->allocator->info.get_sizes (mem, offset, maxsize);
441 * @offset: a new offset
444 * Resize the memory region. @mem should be writable and offset + size should be
445 * less than the maxsize of @mem.
448 gst_memory_resize (GstMemory * mem, gssize offset, gsize size)
450 g_return_if_fail (mem != NULL);
451 g_return_if_fail (GST_MEMORY_IS_WRITABLE (mem));
453 mem->allocator->info.resize (mem, offset, size);
459 * @size: (out) (allow-none): pointer for size
460 * @maxsize: (out) (allow-none): pointer for maxsize
461 * @flags: mapping flags
463 * Get a pointer to the memory of @mem that can be accessed according to @flags.
465 * @size and @maxsize will contain the size of the memory and the maximum
466 * allocated memory of @mem respectively. They can be set to NULL.
468 * Returns: (transfer none): a pointer to the memory of @mem.
471 gst_memory_map (GstMemory * mem, gsize * size, gsize * maxsize,
474 g_return_val_if_fail (mem != NULL, NULL);
475 g_return_val_if_fail (!(flags & GST_MAP_WRITE) ||
476 GST_MEMORY_IS_WRITABLE (mem), NULL);
478 return mem->allocator->info.map (mem, size, maxsize, flags);
484 * @data: data to unmap
485 * @size: new size of @mem, or -1
487 * Release the memory pointer obtained with gst_memory_map() and set the size of
488 * the memory to @size. @size can be set to -1 when the size should not be
491 * It is possible to pass a different @data than that obtained from
492 * gst_memory_map() in which case the offset of @mem will be updated.
494 * Returns: TRUE when the memory was release successfully.
497 gst_memory_unmap (GstMemory * mem, gpointer data, gssize size)
499 g_return_val_if_fail (mem != NULL, FALSE);
501 return mem->allocator->info.unmap (mem, data, size);
507 * @offset: an offset to copy
508 * @size: size to copy or -1 to copy all bytes from offset
510 * Return a copy of @size bytes from @mem starting from @offset. This copy is
511 * guaranteed to be writable. @size can be set to -1 to return a copy all bytes
514 * Returns: a new #GstMemory.
517 gst_memory_copy (GstMemory * mem, gssize offset, gssize size)
519 g_return_val_if_fail (mem != NULL, NULL);
521 return mem->allocator->info.copy (mem, offset, size);
527 * @offset: an offset to share
528 * @size: size to share or -1 to share bytes from offset
530 * Return a shared copy of @size bytes from @mem starting from @offset. No
531 * memory copy is performed and the memory region is simply shared. The result
532 * is guaranteed to be not-writable. @size can be set to -1 to return a share
533 * all bytes from @offset.
535 * Returns: a new #GstMemory.
538 gst_memory_share (GstMemory * mem, gssize offset, gssize size)
540 g_return_val_if_fail (mem != NULL, NULL);
542 return mem->allocator->info.share (mem, offset, size);
546 * gst_memory_is_span:
547 * @mem1: a #GstMemory
548 * @mem2: a #GstMemory
549 * @offset: a pointer to a result offset
551 * Check if @mem1 and mem2 share the memory with a common parent memory object
552 * and that the memory is contiguous.
554 * If this is the case, the memory of @mem1 and @mem2 can be merged
555 * efficiently by performing gst_memory_share() on the parent object from
556 * the returned @offset.
558 * Returns: %TRUE if the memory is contiguous and of a common parent.
561 gst_memory_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
563 g_return_val_if_fail (mem1 != NULL, FALSE);
564 g_return_val_if_fail (mem2 != NULL, FALSE);
566 /* need to have the same allocators */
567 if (mem1->allocator != mem2->allocator)
570 /* need to have the same parent */
571 if (mem1->parent == NULL || mem1->parent != mem2->parent)
574 /* and memory is contiguous */
575 if (!mem1->allocator->info.is_span (mem1, mem2, offset))
582 * gst_allocator_register:
583 * @name: the name of the allocator
584 * @info: #GstMemoryInfo
586 * Registers the memory allocator with @name and implementation functions
589 * All functions in @info are mandatory exept the copy and is_span
590 * functions, which will have a default implementation when left NULL.
592 * The user_data field in @info will be passed to all calls of the alloc
595 * Returns: a new #GstAllocator.
598 gst_allocator_register (const gchar * name, const GstMemoryInfo * info)
600 GstAllocator *allocator;
602 #define INSTALL_FALLBACK(_t) \
603 if (allocator->info._t == NULL) allocator->info._t = _fallback_ ##_t;
605 g_return_val_if_fail (name != NULL, NULL);
606 g_return_val_if_fail (info != NULL, NULL);
607 g_return_val_if_fail (info->alloc != NULL, NULL);
608 g_return_val_if_fail (info->get_sizes != NULL, NULL);
609 g_return_val_if_fail (info->resize != NULL, NULL);
610 g_return_val_if_fail (info->map != NULL, NULL);
611 g_return_val_if_fail (info->unmap != NULL, NULL);
612 g_return_val_if_fail (info->free != NULL, NULL);
613 g_return_val_if_fail (info->share != NULL, NULL);
615 allocator = g_slice_new (GstAllocator);
616 allocator->name = g_quark_from_string (name);
617 allocator->info = *info;
618 INSTALL_FALLBACK (copy);
619 INSTALL_FALLBACK (is_span);
620 #undef INSTALL_FALLBACK
622 GST_DEBUG ("registering allocator \"%s\"", name);
624 g_static_rw_lock_writer_lock (&lock);
625 g_hash_table_insert (allocators, (gpointer) name, (gpointer) allocator);
626 g_static_rw_lock_writer_unlock (&lock);
632 * gst_allocator_find:
633 * @name: the name of the allocator
635 * Find a previously registered allocator with @name. When @name is NULL, the
636 * default allocator will be returned.
638 * Returns: a #GstAllocator or NULL when the allocator with @name was not
642 gst_allocator_find (const gchar * name)
644 const GstAllocator *allocator;
646 g_static_rw_lock_reader_lock (&lock);
648 allocator = g_hash_table_lookup (allocators, (gconstpointer) name);
650 allocator = _default_allocator;
652 g_static_rw_lock_reader_unlock (&lock);
658 * gst_allocator_set_default:
659 * @allocator: a #GstAllocator
661 * Set the default allocator.
664 gst_allocator_set_default (const GstAllocator * allocator)
666 g_return_if_fail (allocator != NULL);
668 g_static_rw_lock_writer_lock (&lock);
669 _default_allocator = allocator;
670 g_static_rw_lock_writer_unlock (&lock);
674 * gst_allocator_alloc:
675 * @allocator: (transfer none) (allow-none): a #GstAllocator to use
676 * @maxsize: allocated size of @data
677 * @align: alignment for the data
679 * Use @allocator to allocate a new memory block with memory that is at least
680 * @maxsize big and has the given alignment.
682 * When @allocator is NULL, the default allocator will be used.
684 * @align is given as a bitmask so that @align + 1 equals the amount of bytes to
685 * align to. For example, to align to 8 bytes, use an alignment of 7.
687 * Returns: (transfer full): a new #GstMemory.
690 gst_allocator_alloc (const GstAllocator * allocator, gsize maxsize, gsize align)
692 g_return_val_if_fail (((align + 1) & align) == 0, NULL);
694 if (allocator == NULL)
695 allocator = _default_allocator;
697 return allocator->info.alloc (allocator, maxsize, align,
698 allocator->info.user_data);