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