docs: improve memory docs
[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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, 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().
42  *
43  * New memory can be created with gst_memory_new_wrapped() that wraps the memory
44  * allocated elsewhere.
45  *
46  * Refcounting of the memory block is performed with gst_memory_ref() and
47  * gst_memory_unref().
48  *
49  * The size of the memory can be retrieved and changed with
50  * gst_memory_get_sizes() and gst_memory_resize() respectively.
51  *
52  * Getting access to the data of the memory is performed with gst_memory_map().
53  * The call will return a pointer to offset bytes into the region of memory.
54  * After the memory access is completed, gst_memory_unmap() should be called.
55  *
56  * Memory can be copied with gst_memory_copy(), which will returnn a writable
57  * copy. gst_memory_share() will create a new memory block that shares the
58  * memory with an existing memory block at a custom offset and with a custom
59  * size.
60  *
61  * Memory can be efficiently merged when gst_memory_is_span() returns TRUE.
62  *
63  * Last reviewed on 2011-06-08 (0.11.0)
64  */
65
66 #include "config.h"
67 #include "gst_private.h"
68 #include "gstmemory.h"
69
70 G_DEFINE_BOXED_TYPE (GstMemory, gst_memory, (GBoxedCopyFunc) gst_memory_ref,
71     (GBoxedFreeFunc) gst_memory_unref);
72
73 /**
74  * gst_memory_alignment:
75  *
76  * The default memory alignment in bytes - 1
77  * an alignment of 7 would be the same as what malloc() guarantees.
78  */
79 #if defined(MEMORY_ALIGNMENT_MALLOC)
80 size_t gst_memory_alignment = 7;
81 #elif defined(MEMORY_ALIGNMENT_PAGESIZE)
82 /* we fill this in in the _init method */
83 size_t gst_memory_alignment = 0;
84 #elif defined(MEMORY_ALIGNMENT)
85 size_t gst_memory_alignment = MEMORY_ALIGNMENT - 1;
86 #else
87 #error "No memory alignment configured"
88 size_t gst_memory_alignment = 0;
89 #endif
90
91 struct _GstAllocator
92 {
93   GQuark name;
94
95   GstMemoryInfo info;
96 };
97
98 /* default memory implementation */
99 typedef struct
100 {
101   GstMemory mem;
102   gsize slice_size;
103   guint8 *data;
104   GFreeFunc free_func;
105   gsize maxsize;
106   gsize offset;
107   gsize size;
108 } GstMemoryDefault;
109
110 /* the default allocator */
111 static const GstAllocator *_default_allocator;
112
113 /* our predefined allocators */
114 static const GstAllocator *_default_mem_impl;
115
116 /* initialize the fields */
117 static void
118 _default_mem_init (GstMemoryDefault * mem, GstMemoryFlags flags,
119     GstMemory * parent, gsize slice_size, gpointer data,
120     GFreeFunc free_func, gsize maxsize, gsize offset, gsize size)
121 {
122   mem->mem.allocator = _default_mem_impl;
123   mem->mem.flags = flags;
124   mem->mem.refcount = 1;
125   mem->mem.parent = parent ? gst_memory_ref (parent) : NULL;
126   mem->slice_size = slice_size;
127   mem->data = data;
128   mem->free_func = free_func;
129   mem->maxsize = maxsize;
130   mem->offset = offset;
131   mem->size = size;
132 }
133
134 /* create a new memory block that manages the given memory */
135 static GstMemoryDefault *
136 _default_mem_new (GstMemoryFlags flags, GstMemory * parent, gpointer data,
137     GFreeFunc free_func, gsize maxsize, gsize offset, gsize size)
138 {
139   GstMemoryDefault *mem;
140   gsize slice_size;
141
142   slice_size = sizeof (GstMemoryDefault);
143
144   mem = g_slice_alloc (slice_size);
145   _default_mem_init (mem, flags, parent, slice_size,
146       data, free_func, maxsize, offset, size);
147
148   return mem;
149 }
150
151 /* allocate the memory and structure in one block */
152 static GstMemoryDefault *
153 _default_mem_new_block (gsize maxsize, gsize align, gsize offset, gsize size)
154 {
155   GstMemoryDefault *mem;
156   gsize aoffset, slice_size;
157   guint8 *data;
158
159   /* ensure configured alignment */
160   align |= gst_memory_alignment;
161   /* allocate more to compensate for alignment */
162   maxsize += align;
163   /* alloc header and data in one block */
164   slice_size = sizeof (GstMemoryDefault) + maxsize;
165
166   mem = g_slice_alloc (slice_size);
167   if (mem == NULL)
168     return NULL;
169
170   data = (guint8 *) mem + sizeof (GstMemoryDefault);
171
172   if ((aoffset = ((guintptr) data & align)))
173     aoffset = (align + 1) - aoffset;
174
175   _default_mem_init (mem, 0, NULL, slice_size, data, NULL, maxsize,
176       aoffset + offset, size);
177
178   return mem;
179 }
180
181 static GstMemory *
182 _default_mem_alloc (const GstAllocator * allocator, gsize maxsize, gsize align)
183 {
184   return (GstMemory *) _default_mem_new_block (maxsize, align, 0, maxsize);
185 }
186
187 static gsize
188 _default_mem_get_sizes (GstMemoryDefault * mem, gsize * offset, gsize * maxsize)
189 {
190   if (offset)
191     *offset = mem->offset;
192   if (maxsize)
193     *maxsize = mem->maxsize;
194
195   return mem->size;
196 }
197
198 static void
199 _default_mem_resize (GstMemoryDefault * mem, gssize offset, gsize size)
200 {
201   g_return_if_fail (offset >= 0 || mem->offset >= -offset);
202   g_return_if_fail (size + mem->offset + offset <= mem->maxsize);
203
204   mem->offset += offset;
205   mem->size = size;
206 }
207
208 static gpointer
209 _default_mem_map (GstMemoryDefault * mem, gsize * size, gsize * maxsize,
210     GstMapFlags flags)
211 {
212   if (size)
213     *size = mem->size;
214   if (maxsize)
215     *maxsize = mem->maxsize - mem->offset;
216
217   return mem->data + mem->offset;
218 }
219
220 static gboolean
221 _default_mem_unmap (GstMemoryDefault * mem, gpointer data, gsize size)
222 {
223   g_return_val_if_fail ((guint8 *) data >= mem->data
224       && (guint8 *) data < mem->data + mem->maxsize, FALSE);
225
226   if (mem->data + mem->offset != data) {
227     gsize newoffset = (guint8 *) data - mem->data;
228     if (size == -1)
229       size = mem->offset + mem->size - newoffset;
230     mem->offset = newoffset;
231   }
232
233   if (size != -1) {
234     g_return_val_if_fail (mem->offset + size <= mem->maxsize, FALSE);
235     mem->size = size;
236   }
237
238   return TRUE;
239 }
240
241 static void
242 _default_mem_free (GstMemoryDefault * mem)
243 {
244   if (mem->mem.parent)
245     gst_memory_unref (mem->mem.parent);
246
247   if (mem->free_func)
248     mem->free_func (mem->data);
249
250   g_slice_free1 (mem->slice_size, mem);
251 }
252
253 static GstMemoryDefault *
254 _default_mem_copy (GstMemoryDefault * mem, gssize offset, gsize size)
255 {
256   GstMemoryDefault *copy;
257
258   if (size == -1)
259     size = mem->size > offset ? mem->size - offset : 0;
260
261   copy = _default_mem_new_block (mem->maxsize, 0, mem->offset + offset, size);
262   memcpy (copy->data, mem->data, mem->maxsize);
263
264   return copy;
265 }
266
267 static GstMemoryDefault *
268 _default_mem_share (GstMemoryDefault * mem, gssize offset, gsize size)
269 {
270   GstMemoryDefault *sub;
271   GstMemory *parent;
272
273   /* find the real parent */
274   if ((parent = mem->mem.parent) == NULL)
275     parent = (GstMemory *) mem;
276
277   if (size == -1)
278     size = mem->size - offset;
279
280   sub = _default_mem_new (parent->flags, parent, mem->data, NULL, mem->maxsize,
281       mem->offset + offset, size);
282
283   return sub;
284 }
285
286 static gboolean
287 _default_mem_is_span (GstMemoryDefault * mem1, GstMemoryDefault * mem2,
288     gsize * offset)
289 {
290
291   if (offset) {
292     GstMemoryDefault *parent;
293
294     parent = (GstMemoryDefault *) mem1->mem.parent;
295
296     *offset = mem1->offset - parent->offset;
297   }
298
299   /* and memory is contiguous */
300   return mem1->data + mem1->offset + mem1->size == mem2->data + mem2->offset;
301 }
302
303 static GstMemory *
304 _fallback_copy (GstMemory * mem, gssize offset, gssize size)
305 {
306   GstMemory *copy;
307   guint8 *data, *dest;
308   gsize msize;
309
310   data = gst_memory_map (mem, &msize, NULL, GST_MAP_READ);
311   if (size == -1)
312     size = msize > offset ? msize - offset : 0;
313   /* use the same allocator as the memory we copy, FIXME, alignment?  */
314   copy = gst_allocator_alloc (mem->allocator, size, 0);
315   dest = gst_memory_map (copy, NULL, NULL, GST_MAP_WRITE);
316   memcpy (dest, data + offset, size);
317   gst_memory_unmap (copy, dest, size);
318
319   gst_memory_unmap (mem, data, msize);
320
321   return (GstMemory *) copy;
322 }
323
324 static gboolean
325 _fallback_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
326 {
327   return FALSE;
328 }
329
330 static GStaticRWLock lock = G_STATIC_RW_LOCK_INIT;
331 static GHashTable *allocators;
332
333 void
334 _priv_gst_memory_initialize (void)
335 {
336   static const GstMemoryInfo _mem_info = {
337     (GstMemoryAllocFunction) _default_mem_alloc,
338     (GstMemoryGetSizesFunction) _default_mem_get_sizes,
339     (GstMemoryResizeFunction) _default_mem_resize,
340     (GstMemoryMapFunction) _default_mem_map,
341     (GstMemoryUnmapFunction) _default_mem_unmap,
342     (GstMemoryFreeFunction) _default_mem_free,
343     (GstMemoryCopyFunction) _default_mem_copy,
344     (GstMemoryShareFunction) _default_mem_share,
345     (GstMemoryIsSpanFunction) _default_mem_is_span,
346     NULL
347   };
348
349   allocators = g_hash_table_new (g_str_hash, g_str_equal);
350
351 #ifdef HAVE_GETPAGESIZE
352 #ifdef MEMORY_ALIGNMENT_PAGESIZE
353   gst_memory_alignment = getpagesize () - 1;
354 #endif
355 #endif
356
357   GST_DEBUG ("memory alignment: %" G_GSIZE_FORMAT, gst_memory_alignment);
358
359   _default_mem_impl = gst_allocator_register (GST_ALLOCATOR_SYSMEM, &_mem_info);
360
361   _default_allocator = _default_mem_impl;
362 }
363
364 /**
365  * gst_memory_new_wrapped:
366  * @flags: #GstMemoryFlags
367  * @data: data to wrap
368  * @free_func: function to free @data
369  * @maxsize: allocated size of @data
370  * @offset: offset in @data
371  * @size: size of valid data
372  *
373  * Allocate a new memory block that wraps the given @data.
374  *
375  * Returns: a new #GstMemory.
376  */
377 GstMemory *
378 gst_memory_new_wrapped (GstMemoryFlags flags, gpointer data,
379     GFreeFunc free_func, gsize maxsize, gsize offset, gsize size)
380 {
381   GstMemoryDefault *mem;
382
383   g_return_val_if_fail (data != NULL, NULL);
384   g_return_val_if_fail (offset + size <= maxsize, NULL);
385
386   mem = _default_mem_new (flags, NULL, data, free_func, maxsize, offset, size);
387
388   return (GstMemory *) mem;
389 }
390
391 /**
392  * gst_memory_ref:
393  * @mem: a #GstMemory
394  *
395  * Increases the refcount of @mem.
396  *
397  * Returns: @mem with increased refcount
398  */
399 GstMemory *
400 gst_memory_ref (GstMemory * mem)
401 {
402   g_return_val_if_fail (mem != NULL, NULL);
403
404   g_atomic_int_inc (&mem->refcount);
405
406   return mem;
407 }
408
409 /**
410  * gst_memory_unref:
411  * @mem: a #GstMemory
412  *
413  * Decreases the refcount of @mem. When the refcount reaches 0, the free
414  * function of @mem will be called.
415  */
416 void
417 gst_memory_unref (GstMemory * mem)
418 {
419   g_return_if_fail (mem != NULL);
420   g_return_if_fail (mem->allocator != NULL);
421
422   if (g_atomic_int_dec_and_test (&mem->refcount))
423     mem->allocator->info.free (mem);
424 }
425
426 /**
427  * gst_memory_get_sizes:
428  * @mem: a #GstMemory
429  * @offset: pointer to offset
430  * @maxsize: pointer to maxsize
431  *
432  * Get the current @size, @offset and @maxsize of @mem.
433  *
434  * Returns: the current sizes of @mem
435  */
436 gsize
437 gst_memory_get_sizes (GstMemory * mem, gsize * offset, gsize * maxsize)
438 {
439   g_return_val_if_fail (mem != NULL, 0);
440
441   return mem->allocator->info.get_sizes (mem, offset, maxsize);
442 }
443
444 /**
445  * gst_memory_resize:
446  * @mem: a #GstMemory
447  * @offset: a new offset
448  * @size: a new size
449  *
450  * Resize the memory region. @mem should be writable and offset + size should be
451  * less than the maxsize of @mem.
452  */
453 void
454 gst_memory_resize (GstMemory * mem, gssize offset, gsize size)
455 {
456   g_return_if_fail (mem != NULL);
457   g_return_if_fail (GST_MEMORY_IS_WRITABLE (mem));
458
459   mem->allocator->info.resize (mem, offset, size);
460 }
461
462 /**
463  * gst_memory_map:
464  * @mem: a #GstMemory
465  * @size: (out) (allow-none): pointer for size
466  * @maxsize: (out) (allow-none): pointer for maxsize
467  * @flags: mapping flags
468  *
469  * Get a pointer to the memory of @mem that can be accessed according to @flags.
470  *
471  * @size and @maxsize will contain the size of the memory and the maximum
472  * allocated memory of @mem respectively. They can be set to NULL.
473  *
474  * Returns: (transfer none): a pointer to the memory of @mem.
475  */
476 gpointer
477 gst_memory_map (GstMemory * mem, gsize * size, gsize * maxsize,
478     GstMapFlags flags)
479 {
480   g_return_val_if_fail (mem != NULL, NULL);
481   g_return_val_if_fail (!(flags & GST_MAP_WRITE) ||
482       GST_MEMORY_IS_WRITABLE (mem), NULL);
483
484   return mem->allocator->info.map (mem, size, maxsize, flags);
485 }
486
487 /**
488  * gst_memory_unmap:
489  * @mem: a #GstMemory
490  * @data: data to unmap
491  * @size: new size of @mem, or -1
492  *
493  * Release the memory pointer obtained with gst_memory_map() and set the size of
494  * the memory to @size. @size can be set to -1 when the size should not be
495  * updated.
496  *
497  * It is possible to pass a different @data than that obtained from
498  * gst_memory_map() in which case the offset of @mem will be updated.
499  *
500  * Returns: TRUE when the memory was release successfully.
501  */
502 gboolean
503 gst_memory_unmap (GstMemory * mem, gpointer data, gssize size)
504 {
505   g_return_val_if_fail (mem != NULL, FALSE);
506
507   return mem->allocator->info.unmap (mem, data, size);
508 }
509
510 /**
511  * gst_memory_copy:
512  * @mem: a #GstMemory
513  * @offset: an offset to copy
514  * @size: size to copy or -1 to copy all bytes from offset
515  *
516  * Return a copy of @size bytes from @mem starting from @offset. This copy is
517  * guaranteed to be writable. @size can be set to -1 to return a copy all bytes
518  * from @offset.
519  *
520  * Returns: a new #GstMemory.
521  */
522 GstMemory *
523 gst_memory_copy (GstMemory * mem, gssize offset, gssize size)
524 {
525   g_return_val_if_fail (mem != NULL, NULL);
526
527   return mem->allocator->info.copy (mem, offset, size);
528 }
529
530 /**
531  * gst_memory_share:
532  * @mem: a #GstMemory
533  * @offset: an offset to share
534  * @size: size to share or -1 to share bytes from offset
535  *
536  * Return a shared copy of @size bytes from @mem starting from @offset. No
537  * memory copy is performed and the memory region is simply shared. The result
538  * is guaranteed to be not-writable. @size can be set to -1 to return a share
539  * all bytes from @offset.
540  *
541  * Returns: a new #GstMemory.
542  */
543 GstMemory *
544 gst_memory_share (GstMemory * mem, gssize offset, gssize size)
545 {
546   g_return_val_if_fail (mem != NULL, NULL);
547
548   return mem->allocator->info.share (mem, offset, size);
549 }
550
551 /**
552  * gst_memory_is_span:
553  * @mem1: a #GstMemory
554  * @mem2: a #GstMemory
555  * @offset: a pointer to a result offset
556  *
557  * Check if @mem1 and mem2 share the memory with a common parent memory object
558  * and that the memory is contiguous.
559  *
560  * If this is the case, the memory of @mem1 and @mem2 can be merged
561  * efficiently by performing gst_memory_share() on the parent object from
562  * the returned @offset.
563  *
564  * Returns: %TRUE if the memory is contiguous and of a common parent.
565  */
566 gboolean
567 gst_memory_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
568 {
569   g_return_val_if_fail (mem1 != NULL, FALSE);
570   g_return_val_if_fail (mem2 != NULL, FALSE);
571
572   /* need to have the same allocators */
573   if (mem1->allocator != mem2->allocator)
574     return FALSE;
575
576   /* need to have the same parent */
577   if (mem1->parent == NULL || mem1->parent != mem2->parent)
578     return FALSE;
579
580   /* and memory is contiguous */
581   if (!mem1->allocator->info.is_span (mem1, mem2, offset))
582     return FALSE;
583
584   return TRUE;
585 }
586
587 /**
588  * gst_allocator_register:
589  * @name: the name of the allocator
590  * @info: #GstMemoryInfo
591  *
592  * Registers the memory allocator with @name and implementation functions
593  * @info.
594  *
595  * All functions in @info are mandatory exept the copy and is_span
596  * functions, which will have a default implementation when left NULL.
597  *
598  * The user_data field in @info will be passed to all calls of the alloc
599  * function.
600  *
601  * Returns: a new #GstAllocator.
602  */
603 const GstAllocator *
604 gst_allocator_register (const gchar * name, const GstMemoryInfo * info)
605 {
606   GstAllocator *allocator;
607
608 #define INSTALL_FALLBACK(_t) \
609   if (allocator->info._t == NULL) allocator->info._t = _fallback_ ##_t;
610
611   g_return_val_if_fail (name != NULL, NULL);
612   g_return_val_if_fail (info != NULL, NULL);
613   g_return_val_if_fail (info->alloc != NULL, NULL);
614   g_return_val_if_fail (info->get_sizes != NULL, NULL);
615   g_return_val_if_fail (info->resize != NULL, NULL);
616   g_return_val_if_fail (info->map != NULL, NULL);
617   g_return_val_if_fail (info->unmap != NULL, NULL);
618   g_return_val_if_fail (info->free != NULL, NULL);
619   g_return_val_if_fail (info->share != NULL, NULL);
620
621   allocator = g_slice_new (GstAllocator);
622   allocator->name = g_quark_from_string (name);
623   allocator->info = *info;
624   INSTALL_FALLBACK (copy);
625   INSTALL_FALLBACK (is_span);
626 #undef INSTALL_FALLBACK
627
628   GST_DEBUG ("registering allocator \"%s\"", name);
629
630   g_static_rw_lock_writer_lock (&lock);
631   g_hash_table_insert (allocators, (gpointer) name, (gpointer) allocator);
632   g_static_rw_lock_writer_unlock (&lock);
633
634   return allocator;
635 }
636
637 /**
638  * gst_allocator_find:
639  * @name: the name of the allocator
640  *
641  * Find a previously registered allocator with @name. When @name is NULL, the
642  * default allocator will be returned.
643  *
644  * Returns: a #GstAllocator or NULL when the allocator with @name was not
645  * registered.
646  */
647 const GstAllocator *
648 gst_allocator_find (const gchar * name)
649 {
650   const GstAllocator *allocator;
651
652   g_static_rw_lock_reader_lock (&lock);
653   if (name) {
654     allocator = g_hash_table_lookup (allocators, (gconstpointer) name);
655   } else {
656     allocator = _default_allocator;
657   }
658   g_static_rw_lock_reader_unlock (&lock);
659
660   return allocator;
661 }
662
663 /**
664  * gst_allocator_set_default:
665  * @allocator: a #GstAllocator
666  *
667  * Set the default allocator.
668  */
669 void
670 gst_allocator_set_default (const GstAllocator * allocator)
671 {
672   g_return_if_fail (allocator != NULL);
673
674   g_static_rw_lock_writer_lock (&lock);
675   _default_allocator = allocator;
676   g_static_rw_lock_writer_unlock (&lock);
677 }
678
679 /**
680  * gst_allocator_alloc:
681  * @allocator: (transfer none) (allow-none): a #GstAllocator to use
682  * @maxsize: allocated size of @data
683  * @align: alignment for the data
684  *
685  * Use @allocator to allocate a new memory block with memory that is at least
686  * @maxsize big and has the given alignment.
687  *
688  * When @allocator is NULL, the default allocator will be used.
689  *
690  * @align is given as a bitmask so that @align + 1 equals the amount of bytes to
691  * align to. For example, to align to 8 bytes, use an alignment of 7.
692  *
693  * Returns: (transfer full): a new #GstMemory.
694  */
695 GstMemory *
696 gst_allocator_alloc (const GstAllocator * allocator, gsize maxsize, gsize align)
697 {
698   g_return_val_if_fail (((align + 1) & align) == 0, NULL);
699
700   if (allocator == NULL)
701     allocator = _default_allocator;
702
703   return allocator->info.alloc (allocator, maxsize, align,
704       allocator->info.user_data);
705 }