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