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