docs: specify possibility of a NULL return
[platform/upstream/gstreamer.git] / subprojects / gstreamer / 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  * @title: GstMemory
25  * @short_description: refcounted wrapper for memory blocks
26  * @see_also: #GstBuffer
27  *
28  * GstMemory is a lightweight refcounted object that wraps a region of memory.
29  * They are typically used to manage the data of a #GstBuffer.
30  *
31  * A GstMemory object has an allocated region of memory of maxsize. The maximum
32  * size does not change during the lifetime of the memory object. The memory
33  * also has an offset and size property that specifies the valid range of memory
34  * in the allocated region.
35  *
36  * Memory is usually created by allocators with a gst_allocator_alloc()
37  * method call. When %NULL is used as the allocator, the default allocator will
38  * be used.
39  *
40  * New allocators can be registered with gst_allocator_register().
41  * Allocators are identified by name and can be retrieved with
42  * gst_allocator_find(). gst_allocator_set_default() can be used to change the
43  * default allocator.
44  *
45  * New memory can be created with gst_memory_new_wrapped() that wraps the memory
46  * allocated elsewhere.
47  *
48  * Refcounting of the memory block is performed with gst_memory_ref() and
49  * gst_memory_unref().
50  *
51  * The size of the memory can be retrieved and changed with
52  * gst_memory_get_sizes() and gst_memory_resize() respectively.
53  *
54  * Getting access to the data of the memory is performed with gst_memory_map().
55  * The call will return a pointer to offset bytes into the region of memory.
56  * After the memory access is completed, gst_memory_unmap() should be called.
57  *
58  * Memory can be copied with gst_memory_copy(), which will return a writable
59  * copy. gst_memory_share() will create a new memory block that shares the
60  * memory with an existing memory block at a custom offset and with a custom
61  * size.
62  *
63  * Memory can be efficiently merged when gst_memory_is_span() returns %TRUE.
64  */
65
66 #ifdef HAVE_CONFIG_H
67 #include "config.h"
68 #endif
69
70 #define GST_DISABLE_MINIOBJECT_INLINE_FUNCTIONS
71 #include "gst_private.h"
72 #include "gstmemory.h"
73
74 GType _gst_memory_type = 0;
75 GST_DEFINE_MINI_OBJECT_TYPE (GstMemory, gst_memory);
76
77 static GstMemory *
78 _gst_memory_copy (GstMemory * mem)
79 {
80   GST_CAT_DEBUG (GST_CAT_MEMORY, "copy memory %p", mem);
81   return gst_memory_copy (mem, 0, -1);
82 }
83
84 static void
85 _gst_memory_free (GstMemory * mem)
86 {
87   GstAllocator *allocator;
88
89   GST_CAT_DEBUG (GST_CAT_MEMORY, "free memory %p", mem);
90
91   if (mem->parent) {
92     gst_memory_unlock (mem->parent, GST_LOCK_FLAG_EXCLUSIVE);
93     gst_memory_unref (mem->parent);
94   }
95
96   allocator = mem->allocator;
97
98   gst_allocator_free (allocator, mem);
99
100   gst_object_unref (allocator);
101 }
102
103 /**
104  * gst_memory_init: (skip)
105  * @mem: a #GstMemory
106  * @flags: #GstMemoryFlags
107  * @allocator: the #GstAllocator
108  * @parent: the parent of @mem
109  * @maxsize: the total size of the memory
110  * @align: the alignment of the memory
111  * @offset: The offset in the memory
112  * @size: the size of valid data in the memory
113
114  * Initializes a newly allocated @mem with the given parameters. This function
115  * will call gst_mini_object_init() with the default memory parameters.
116  */
117 void
118 gst_memory_init (GstMemory * mem, GstMemoryFlags flags,
119     GstAllocator * allocator, GstMemory * parent, gsize maxsize, gsize align,
120     gsize offset, gsize size)
121 {
122   gst_mini_object_init (GST_MINI_OBJECT_CAST (mem),
123       flags | GST_MINI_OBJECT_FLAG_LOCKABLE, GST_TYPE_MEMORY,
124       (GstMiniObjectCopyFunction) _gst_memory_copy, NULL,
125       (GstMiniObjectFreeFunction) _gst_memory_free);
126
127   mem->allocator = gst_object_ref (allocator);
128   if (parent) {
129     /* FIXME 2.0: this can fail if the memory is already write locked */
130     gst_memory_lock (parent, GST_LOCK_FLAG_EXCLUSIVE);
131     gst_memory_ref (parent);
132   }
133   mem->parent = parent;
134   mem->maxsize = maxsize;
135   mem->align = align;
136   mem->offset = offset;
137   mem->size = size;
138
139   GST_CAT_DEBUG (GST_CAT_MEMORY, "new memory %p, maxsize:%" G_GSIZE_FORMAT
140       " offset:%" G_GSIZE_FORMAT " size:%" G_GSIZE_FORMAT, mem, maxsize,
141       offset, size);
142 }
143
144 /**
145  * gst_memory_is_type:
146  * @mem: a #GstMemory
147  * @mem_type: a memory type
148  *
149  * Check if @mem if allocated with an allocator for @mem_type.
150  *
151  * Returns: %TRUE if @mem was allocated from an allocator for @mem_type.
152  *
153  * Since: 1.2
154  */
155 gboolean
156 gst_memory_is_type (GstMemory * mem, const gchar * mem_type)
157 {
158   g_return_val_if_fail (mem != NULL, FALSE);
159   g_return_val_if_fail (mem->allocator != NULL, FALSE);
160   g_return_val_if_fail (mem_type != NULL, FALSE);
161
162   return (g_strcmp0 (mem->allocator->mem_type, mem_type) == 0);
163 }
164
165 /**
166  * gst_memory_get_sizes:
167  * @mem: a #GstMemory
168  * @offset: (out) (allow-none): pointer to offset
169  * @maxsize: (out) (allow-none): pointer to maxsize
170  *
171  * Get the current @size, @offset and @maxsize of @mem.
172  *
173  * Returns: the current size of @mem
174  */
175 gsize
176 gst_memory_get_sizes (GstMemory * mem, gsize * offset, gsize * maxsize)
177 {
178   g_return_val_if_fail (mem != NULL, 0);
179
180   if (offset)
181     *offset = mem->offset;
182   if (maxsize)
183     *maxsize = mem->maxsize;
184
185   return mem->size;
186 }
187
188 /**
189  * gst_memory_resize:
190  * @mem: a #GstMemory
191  * @offset: a new offset
192  * @size: a new size
193  *
194  * Resize the memory region. @mem should be writable and offset + size should be
195  * less than the maxsize of @mem.
196  *
197  * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED will be
198  * cleared when offset or padding is increased respectively.
199  */
200 void
201 gst_memory_resize (GstMemory * mem, gssize offset, gsize size)
202 {
203   g_return_if_fail (mem != NULL);
204   g_return_if_fail (gst_memory_is_writable (mem));
205   g_return_if_fail (offset >= 0 || mem->offset >= -offset);
206   g_return_if_fail (size + mem->offset + offset <= mem->maxsize);
207
208   /* if we increase the prefix, we can't guarantee it is still 0 filled */
209   if ((offset > 0) && GST_MEMORY_IS_ZERO_PREFIXED (mem))
210     GST_MEMORY_FLAG_UNSET (mem, GST_MEMORY_FLAG_ZERO_PREFIXED);
211
212   /* if we increase the padding, we can't guarantee it is still 0 filled */
213   if ((offset + size < mem->size) && GST_MEMORY_IS_ZERO_PADDED (mem))
214     GST_MEMORY_FLAG_UNSET (mem, GST_MEMORY_FLAG_ZERO_PADDED);
215
216   mem->offset += offset;
217   mem->size = size;
218 }
219
220 /**
221  * gst_memory_make_mapped:
222  * @mem: (transfer full): a #GstMemory
223  * @info: (out caller-allocates): pointer for info
224  * @flags: mapping flags
225  *
226  * Create a #GstMemory object that is mapped with @flags. If @mem is mappable
227  * with @flags, this function returns the mapped @mem directly. Otherwise a
228  * mapped copy of @mem is returned.
229  *
230  * This function takes ownership of old @mem and returns a reference to a new
231  * #GstMemory.
232  *
233  * Returns: (transfer full) (nullable): a #GstMemory object mapped
234  * with @flags or %NULL when a mapping is not possible.
235  */
236 GstMemory *
237 gst_memory_make_mapped (GstMemory * mem, GstMapInfo * info, GstMapFlags flags)
238 {
239   GstMemory *result;
240
241   if (gst_memory_map (mem, info, flags)) {
242     result = mem;
243   } else {
244     result = gst_memory_copy (mem, 0, -1);
245     gst_memory_unref (mem);
246
247     if (result == NULL)
248       goto cannot_copy;
249
250     if (!gst_memory_map (result, info, flags))
251       goto cannot_map;
252   }
253   return result;
254
255   /* ERRORS */
256 cannot_copy:
257   {
258     GST_CAT_DEBUG (GST_CAT_MEMORY, "cannot copy memory %p", mem);
259     return NULL;
260   }
261 cannot_map:
262   {
263     GST_CAT_DEBUG (GST_CAT_MEMORY, "cannot map memory %p with flags %d", mem,
264         flags);
265     gst_memory_unref (result);
266     return NULL;
267   }
268 }
269
270 /**
271  * gst_memory_map:
272  * @mem: a #GstMemory
273  * @info: (out caller-allocates): pointer for info
274  * @flags: mapping flags
275  *
276  * Fill @info with the pointer and sizes of the memory in @mem that can be
277  * accessed according to @flags.
278  *
279  * This function can return %FALSE for various reasons:
280  * - the memory backed by @mem is not accessible with the given @flags.
281  * - the memory was already mapped with a different mapping.
282  *
283  * @info and its contents remain valid for as long as @mem is valid and
284  * until gst_memory_unmap() is called.
285  *
286  * For each gst_memory_map() call, a corresponding gst_memory_unmap() call
287  * should be done.
288  *
289  * Returns: %TRUE if the map operation was successful.
290  */
291 gboolean
292 gst_memory_map (GstMemory * mem, GstMapInfo * info, GstMapFlags flags)
293 {
294   g_return_val_if_fail (mem != NULL, FALSE);
295   g_return_val_if_fail (info != NULL, FALSE);
296
297   if (!gst_memory_lock (mem, (GstLockFlags) flags))
298     goto lock_failed;
299
300   info->flags = flags;
301   info->memory = mem;
302   info->size = mem->size;
303   info->maxsize = mem->maxsize - mem->offset;
304
305   if (mem->allocator->mem_map_full)
306     info->data = mem->allocator->mem_map_full (mem, info, mem->maxsize);
307   else
308     info->data = mem->allocator->mem_map (mem, mem->maxsize, flags);
309
310   if (G_UNLIKELY (info->data == NULL))
311     goto error;
312
313   info->data = info->data + mem->offset;
314
315   return TRUE;
316
317   /* ERRORS */
318 lock_failed:
319   {
320     GST_CAT_DEBUG (GST_CAT_MEMORY, "mem %p: lock %d failed", mem, flags);
321     memset (info, 0, sizeof (GstMapInfo));
322     return FALSE;
323   }
324 error:
325   {
326     /* something went wrong, restore the original state again
327      * it is up to the subclass to log an error if needed. */
328     GST_CAT_INFO (GST_CAT_MEMORY, "mem %p: subclass map failed", mem);
329     gst_memory_unlock (mem, (GstLockFlags) flags);
330     memset (info, 0, sizeof (GstMapInfo));
331     return FALSE;
332   }
333 }
334
335 /**
336  * gst_memory_unmap:
337  * @mem: a #GstMemory
338  * @info: a #GstMapInfo
339  *
340  * Release the memory obtained with gst_memory_map()
341  */
342 void
343 gst_memory_unmap (GstMemory * mem, GstMapInfo * info)
344 {
345   g_return_if_fail (mem != NULL);
346   g_return_if_fail (info != NULL);
347   g_return_if_fail (info->memory == mem);
348
349   if (mem->allocator->mem_unmap_full)
350     mem->allocator->mem_unmap_full (mem, info);
351   else
352     mem->allocator->mem_unmap (mem);
353   gst_memory_unlock (mem, (GstLockFlags) info->flags);
354 }
355
356 /**
357  * gst_memory_copy:
358  * @mem: a #GstMemory
359  * @offset: offset to copy from
360  * @size: size to copy, or -1 to copy to the end of the memory region
361  *
362  * Return a copy of @size bytes from @mem starting from @offset. This copy is
363  * guaranteed to be writable. @size can be set to -1 to return a copy
364  * from @offset to the end of the memory region.
365  *
366  * Returns: (transfer full) (nullable): a new copy of @mem if the copy succeeded, %NULL otherwise.
367  */
368 GstMemory *
369 gst_memory_copy (GstMemory * mem, gssize offset, gssize size)
370 {
371   GstMemory *copy;
372
373   g_return_val_if_fail (mem != NULL, NULL);
374
375   copy = mem->allocator->mem_copy (mem, offset, size);
376
377   return copy;
378 }
379
380 /**
381  * gst_memory_share:
382  * @mem: a #GstMemory
383  * @offset: offset to share from
384  * @size: size to share, or -1 to share to the end of the memory region
385  *
386  * Return a shared copy of @size bytes from @mem starting from @offset. No
387  * memory copy is performed and the memory region is simply shared. The result
388  * is guaranteed to be non-writable. @size can be set to -1 to return a shared
389  * copy from @offset to the end of the memory region.
390  *
391  * Returns: a new #GstMemory.
392  */
393 GstMemory *
394 gst_memory_share (GstMemory * mem, gssize offset, gssize size)
395 {
396   GstMemory *shared;
397
398   g_return_val_if_fail (mem != NULL, NULL);
399   g_return_val_if_fail (!GST_MEMORY_FLAG_IS_SET (mem, GST_MEMORY_FLAG_NO_SHARE),
400       NULL);
401
402   /* whether we can lock the memory exclusively */
403   /* in order to maintain backwards compatibility by not requiring subclasses
404    * to lock the memory themselves and propagate the possible failure in their
405    * mem_share implementation */
406   /* FIXME 2.0: remove and fix gst_memory_init() and/or all memory subclasses
407    * to propagate this failure case */
408   if (!gst_memory_lock (mem, GST_LOCK_FLAG_EXCLUSIVE))
409     return NULL;
410
411   /* double lock to ensure we are not mapped writable without an
412    * exclusive lock. */
413   if (!gst_memory_lock (mem, GST_LOCK_FLAG_EXCLUSIVE)) {
414     gst_memory_unlock (mem, GST_LOCK_FLAG_EXCLUSIVE);
415     return NULL;
416   }
417
418   shared = mem->allocator->mem_share (mem, offset, size);
419
420   /* unlocking before calling the subclass would be racy */
421   gst_memory_unlock (mem, GST_LOCK_FLAG_EXCLUSIVE);
422   gst_memory_unlock (mem, GST_LOCK_FLAG_EXCLUSIVE);
423
424   return shared;
425 }
426
427 /**
428  * gst_memory_is_span:
429  * @mem1: a #GstMemory
430  * @mem2: a #GstMemory
431  * @offset: (out): a pointer to a result offset
432  *
433  * Check if @mem1 and mem2 share the memory with a common parent memory object
434  * and that the memory is contiguous.
435  *
436  * If this is the case, the memory of @mem1 and @mem2 can be merged
437  * efficiently by performing gst_memory_share() on the parent object from
438  * the returned @offset.
439  *
440  * Returns: %TRUE if the memory is contiguous and of a common parent.
441  */
442 gboolean
443 gst_memory_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
444 {
445   g_return_val_if_fail (mem1 != NULL, FALSE);
446   g_return_val_if_fail (mem2 != NULL, FALSE);
447
448   /* need to have the same allocators */
449   if (mem1->allocator != mem2->allocator)
450     return FALSE;
451
452   /* need to have the same parent */
453   if (mem1->parent == NULL || mem1->parent != mem2->parent)
454     return FALSE;
455
456   /* and memory is contiguous */
457   if (!mem1->allocator->mem_is_span (mem1, mem2, offset))
458     return FALSE;
459
460   return TRUE;
461 }
462
463 void
464 _priv_gst_memory_initialize (void)
465 {
466   _gst_memory_type = gst_memory_get_type ();
467 }
468
469 /**
470  * gst_memory_ref: (skip)
471  * @memory: The memory to refcount
472  *
473  * Increase the refcount of this memory.
474  *
475  * Returns: (transfer full): @memory (for convenience when doing assignments)
476  */
477 GstMemory *
478 gst_memory_ref (GstMemory * memory)
479 {
480   return (GstMemory *) gst_mini_object_ref (GST_MINI_OBJECT_CAST (memory));
481 }
482
483 /**
484  * gst_memory_unref: (skip)
485  * @memory: (transfer full): the memory to refcount
486  *
487  * Decrease the refcount of a memory, freeing it if the refcount reaches 0.
488  */
489 void
490 gst_memory_unref (GstMemory * memory)
491 {
492   gst_mini_object_unref (GST_MINI_OBJECT_CAST (memory));
493 }