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