memory: remove unused macros
[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(). gst_allocator_set_default() can be used to change the
42  * default allocator.
43  *
44  * New memory can be created with gst_memory_new_wrapped() that wraps the memory
45  * allocated elsewhere.
46  *
47  * Refcounting of the memory block is performed with gst_memory_ref() and
48  * gst_memory_unref().
49  *
50  * The size of the memory can be retrieved and changed with
51  * gst_memory_get_sizes() and gst_memory_resize() respectively.
52  *
53  * Getting access to the data of the memory is performed with gst_memory_map().
54  * The call will return a pointer to offset bytes into the region of memory.
55  * After the memory access is completed, gst_memory_unmap() should be called.
56  *
57  * Memory can be copied with gst_memory_copy(), which will return a writable
58  * copy. gst_memory_share() will create a new memory block that shares the
59  * memory with an existing memory block at a custom offset and with a custom
60  * size.
61  *
62  * Memory can be efficiently merged when gst_memory_is_span() returns TRUE.
63  *
64  * Last reviewed on 2012-03-28 (0.11.3)
65  */
66
67 #ifdef HAVE_CONFIG_H
68 #include "config.h"
69 #endif
70
71 #include "gst_private.h"
72 #include "gstmemory.h"
73
74 GST_DEFINE_MINI_OBJECT_TYPE (GstMemory, gst_memory);
75
76 GST_DEFINE_MINI_OBJECT_TYPE (GstAllocator, gst_allocator);
77
78 G_DEFINE_BOXED_TYPE (GstAllocationParams, gst_allocation_params,
79     (GBoxedCopyFunc) gst_allocation_params_copy,
80     (GBoxedFreeFunc) gst_allocation_params_free);
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 /* default memory implementation */
95 typedef struct
96 {
97   GstMemory mem;
98   gsize slice_size;
99   guint8 *data;
100   gpointer user_data;
101   GDestroyNotify notify;
102 } GstMemoryDefault;
103
104 /* the default allocator */
105 static GstAllocator *_default_allocator;
106
107 /* our predefined allocators */
108 static GstAllocator *_default_mem_impl;
109
110 static GstMemory *
111 _gst_memory_copy (GstMemory * mem)
112 {
113   GST_CAT_DEBUG (GST_CAT_MEMORY, "copy memory %p", mem);
114   return gst_memory_copy (mem, 0, -1);
115 }
116
117 static void
118 _gst_memory_free (GstMemory * mem)
119 {
120   GST_CAT_DEBUG (GST_CAT_MEMORY, "free memory %p", mem);
121
122   if (mem->parent) {
123     gst_memory_unlock (mem->parent, GST_LOCK_FLAG_EXCLUSIVE);
124     gst_memory_unref (mem->parent);
125   }
126
127   mem->allocator->info.mem_free (mem);
128 }
129
130 /**
131  * gst_memory_init: (skip)
132  * @mem: a #GstMemory
133  * @flags: #GstMemoryFlags
134  * @allocator: the #GstAllocator
135  * @parent: the parent of @mem
136  * @maxsize: the total size of the memory
137  * @align: the alignment of the memory
138  * @offset: The offset in the memory
139  * @size: the size of valid data in the memory
140
141  * Initializes a newly allocated @mem with the given parameters. This function
142  * will call gst_mini_object_init() with the default memory parameters.
143  */
144 void
145 gst_memory_init (GstMemory * mem, GstMemoryFlags flags,
146     GstAllocator * allocator, GstMemory * parent, gsize maxsize, gsize align,
147     gsize offset, gsize size)
148 {
149   gst_mini_object_init (GST_MINI_OBJECT_CAST (mem),
150       flags | GST_MINI_OBJECT_FLAG_LOCKABLE, GST_TYPE_MEMORY,
151       (GstMiniObjectCopyFunction) _gst_memory_copy, NULL,
152       (GstMiniObjectFreeFunction) _gst_memory_free);
153
154   mem->allocator = allocator;
155   if (parent) {
156     gst_memory_lock (parent, GST_LOCK_FLAG_EXCLUSIVE);
157     gst_memory_ref (parent);
158   }
159   mem->parent = parent;
160   mem->maxsize = maxsize;
161   mem->align = align;
162   mem->offset = offset;
163   mem->size = size;
164
165   GST_CAT_DEBUG (GST_CAT_MEMORY, "new memory %p, maxsize:%" G_GSIZE_FORMAT
166       " offset:%" G_GSIZE_FORMAT " size:%" G_GSIZE_FORMAT, mem, maxsize,
167       offset, size);
168 }
169
170 /* initialize the fields */
171 static inline void
172 _default_mem_init (GstMemoryDefault * mem, GstMemoryFlags flags,
173     GstMemory * parent, gsize slice_size, gpointer data,
174     gsize maxsize, gsize align, gsize offset, gsize size,
175     gpointer user_data, GDestroyNotify notify)
176 {
177   gst_memory_init (GST_MEMORY_CAST (mem),
178       flags, _default_mem_impl, parent, maxsize, align, offset, size);
179
180   mem->slice_size = slice_size;
181   mem->data = data;
182   mem->user_data = user_data;
183   mem->notify = notify;
184 }
185
186 /* create a new memory block that manages the given memory */
187 static inline GstMemoryDefault *
188 _default_mem_new (GstMemoryFlags flags, GstMemory * parent, gpointer data,
189     gsize maxsize, gsize align, gsize offset, gsize size, gpointer user_data,
190     GDestroyNotify notify)
191 {
192   GstMemoryDefault *mem;
193   gsize slice_size;
194
195   slice_size = sizeof (GstMemoryDefault);
196
197   mem = g_slice_alloc (slice_size);
198   _default_mem_init (mem, flags, parent, slice_size,
199       data, maxsize, align, offset, size, user_data, notify);
200
201   return mem;
202 }
203
204 /* allocate the memory and structure in one block */
205 static GstMemoryDefault *
206 _default_mem_new_block (GstMemoryFlags flags, gsize maxsize, gsize align,
207     gsize offset, gsize size)
208 {
209   GstMemoryDefault *mem;
210   gsize aoffset, slice_size, padding;
211   guint8 *data;
212
213   /* ensure configured alignment */
214   align |= gst_memory_alignment;
215   /* allocate more to compensate for alignment */
216   maxsize += align;
217   /* alloc header and data in one block */
218   slice_size = sizeof (GstMemoryDefault) + maxsize;
219
220   mem = g_slice_alloc (slice_size);
221   if (mem == NULL)
222     return NULL;
223
224   data = (guint8 *) mem + sizeof (GstMemoryDefault);
225
226   /* do alignment */
227   if ((aoffset = ((guintptr) data & align))) {
228     aoffset = (align + 1) - aoffset;
229     data += aoffset;
230     maxsize -= aoffset;
231   }
232
233   if (offset && (flags & GST_MEMORY_FLAG_ZERO_PREFIXED))
234     memset (data, 0, offset);
235
236   padding = maxsize - (offset + size);
237   if (padding && (flags & GST_MEMORY_FLAG_ZERO_PADDED))
238     memset (data + offset + size, 0, padding);
239
240   _default_mem_init (mem, flags, NULL, slice_size, data, maxsize,
241       align, offset, size, NULL, NULL);
242
243   return mem;
244 }
245
246 static GstMemory *
247 _default_alloc_alloc (GstAllocator * allocator, gsize size,
248     GstAllocationParams * params, gpointer user_data)
249 {
250   gsize maxsize = size + params->prefix + params->padding;
251
252   return (GstMemory *) _default_mem_new_block (params->flags,
253       maxsize, params->align, params->prefix, size);
254 }
255
256 static gpointer
257 _default_mem_map (GstMemoryDefault * mem, gsize maxsize, GstMapFlags flags)
258 {
259   return mem->data;
260 }
261
262 static gboolean
263 _default_mem_unmap (GstMemoryDefault * mem)
264 {
265   return TRUE;
266 }
267
268 static void
269 _default_mem_free (GstMemoryDefault * mem)
270 {
271   if (mem->notify)
272     mem->notify (mem->user_data);
273
274   g_slice_free1 (mem->slice_size, mem);
275 }
276
277 static GstMemoryDefault *
278 _default_mem_copy (GstMemoryDefault * mem, gssize offset, gsize size)
279 {
280   GstMemoryDefault *copy;
281
282   if (size == -1)
283     size = mem->mem.size > offset ? mem->mem.size - offset : 0;
284
285   copy =
286       _default_mem_new_block (0, mem->mem.maxsize, 0, mem->mem.offset + offset,
287       size);
288   GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
289       "memcpy %" G_GSIZE_FORMAT " memory %p -> %p", mem->mem.maxsize, mem,
290       copy);
291   memcpy (copy->data, mem->data, mem->mem.maxsize);
292
293   return copy;
294 }
295
296 static GstMemoryDefault *
297 _default_mem_share (GstMemoryDefault * mem, gssize offset, gsize size)
298 {
299   GstMemoryDefault *sub;
300   GstMemory *parent;
301
302   /* find the real parent */
303   if ((parent = mem->mem.parent) == NULL)
304     parent = (GstMemory *) mem;
305
306   if (size == -1)
307     size = mem->mem.size - offset;
308
309   /* the shared memory is always readonly */
310   sub =
311       _default_mem_new (GST_MINI_OBJECT_FLAGS (parent) |
312       GST_MINI_OBJECT_FLAG_LOCK_READONLY, parent, mem->data, mem->mem.maxsize,
313       mem->mem.align, mem->mem.offset + offset, size, NULL, NULL);
314
315   return sub;
316 }
317
318 static gboolean
319 _default_mem_is_span (GstMemoryDefault * mem1, GstMemoryDefault * mem2,
320     gsize * offset)
321 {
322
323   if (offset) {
324     GstMemoryDefault *parent;
325
326     parent = (GstMemoryDefault *) mem1->mem.parent;
327
328     *offset = mem1->mem.offset - parent->mem.offset;
329   }
330
331   /* and memory is contiguous */
332   return mem1->data + mem1->mem.offset + mem1->mem.size ==
333       mem2->data + mem2->mem.offset;
334 }
335
336 static GstMemory *
337 _fallback_mem_copy (GstMemory * mem, gssize offset, gssize size)
338 {
339   GstMemory *copy;
340   GstMapInfo sinfo, dinfo;
341   GstAllocationParams params = { 0, 0, 0, mem->align, };
342
343   if (!gst_memory_map (mem, &sinfo, GST_MAP_READ))
344     return NULL;
345
346   if (size == -1)
347     size = sinfo.size > offset ? sinfo.size - offset : 0;
348
349   /* use the same allocator as the memory we copy  */
350   copy = gst_allocator_alloc (mem->allocator, size, &params);
351   if (!gst_memory_map (copy, &dinfo, GST_MAP_WRITE)) {
352     GST_CAT_WARNING (GST_CAT_MEMORY, "could not write map memory %p", copy);
353     gst_memory_unmap (mem, &sinfo);
354     return NULL;
355   }
356
357   GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
358       "memcpy %" G_GSSIZE_FORMAT " memory %p -> %p", size, mem, copy);
359   memcpy (dinfo.data, sinfo.data + offset, size);
360   gst_memory_unmap (copy, &dinfo);
361   gst_memory_unmap (mem, &sinfo);
362
363   return copy;
364 }
365
366 static gboolean
367 _fallback_mem_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
368 {
369   return FALSE;
370 }
371
372 static GRWLock lock;
373 static GHashTable *allocators;
374
375 static void
376 _priv_sysmem_free (GstMiniObject * obj)
377 {
378   g_warning ("The default memory allocator was freed!");
379 }
380
381 void
382 _priv_gst_memory_initialize (void)
383 {
384   static const GstMemoryInfo _mem_info = {
385     GST_ALLOCATOR_SYSMEM,
386     (GstAllocatorAllocFunction) _default_alloc_alloc,
387     (GstMemoryMapFunction) _default_mem_map,
388     (GstMemoryUnmapFunction) _default_mem_unmap,
389     (GstMemoryFreeFunction) _default_mem_free,
390     (GstMemoryCopyFunction) _default_mem_copy,
391     (GstMemoryShareFunction) _default_mem_share,
392     (GstMemoryIsSpanFunction) _default_mem_is_span,
393   };
394
395   g_rw_lock_init (&lock);
396   allocators = g_hash_table_new (g_str_hash, g_str_equal);
397
398 #ifdef HAVE_GETPAGESIZE
399 #ifdef MEMORY_ALIGNMENT_PAGESIZE
400   gst_memory_alignment = getpagesize () - 1;
401 #endif
402 #endif
403
404   GST_CAT_DEBUG (GST_CAT_MEMORY, "memory alignment: %" G_GSIZE_FORMAT,
405       gst_memory_alignment);
406
407   _default_mem_impl = g_slice_new (GstAllocator);
408   gst_allocator_init (_default_mem_impl, 0, &_mem_info, _priv_sysmem_free);
409
410   _default_allocator = gst_allocator_ref (_default_mem_impl);
411   gst_allocator_register (GST_ALLOCATOR_SYSMEM,
412       gst_allocator_ref (_default_mem_impl));
413 }
414
415 /**
416  * gst_memory_new_wrapped:
417  * @flags: #GstMemoryFlags
418  * @data: data to wrap
419  * @maxsize: allocated size of @data
420  * @offset: offset in @data
421  * @size: size of valid data
422  * @user_data: user_data
423  * @notify: called with @user_data when the memory is freed
424  *
425  * Allocate a new memory block that wraps the given @data.
426  *
427  * The prefix/padding must be filled with 0 if @flags contains
428  * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED respectively.
429  *
430  * Returns: a new #GstMemory.
431  */
432 GstMemory *
433 gst_memory_new_wrapped (GstMemoryFlags flags, gpointer data,
434     gsize maxsize, gsize offset, gsize size, gpointer user_data,
435     GDestroyNotify notify)
436 {
437   GstMemoryDefault *mem;
438
439   g_return_val_if_fail (data != NULL, NULL);
440   g_return_val_if_fail (offset + size <= maxsize, NULL);
441
442   mem =
443       _default_mem_new (flags, NULL, data, maxsize, 0, offset, size, user_data,
444       notify);
445
446   return (GstMemory *) mem;
447 }
448
449 /**
450  * gst_memory_get_sizes:
451  * @mem: a #GstMemory
452  * @offset: pointer to offset
453  * @maxsize: pointer to maxsize
454  *
455  * Get the current @size, @offset and @maxsize of @mem.
456  *
457  * Returns: the current sizes of @mem
458  */
459 gsize
460 gst_memory_get_sizes (GstMemory * mem, gsize * offset, gsize * maxsize)
461 {
462   g_return_val_if_fail (mem != NULL, 0);
463
464   if (offset)
465     *offset = mem->offset;
466   if (maxsize)
467     *maxsize = mem->maxsize;
468
469   return mem->size;
470 }
471
472 /**
473  * gst_memory_resize:
474  * @mem: a #GstMemory
475  * @offset: a new offset
476  * @size: a new size
477  *
478  * Resize the memory region. @mem should be writable and offset + size should be
479  * less than the maxsize of @mem.
480  *
481  * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED will be
482  * cleared when offset or padding is increased respectively.
483  */
484 void
485 gst_memory_resize (GstMemory * mem, gssize offset, gsize size)
486 {
487   g_return_if_fail (mem != NULL);
488   g_return_if_fail (offset >= 0 || mem->offset >= -offset);
489   g_return_if_fail (size + mem->offset + offset <= mem->maxsize);
490
491   /* if we increase the prefix, we can't guarantee it is still 0 filled */
492   if ((offset > 0) && GST_MEMORY_IS_ZERO_PREFIXED (mem))
493     GST_MEMORY_FLAG_UNSET (mem, GST_MEMORY_FLAG_ZERO_PREFIXED);
494
495   /* if we increase the padding, we can't guarantee it is still 0 filled */
496   if ((offset + size < mem->size) && GST_MEMORY_IS_ZERO_PADDED (mem))
497     GST_MEMORY_FLAG_UNSET (mem, GST_MEMORY_FLAG_ZERO_PADDED);
498
499   mem->offset += offset;
500   mem->size = size;
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     gst_memory_unref (mem);
529
530     if (result == NULL)
531       goto cannot_copy;
532
533     if (!gst_memory_map (result, info, flags))
534       goto cannot_map;
535   }
536   return result;
537
538   /* ERRORS */
539 cannot_copy:
540   {
541     GST_CAT_DEBUG (GST_CAT_MEMORY, "cannot copy memory %p", mem);
542     return NULL;
543   }
544 cannot_map:
545   {
546     GST_CAT_DEBUG (GST_CAT_MEMORY, "cannot map memory %p with flags %d", mem,
547         flags);
548     gst_memory_unref (result);
549     return NULL;
550   }
551 }
552
553 /**
554  * gst_memory_map:
555  * @mem: a #GstMemory
556  * @info: (out): pointer for info
557  * @flags: mapping flags
558  *
559  * Fill @info with the pointer and sizes of the memory in @mem that can be
560  * accessed according to @flags.
561  *
562  * This function can return %FALSE for various reasons:
563  * - the memory backed by @mem is not accessible with the given @flags.
564  * - the memory was already mapped with a different mapping.
565  *
566  * @info and its contents remain valid for as long as @mem is valid and
567  * until gst_memory_unmap() is called.
568  *
569  * For each gst_memory_map() call, a corresponding gst_memory_unmap() call
570  * should be done.
571  *
572  * Returns: %TRUE if the map operation was successful.
573  */
574 gboolean
575 gst_memory_map (GstMemory * mem, GstMapInfo * info, GstMapFlags flags)
576 {
577   g_return_val_if_fail (mem != NULL, FALSE);
578   g_return_val_if_fail (info != NULL, FALSE);
579
580   if (!gst_memory_lock (mem, flags))
581     goto lock_failed;
582
583   info->data = mem->allocator->info.mem_map (mem, mem->maxsize, flags);
584
585   if (G_UNLIKELY (info->data == NULL))
586     goto error;
587
588   info->memory = mem;
589   info->flags = flags;
590   info->size = mem->size;
591   info->maxsize = mem->maxsize - mem->offset;
592   info->data = info->data + mem->offset;
593
594   return TRUE;
595
596   /* ERRORS */
597 lock_failed:
598   {
599     GST_CAT_DEBUG (GST_CAT_MEMORY, "mem %p: lock %d failed", mem, flags);
600     return FALSE;
601   }
602 error:
603   {
604     /* something went wrong, restore the orginal state again */
605     GST_CAT_ERROR (GST_CAT_MEMORY, "mem %p: subclass map failed", mem);
606     gst_memory_unlock (mem, flags);
607     return FALSE;
608   }
609 }
610
611 /**
612  * gst_memory_unmap:
613  * @mem: a #GstMemory
614  * @info: a #GstMapInfo
615  *
616  * Release the memory obtained with gst_memory_map()
617  */
618 void
619 gst_memory_unmap (GstMemory * mem, GstMapInfo * info)
620 {
621   g_return_if_fail (mem != NULL);
622   g_return_if_fail (info != NULL);
623   g_return_if_fail (info->memory == mem);
624
625   mem->allocator->info.mem_unmap (mem);
626   gst_memory_unlock (mem, info->flags);
627 }
628
629 /**
630  * gst_memory_copy:
631  * @mem: a #GstMemory
632  * @offset: an offset to copy
633  * @size: size to copy or -1 to copy all bytes from offset
634  *
635  * Return a copy of @size bytes from @mem starting from @offset. This copy is
636  * guaranteed to be writable. @size can be set to -1 to return a copy all bytes
637  * from @offset.
638  *
639  * Returns: a new #GstMemory.
640  */
641 GstMemory *
642 gst_memory_copy (GstMemory * mem, gssize offset, gssize size)
643 {
644   GstMemory *copy;
645
646   g_return_val_if_fail (mem != NULL, NULL);
647
648   copy = mem->allocator->info.mem_copy (mem, offset, size);
649
650   return copy;
651 }
652
653 /**
654  * gst_memory_share:
655  * @mem: a #GstMemory
656  * @offset: an offset to share
657  * @size: size to share or -1 to share bytes from offset
658  *
659  * Return a shared copy of @size bytes from @mem starting from @offset. No
660  * memory copy is performed and the memory region is simply shared. The result
661  * is guaranteed to be not-writable. @size can be set to -1 to return a share
662  * all bytes from @offset.
663  *
664  * Returns: a new #GstMemory.
665  */
666 GstMemory *
667 gst_memory_share (GstMemory * mem, gssize offset, gssize size)
668 {
669   GstMemory *shared;
670
671   g_return_val_if_fail (mem != NULL, NULL);
672   g_return_val_if_fail (!GST_MEMORY_FLAG_IS_SET (mem, GST_MEMORY_FLAG_NO_SHARE),
673       NULL);
674
675   shared = mem->allocator->info.mem_share (mem, offset, size);
676
677   return shared;
678 }
679
680 /**
681  * gst_memory_is_span:
682  * @mem1: a #GstMemory
683  * @mem2: a #GstMemory
684  * @offset: a pointer to a result offset
685  *
686  * Check if @mem1 and mem2 share the memory with a common parent memory object
687  * and that the memory is contiguous.
688  *
689  * If this is the case, the memory of @mem1 and @mem2 can be merged
690  * efficiently by performing gst_memory_share() on the parent object from
691  * the returned @offset.
692  *
693  * Returns: %TRUE if the memory is contiguous and of a common parent.
694  */
695 gboolean
696 gst_memory_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
697 {
698   g_return_val_if_fail (mem1 != NULL, FALSE);
699   g_return_val_if_fail (mem2 != NULL, FALSE);
700
701   /* need to have the same allocators */
702   if (mem1->allocator != mem2->allocator)
703     return FALSE;
704
705   /* need to have the same parent */
706   if (mem1->parent == NULL || mem1->parent != mem2->parent)
707     return FALSE;
708
709   /* and memory is contiguous */
710   if (!mem1->allocator->info.mem_is_span (mem1, mem2, offset))
711     return FALSE;
712
713   return TRUE;
714 }
715
716 static GstAllocator *
717 _gst_allocator_copy (GstAllocator * allocator)
718 {
719   return gst_allocator_ref (allocator);
720 }
721
722 /**
723  * gst_allocator_init:
724  * @allocator: a #GstAllocator
725  * @flags: #GstAllocatorFlags
726  * @info: a #GstMemoryInfo
727  * @free_func: a function to free @allocator
728  *
729  * Initialize a new memory allocator. The caller should have allocated the
730  * memory to hold at least sizeof (GstAllocator) bytes.
731  *
732  * All functions in @info are mandatory exept the copy and is_span
733  * functions, which will have a default implementation when left NULL.
734  *
735  * Returns: a new #GstAllocator.
736  */
737 void
738 gst_allocator_init (GstAllocator * allocator, GstAllocatorFlags flags,
739     const GstMemoryInfo * info, GstMiniObjectFreeFunction free_func)
740 {
741   g_return_if_fail (allocator != NULL);
742   g_return_if_fail (info != NULL);
743   g_return_if_fail (info->alloc != NULL);
744   g_return_if_fail (info->mem_map != NULL);
745   g_return_if_fail (info->mem_unmap != NULL);
746   g_return_if_fail (info->mem_free != NULL);
747   g_return_if_fail (info->mem_share != NULL);
748
749   gst_mini_object_init (GST_MINI_OBJECT_CAST (allocator), flags,
750       GST_TYPE_ALLOCATOR, (GstMiniObjectCopyFunction) _gst_allocator_copy, NULL,
751       (GstMiniObjectFreeFunction) free_func);
752
753   allocator->info = *info;
754
755 #define INSTALL_FALLBACK(_t) \
756   if (allocator->info._t == NULL) allocator->info._t = _fallback_ ##_t;
757   INSTALL_FALLBACK (mem_copy);
758   INSTALL_FALLBACK (mem_is_span);
759 #undef INSTALL_FALLBACK
760
761   GST_CAT_DEBUG (GST_CAT_MEMORY, "init allocator %p", allocator);
762 }
763
764 /**
765  * gst_allocator_register:
766  * @name: the name of the allocator
767  * @allocator: (transfer full): #GstAllocator
768  *
769  * Registers the memory @allocator with @name. This function takes ownership of
770  * @allocator.
771  */
772 void
773 gst_allocator_register (const gchar * name, GstAllocator * allocator)
774 {
775   g_return_if_fail (name != NULL);
776   g_return_if_fail (allocator != NULL);
777
778   GST_CAT_DEBUG (GST_CAT_MEMORY, "registering allocator %p with name \"%s\"",
779       allocator, name);
780
781   g_rw_lock_writer_lock (&lock);
782   g_hash_table_insert (allocators, (gpointer) name, (gpointer) allocator);
783   g_rw_lock_writer_unlock (&lock);
784 }
785
786 /**
787  * gst_allocator_find:
788  * @name: the name of the allocator
789  *
790  * Find a previously registered allocator with @name. When @name is NULL, the
791  * default allocator will be returned.
792  *
793  * Returns: (transfer full): a #GstAllocator or NULL when the allocator with @name was not
794  * registered. Use gst_allocator_unref() to release the allocator after usage.
795  */
796 GstAllocator *
797 gst_allocator_find (const gchar * name)
798 {
799   GstAllocator *allocator;
800
801   g_rw_lock_reader_lock (&lock);
802   if (name) {
803     allocator = g_hash_table_lookup (allocators, (gconstpointer) name);
804   } else {
805     allocator = _default_allocator;
806   }
807   if (allocator)
808     gst_allocator_ref (allocator);
809   g_rw_lock_reader_unlock (&lock);
810
811   return allocator;
812 }
813
814 /**
815  * gst_allocator_set_default:
816  * @allocator: (transfer full): a #GstAllocator
817  *
818  * Set the default allocator. This function takes ownership of @allocator.
819  */
820 void
821 gst_allocator_set_default (GstAllocator * allocator)
822 {
823   GstAllocator *old;
824   g_return_if_fail (allocator != NULL);
825
826   g_rw_lock_writer_lock (&lock);
827   old = _default_allocator;
828   _default_allocator = allocator;
829   g_rw_lock_writer_unlock (&lock);
830
831   if (old)
832     gst_allocator_unref (old);
833 }
834
835 /**
836  * gst_allocation_params_init:
837  * @params: a #GstAllocationParams
838  *
839  * Initialize @params to its default values
840  */
841 void
842 gst_allocation_params_init (GstAllocationParams * params)
843 {
844   g_return_if_fail (params != NULL);
845
846   memset (params, 0, sizeof (GstAllocationParams));
847 }
848
849 /**
850  * gst_allocation_params_copy:
851  * @params: (transfer none): a #GstAllocationParams
852  *
853  * Create a copy of @params.
854  *
855  * Free-function: gst_allocation_params_free
856  *
857  * Returns: (transfer full): a new ##GstAllocationParams, free with
858  * gst_allocation_params_free().
859  */
860 GstAllocationParams *
861 gst_allocation_params_copy (const GstAllocationParams * params)
862 {
863   GstAllocationParams *result = NULL;
864
865   if (params) {
866     result =
867         (GstAllocationParams *) g_slice_copy (sizeof (GstAllocationParams),
868         params);
869   }
870   return result;
871 }
872
873 /**
874  * gst_allocation_params_free:
875  * @params: (in) (transfer full): a #GstAllocationParams
876  *
877  * Free @params
878  */
879 void
880 gst_allocation_params_free (GstAllocationParams * params)
881 {
882   g_slice_free (GstAllocationParams, params);
883 }
884
885 /**
886  * gst_allocator_alloc:
887  * @allocator: (transfer none) (allow-none): a #GstAllocator to use
888  * @size: size of the visible memory area
889  * @params: (transfer none) (allow-none): optional parameters
890  *
891  * Use @allocator to allocate a new memory block with memory that is at least
892  * @size big.
893  *
894  * The optional @params can specify the prefix and padding for the memory. If
895  * NULL is passed, no flags, no extra prefix/padding and a default alignment is
896  * used.
897  *
898  * The prefix/padding will be filled with 0 if flags contains
899  * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED respectively.
900  *
901  * When @allocator is NULL, the default allocator will be used.
902  *
903  * The alignment in @params is given as a bitmask so that @align + 1 equals
904  * the amount of bytes to align to. For example, to align to 8 bytes,
905  * use an alignment of 7.
906  *
907  * Returns: (transfer full): a new #GstMemory.
908  */
909 GstMemory *
910 gst_allocator_alloc (GstAllocator * allocator, gsize size,
911     GstAllocationParams * params)
912 {
913   GstMemory *mem;
914   static GstAllocationParams defparams = { 0, 0, 0, 0, };
915
916   if (params) {
917     g_return_val_if_fail (((params->align + 1) & params->align) == 0, NULL);
918   } else {
919     params = &defparams;
920   }
921
922   if (allocator == NULL)
923     allocator = _default_allocator;
924
925   mem = allocator->info.alloc (allocator, size, params, NULL);
926
927   return mem;
928 }