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