Move GstAggregator from -bad to core
[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  * @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 #include "gst_private.h"
71 #include "gstmemory.h"
72
73 GType _gst_memory_type = 0;
74 GST_DEFINE_MINI_OBJECT_TYPE (GstMemory, gst_memory);
75
76 static GstMemory *
77 _gst_memory_copy (GstMemory * mem)
78 {
79   GST_CAT_DEBUG (GST_CAT_MEMORY, "copy memory %p", mem);
80   return gst_memory_copy (mem, 0, -1);
81 }
82
83 static void
84 _gst_memory_free (GstMemory * mem)
85 {
86   GstAllocator *allocator;
87
88   GST_CAT_DEBUG (GST_CAT_MEMORY, "free memory %p", mem);
89
90   if (mem->parent) {
91     gst_memory_unlock (mem->parent, GST_LOCK_FLAG_EXCLUSIVE);
92     gst_memory_unref (mem->parent);
93   }
94
95   allocator = mem->allocator;
96
97   gst_allocator_free (allocator, mem);
98   gst_object_unref (allocator);
99 }
100
101 /**
102  * gst_memory_init: (skip)
103  * @mem: a #GstMemory
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
111
112  * Initializes a newly allocated @mem with the given parameters. This function
113  * will call gst_mini_object_init() with the default memory parameters.
114  */
115 void
116 gst_memory_init (GstMemory * mem, GstMemoryFlags flags,
117     GstAllocator * allocator, GstMemory * parent, gsize maxsize, gsize align,
118     gsize offset, gsize size)
119 {
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);
124
125   mem->allocator = gst_object_ref (allocator);
126   if (parent) {
127     /* FIXME 2.0: this can fail if the memory is already write locked */
128     gst_memory_lock (parent, GST_LOCK_FLAG_EXCLUSIVE);
129     gst_memory_ref (parent);
130   }
131   mem->parent = parent;
132   mem->maxsize = maxsize;
133   mem->align = align;
134   mem->offset = offset;
135   mem->size = size;
136
137   GST_CAT_DEBUG (GST_CAT_MEMORY, "new memory %p, maxsize:%" G_GSIZE_FORMAT
138       " offset:%" G_GSIZE_FORMAT " size:%" G_GSIZE_FORMAT, mem, maxsize,
139       offset, size);
140 }
141
142 /**
143  * gst_memory_is_type:
144  * @mem: a #GstMemory
145  * @mem_type: a memory type
146  *
147  * Check if @mem if allocated with an allocator for @mem_type.
148  *
149  * Returns: %TRUE if @mem was allocated from an allocator for @mem_type.
150  *
151  * Since: 1.2
152  */
153 gboolean
154 gst_memory_is_type (GstMemory * mem, const gchar * mem_type)
155 {
156   g_return_val_if_fail (mem != NULL, FALSE);
157   g_return_val_if_fail (mem->allocator != NULL, FALSE);
158   g_return_val_if_fail (mem_type != NULL, FALSE);
159
160   return (g_strcmp0 (mem->allocator->mem_type, mem_type) == 0);
161 }
162
163 /**
164  * gst_memory_get_sizes:
165  * @mem: a #GstMemory
166  * @offset: pointer to offset
167  * @maxsize: pointer to maxsize
168  *
169  * Get the current @size, @offset and @maxsize of @mem.
170  *
171  * Returns: the current sizes of @mem
172  */
173 gsize
174 gst_memory_get_sizes (GstMemory * mem, gsize * offset, gsize * maxsize)
175 {
176   g_return_val_if_fail (mem != NULL, 0);
177
178   if (offset)
179     *offset = mem->offset;
180   if (maxsize)
181     *maxsize = mem->maxsize;
182
183   return mem->size;
184 }
185
186 /**
187  * gst_memory_resize:
188  * @mem: a #GstMemory
189  * @offset: a new offset
190  * @size: a new size
191  *
192  * Resize the memory region. @mem should be writable and offset + size should be
193  * less than the maxsize of @mem.
194  *
195  * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED will be
196  * cleared when offset or padding is increased respectively.
197  */
198 void
199 gst_memory_resize (GstMemory * mem, gssize offset, gsize size)
200 {
201   g_return_if_fail (mem != NULL);
202   g_return_if_fail (gst_memory_is_writable (mem));
203   g_return_if_fail (offset >= 0 || mem->offset >= -offset);
204   g_return_if_fail (size + mem->offset + offset <= mem->maxsize);
205
206   /* if we increase the prefix, we can't guarantee it is still 0 filled */
207   if ((offset > 0) && GST_MEMORY_IS_ZERO_PREFIXED (mem))
208     GST_MEMORY_FLAG_UNSET (mem, GST_MEMORY_FLAG_ZERO_PREFIXED);
209
210   /* if we increase the padding, we can't guarantee it is still 0 filled */
211   if ((offset + size < mem->size) && GST_MEMORY_IS_ZERO_PADDED (mem))
212     GST_MEMORY_FLAG_UNSET (mem, GST_MEMORY_FLAG_ZERO_PADDED);
213
214   mem->offset += offset;
215   mem->size = size;
216 }
217
218 /**
219  * gst_memory_make_mapped:
220  * @mem: (transfer full): a #GstMemory
221  * @info: (out): pointer for info
222  * @flags: mapping flags
223  *
224  * Create a #GstMemory object that is mapped with @flags. If @mem is mappable
225  * with @flags, this function returns the mapped @mem directly. Otherwise a
226  * mapped copy of @mem is returned.
227  *
228  * This function takes ownership of old @mem and returns a reference to a new
229  * #GstMemory.
230  *
231  * Returns: (transfer full) (nullable): a #GstMemory object mapped
232  * with @flags or %NULL when a mapping is not possible.
233  */
234 GstMemory *
235 gst_memory_make_mapped (GstMemory * mem, GstMapInfo * info, GstMapFlags flags)
236 {
237   GstMemory *result;
238
239   if (gst_memory_map (mem, info, flags)) {
240     result = mem;
241   } else {
242     result = gst_memory_copy (mem, 0, -1);
243     gst_memory_unref (mem);
244
245     if (result == NULL)
246       goto cannot_copy;
247
248     if (!gst_memory_map (result, info, flags))
249       goto cannot_map;
250   }
251   return result;
252
253   /* ERRORS */
254 cannot_copy:
255   {
256     GST_CAT_DEBUG (GST_CAT_MEMORY, "cannot copy memory %p", mem);
257     return NULL;
258   }
259 cannot_map:
260   {
261     GST_CAT_DEBUG (GST_CAT_MEMORY, "cannot map memory %p with flags %d", mem,
262         flags);
263     gst_memory_unref (result);
264     return NULL;
265   }
266 }
267
268 /**
269  * gst_memory_map:
270  * @mem: a #GstMemory
271  * @info: (out): pointer for info
272  * @flags: mapping flags
273  *
274  * Fill @info with the pointer and sizes of the memory in @mem that can be
275  * accessed according to @flags.
276  *
277  * This function can return %FALSE for various reasons:
278  * - the memory backed by @mem is not accessible with the given @flags.
279  * - the memory was already mapped with a different mapping.
280  *
281  * @info and its contents remain valid for as long as @mem is valid and
282  * until gst_memory_unmap() is called.
283  *
284  * For each gst_memory_map() call, a corresponding gst_memory_unmap() call
285  * should be done.
286  *
287  * Returns: %TRUE if the map operation was successful.
288  */
289 gboolean
290 gst_memory_map (GstMemory * mem, GstMapInfo * info, GstMapFlags flags)
291 {
292   g_return_val_if_fail (mem != NULL, FALSE);
293   g_return_val_if_fail (info != NULL, FALSE);
294
295   if (!gst_memory_lock (mem, (GstLockFlags) flags))
296     goto lock_failed;
297
298   info->flags = flags;
299   info->memory = mem;
300   info->size = mem->size;
301   info->maxsize = mem->maxsize - mem->offset;
302
303   if (mem->allocator->mem_map_full)
304     info->data = mem->allocator->mem_map_full (mem, info, mem->maxsize);
305   else
306     info->data = mem->allocator->mem_map (mem, mem->maxsize, flags);
307
308   if (G_UNLIKELY (info->data == NULL))
309     goto error;
310
311   info->data = info->data + mem->offset;
312
313   return TRUE;
314
315   /* ERRORS */
316 lock_failed:
317   {
318     GST_CAT_DEBUG (GST_CAT_MEMORY, "mem %p: lock %d failed", mem, flags);
319     memset (info, 0, sizeof (GstMapInfo));
320     return FALSE;
321   }
322 error:
323   {
324     /* something went wrong, restore the orginal state again
325      * it is up to the subclass to log an error if needed. */
326     GST_CAT_INFO (GST_CAT_MEMORY, "mem %p: subclass map failed", mem);
327     gst_memory_unlock (mem, (GstLockFlags) flags);
328     memset (info, 0, sizeof (GstMapInfo));
329     return FALSE;
330   }
331 }
332
333 /**
334  * gst_memory_unmap:
335  * @mem: a #GstMemory
336  * @info: a #GstMapInfo
337  *
338  * Release the memory obtained with gst_memory_map()
339  */
340 void
341 gst_memory_unmap (GstMemory * mem, GstMapInfo * info)
342 {
343   g_return_if_fail (mem != NULL);
344   g_return_if_fail (info != NULL);
345   g_return_if_fail (info->memory == mem);
346
347   if (mem->allocator->mem_unmap_full)
348     mem->allocator->mem_unmap_full (mem, info);
349   else
350     mem->allocator->mem_unmap (mem);
351   gst_memory_unlock (mem, (GstLockFlags) info->flags);
352 }
353
354 /**
355  * gst_memory_copy:
356  * @mem: a #GstMemory
357  * @offset: offset to copy from
358  * @size: size to copy, or -1 to copy to the end of the memory region
359  *
360  * Return a copy of @size bytes from @mem starting from @offset. This copy is
361  * guaranteed to be writable. @size can be set to -1 to return a copy
362  * from @offset to the end of the memory region.
363  *
364  * Returns: a new #GstMemory.
365  */
366 GstMemory *
367 gst_memory_copy (GstMemory * mem, gssize offset, gssize size)
368 {
369   GstMemory *copy;
370
371   g_return_val_if_fail (mem != NULL, NULL);
372
373   copy = mem->allocator->mem_copy (mem, offset, size);
374
375   return copy;
376 }
377
378 /**
379  * gst_memory_share:
380  * @mem: a #GstMemory
381  * @offset: offset to share from
382  * @size: size to share, or -1 to share to the end of the memory region
383  *
384  * Return a shared copy of @size bytes from @mem starting from @offset. No
385  * memory copy is performed and the memory region is simply shared. The result
386  * is guaranteed to be non-writable. @size can be set to -1 to return a shared
387  * copy from @offset to the end of the memory region.
388  *
389  * Returns: a new #GstMemory.
390  */
391 GstMemory *
392 gst_memory_share (GstMemory * mem, gssize offset, gssize size)
393 {
394   GstMemory *shared;
395
396   g_return_val_if_fail (mem != NULL, NULL);
397   g_return_val_if_fail (!GST_MEMORY_FLAG_IS_SET (mem, GST_MEMORY_FLAG_NO_SHARE),
398       NULL);
399
400   /* whether we can lock the memory exclusively */
401   /* in order to maintain backwards compatibility by not requiring subclasses
402    * to lock the memory themselves and propagate the possible failure in their
403    * mem_share implementation */
404   /* FIXME 2.0: remove and fix gst_memory_init() and/or all memory subclasses
405    * to propagate this failure case */
406   if (!gst_memory_lock (mem, GST_LOCK_FLAG_EXCLUSIVE))
407     return NULL;
408
409   /* double lock to ensure we are not mapped writable without an
410    * exclusive lock. */
411   if (!gst_memory_lock (mem, GST_LOCK_FLAG_EXCLUSIVE)) {
412     gst_memory_unlock (mem, GST_LOCK_FLAG_EXCLUSIVE);
413     return NULL;
414   }
415
416   shared = mem->allocator->mem_share (mem, offset, size);
417
418   /* unlocking before calling the subclass would be racy */
419   gst_memory_unlock (mem, GST_LOCK_FLAG_EXCLUSIVE);
420   gst_memory_unlock (mem, GST_LOCK_FLAG_EXCLUSIVE);
421
422   return shared;
423 }
424
425 /**
426  * gst_memory_is_span:
427  * @mem1: a #GstMemory
428  * @mem2: a #GstMemory
429  * @offset: a pointer to a result offset
430  *
431  * Check if @mem1 and mem2 share the memory with a common parent memory object
432  * and that the memory is contiguous.
433  *
434  * If this is the case, the memory of @mem1 and @mem2 can be merged
435  * efficiently by performing gst_memory_share() on the parent object from
436  * the returned @offset.
437  *
438  * Returns: %TRUE if the memory is contiguous and of a common parent.
439  */
440 gboolean
441 gst_memory_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
442 {
443   g_return_val_if_fail (mem1 != NULL, FALSE);
444   g_return_val_if_fail (mem2 != NULL, FALSE);
445
446   /* need to have the same allocators */
447   if (mem1->allocator != mem2->allocator)
448     return FALSE;
449
450   /* need to have the same parent */
451   if (mem1->parent == NULL || mem1->parent != mem2->parent)
452     return FALSE;
453
454   /* and memory is contiguous */
455   if (!mem1->allocator->mem_is_span (mem1, mem2, offset))
456     return FALSE;
457
458   return TRUE;
459 }
460
461 void
462 _priv_gst_memory_initialize (void)
463 {
464   _gst_memory_type = gst_memory_get_type ();
465 }