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 * New memory can be created with gst_memory_new_wrapped() that wraps the memory
31 * allocated elsewhere and gst_memory_new_alloc() that creates a new GstMemory
32 * and the memory inside it.
34 * Refcounting of the memory block is performed with gst_memory_ref() and
37 * The size of the memory can be retrieved and changed with
38 * gst_memory_get_sizes() and gst_memory_resize() respectively.
40 * Getting access to the data of the memory is performed with gst_memory_map().
41 * After the memory access is completed, gst_memory_unmap() should be called.
43 * Memory can be copied with gst_memory_copy(), which will returnn a writable
44 * copy. gst_memory_share() will create a new memory block that shares the
45 * memory with an existing memory block at a custom offset and with a custom
48 * Memory can be efficiently merged when gst_memory_is_span() returns TRUE and
49 * with the function gst_memory_span().
51 * Last reviewed on 2011-03-30 (0.11.0)
55 #include "gst_private.h"
56 #include "gstmemory.h"
66 /* default memory implementation */
78 static const GstMemoryImpl *_default_mem_impl;
79 static const GstMemoryImpl *_default_share_impl;
81 /* initialize the fields */
83 _default_mem_init (GstMemoryDefault * mem, GstMemoryFlags flags,
84 GstMemory * parent, gsize slice_size, gpointer data,
85 GFreeFunc free_func, gsize maxsize, gsize offset, gsize size)
87 mem->mem.impl = data ? _default_mem_impl : _default_share_impl;
88 mem->mem.flags = flags;
89 mem->mem.refcount = 1;
90 mem->mem.parent = parent ? gst_memory_ref (parent) : NULL;
91 mem->slice_size = slice_size;
93 mem->free_func = free_func;
94 mem->maxsize = maxsize;
99 /* create a new memory block that manages the given memory */
100 static GstMemoryDefault *
101 _default_mem_new (GstMemoryFlags flags, GstMemory * parent, gpointer data,
102 GFreeFunc free_func, gsize maxsize, gsize offset, gsize size)
104 GstMemoryDefault *mem;
107 slice_size = sizeof (GstMemoryDefault);
109 mem = g_slice_alloc (slice_size);
110 _default_mem_init (mem, flags, parent, slice_size,
111 data, free_func, maxsize, offset, size);
116 /* allocate the memory and structure in one block */
117 static GstMemoryDefault *
118 _default_mem_new_block (gsize maxsize, gsize align, gsize offset, gsize size)
120 GstMemoryDefault *mem;
121 gsize aoffset, slice_size;
124 /* alloc header and data in one block */
125 slice_size = sizeof (GstMemoryDefault) + maxsize + align;
127 mem = g_slice_alloc (slice_size);
131 data = (guint8 *) mem + sizeof (GstMemoryDefault);
133 if ((aoffset = ((guintptr) data & align)))
134 aoffset = align - aoffset;
136 _default_mem_init (mem, 0, NULL, slice_size, data, NULL, maxsize + align,
137 aoffset + offset, size);
143 _default_mem_get_sizes (GstMemoryDefault * mem, gsize * maxsize)
146 *maxsize = mem->maxsize;
152 _default_mem_resize (GstMemoryDefault * mem, gsize offset, gsize size)
154 g_return_if_fail (size + mem->offset + offset <= mem->maxsize);
156 mem->offset += offset;
161 _default_mem_map (GstMemoryDefault * mem, gsize * size, gsize * maxsize,
167 *maxsize = mem->maxsize;
169 return mem->data + mem->offset;
173 _default_share_map (GstMemoryDefault * mem, gsize * size, gsize * maxsize,
178 data = gst_memory_map (mem->mem.parent, size, maxsize, flags);
183 *maxsize -= mem->offset;
185 return data + mem->offset;
189 _default_mem_unmap (GstMemoryDefault * mem, gpointer data, gsize size)
197 _default_share_unmap (GstMemoryDefault * mem, gpointer data, gsize size)
205 size = mem->size - mem->offset;
208 gst_memory_unmap (mem->mem.parent, ptr - mem->offset, size + mem->offset);
214 _default_mem_free (GstMemoryDefault * mem)
217 gst_memory_unref (mem->mem.parent);
220 mem->free_func (mem->data);
222 g_slice_free1 (mem->slice_size, mem);
225 static GstMemoryDefault *
226 _default_mem_copy (GstMemoryDefault * mem, gsize offset, gsize size)
228 GstMemoryDefault *copy;
231 size = mem->size > offset ? mem->size - offset : 0;
233 copy = _default_mem_new_block (mem->maxsize, 0, mem->offset + offset, size);
234 memcpy (copy->data, mem->data, mem->maxsize);
239 static GstMemoryDefault *
240 _default_mem_share (GstMemoryDefault * mem, gsize 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->size - offset;
252 sub = _default_mem_new (parent->flags, parent, mem->data, NULL, mem->maxsize,
253 mem->offset + offset, size);
259 _default_mem_is_span (GstMemoryDefault * mem1, GstMemoryDefault * mem2,
263 *offset = mem1->offset;
265 /* and memory is contiguous */
266 return mem1->data + mem1->offset + mem1->size == mem2->data + mem2->offset;
270 _fallback_copy (GstMemory * mem, gsize offset, gsize size)
272 GstMemoryDefault *copy;
276 data = gst_memory_map (mem, &msize, NULL, GST_MAP_READ);
278 size = msize > offset ? msize - offset : 0;
279 copy = _default_mem_new_block (size, 0, 0, size);
280 memcpy (copy->data, data + offset, size);
281 gst_memory_unmap (mem, data, msize);
283 return (GstMemory *) copy;
287 _fallback_share (GstMemory * mem, gsize offset, gsize size)
289 GstMemoryDefault *sub;
292 /* find the real parent */
293 parent = mem->parent ? mem->parent : mem;
295 sub = _default_mem_new (0, parent, NULL, NULL, size, offset, size);
297 return (GstMemory *) sub;
301 _fallback_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
307 _gst_memory_init (void)
309 static const GstMemoryInfo _mem_info = {
310 (GstMemoryGetSizesFunction) _default_mem_get_sizes,
311 (GstMemoryResizeFunction) _default_mem_resize,
312 (GstMemoryMapFunction) _default_mem_map,
313 (GstMemoryUnmapFunction) _default_mem_unmap,
314 (GstMemoryFreeFunction) _default_mem_free,
315 (GstMemoryCopyFunction) _default_mem_copy,
316 (GstMemoryShareFunction) _default_mem_share,
317 (GstMemoryIsSpanFunction) _default_mem_is_span
319 static const GstMemoryInfo _share_info = {
320 (GstMemoryGetSizesFunction) _default_mem_get_sizes,
321 (GstMemoryResizeFunction) _default_mem_resize,
322 (GstMemoryMapFunction) _default_share_map,
323 (GstMemoryUnmapFunction) _default_share_unmap,
324 (GstMemoryFreeFunction) _default_mem_free,
330 _default_mem_impl = gst_memory_register ("GstMemoryDefault", &_mem_info);
331 _default_share_impl =
332 gst_memory_register ("GstMemorySharebuffer", &_share_info);
336 * gst_memory_register:
337 * @name: the name of the implementation
338 * @info: #GstMemoryInfo
340 * Registers the memory implementation with @name and implementation functions
343 * Returns: a new #GstMemoryImpl.
345 const GstMemoryImpl *
346 gst_memory_register (const gchar * name, const GstMemoryInfo * info)
350 #define INSTALL_FALLBACK(_t) \
351 if (impl->info._t == NULL) impl->info._t = _fallback_ ##_t;
353 g_return_val_if_fail (name != NULL, NULL);
354 g_return_val_if_fail (info != NULL, NULL);
355 g_return_val_if_fail (info->get_sizes != NULL, NULL);
356 g_return_val_if_fail (info->resize != NULL, NULL);
357 g_return_val_if_fail (info->map != NULL, NULL);
358 g_return_val_if_fail (info->unmap != NULL, NULL);
359 g_return_val_if_fail (info->free != NULL, NULL);
361 impl = g_slice_new (GstMemoryImpl);
362 impl->name = g_quark_from_string (name);
364 INSTALL_FALLBACK (copy);
365 INSTALL_FALLBACK (share);
366 INSTALL_FALLBACK (is_span);
368 GST_DEBUG ("register \"%s\" of size %" G_GSIZE_FORMAT, name);
371 g_static_rw_lock_writer_lock (&lock);
372 g_hash_table_insert (memoryimpl, (gpointer) name, (gpointer) impl);
373 g_static_rw_lock_writer_unlock (&lock);
375 #undef INSTALL_FALLBACK
381 * gst_memory_new_wrapped:
382 * @flags: #GstMemoryFlags
383 * @data: data to wrap
384 * @free_func: function to free @data
385 * @maxsize: allocated size of @data
386 * @offset: offset in @data
387 * @size: size of valid data
389 * Allocate a new memory block that wraps the given @data.
391 * Returns: a new #GstMemory.
394 gst_memory_new_wrapped (GstMemoryFlags flags, gpointer data,
395 GFreeFunc free_func, gsize maxsize, gsize offset, gsize size)
397 GstMemoryDefault *mem;
399 g_return_val_if_fail (data != NULL, NULL);
400 g_return_val_if_fail (offset + size <= maxsize, NULL);
402 mem = _default_mem_new (flags, NULL, data, free_func, maxsize, offset, size);
404 return (GstMemory *) mem;
408 * gst_memory_new_alloc:
409 * @maxsize: allocated size of @data
410 * @align: alignment for the data
412 * Allocate a new memory block with memory that is at least @maxsize big and las
413 * the given alignment.
415 * Returns: a new #GstMemory.
418 gst_memory_new_alloc (gsize maxsize, gsize align)
420 GstMemoryDefault *mem;
422 mem = _default_mem_new_block (maxsize, align, 0, maxsize);
424 return (GstMemory *) mem;
431 * Increases the refcount of @mem.
433 * Returns: @mem with increased refcount
436 gst_memory_ref (GstMemory * mem)
438 g_return_val_if_fail (mem != NULL, NULL);
440 g_atomic_int_inc (&mem->refcount);
449 * Decreases the refcount of @mem. When the refcount reaches 0, the free
450 * function of @mem will be called.
453 gst_memory_unref (GstMemory * mem)
455 g_return_if_fail (mem != NULL);
456 g_return_if_fail (mem->impl != NULL);
458 if (g_atomic_int_dec_and_test (&mem->refcount))
459 mem->impl->info.free (mem);
463 * gst_memory_get_sizes:
465 * @maxsize: pointer to maxsize
467 * Get the current @size and @maxsize of @mem.
469 * Returns: the current sizes of @mem
472 gst_memory_get_sizes (GstMemory * mem, gsize * maxsize)
474 g_return_val_if_fail (mem != NULL, 0);
476 return mem->impl->info.get_sizes (mem, maxsize);
482 * @offset: a new offset
485 * Resize the memory region. @mem should be writable and offset + size should be
486 * less than the maxsize of @mem.
489 gst_memory_resize (GstMemory * mem, gsize offset, gsize size)
491 g_return_if_fail (mem != NULL);
492 g_return_if_fail (GST_MEMORY_IS_WRITABLE (mem));
494 mem->impl->info.resize (mem, offset, size);
500 * @size: pointer for size
501 * @maxsize: pointer for maxsize
502 * @flags: mapping flags
504 * Get a pointer to the memory of @mem that can be accessed according to @flags.
506 * @size and @maxsize will contain the size of the memory and the maximum
507 * allocated memory of @mem respectively. They can be set to NULL.
509 * Returns: a pointer to the memory of @mem.
512 gst_memory_map (GstMemory * mem, gsize * size, gsize * maxsize,
515 g_return_val_if_fail (mem != NULL, NULL);
516 g_return_val_if_fail (!(flags & GST_MAP_WRITE) ||
517 GST_MEMORY_IS_WRITABLE (mem), NULL);
519 return mem->impl->info.map (mem, size, maxsize, flags);
525 * @data: data to unmap
526 * @size: new size of @mem
528 * Release the memory pointer obtained with gst_memory_map() and set the size of
529 * the memory to @size. @size can be set to -1 when the size should not be
532 * Returns: TRUE when the memory was release successfully.
535 gst_memory_unmap (GstMemory * mem, gpointer data, gsize size)
537 g_return_val_if_fail (mem != NULL, FALSE);
539 return mem->impl->info.unmap (mem, data, size);
545 * @offset: an offset to copy
546 * @size: size to copy
548 * Return a copy of @size bytes from @mem starting from @offset. This copy is
549 * guaranteed to be writable. @size can be set to -1 to return a copy all bytes
552 * Returns: a new #GstMemory.
555 gst_memory_copy (GstMemory * mem, gsize offset, gsize size)
557 g_return_val_if_fail (mem != NULL, NULL);
559 return mem->impl->info.copy (mem, offset, size);
565 * @offset: an offset to share
566 * @size: size to share
568 * Return a shared copy of @size bytes from @mem starting from @offset. No memory
569 * copy is performed and the memory region is simply shared. The result is
570 * guaranteed to be not-writable. @size can be set to -1 to return a share all bytes
573 * Returns: a new #GstMemory.
576 gst_memory_share (GstMemory * mem, gsize offset, gsize size)
578 g_return_val_if_fail (mem != NULL, NULL);
580 return mem->impl->info.share (mem, offset, size);
584 * gst_memory_is_span:
585 * @mem1: a #GstMemory
586 * @mem2: a #GstMemory
587 * @offset: a pointer to a result offset
589 * Check if @mem1 and mem2 share the memory with a common parent memory object
590 * and that the memory is contiguous.
592 * If this is the case, the memory of @mem1 and @mem2 can be merged
593 * efficiently by performing gst_memory_share() on the parent object from
594 * the returned @offset.
596 * Returns: %TRUE if the memory is contiguous and of a common parent.
599 gst_memory_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
601 g_return_val_if_fail (mem1 != NULL, FALSE);
602 g_return_val_if_fail (mem2 != NULL, FALSE);
604 /* need to have the same implementation */
605 if (mem1->impl != mem2->impl)
608 /* need to have the same parent */
609 if (mem1->parent == NULL || mem1->parent != mem2->parent)
612 /* and memory is contiguous */
613 if (!mem1->impl->info.is_span (mem1, mem2, offset))