docs: convert NULL, TRUE, and FALSE to %NULL, %TRUE, and %FALSE
[platform/upstream/gstreamer.git] / gst / gstmemory.c
1 /* GStreamer
2  * Copyright (C) 2011 Wim Taymans <wim.taymans@gmail.be>
3  *
4  * gstmemory.c: memory block handling
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 /**
23  * SECTION:gstmemory
24  * @short_description: refcounted wrapper for memory blocks
25  * @see_also: #GstBuffer
26  *
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.
29  *
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.
34  *
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
37  * be used.
38  *
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
42  * default allocator.
43  *
44  * New memory can be created with gst_memory_new_wrapped() that wraps the memory
45  * allocated elsewhere.
46  *
47  * Refcounting of the memory block is performed with gst_memory_ref() and
48  * gst_memory_unref().
49  *
50  * The size of the memory can be retrieved and changed with
51  * gst_memory_get_sizes() and gst_memory_resize() respectively.
52  *
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.
56  *
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
60  * size.
61  *
62  * Memory can be efficiently merged when gst_memory_is_span() returns %TRUE.
63  */
64
65 #ifdef HAVE_CONFIG_H
66 #include "config.h"
67 #endif
68
69 #include "gst_private.h"
70 #include "gstmemory.h"
71
72 GST_DEFINE_MINI_OBJECT_TYPE (GstMemory, gst_memory);
73
74 static GstMemory *
75 _gst_memory_copy (GstMemory * mem)
76 {
77   GST_CAT_DEBUG (GST_CAT_MEMORY, "copy memory %p", mem);
78   return gst_memory_copy (mem, 0, -1);
79 }
80
81 static void
82 _gst_memory_free (GstMemory * mem)
83 {
84   GstAllocator *allocator;
85
86   GST_CAT_DEBUG (GST_CAT_MEMORY, "free memory %p", mem);
87
88   if (mem->parent) {
89     gst_memory_unlock (mem->parent, GST_LOCK_FLAG_EXCLUSIVE);
90     gst_memory_unref (mem->parent);
91   }
92
93   allocator = mem->allocator;
94
95   gst_allocator_free (allocator, mem);
96   gst_object_unref (allocator);
97 }
98
99 /**
100  * gst_memory_init: (skip)
101  * @mem: a #GstMemory
102  * @flags: #GstMemoryFlags
103  * @allocator: the #GstAllocator
104  * @parent: the parent of @mem
105  * @maxsize: the total size of the memory
106  * @align: the alignment of the memory
107  * @offset: The offset in the memory
108  * @size: the size of valid data in the memory
109
110  * Initializes a newly allocated @mem with the given parameters. This function
111  * will call gst_mini_object_init() with the default memory parameters.
112  */
113 void
114 gst_memory_init (GstMemory * mem, GstMemoryFlags flags,
115     GstAllocator * allocator, GstMemory * parent, gsize maxsize, gsize align,
116     gsize offset, gsize size)
117 {
118   gst_mini_object_init (GST_MINI_OBJECT_CAST (mem),
119       flags | GST_MINI_OBJECT_FLAG_LOCKABLE, GST_TYPE_MEMORY,
120       (GstMiniObjectCopyFunction) _gst_memory_copy, NULL,
121       (GstMiniObjectFreeFunction) _gst_memory_free);
122
123   mem->allocator = gst_object_ref (allocator);
124   if (parent) {
125     gst_memory_lock (parent, GST_LOCK_FLAG_EXCLUSIVE);
126     gst_memory_ref (parent);
127   }
128   mem->parent = parent;
129   mem->maxsize = maxsize;
130   mem->align = align;
131   mem->offset = offset;
132   mem->size = size;
133
134   GST_CAT_DEBUG (GST_CAT_MEMORY, "new memory %p, maxsize:%" G_GSIZE_FORMAT
135       " offset:%" G_GSIZE_FORMAT " size:%" G_GSIZE_FORMAT, mem, maxsize,
136       offset, size);
137 }
138
139 /**
140  * gst_memory_is_type:
141  * @mem: a #GstMemory
142  * @mem_type: a memory type
143  *
144  * Check if @mem if allocated with an allocator for @mem_type.
145  *
146  * Returns: %TRUE if @mem was allocated from an allocator for @mem_type.
147  *
148  * Since: 1.2
149  */
150 gboolean
151 gst_memory_is_type (GstMemory * mem, const gchar * mem_type)
152 {
153   g_return_val_if_fail (mem != NULL, FALSE);
154   g_return_val_if_fail (mem->allocator != NULL, FALSE);
155   g_return_val_if_fail (mem_type != NULL, FALSE);
156
157   return (g_strcmp0 (mem->allocator->mem_type, mem_type) == 0);
158 }
159
160 /**
161  * gst_memory_get_sizes:
162  * @mem: a #GstMemory
163  * @offset: pointer to offset
164  * @maxsize: pointer to maxsize
165  *
166  * Get the current @size, @offset and @maxsize of @mem.
167  *
168  * Returns: the current sizes of @mem
169  */
170 gsize
171 gst_memory_get_sizes (GstMemory * mem, gsize * offset, gsize * maxsize)
172 {
173   g_return_val_if_fail (mem != NULL, 0);
174
175   if (offset)
176     *offset = mem->offset;
177   if (maxsize)
178     *maxsize = mem->maxsize;
179
180   return mem->size;
181 }
182
183 /**
184  * gst_memory_resize:
185  * @mem: a #GstMemory
186  * @offset: a new offset
187  * @size: a new size
188  *
189  * Resize the memory region. @mem should be writable and offset + size should be
190  * less than the maxsize of @mem.
191  *
192  * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED will be
193  * cleared when offset or padding is increased respectively.
194  */
195 void
196 gst_memory_resize (GstMemory * mem, gssize offset, gsize size)
197 {
198   g_return_if_fail (mem != NULL);
199   g_return_if_fail (offset >= 0 || mem->offset >= -offset);
200   g_return_if_fail (size + mem->offset + offset <= mem->maxsize);
201
202   /* if we increase the prefix, we can't guarantee it is still 0 filled */
203   if ((offset > 0) && GST_MEMORY_IS_ZERO_PREFIXED (mem))
204     GST_MEMORY_FLAG_UNSET (mem, GST_MEMORY_FLAG_ZERO_PREFIXED);
205
206   /* if we increase the padding, we can't guarantee it is still 0 filled */
207   if ((offset + size < mem->size) && GST_MEMORY_IS_ZERO_PADDED (mem))
208     GST_MEMORY_FLAG_UNSET (mem, GST_MEMORY_FLAG_ZERO_PADDED);
209
210   mem->offset += offset;
211   mem->size = size;
212 }
213
214 /**
215  * gst_memory_make_mapped:
216  * @mem: (transfer full): a #GstMemory
217  * @info: (out): pointer for info
218  * @flags: mapping flags
219  *
220  * Create a #GstMemory object that is mapped with @flags. If @mem is mappable
221  * with @flags, this function returns the mapped @mem directly. Otherwise a
222  * mapped copy of @mem is returned.
223  *
224  * This function takes ownership of old @mem and returns a reference to a new
225  * #GstMemory.
226  *
227  * Returns: (transfer full): a #GstMemory object mapped with @flags or %NULL when
228  * a mapping is not possible.
229  */
230 GstMemory *
231 gst_memory_make_mapped (GstMemory * mem, GstMapInfo * info, GstMapFlags flags)
232 {
233   GstMemory *result;
234
235   if (gst_memory_map (mem, info, flags)) {
236     result = mem;
237   } else {
238     result = gst_memory_copy (mem, 0, -1);
239     gst_memory_unref (mem);
240
241     if (result == NULL)
242       goto cannot_copy;
243
244     if (!gst_memory_map (result, info, flags))
245       goto cannot_map;
246   }
247   return result;
248
249   /* ERRORS */
250 cannot_copy:
251   {
252     GST_CAT_DEBUG (GST_CAT_MEMORY, "cannot copy memory %p", mem);
253     return NULL;
254   }
255 cannot_map:
256   {
257     GST_CAT_DEBUG (GST_CAT_MEMORY, "cannot map memory %p with flags %d", mem,
258         flags);
259     gst_memory_unref (result);
260     return NULL;
261   }
262 }
263
264 /**
265  * gst_memory_map:
266  * @mem: a #GstMemory
267  * @info: (out): pointer for info
268  * @flags: mapping flags
269  *
270  * Fill @info with the pointer and sizes of the memory in @mem that can be
271  * accessed according to @flags.
272  *
273  * This function can return %FALSE for various reasons:
274  * - the memory backed by @mem is not accessible with the given @flags.
275  * - the memory was already mapped with a different mapping.
276  *
277  * @info and its contents remain valid for as long as @mem is valid and
278  * until gst_memory_unmap() is called.
279  *
280  * For each gst_memory_map() call, a corresponding gst_memory_unmap() call
281  * should be done.
282  *
283  * Returns: %TRUE if the map operation was successful.
284  */
285 gboolean
286 gst_memory_map (GstMemory * mem, GstMapInfo * info, GstMapFlags flags)
287 {
288   g_return_val_if_fail (mem != NULL, FALSE);
289   g_return_val_if_fail (info != NULL, FALSE);
290
291   if (!gst_memory_lock (mem, (GstLockFlags) flags))
292     goto lock_failed;
293
294   info->data = mem->allocator->mem_map (mem, mem->maxsize, flags);
295
296   if (G_UNLIKELY (info->data == NULL))
297     goto error;
298
299   info->memory = mem;
300   info->flags = flags;
301   info->size = mem->size;
302   info->maxsize = mem->maxsize - mem->offset;
303   info->data = info->data + mem->offset;
304
305   return TRUE;
306
307   /* ERRORS */
308 lock_failed:
309   {
310     GST_CAT_DEBUG (GST_CAT_MEMORY, "mem %p: lock %d failed", mem, flags);
311     memset (info, 0, sizeof (GstMapInfo));
312     return FALSE;
313   }
314 error:
315   {
316     /* something went wrong, restore the orginal state again */
317     GST_CAT_ERROR (GST_CAT_MEMORY, "mem %p: subclass map failed", mem);
318     gst_memory_unlock (mem, (GstLockFlags) flags);
319     memset (info, 0, sizeof (GstMapInfo));
320     return FALSE;
321   }
322 }
323
324 /**
325  * gst_memory_unmap:
326  * @mem: a #GstMemory
327  * @info: a #GstMapInfo
328  *
329  * Release the memory obtained with gst_memory_map()
330  */
331 void
332 gst_memory_unmap (GstMemory * mem, GstMapInfo * info)
333 {
334   g_return_if_fail (mem != NULL);
335   g_return_if_fail (info != NULL);
336   g_return_if_fail (info->memory == mem);
337
338   mem->allocator->mem_unmap (mem);
339   gst_memory_unlock (mem, (GstLockFlags) info->flags);
340 }
341
342 /**
343  * gst_memory_copy:
344  * @mem: a #GstMemory
345  * @offset: an offset to copy
346  * @size: size to copy or -1 to copy all bytes from offset
347  *
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
350  * from @offset.
351  *
352  * Returns: a new #GstMemory.
353  */
354 GstMemory *
355 gst_memory_copy (GstMemory * mem, gssize offset, gssize size)
356 {
357   GstMemory *copy;
358
359   g_return_val_if_fail (mem != NULL, NULL);
360
361   copy = mem->allocator->mem_copy (mem, offset, size);
362
363   return copy;
364 }
365
366 /**
367  * gst_memory_share:
368  * @mem: a #GstMemory
369  * @offset: an offset to share
370  * @size: size to share or -1 to share bytes from offset
371  *
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.
376  *
377  * Returns: a new #GstMemory.
378  */
379 GstMemory *
380 gst_memory_share (GstMemory * mem, gssize offset, gssize size)
381 {
382   GstMemory *shared;
383
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),
386       NULL);
387
388   shared = mem->allocator->mem_share (mem, offset, size);
389
390   return shared;
391 }
392
393 /**
394  * gst_memory_is_span:
395  * @mem1: a #GstMemory
396  * @mem2: a #GstMemory
397  * @offset: a pointer to a result offset
398  *
399  * Check if @mem1 and mem2 share the memory with a common parent memory object
400  * and that the memory is contiguous.
401  *
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.
405  *
406  * Returns: %TRUE if the memory is contiguous and of a common parent.
407  */
408 gboolean
409 gst_memory_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
410 {
411   g_return_val_if_fail (mem1 != NULL, FALSE);
412   g_return_val_if_fail (mem2 != NULL, FALSE);
413
414   /* need to have the same allocators */
415   if (mem1->allocator != mem2->allocator)
416     return FALSE;
417
418   /* need to have the same parent */
419   if (mem1->parent == NULL || mem1->parent != mem2->parent)
420     return FALSE;
421
422   /* and memory is contiguous */
423   if (!mem1->allocator->mem_is_span (mem1, mem2, offset))
424     return FALSE;
425
426   return TRUE;
427 }