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