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