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