Add new GstMapInfo
[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, (guint8 *) 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  * gst_memory_map:
504  * @mem: a #GstMemory
505  * @info: (out): pointer for info
506  * @flags: mapping flags
507  *
508  * Fill @info with the pointer and sizes of the memory in @mem that can be
509  * accessed according to @flags.
510  *
511  * This function can return %FALSE for various reasons:
512  * - the memory backed by @mem is not accessible with the given @flags.
513  * - the memory was already mapped with a different mapping.
514  *
515  * @info and its contents remains valid for as long as @mem is alive and until
516  * gst_memory_unmap() is called.
517  *
518  * For each gst_memory_map() call, a corresponding gst_memory_unmap() call
519  * should be done.
520  *
521  * Returns: %TRUE if the map operation was successful.
522  */
523 gboolean
524 gst_memory_map (GstMemory * mem, GstMapInfo * info, GstMapFlags flags)
525 {
526   g_return_val_if_fail (mem != NULL, FALSE);
527   g_return_val_if_fail (info != NULL, FALSE);
528
529   if (!gst_memory_lock (mem, flags))
530     goto lock_failed;
531
532   info->data = mem->allocator->info.map (mem, mem->maxsize, flags);
533
534   if (G_UNLIKELY (info->data == NULL))
535     goto error;
536
537   info->memory = mem;
538   info->size = mem->size;
539   info->maxsize = mem->maxsize - mem->offset;
540   info->data = (guint8 *) info->data + mem->offset;
541
542   return TRUE;
543
544   /* ERRORS */
545 lock_failed:
546   {
547     GST_DEBUG ("mem %p: lock %d failed", flags);
548     return FALSE;
549   }
550 error:
551   {
552     /* something went wrong, restore the orginal state again */
553     GST_ERROR ("mem %p: map failed", mem);
554     gst_memory_unlock (mem);
555     return FALSE;
556   }
557 }
558
559 /**
560  * gst_memory_unmap:
561  * @mem: a #GstMemory
562  * @info: a #GstMapInfo
563  *
564  * Release the memory obtained with gst_memory_map()
565  */
566 void
567 gst_memory_unmap (GstMemory * mem, GstMapInfo * info)
568 {
569   g_return_if_fail (mem != NULL);
570   g_return_if_fail (info != NULL);
571   g_return_if_fail (info->memory == mem);
572   /* there must be a ref */
573   g_return_if_fail (g_atomic_int_get (&mem->state) >= 4);
574
575   mem->allocator->info.unmap (mem);
576   gst_memory_unlock (mem);
577 }
578
579 /**
580  * gst_memory_copy:
581  * @mem: a #GstMemory
582  * @offset: an offset to copy
583  * @size: size to copy or -1 to copy all bytes from offset
584  *
585  * Return a copy of @size bytes from @mem starting from @offset. This copy is
586  * guaranteed to be writable. @size can be set to -1 to return a copy all bytes
587  * from @offset.
588  *
589  * Returns: a new #GstMemory.
590  */
591 GstMemory *
592 gst_memory_copy (GstMemory * mem, gssize offset, gssize size)
593 {
594   GstMemory *copy;
595
596   g_return_val_if_fail (mem != NULL, NULL);
597   g_return_val_if_fail (gst_memory_lock (mem, GST_MAP_READ), NULL);
598
599   copy = mem->allocator->info.copy (mem, offset, size);
600
601   gst_memory_unlock (mem);
602
603   return copy;
604 }
605
606 /**
607  * gst_memory_share:
608  * @mem: a #GstMemory
609  * @offset: an offset to share
610  * @size: size to share or -1 to share bytes from offset
611  *
612  * Return a shared copy of @size bytes from @mem starting from @offset. No
613  * memory copy is performed and the memory region is simply shared. The result
614  * is guaranteed to be not-writable. @size can be set to -1 to return a share
615  * all bytes from @offset.
616  *
617  * Returns: a new #GstMemory.
618  */
619 GstMemory *
620 gst_memory_share (GstMemory * mem, gssize offset, gssize size)
621 {
622   g_return_val_if_fail (mem != NULL, NULL);
623
624   return mem->allocator->info.share (mem, offset, size);
625 }
626
627 /**
628  * gst_memory_is_span:
629  * @mem1: a #GstMemory
630  * @mem2: a #GstMemory
631  * @offset: a pointer to a result offset
632  *
633  * Check if @mem1 and mem2 share the memory with a common parent memory object
634  * and that the memory is contiguous.
635  *
636  * If this is the case, the memory of @mem1 and @mem2 can be merged
637  * efficiently by performing gst_memory_share() on the parent object from
638  * the returned @offset.
639  *
640  * Returns: %TRUE if the memory is contiguous and of a common parent.
641  */
642 gboolean
643 gst_memory_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
644 {
645   g_return_val_if_fail (mem1 != NULL, FALSE);
646   g_return_val_if_fail (mem2 != NULL, FALSE);
647
648   /* need to have the same allocators */
649   if (mem1->allocator != mem2->allocator)
650     return FALSE;
651
652   /* need to have the same parent */
653   if (mem1->parent == NULL || mem1->parent != mem2->parent)
654     return FALSE;
655
656   /* and memory is contiguous */
657   if (!mem1->allocator->info.is_span (mem1, mem2, offset))
658     return FALSE;
659
660   return TRUE;
661 }
662
663 /**
664  * gst_allocator_register:
665  * @name: the name of the allocator
666  * @info: #GstMemoryInfo
667  *
668  * Registers the memory allocator with @name and implementation functions
669  * @info.
670  *
671  * All functions in @info are mandatory exept the copy and is_span
672  * functions, which will have a default implementation when left NULL.
673  *
674  * The user_data field in @info will be passed to all calls of the alloc
675  * function.
676  *
677  * Returns: a new #GstAllocator.
678  */
679 const GstAllocator *
680 gst_allocator_register (const gchar * name, const GstMemoryInfo * info)
681 {
682   GstAllocator *allocator;
683
684 #define INSTALL_FALLBACK(_t) \
685   if (allocator->info._t == NULL) allocator->info._t = _fallback_ ##_t;
686
687   g_return_val_if_fail (name != NULL, NULL);
688   g_return_val_if_fail (info != NULL, NULL);
689   g_return_val_if_fail (info->alloc != NULL, NULL);
690   g_return_val_if_fail (info->map != NULL, NULL);
691   g_return_val_if_fail (info->unmap != NULL, NULL);
692   g_return_val_if_fail (info->free != NULL, NULL);
693   g_return_val_if_fail (info->share != NULL, NULL);
694
695   allocator = g_slice_new (GstAllocator);
696   allocator->name = g_quark_from_string (name);
697   allocator->info = *info;
698   INSTALL_FALLBACK (copy);
699   INSTALL_FALLBACK (is_span);
700 #undef INSTALL_FALLBACK
701
702   GST_DEBUG ("registering allocator \"%s\"", name);
703
704   g_rw_lock_writer_lock (&lock);
705   g_hash_table_insert (allocators, (gpointer) name, (gpointer) allocator);
706   g_rw_lock_writer_unlock (&lock);
707
708   return allocator;
709 }
710
711 /**
712  * gst_allocator_find:
713  * @name: the name of the allocator
714  *
715  * Find a previously registered allocator with @name. When @name is NULL, the
716  * default allocator will be returned.
717  *
718  * Returns: a #GstAllocator or NULL when the allocator with @name was not
719  * registered.
720  */
721 const GstAllocator *
722 gst_allocator_find (const gchar * name)
723 {
724   const GstAllocator *allocator;
725
726   g_rw_lock_reader_lock (&lock);
727   if (name) {
728     allocator = g_hash_table_lookup (allocators, (gconstpointer) name);
729   } else {
730     allocator = _default_allocator;
731   }
732   g_rw_lock_reader_unlock (&lock);
733
734   return allocator;
735 }
736
737 /**
738  * gst_allocator_set_default:
739  * @allocator: a #GstAllocator
740  *
741  * Set the default allocator.
742  */
743 void
744 gst_allocator_set_default (const GstAllocator * allocator)
745 {
746   g_return_if_fail (allocator != NULL);
747
748   g_rw_lock_writer_lock (&lock);
749   _default_allocator = allocator;
750   g_rw_lock_writer_unlock (&lock);
751 }
752
753 /**
754  * gst_allocator_alloc:
755  * @allocator: (transfer none) (allow-none): a #GstAllocator to use
756  * @maxsize: allocated size of @data
757  * @align: alignment for the data
758  *
759  * Use @allocator to allocate a new memory block with memory that is at least
760  * @maxsize big and has the given alignment.
761  *
762  * When @allocator is NULL, the default allocator will be used.
763  *
764  * @align is given as a bitmask so that @align + 1 equals the amount of bytes to
765  * align to. For example, to align to 8 bytes, use an alignment of 7.
766  *
767  * Returns: (transfer full): a new #GstMemory.
768  */
769 GstMemory *
770 gst_allocator_alloc (const GstAllocator * allocator, gsize maxsize, gsize align)
771 {
772   g_return_val_if_fail (((align + 1) & align) == 0, NULL);
773
774   if (allocator == NULL)
775     allocator = _default_allocator;
776
777   return allocator->info.alloc (allocator, maxsize, align,
778       allocator->info.user_data);
779 }