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