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