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