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