memory: Keep a reference to the allocator
[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  * Last reviewed on 2012-03-28 (0.11.3)
65  */
66
67 #ifdef HAVE_CONFIG_H
68 #include "config.h"
69 #endif
70
71 #include "gst_private.h"
72 #include "gstmemory.h"
73
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   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   g_object_unref (mem->allocator);
94   gst_allocator_free (mem->allocator, mem);
95 }
96
97 /**
98  * gst_memory_init: (skip)
99  * @mem: a #GstMemory
100  * @flags: #GstMemoryFlags
101  * @allocator: the #GstAllocator
102  * @parent: the parent of @mem
103  * @maxsize: the total size of the memory
104  * @align: the alignment of the memory
105  * @offset: The offset in the memory
106  * @size: the size of valid data in the memory
107
108  * Initializes a newly allocated @mem with the given parameters. This function
109  * will call gst_mini_object_init() with the default memory parameters.
110  */
111 void
112 gst_memory_init (GstMemory * mem, GstMemoryFlags flags,
113     GstAllocator * allocator, GstMemory * parent, gsize maxsize, gsize align,
114     gsize offset, gsize size)
115 {
116   gst_mini_object_init (GST_MINI_OBJECT_CAST (mem),
117       flags | GST_MINI_OBJECT_FLAG_LOCKABLE, GST_TYPE_MEMORY,
118       (GstMiniObjectCopyFunction) _gst_memory_copy, NULL,
119       (GstMiniObjectFreeFunction) _gst_memory_free);
120
121   mem->allocator = g_object_ref (allocator);
122   if (parent) {
123     gst_memory_lock (parent, GST_LOCK_FLAG_EXCLUSIVE);
124     gst_memory_ref (parent);
125   }
126   mem->parent = parent;
127   mem->maxsize = maxsize;
128   mem->align = align;
129   mem->offset = offset;
130   mem->size = size;
131
132   GST_CAT_DEBUG (GST_CAT_MEMORY, "new memory %p, maxsize:%" G_GSIZE_FORMAT
133       " offset:%" G_GSIZE_FORMAT " size:%" G_GSIZE_FORMAT, mem, maxsize,
134       offset, size);
135 }
136
137 /**
138  * gst_memory_get_sizes:
139  * @mem: a #GstMemory
140  * @offset: pointer to offset
141  * @maxsize: pointer to maxsize
142  *
143  * Get the current @size, @offset and @maxsize of @mem.
144  *
145  * Returns: the current sizes of @mem
146  */
147 gsize
148 gst_memory_get_sizes (GstMemory * mem, gsize * offset, gsize * maxsize)
149 {
150   g_return_val_if_fail (mem != NULL, 0);
151
152   if (offset)
153     *offset = mem->offset;
154   if (maxsize)
155     *maxsize = mem->maxsize;
156
157   return mem->size;
158 }
159
160 /**
161  * gst_memory_resize:
162  * @mem: a #GstMemory
163  * @offset: a new offset
164  * @size: a new size
165  *
166  * Resize the memory region. @mem should be writable and offset + size should be
167  * less than the maxsize of @mem.
168  *
169  * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED will be
170  * cleared when offset or padding is increased respectively.
171  */
172 void
173 gst_memory_resize (GstMemory * mem, gssize offset, gsize size)
174 {
175   g_return_if_fail (mem != NULL);
176   g_return_if_fail (offset >= 0 || mem->offset >= -offset);
177   g_return_if_fail (size + mem->offset + offset <= mem->maxsize);
178
179   /* if we increase the prefix, we can't guarantee it is still 0 filled */
180   if ((offset > 0) && GST_MEMORY_IS_ZERO_PREFIXED (mem))
181     GST_MEMORY_FLAG_UNSET (mem, GST_MEMORY_FLAG_ZERO_PREFIXED);
182
183   /* if we increase the padding, we can't guarantee it is still 0 filled */
184   if ((offset + size < mem->size) && GST_MEMORY_IS_ZERO_PADDED (mem))
185     GST_MEMORY_FLAG_UNSET (mem, GST_MEMORY_FLAG_ZERO_PADDED);
186
187   mem->offset += offset;
188   mem->size = size;
189 }
190
191 /**
192  * gst_memory_make_mapped:
193  * @mem: (transfer full): a #GstMemory
194  * @info: (out): pointer for info
195  * @flags: mapping flags
196  *
197  * Create a #GstMemory object that is mapped with @flags. If @mem is mappable
198  * with @flags, this function returns the mapped @mem directly. Otherwise a
199  * mapped copy of @mem is returned.
200  *
201  * This function takes ownership of old @mem and returns a reference to a new
202  * #GstMemory.
203  *
204  * Returns: (transfer full): a #GstMemory object mapped with @flags or NULL when
205  * a mapping is not possible.
206  */
207 GstMemory *
208 gst_memory_make_mapped (GstMemory * mem, GstMapInfo * info, GstMapFlags flags)
209 {
210   GstMemory *result;
211
212   if (gst_memory_map (mem, info, flags)) {
213     result = mem;
214   } else {
215     result = gst_memory_copy (mem, 0, -1);
216     gst_memory_unref (mem);
217
218     if (result == NULL)
219       goto cannot_copy;
220
221     if (!gst_memory_map (result, info, flags))
222       goto cannot_map;
223   }
224   return result;
225
226   /* ERRORS */
227 cannot_copy:
228   {
229     GST_CAT_DEBUG (GST_CAT_MEMORY, "cannot copy memory %p", mem);
230     return NULL;
231   }
232 cannot_map:
233   {
234     GST_CAT_DEBUG (GST_CAT_MEMORY, "cannot map memory %p with flags %d", mem,
235         flags);
236     gst_memory_unref (result);
237     return NULL;
238   }
239 }
240
241 /**
242  * gst_memory_map:
243  * @mem: a #GstMemory
244  * @info: (out): pointer for info
245  * @flags: mapping flags
246  *
247  * Fill @info with the pointer and sizes of the memory in @mem that can be
248  * accessed according to @flags.
249  *
250  * This function can return %FALSE for various reasons:
251  * - the memory backed by @mem is not accessible with the given @flags.
252  * - the memory was already mapped with a different mapping.
253  *
254  * @info and its contents remain valid for as long as @mem is valid and
255  * until gst_memory_unmap() is called.
256  *
257  * For each gst_memory_map() call, a corresponding gst_memory_unmap() call
258  * should be done.
259  *
260  * Returns: %TRUE if the map operation was successful.
261  */
262 gboolean
263 gst_memory_map (GstMemory * mem, GstMapInfo * info, GstMapFlags flags)
264 {
265   g_return_val_if_fail (mem != NULL, FALSE);
266   g_return_val_if_fail (info != NULL, FALSE);
267
268   if (!gst_memory_lock (mem, flags))
269     goto lock_failed;
270
271   info->data = mem->allocator->mem_map (mem, mem->maxsize, flags);
272
273   if (G_UNLIKELY (info->data == NULL))
274     goto error;
275
276   info->memory = mem;
277   info->flags = flags;
278   info->size = mem->size;
279   info->maxsize = mem->maxsize - mem->offset;
280   info->data = info->data + mem->offset;
281
282   return TRUE;
283
284   /* ERRORS */
285 lock_failed:
286   {
287     GST_CAT_DEBUG (GST_CAT_MEMORY, "mem %p: lock %d failed", mem, flags);
288     return FALSE;
289   }
290 error:
291   {
292     /* something went wrong, restore the orginal state again */
293     GST_CAT_ERROR (GST_CAT_MEMORY, "mem %p: subclass map failed", mem);
294     gst_memory_unlock (mem, flags);
295     return FALSE;
296   }
297 }
298
299 /**
300  * gst_memory_unmap:
301  * @mem: a #GstMemory
302  * @info: a #GstMapInfo
303  *
304  * Release the memory obtained with gst_memory_map()
305  */
306 void
307 gst_memory_unmap (GstMemory * mem, GstMapInfo * info)
308 {
309   g_return_if_fail (mem != NULL);
310   g_return_if_fail (info != NULL);
311   g_return_if_fail (info->memory == mem);
312
313   mem->allocator->mem_unmap (mem);
314   gst_memory_unlock (mem, info->flags);
315 }
316
317 /**
318  * gst_memory_copy:
319  * @mem: a #GstMemory
320  * @offset: an offset to copy
321  * @size: size to copy or -1 to copy all bytes from offset
322  *
323  * Return a copy of @size bytes from @mem starting from @offset. This copy is
324  * guaranteed to be writable. @size can be set to -1 to return a copy all bytes
325  * from @offset.
326  *
327  * Returns: a new #GstMemory.
328  */
329 GstMemory *
330 gst_memory_copy (GstMemory * mem, gssize offset, gssize size)
331 {
332   GstMemory *copy;
333
334   g_return_val_if_fail (mem != NULL, NULL);
335
336   copy = mem->allocator->mem_copy (mem, offset, size);
337
338   return copy;
339 }
340
341 /**
342  * gst_memory_share:
343  * @mem: a #GstMemory
344  * @offset: an offset to share
345  * @size: size to share or -1 to share bytes from offset
346  *
347  * Return a shared copy of @size bytes from @mem starting from @offset. No
348  * memory copy is performed and the memory region is simply shared. The result
349  * is guaranteed to be not-writable. @size can be set to -1 to return a share
350  * all bytes from @offset.
351  *
352  * Returns: a new #GstMemory.
353  */
354 GstMemory *
355 gst_memory_share (GstMemory * mem, gssize offset, gssize size)
356 {
357   GstMemory *shared;
358
359   g_return_val_if_fail (mem != NULL, NULL);
360   g_return_val_if_fail (!GST_MEMORY_FLAG_IS_SET (mem, GST_MEMORY_FLAG_NO_SHARE),
361       NULL);
362
363   shared = mem->allocator->mem_share (mem, offset, size);
364
365   return shared;
366 }
367
368 /**
369  * gst_memory_is_span:
370  * @mem1: a #GstMemory
371  * @mem2: a #GstMemory
372  * @offset: a pointer to a result offset
373  *
374  * Check if @mem1 and mem2 share the memory with a common parent memory object
375  * and that the memory is contiguous.
376  *
377  * If this is the case, the memory of @mem1 and @mem2 can be merged
378  * efficiently by performing gst_memory_share() on the parent object from
379  * the returned @offset.
380  *
381  * Returns: %TRUE if the memory is contiguous and of a common parent.
382  */
383 gboolean
384 gst_memory_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
385 {
386   g_return_val_if_fail (mem1 != NULL, FALSE);
387   g_return_val_if_fail (mem2 != NULL, FALSE);
388
389   /* need to have the same allocators */
390   if (mem1->allocator != mem2->allocator)
391     return FALSE;
392
393   /* need to have the same parent */
394   if (mem1->parent == NULL || mem1->parent != mem2->parent)
395     return FALSE;
396
397   /* and memory is contiguous */
398   if (!mem1->allocator->mem_is_span (mem1, mem2, offset))
399     return FALSE;
400
401   return TRUE;
402 }