memory: Add 0 padding
[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 return 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 #ifdef HAVE_CONFIG_H
67 #include "config.h"
68 #endif
69
70 #include "gst_private.h"
71 #include "gstmemory.h"
72
73 #ifndef GST_DISABLE_TRACE
74 #include "gsttrace.h"
75 static GstAllocTrace *_gst_memory_trace;
76 static GstAllocTrace *_gst_allocator_trace;
77 #endif
78
79 G_DEFINE_BOXED_TYPE (GstMemory, gst_memory, (GBoxedCopyFunc) gst_memory_ref,
80     (GBoxedFreeFunc) gst_memory_unref);
81
82 G_DEFINE_BOXED_TYPE (GstAllocator, gst_allocator,
83     (GBoxedCopyFunc) gst_allocator_ref, (GBoxedFreeFunc) gst_allocator_unref);
84
85 /**
86  * gst_memory_alignment:
87  *
88  * The default memory alignment in bytes - 1
89  * an alignment of 7 would be the same as what malloc() guarantees.
90  */
91 #if defined(MEMORY_ALIGNMENT_MALLOC)
92 size_t gst_memory_alignment = 7;
93 #elif defined(MEMORY_ALIGNMENT_PAGESIZE)
94 /* we fill this in in the _init method */
95 size_t gst_memory_alignment = 0;
96 #elif defined(MEMORY_ALIGNMENT)
97 size_t gst_memory_alignment = MEMORY_ALIGNMENT - 1;
98 #else
99 #error "No memory alignment configured"
100 size_t gst_memory_alignment = 0;
101 #endif
102
103 struct _GstAllocator
104 {
105   gint refcount;
106
107   GstMemoryInfo info;
108
109   gpointer user_data;
110   GDestroyNotify notify;
111 };
112
113 /* default memory implementation */
114 typedef struct
115 {
116   GstMemory mem;
117   gsize slice_size;
118   guint8 *data;
119   gpointer user_data;
120   GDestroyNotify notify;
121 } GstMemoryDefault;
122
123 /* the default allocator */
124 static GstAllocator *_default_allocator;
125
126 /* our predefined allocators */
127 static GstAllocator *_default_mem_impl;
128
129 /* initialize the fields */
130 static void
131 _default_mem_init (GstMemoryDefault * mem, GstMemoryFlags flags,
132     GstMemory * parent, gsize slice_size, gpointer data,
133     gsize maxsize, gsize offset, gsize size, gpointer user_data,
134     GDestroyNotify notify)
135 {
136   mem->mem.allocator = _default_mem_impl;
137   mem->mem.flags = flags;
138   mem->mem.refcount = 1;
139   mem->mem.parent = parent ? gst_memory_ref (parent) : NULL;
140   mem->mem.state = (flags & GST_MEMORY_FLAG_READONLY ? 0x1 : 0);
141   mem->mem.maxsize = maxsize;
142   mem->mem.offset = offset;
143   mem->mem.size = size;
144   mem->slice_size = slice_size;
145   mem->data = data;
146   mem->user_data = user_data;
147   mem->notify = notify;
148
149   GST_CAT_DEBUG (GST_CAT_MEMORY, "new memory %p", mem);
150 }
151
152 /* create a new memory block that manages the given memory */
153 static GstMemoryDefault *
154 _default_mem_new (GstMemoryFlags flags, GstMemory * parent, gpointer data,
155     gsize maxsize, gsize offset, gsize size, gpointer user_data,
156     GDestroyNotify notify)
157 {
158   GstMemoryDefault *mem;
159   gsize slice_size;
160
161   slice_size = sizeof (GstMemoryDefault);
162
163   mem = g_slice_alloc (slice_size);
164   _default_mem_init (mem, flags, parent, slice_size,
165       data, maxsize, offset, size, user_data, notify);
166
167   return mem;
168 }
169
170 /* allocate the memory and structure in one block */
171 static GstMemoryDefault *
172 _default_mem_new_block (GstMemoryFlags flags, gsize maxsize, gsize align,
173     gsize offset, gsize size)
174 {
175   GstMemoryDefault *mem;
176   gsize aoffset, slice_size, padding;
177   guint8 *data;
178
179   /* ensure configured alignment */
180   align |= gst_memory_alignment;
181   /* allocate more to compensate for alignment */
182   maxsize += align;
183   /* alloc header and data in one block */
184   slice_size = sizeof (GstMemoryDefault) + maxsize;
185
186   mem = g_slice_alloc (slice_size);
187   if (mem == NULL)
188     return NULL;
189
190   data = (guint8 *) mem + sizeof (GstMemoryDefault);
191
192   if ((aoffset = ((guintptr) data & align)))
193     data += (align + 1) - aoffset;
194
195   if (offset && (flags & GST_MEMORY_FLAG_ZERO_PREFIXED))
196     memset (data, 0, offset);
197
198   padding = maxsize - (offset + size);
199   if (padding && (flags & GST_MEMORY_FLAG_ZERO_PADDED))
200     memset (data + offset + size, 0, padding);
201
202   _default_mem_init (mem, flags, NULL, slice_size, data, maxsize,
203       offset, size, NULL, NULL);
204
205   return mem;
206 }
207
208 static GstMemory *
209 _default_alloc_alloc (GstAllocator * allocator, GstMemoryFlags flags,
210     gsize maxsize, gsize offset, gsize size, gsize align, gpointer user_data)
211 {
212   return (GstMemory *) _default_mem_new_block (flags, maxsize, align, offset,
213       size);
214 }
215
216 static gpointer
217 _default_mem_map (GstMemoryDefault * mem, GstMapFlags flags)
218 {
219   return mem->data;
220 }
221
222 static gboolean
223 _default_mem_unmap (GstMemoryDefault * mem)
224 {
225   return TRUE;
226 }
227
228 static void
229 _default_mem_free (GstMemoryDefault * mem)
230 {
231   GST_CAT_DEBUG (GST_CAT_MEMORY, "free memory %p", mem);
232
233   if (mem->mem.parent)
234     gst_memory_unref (mem->mem.parent);
235
236   if (mem->notify)
237     mem->notify (mem->user_data);
238
239   g_slice_free1 (mem->slice_size, mem);
240 }
241
242 static GstMemoryDefault *
243 _default_mem_copy (GstMemoryDefault * mem, gssize offset, gsize size)
244 {
245   GstMemoryDefault *copy;
246
247   if (size == -1)
248     size = mem->mem.size > offset ? mem->mem.size - offset : 0;
249
250   copy =
251       _default_mem_new_block (0, mem->mem.maxsize, 0, mem->mem.offset + offset,
252       size);
253   memcpy (copy->data, mem->data, mem->mem.maxsize);
254   GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy memory %p -> %p", mem, copy);
255
256   return copy;
257 }
258
259 static GstMemoryDefault *
260 _default_mem_share (GstMemoryDefault * mem, gssize offset, gsize size)
261 {
262   GstMemoryDefault *sub;
263   GstMemory *parent;
264
265   /* find the real parent */
266   if ((parent = mem->mem.parent) == NULL)
267     parent = (GstMemory *) mem;
268
269   if (size == -1)
270     size = mem->mem.size - offset;
271
272   sub =
273       _default_mem_new (parent->flags, parent, mem->data,
274       mem->mem.maxsize, mem->mem.offset + offset, size, NULL, NULL);
275
276   return sub;
277 }
278
279 static gboolean
280 _default_mem_is_span (GstMemoryDefault * mem1, GstMemoryDefault * mem2,
281     gsize * offset)
282 {
283
284   if (offset) {
285     GstMemoryDefault *parent;
286
287     parent = (GstMemoryDefault *) mem1->mem.parent;
288
289     *offset = mem1->mem.offset - parent->mem.offset;
290   }
291
292   /* and memory is contiguous */
293   return mem1->data + mem1->mem.offset + mem1->mem.size ==
294       mem2->data + mem2->mem.offset;
295 }
296
297 static GstMemory *
298 _fallback_mem_copy (GstMemory * mem, gssize offset, gssize size)
299 {
300   GstMemory *copy;
301   GstMapInfo sinfo, dinfo;
302
303   if (!gst_memory_map (mem, &sinfo, GST_MAP_READ))
304     return NULL;
305
306   if (size == -1)
307     size = sinfo.size > offset ? sinfo.size - offset : 0;
308
309   /* use the same allocator as the memory we copy  */
310   copy = gst_allocator_alloc (mem->allocator, 0, size, 0, size, mem->align);
311   if (!gst_memory_map (copy, &dinfo, GST_MAP_WRITE)) {
312     GST_CAT_WARNING (GST_CAT_MEMORY, "could not write map memory %p", copy);
313     gst_memory_unmap (mem, &sinfo);
314     return NULL;
315   }
316
317   memcpy (dinfo.data, sinfo.data + offset, size);
318   GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy memory %p -> %p", mem, copy);
319   gst_memory_unmap (copy, &dinfo);
320   gst_memory_unmap (mem, &sinfo);
321
322   return copy;
323 }
324
325 static gboolean
326 _fallback_mem_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
327 {
328   return FALSE;
329 }
330
331 static GRWLock lock;
332 static GHashTable *allocators;
333
334 static void
335 _priv_sysmem_notify (gpointer user_data)
336 {
337   g_warning ("The default memory allocator was freed!");
338 }
339
340 void
341 _priv_gst_memory_initialize (void)
342 {
343   static const GstMemoryInfo _mem_info = {
344     GST_ALLOCATOR_SYSMEM,
345     (GstAllocatorAllocFunction) _default_alloc_alloc,
346     (GstMemoryMapFunction) _default_mem_map,
347     (GstMemoryUnmapFunction) _default_mem_unmap,
348     (GstMemoryFreeFunction) _default_mem_free,
349     (GstMemoryCopyFunction) _default_mem_copy,
350     (GstMemoryShareFunction) _default_mem_share,
351     (GstMemoryIsSpanFunction) _default_mem_is_span,
352   };
353
354 #ifndef GST_DISABLE_TRACE
355   _gst_memory_trace = _gst_alloc_trace_register ("GstMemory", -1);
356   _gst_allocator_trace = _gst_alloc_trace_register ("GstAllocator", -1);
357 #endif
358
359   g_rw_lock_init (&lock);
360   allocators = g_hash_table_new (g_str_hash, g_str_equal);
361
362 #ifdef HAVE_GETPAGESIZE
363 #ifdef MEMORY_ALIGNMENT_PAGESIZE
364   gst_memory_alignment = getpagesize () - 1;
365 #endif
366 #endif
367
368   GST_CAT_DEBUG (GST_CAT_MEMORY, "memory alignment: %" G_GSIZE_FORMAT,
369       gst_memory_alignment);
370
371   _default_mem_impl = gst_allocator_new (&_mem_info, NULL, _priv_sysmem_notify);
372
373   _default_allocator = gst_allocator_ref (_default_mem_impl);
374   gst_allocator_register (GST_ALLOCATOR_SYSMEM,
375       gst_allocator_ref (_default_mem_impl));
376 }
377
378 /**
379  * gst_memory_new_wrapped:
380  * @flags: #GstMemoryFlags
381  * @data: data to wrap
382  * @maxsize: allocated size of @data
383  * @offset: offset in @data
384  * @size: size of valid data
385  * @user_data: user_data
386  * @notify: called with @user_data when the memory is freed
387  *
388  * Allocate a new memory block that wraps the given @data.
389  *
390  * The prefix/padding must be filled with 0 if @flags contains
391  * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED respectively.
392  *
393  * Returns: a new #GstMemory.
394  */
395 GstMemory *
396 gst_memory_new_wrapped (GstMemoryFlags flags, gpointer data,
397     gsize maxsize, gsize offset, gsize size, gpointer user_data,
398     GDestroyNotify notify)
399 {
400   GstMemoryDefault *mem;
401
402   g_return_val_if_fail (data != NULL, NULL);
403   g_return_val_if_fail (offset + size <= maxsize, NULL);
404
405   mem =
406       _default_mem_new (flags, NULL, data, maxsize, offset, size, user_data,
407       notify);
408
409 #ifndef GST_DISABLE_TRACE
410   _gst_alloc_trace_new (_gst_memory_trace, mem);
411 #endif
412
413   return (GstMemory *) mem;
414 }
415
416 /**
417  * gst_memory_ref:
418  * @mem: a #GstMemory
419  *
420  * Increases the refcount of @mem.
421  *
422  * Returns: @mem with increased refcount
423  */
424 GstMemory *
425 gst_memory_ref (GstMemory * mem)
426 {
427   g_return_val_if_fail (mem != NULL, NULL);
428
429   GST_CAT_TRACE (GST_CAT_MEMORY, "memory %p, %d->%d", mem, mem->refcount,
430       mem->refcount + 1);
431
432   g_atomic_int_inc (&mem->refcount);
433
434   return mem;
435 }
436
437 /**
438  * gst_memory_unref:
439  * @mem: a #GstMemory
440  *
441  * Decreases the refcount of @mem. When the refcount reaches 0, the free
442  * function of @mem will be called.
443  */
444 void
445 gst_memory_unref (GstMemory * mem)
446 {
447   g_return_if_fail (mem != NULL);
448   g_return_if_fail (mem->allocator != NULL);
449
450   GST_CAT_TRACE (GST_CAT_MEMORY, "memory %p, %d->%d", mem, mem->refcount,
451       mem->refcount - 1);
452
453   if (g_atomic_int_dec_and_test (&mem->refcount)) {
454     /* there should be no outstanding mappings */
455     g_return_if_fail (g_atomic_int_get (&mem->state) < 4);
456 #ifndef GST_DISABLE_TRACE
457     _gst_alloc_trace_free (_gst_memory_trace, mem);
458 #endif
459     mem->allocator->info.mem_free (mem);
460   }
461 }
462
463 /**
464  * gst_memory_is_exclusive:
465  * @mem: a #GstMemory
466  *
467  * Check if the current ref to @mem is exclusive, this means that no other
468  * references exist other than @mem.
469  */
470 gboolean
471 gst_memory_is_exclusive (GstMemory * mem)
472 {
473   g_return_val_if_fail (mem != NULL, FALSE);
474
475   return (g_atomic_int_get (&mem->refcount) == 1);
476 }
477
478 /**
479  * gst_memory_get_sizes:
480  * @mem: a #GstMemory
481  * @offset: pointer to offset
482  * @maxsize: pointer to maxsize
483  *
484  * Get the current @size, @offset and @maxsize of @mem.
485  *
486  * Returns: the current sizes of @mem
487  */
488 gsize
489 gst_memory_get_sizes (GstMemory * mem, gsize * offset, gsize * maxsize)
490 {
491   g_return_val_if_fail (mem != NULL, 0);
492
493   if (offset)
494     *offset = mem->offset;
495   if (maxsize)
496     *maxsize = mem->maxsize;
497
498   return mem->size;
499 }
500
501 /**
502  * gst_memory_resize:
503  * @mem: a #GstMemory
504  * @offset: a new offset
505  * @size: a new size
506  *
507  * Resize the memory region. @mem should be writable and offset + size should be
508  * less than the maxsize of @mem.
509  *
510  * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED will be
511  * cleared when offset or padding is increased respectively.
512  */
513 void
514 gst_memory_resize (GstMemory * mem, gssize offset, gsize size)
515 {
516   g_return_if_fail (mem != NULL);
517   g_return_if_fail (offset >= 0 || mem->offset >= -offset);
518   g_return_if_fail (size + mem->offset + offset <= mem->maxsize);
519
520   /* if we increase the prefix, we can't guarantee it is still 0 filled */
521   if ((offset > 0) && GST_MEMORY_IS_ZERO_PREFIXED (mem))
522     GST_MEMORY_FLAG_UNSET (mem, GST_MEMORY_FLAG_ZERO_PREFIXED);
523
524   /* if we increase the padding, we can't guarantee it is still 0 filled */
525   if ((offset + size < mem->size) && GST_MEMORY_IS_ZERO_PADDED (mem))
526     GST_MEMORY_FLAG_UNSET (mem, GST_MEMORY_FLAG_ZERO_PADDED);
527
528   mem->offset += offset;
529   mem->size = size;
530 }
531
532 static gboolean
533 gst_memory_lock (GstMemory * mem, GstMapFlags flags)
534 {
535   gint access_mode, state, newstate;
536
537   access_mode = flags & 3;
538
539   do {
540     state = g_atomic_int_get (&mem->state);
541     if (state == 0) {
542       /* nothing mapped, set access_mode and refcount */
543       newstate = 4 | access_mode;
544     } else {
545       /* access_mode must match */
546       if ((state & access_mode) != access_mode)
547         goto lock_failed;
548       /* increase refcount */
549       newstate = state + 4;
550     }
551   } while (!g_atomic_int_compare_and_exchange (&mem->state, state, newstate));
552
553   return TRUE;
554
555 lock_failed:
556   {
557     GST_CAT_DEBUG (GST_CAT_MEMORY, "lock failed %p: state %d, access_mode %d",
558         mem, state, access_mode);
559     return FALSE;
560   }
561 }
562
563 static void
564 gst_memory_unlock (GstMemory * mem)
565 {
566   gint state, newstate;
567
568   do {
569     state = g_atomic_int_get (&mem->state);
570     /* decrease the refcount */
571     newstate = state - 4;
572     /* last refcount, unset access_mode */
573     if (newstate < 4)
574       newstate = 0;
575   } while (!g_atomic_int_compare_and_exchange (&mem->state, state, newstate));
576 }
577
578
579 /**
580  * gst_memory_make_mapped:
581  * @mem: (transfer full): a #GstMemory
582  * @info: (out): pointer for info
583  * @flags: mapping flags
584  *
585  * Create a #GstMemory object that is mapped with @flags. If @mem is mappable
586  * with @flags, this function returns the mapped @mem directly. Otherwise a
587  * mapped copy of @mem is returned.
588  *
589  * This function takes ownership of old @mem and returns a reference to a new
590  * #GstMemory.
591  *
592  * Returns: (transfer full): a #GstMemory object mapped with @flags or NULL when
593  * a mapping is not possible.
594  */
595 GstMemory *
596 gst_memory_make_mapped (GstMemory * mem, GstMapInfo * info, GstMapFlags flags)
597 {
598   GstMemory *result;
599
600   if (gst_memory_map (mem, info, flags)) {
601     result = mem;
602   } else {
603     result = gst_memory_copy (mem, 0, -1);
604     gst_memory_unref (mem);
605
606     if (result == NULL)
607       goto cannot_copy;
608
609     if (!gst_memory_map (result, info, flags))
610       goto cannot_map;
611   }
612   return result;
613
614   /* ERRORS */
615 cannot_copy:
616   {
617     GST_CAT_DEBUG (GST_CAT_MEMORY, "cannot copy memory %p", mem);
618     return NULL;
619   }
620 cannot_map:
621   {
622     GST_CAT_DEBUG (GST_CAT_MEMORY, "cannot map memory %p with flags %d", mem,
623         flags);
624     gst_memory_unref (result);
625     return NULL;
626   }
627 }
628
629 /**
630  * gst_memory_map:
631  * @mem: a #GstMemory
632  * @info: (out): pointer for info
633  * @flags: mapping flags
634  *
635  * Fill @info with the pointer and sizes of the memory in @mem that can be
636  * accessed according to @flags.
637  *
638  * This function can return %FALSE for various reasons:
639  * - the memory backed by @mem is not accessible with the given @flags.
640  * - the memory was already mapped with a different mapping.
641  *
642  * @info and its contents remain valid for as long as @mem is valid and
643  * until gst_memory_unmap() is called.
644  *
645  * For each gst_memory_map() call, a corresponding gst_memory_unmap() call
646  * should be done.
647  *
648  * Returns: %TRUE if the map operation was successful.
649  */
650 gboolean
651 gst_memory_map (GstMemory * mem, GstMapInfo * info, GstMapFlags flags)
652 {
653   g_return_val_if_fail (mem != NULL, FALSE);
654   g_return_val_if_fail (info != NULL, FALSE);
655
656   if (!gst_memory_lock (mem, flags))
657     goto lock_failed;
658
659   info->data = mem->allocator->info.mem_map (mem, mem->maxsize, flags);
660
661   if (G_UNLIKELY (info->data == NULL))
662     goto error;
663
664   info->memory = mem;
665   info->flags = flags;
666   info->size = mem->size;
667   info->maxsize = mem->maxsize - mem->offset;
668   info->data = info->data + mem->offset;
669
670   return TRUE;
671
672   /* ERRORS */
673 lock_failed:
674   {
675     GST_CAT_DEBUG (GST_CAT_MEMORY, "mem %p: lock %d failed", mem, flags);
676     return FALSE;
677   }
678 error:
679   {
680     /* something went wrong, restore the orginal state again */
681     GST_CAT_ERROR (GST_CAT_MEMORY, "mem %p: map failed", mem);
682     gst_memory_unlock (mem);
683     return FALSE;
684   }
685 }
686
687 /**
688  * gst_memory_unmap:
689  * @mem: a #GstMemory
690  * @info: a #GstMapInfo
691  *
692  * Release the memory obtained with gst_memory_map()
693  */
694 void
695 gst_memory_unmap (GstMemory * mem, GstMapInfo * info)
696 {
697   g_return_if_fail (mem != NULL);
698   g_return_if_fail (info != NULL);
699   g_return_if_fail (info->memory == mem);
700   /* there must be a ref */
701   g_return_if_fail (g_atomic_int_get (&mem->state) >= 4);
702
703   mem->allocator->info.mem_unmap (mem);
704   gst_memory_unlock (mem);
705 }
706
707 /**
708  * gst_memory_copy:
709  * @mem: a #GstMemory
710  * @offset: an offset to copy
711  * @size: size to copy or -1 to copy all bytes from offset
712  *
713  * Return a copy of @size bytes from @mem starting from @offset. This copy is
714  * guaranteed to be writable. @size can be set to -1 to return a copy all bytes
715  * from @offset.
716  *
717  * Returns: a new #GstMemory.
718  */
719 GstMemory *
720 gst_memory_copy (GstMemory * mem, gssize offset, gssize size)
721 {
722   GstMemory *copy;
723
724   g_return_val_if_fail (mem != NULL, NULL);
725
726   copy = mem->allocator->info.mem_copy (mem, offset, size);
727
728 #ifndef GST_DISABLE_TRACE
729   _gst_alloc_trace_new (_gst_memory_trace, copy);
730 #endif
731
732   return copy;
733 }
734
735 /**
736  * gst_memory_share:
737  * @mem: a #GstMemory
738  * @offset: an offset to share
739  * @size: size to share or -1 to share bytes from offset
740  *
741  * Return a shared copy of @size bytes from @mem starting from @offset. No
742  * memory copy is performed and the memory region is simply shared. The result
743  * is guaranteed to be not-writable. @size can be set to -1 to return a share
744  * all bytes from @offset.
745  *
746  * Returns: a new #GstMemory.
747  */
748 GstMemory *
749 gst_memory_share (GstMemory * mem, gssize offset, gssize size)
750 {
751   GstMemory *shared;
752
753   g_return_val_if_fail (mem != NULL, NULL);
754   g_return_val_if_fail (!GST_MEMORY_FLAG_IS_SET (mem, GST_MEMORY_FLAG_NO_SHARE),
755       NULL);
756
757   shared = mem->allocator->info.mem_share (mem, offset, size);
758
759 #ifndef GST_DISABLE_TRACE
760   _gst_alloc_trace_new (_gst_memory_trace, shared);
761 #endif
762
763   return shared;
764 }
765
766 /**
767  * gst_memory_is_span:
768  * @mem1: a #GstMemory
769  * @mem2: a #GstMemory
770  * @offset: a pointer to a result offset
771  *
772  * Check if @mem1 and mem2 share the memory with a common parent memory object
773  * and that the memory is contiguous.
774  *
775  * If this is the case, the memory of @mem1 and @mem2 can be merged
776  * efficiently by performing gst_memory_share() on the parent object from
777  * the returned @offset.
778  *
779  * Returns: %TRUE if the memory is contiguous and of a common parent.
780  */
781 gboolean
782 gst_memory_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
783 {
784   g_return_val_if_fail (mem1 != NULL, FALSE);
785   g_return_val_if_fail (mem2 != NULL, FALSE);
786
787   /* need to have the same allocators */
788   if (mem1->allocator != mem2->allocator)
789     return FALSE;
790
791   /* need to have the same parent */
792   if (mem1->parent == NULL || mem1->parent != mem2->parent)
793     return FALSE;
794
795   /* and memory is contiguous */
796   if (!mem1->allocator->info.mem_is_span (mem1, mem2, offset))
797     return FALSE;
798
799   return TRUE;
800 }
801
802 /**
803  * gst_allocator_register:
804  * @info: a #GstMemoryInfo
805  * @user_data: user data
806  * @notify: a #GDestroyNotify for @user_data
807  *
808  * Create a new memory allocator with @info and @user_data.
809  *
810  * All functions in @info are mandatory exept the copy and is_span
811  * functions, which will have a default implementation when left NULL.
812  *
813  * The @user_data will be passed to all calls of the alloc function. @notify
814  * will be called with @user_data when the allocator is freed.
815  *
816  * Returns: a new #GstAllocator.
817  */
818 GstAllocator *
819 gst_allocator_new (const GstMemoryInfo * info, gpointer user_data,
820     GDestroyNotify notify)
821 {
822   GstAllocator *allocator;
823
824 #define INSTALL_FALLBACK(_t) \
825   if (allocator->info._t == NULL) allocator->info._t = _fallback_ ##_t;
826
827   g_return_val_if_fail (info != NULL, NULL);
828   g_return_val_if_fail (info->alloc != NULL, NULL);
829   g_return_val_if_fail (info->mem_map != NULL, NULL);
830   g_return_val_if_fail (info->mem_unmap != NULL, NULL);
831   g_return_val_if_fail (info->mem_free != NULL, NULL);
832   g_return_val_if_fail (info->mem_share != NULL, NULL);
833
834   allocator = g_slice_new (GstAllocator);
835   allocator->refcount = 1;
836   allocator->info = *info;
837   allocator->user_data = user_data;
838   allocator->notify = notify;
839   INSTALL_FALLBACK (mem_copy);
840   INSTALL_FALLBACK (mem_is_span);
841 #undef INSTALL_FALLBACK
842
843   GST_CAT_DEBUG (GST_CAT_MEMORY, "new allocator %p", allocator);
844
845 #ifndef GST_DISABLE_TRACE
846   _gst_alloc_trace_new (_gst_allocator_trace, allocator);
847 #endif
848
849   return allocator;
850 }
851
852 /**
853  * gst_alocator_get_memory_type:
854  * @allocator: a #GstAllocator
855  *
856  * Get the memory type allocated by this allocator
857  *
858  * Returns: the memory type provided by @allocator
859  */
860 const gchar *
861 gst_allocator_get_memory_type (GstAllocator * allocator)
862 {
863   g_return_val_if_fail (allocator != NULL, NULL);
864
865   return allocator->info.mem_type;
866 }
867
868 /**
869  * gst_alocator_ref:
870  * @allocator: a #GstAllocator
871  *
872  * Increases the refcount of @allocator.
873  *
874  * Returns: @allocator with increased refcount
875  */
876 GstAllocator *
877 gst_allocator_ref (GstAllocator * allocator)
878 {
879   g_return_val_if_fail (allocator != NULL, NULL);
880
881   GST_CAT_TRACE (GST_CAT_MEMORY, "alocator %p, %d->%d", allocator,
882       allocator->refcount, allocator->refcount + 1);
883
884   g_atomic_int_inc (&allocator->refcount);
885
886   return allocator;
887 }
888
889 /**
890  * gst_allocator_unref:
891  * @allocator: a #GstAllocator
892  *
893  * Decreases the refcount of @allocator. When the refcount reaches 0, the notify
894  * function of @allocator will be called and the allocator will be freed.
895  */
896 void
897 gst_allocator_unref (GstAllocator * allocator)
898 {
899   g_return_if_fail (allocator != NULL);
900
901   GST_CAT_TRACE (GST_CAT_MEMORY, "allocator %p, %d->%d", allocator,
902       allocator->refcount, allocator->refcount - 1);
903
904   if (g_atomic_int_dec_and_test (&allocator->refcount)) {
905     if (allocator->notify)
906       allocator->notify (allocator->user_data);
907 #ifndef GST_DISABLE_TRACE
908     _gst_alloc_trace_free (_gst_allocator_trace, allocator);
909 #endif
910     g_slice_free1 (sizeof (GstAllocator), allocator);
911   }
912 }
913
914 /**
915  * gst_allocator_register:
916  * @name: the name of the allocator
917  * @allocator: (transfer full): #GstAllocator
918  *
919  * Registers the memory @allocator with @name. This function takes ownership of
920  * @allocator.
921  */
922 void
923 gst_allocator_register (const gchar * name, GstAllocator * allocator)
924 {
925   g_return_if_fail (name != NULL);
926   g_return_if_fail (allocator != NULL);
927
928   GST_CAT_DEBUG (GST_CAT_MEMORY, "registering allocator %p with name \"%s\"",
929       allocator, name);
930
931   g_rw_lock_writer_lock (&lock);
932   g_hash_table_insert (allocators, (gpointer) name, (gpointer) allocator);
933   g_rw_lock_writer_unlock (&lock);
934 }
935
936 /**
937  * gst_allocator_find:
938  * @name: the name of the allocator
939  *
940  * Find a previously registered allocator with @name. When @name is NULL, the
941  * default allocator will be returned.
942  *
943  * Returns: (transfer full): a #GstAllocator or NULL when the allocator with @name was not
944  * registered. Use gst_allocator_unref() to release the allocator after usage.
945  */
946 GstAllocator *
947 gst_allocator_find (const gchar * name)
948 {
949   GstAllocator *allocator;
950
951   g_rw_lock_reader_lock (&lock);
952   if (name) {
953     allocator = g_hash_table_lookup (allocators, (gconstpointer) name);
954   } else {
955     allocator = _default_allocator;
956   }
957   if (allocator)
958     gst_allocator_ref (allocator);
959   g_rw_lock_reader_unlock (&lock);
960
961   return allocator;
962 }
963
964 /**
965  * gst_allocator_set_default:
966  * @allocator: (transfer full): a #GstAllocator
967  *
968  * Set the default allocator. This function takes ownership of @allocator.
969  */
970 void
971 gst_allocator_set_default (GstAllocator * allocator)
972 {
973   GstAllocator *old;
974   g_return_if_fail (allocator != NULL);
975
976   g_rw_lock_writer_lock (&lock);
977   old = _default_allocator;
978   _default_allocator = allocator;
979   g_rw_lock_writer_unlock (&lock);
980
981   if (old)
982     gst_allocator_unref (old);
983 }
984
985 /**
986  * gst_allocator_alloc:
987  * @allocator: (transfer none) (allow-none): a #GstAllocator to use
988  * @flags: the flags
989  * @maxsize: allocated size of @data
990  * @offset: offset in allocated memory
991  * @size: size of visible
992  * @align: alignment for the data
993  *
994  * Use @allocator to allocate a new memory block with memory that is at least
995  * @maxsize big and has the given alignment.
996  *
997  * @offset and @size describe the start and size of the accessible memory.
998  *
999  * The prefix/padding will be filled with 0 if @flags contains
1000  * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED respectively.
1001  *
1002  * When @allocator is NULL, the default allocator will be used.
1003  *
1004  * @align is given as a bitmask so that @align + 1 equals the amount of bytes to
1005  * align to. For example, to align to 8 bytes, use an alignment of 7.
1006  *
1007  * Returns: (transfer full): a new #GstMemory.
1008  */
1009 GstMemory *
1010 gst_allocator_alloc (GstAllocator * allocator, GstMemoryFlags flags,
1011     gsize maxsize, gsize offset, gsize size, gsize align)
1012 {
1013   GstMemory *mem;
1014
1015   g_return_val_if_fail (((align + 1) & align) == 0, NULL);
1016
1017   if (allocator == NULL)
1018     allocator = _default_allocator;
1019
1020   mem = allocator->info.alloc (allocator, flags, maxsize, offset, size,
1021       align, allocator->user_data);
1022 #ifndef GST_DISABLE_TRACE
1023   _gst_alloc_trace_new (_gst_memory_trace, mem);
1024 #endif
1025   return mem;
1026 }