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