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