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