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