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