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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, 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(). gst_allocator_set_default() can be used to change the
44 * New memory can be created with gst_memory_new_wrapped() that wraps the memory
45 * allocated elsewhere.
47 * Refcounting of the memory block is performed with gst_memory_ref() and
50 * The size of the memory can be retrieved and changed with
51 * gst_memory_get_sizes() and gst_memory_resize() respectively.
53 * Getting access to the data of the memory is performed with gst_memory_map().
54 * The call will return a pointer to offset bytes into the region of memory.
55 * After the memory access is completed, gst_memory_unmap() should be called.
57 * Memory can be copied with gst_memory_copy(), which will return a writable
58 * copy. gst_memory_share() will create a new memory block that shares the
59 * memory with an existing memory block at a custom offset and with a custom
62 * Memory can be efficiently merged when gst_memory_is_span() returns TRUE.
64 * Last reviewed on 2012-03-28 (0.11.3)
71 #include "gst_private.h"
72 #include "gstmemory.h"
74 GST_DEFINE_MINI_OBJECT_TYPE (GstMemory, gst_memory);
77 _gst_memory_copy (GstMemory * mem)
79 GST_CAT_DEBUG (GST_CAT_MEMORY, "copy memory %p", mem);
80 return gst_memory_copy (mem, 0, -1);
84 _gst_memory_free (GstMemory * mem)
86 GstAllocator *allocator;
88 GST_CAT_DEBUG (GST_CAT_MEMORY, "free memory %p", mem);
91 gst_memory_unlock (mem->parent, GST_LOCK_FLAG_EXCLUSIVE);
92 gst_memory_unref (mem->parent);
95 allocator = mem->allocator;
97 gst_allocator_free (allocator, mem);
98 gst_object_unref (allocator);
102 * gst_memory_init: (skip)
104 * @flags: #GstMemoryFlags
105 * @allocator: the #GstAllocator
106 * @parent: the parent of @mem
107 * @maxsize: the total size of the memory
108 * @align: the alignment of the memory
109 * @offset: The offset in the memory
110 * @size: the size of valid data in the memory
112 * Initializes a newly allocated @mem with the given parameters. This function
113 * will call gst_mini_object_init() with the default memory parameters.
116 gst_memory_init (GstMemory * mem, GstMemoryFlags flags,
117 GstAllocator * allocator, GstMemory * parent, gsize maxsize, gsize align,
118 gsize offset, gsize size)
120 gst_mini_object_init (GST_MINI_OBJECT_CAST (mem),
121 flags | GST_MINI_OBJECT_FLAG_LOCKABLE, GST_TYPE_MEMORY,
122 (GstMiniObjectCopyFunction) _gst_memory_copy, NULL,
123 (GstMiniObjectFreeFunction) _gst_memory_free);
125 mem->allocator = gst_object_ref (allocator);
127 gst_memory_lock (parent, GST_LOCK_FLAG_EXCLUSIVE);
128 gst_memory_ref (parent);
130 mem->parent = parent;
131 mem->maxsize = maxsize;
133 mem->offset = offset;
136 GST_CAT_DEBUG (GST_CAT_MEMORY, "new memory %p, maxsize:%" G_GSIZE_FORMAT
137 " offset:%" G_GSIZE_FORMAT " size:%" G_GSIZE_FORMAT, mem, maxsize,
142 * gst_memory_is_type:
144 * @mem_type: a memory type
146 * Check if @mem if allocated with an allocator for @mem_type.
148 * Returns: %TRUE if @mem was allocated from an allocator for @mem_type.
153 gst_memory_is_type (GstMemory * mem, const gchar * mem_type)
155 g_return_val_if_fail (mem != NULL, FALSE);
156 g_return_val_if_fail (mem->allocator != NULL, FALSE);
157 g_return_val_if_fail (mem_type != NULL, FALSE);
159 return (g_strcmp0 (mem->allocator->mem_type, mem_type) == 0);
163 * gst_memory_get_sizes:
165 * @offset: pointer to offset
166 * @maxsize: pointer to maxsize
168 * Get the current @size, @offset and @maxsize of @mem.
170 * Returns: the current sizes of @mem
173 gst_memory_get_sizes (GstMemory * mem, gsize * offset, gsize * maxsize)
175 g_return_val_if_fail (mem != NULL, 0);
178 *offset = mem->offset;
180 *maxsize = mem->maxsize;
188 * @offset: a new offset
191 * Resize the memory region. @mem should be writable and offset + size should be
192 * less than the maxsize of @mem.
194 * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED will be
195 * cleared when offset or padding is increased respectively.
198 gst_memory_resize (GstMemory * mem, gssize offset, gsize size)
200 g_return_if_fail (mem != NULL);
201 g_return_if_fail (offset >= 0 || mem->offset >= -offset);
202 g_return_if_fail (size + mem->offset + offset <= mem->maxsize);
204 /* if we increase the prefix, we can't guarantee it is still 0 filled */
205 if ((offset > 0) && GST_MEMORY_IS_ZERO_PREFIXED (mem))
206 GST_MEMORY_FLAG_UNSET (mem, GST_MEMORY_FLAG_ZERO_PREFIXED);
208 /* if we increase the padding, we can't guarantee it is still 0 filled */
209 if ((offset + size < mem->size) && GST_MEMORY_IS_ZERO_PADDED (mem))
210 GST_MEMORY_FLAG_UNSET (mem, GST_MEMORY_FLAG_ZERO_PADDED);
212 mem->offset += offset;
217 * gst_memory_make_mapped:
218 * @mem: (transfer full): a #GstMemory
219 * @info: (out): pointer for info
220 * @flags: mapping flags
222 * Create a #GstMemory object that is mapped with @flags. If @mem is mappable
223 * with @flags, this function returns the mapped @mem directly. Otherwise a
224 * mapped copy of @mem is returned.
226 * This function takes ownership of old @mem and returns a reference to a new
229 * Returns: (transfer full): a #GstMemory object mapped with @flags or NULL when
230 * a mapping is not possible.
233 gst_memory_make_mapped (GstMemory * mem, GstMapInfo * info, GstMapFlags flags)
237 if (gst_memory_map (mem, info, flags)) {
240 result = gst_memory_copy (mem, 0, -1);
241 gst_memory_unref (mem);
246 if (!gst_memory_map (result, info, flags))
254 GST_CAT_DEBUG (GST_CAT_MEMORY, "cannot copy memory %p", mem);
259 GST_CAT_DEBUG (GST_CAT_MEMORY, "cannot map memory %p with flags %d", mem,
261 gst_memory_unref (result);
269 * @info: (out): pointer for info
270 * @flags: mapping flags
272 * Fill @info with the pointer and sizes of the memory in @mem that can be
273 * accessed according to @flags.
275 * This function can return %FALSE for various reasons:
276 * - the memory backed by @mem is not accessible with the given @flags.
277 * - the memory was already mapped with a different mapping.
279 * @info and its contents remain valid for as long as @mem is valid and
280 * until gst_memory_unmap() is called.
282 * For each gst_memory_map() call, a corresponding gst_memory_unmap() call
285 * Returns: %TRUE if the map operation was successful.
288 gst_memory_map (GstMemory * mem, GstMapInfo * info, GstMapFlags flags)
290 g_return_val_if_fail (mem != NULL, FALSE);
291 g_return_val_if_fail (info != NULL, FALSE);
293 if (!gst_memory_lock (mem, flags))
296 info->data = mem->allocator->mem_map (mem, mem->maxsize, flags);
298 if (G_UNLIKELY (info->data == NULL))
303 info->size = mem->size;
304 info->maxsize = mem->maxsize - mem->offset;
305 info->data = info->data + mem->offset;
312 GST_CAT_DEBUG (GST_CAT_MEMORY, "mem %p: lock %d failed", mem, flags);
317 /* something went wrong, restore the orginal state again */
318 GST_CAT_ERROR (GST_CAT_MEMORY, "mem %p: subclass map failed", mem);
319 gst_memory_unlock (mem, flags);
327 * @info: a #GstMapInfo
329 * Release the memory obtained with gst_memory_map()
332 gst_memory_unmap (GstMemory * mem, GstMapInfo * info)
334 g_return_if_fail (mem != NULL);
335 g_return_if_fail (info != NULL);
336 g_return_if_fail (info->memory == mem);
338 mem->allocator->mem_unmap (mem);
339 gst_memory_unlock (mem, info->flags);
345 * @offset: an offset to copy
346 * @size: size to copy or -1 to copy all bytes from offset
348 * Return a copy of @size bytes from @mem starting from @offset. This copy is
349 * guaranteed to be writable. @size can be set to -1 to return a copy all bytes
352 * Returns: a new #GstMemory.
355 gst_memory_copy (GstMemory * mem, gssize offset, gssize size)
359 g_return_val_if_fail (mem != NULL, NULL);
361 copy = mem->allocator->mem_copy (mem, offset, size);
369 * @offset: an offset to share
370 * @size: size to share or -1 to share bytes from offset
372 * Return a shared copy of @size bytes from @mem starting from @offset. No
373 * memory copy is performed and the memory region is simply shared. The result
374 * is guaranteed to be not-writable. @size can be set to -1 to return a share
375 * all bytes from @offset.
377 * Returns: a new #GstMemory.
380 gst_memory_share (GstMemory * mem, gssize offset, gssize size)
384 g_return_val_if_fail (mem != NULL, NULL);
385 g_return_val_if_fail (!GST_MEMORY_FLAG_IS_SET (mem, GST_MEMORY_FLAG_NO_SHARE),
388 shared = mem->allocator->mem_share (mem, offset, size);
394 * gst_memory_is_span:
395 * @mem1: a #GstMemory
396 * @mem2: a #GstMemory
397 * @offset: a pointer to a result offset
399 * Check if @mem1 and mem2 share the memory with a common parent memory object
400 * and that the memory is contiguous.
402 * If this is the case, the memory of @mem1 and @mem2 can be merged
403 * efficiently by performing gst_memory_share() on the parent object from
404 * the returned @offset.
406 * Returns: %TRUE if the memory is contiguous and of a common parent.
409 gst_memory_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
411 g_return_val_if_fail (mem1 != NULL, FALSE);
412 g_return_val_if_fail (mem2 != NULL, FALSE);
414 /* need to have the same allocators */
415 if (mem1->allocator != mem2->allocator)
418 /* need to have the same parent */
419 if (mem1->parent == NULL || mem1->parent != mem2->parent)
422 /* and memory is contiguous */
423 if (!mem1->allocator->mem_is_span (mem1, mem2, offset))