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