Port gtk-doc comments to their equivalent markdown syntax
[platform/upstream/gstreamer.git] / gst / gstallocator.c
1 /* GStreamer
2  * Copyright (C) 2011 Wim Taymans <wim.taymans@gmail.be>
3  *
4  * gstallocator.c: memory block allocator
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., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 /**
23  * SECTION:gstallocator
24  * @title: GstAllocator
25  * @short_description: allocate memory blocks
26  * @see_also: #GstMemory
27  *
28  * Memory is usually created by allocators with a gst_allocator_alloc()
29  * method call. When %NULL is used as the allocator, the default allocator will
30  * be used.
31  *
32  * New allocators can be registered with gst_allocator_register().
33  * Allocators are identified by name and can be retrieved with
34  * gst_allocator_find(). gst_allocator_set_default() can be used to change the
35  * default allocator.
36  *
37  * New memory can be created with gst_memory_new_wrapped() that wraps the memory
38  * allocated elsewhere.
39  */
40
41 #ifdef HAVE_CONFIG_H
42 #include "config.h"
43 #endif
44
45 #include "gst_private.h"
46 #include "gstmemory.h"
47
48 GST_DEBUG_CATEGORY_STATIC (gst_allocator_debug);
49 #define GST_CAT_DEFAULT gst_allocator_debug
50
51 #define GST_ALLOCATOR_GET_PRIVATE(obj)  \
52      (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_ALLOCATOR, GstAllocatorPrivate))
53
54 struct _GstAllocatorPrivate
55 {
56   gpointer dummy;
57 };
58
59 #if defined(MEMORY_ALIGNMENT_MALLOC)
60 gsize gst_memory_alignment = 7;
61 #elif defined(MEMORY_ALIGNMENT_PAGESIZE)
62 /* we fill this in in the _init method */
63 gsize gst_memory_alignment = 0;
64 #elif defined(MEMORY_ALIGNMENT)
65 gsize gst_memory_alignment = MEMORY_ALIGNMENT - 1;
66 #else
67 #error "No memory alignment configured"
68 gsize gst_memory_alignment = 0;
69 #endif
70
71 /* the default allocator */
72 static GstAllocator *_default_allocator;
73
74 static GstAllocator *_sysmem_allocator;
75
76 /* registered allocators */
77 static GRWLock lock;
78 static GHashTable *allocators;
79
80 G_DEFINE_ABSTRACT_TYPE (GstAllocator, gst_allocator, GST_TYPE_OBJECT);
81
82 static void
83 gst_allocator_class_init (GstAllocatorClass * klass)
84 {
85   g_type_class_add_private (klass, sizeof (GstAllocatorPrivate));
86
87   GST_DEBUG_CATEGORY_INIT (gst_allocator_debug, "allocator", 0,
88       "allocator debug");
89 }
90
91 static GstMemory *
92 _fallback_mem_copy (GstMemory * mem, gssize offset, gssize size)
93 {
94   GstMemory *copy;
95   GstMapInfo sinfo, dinfo;
96   GstAllocationParams params = { 0, mem->align, 0, 0, };
97   GstAllocator *allocator;
98
99   if (!gst_memory_map (mem, &sinfo, GST_MAP_READ))
100     return NULL;
101
102   if (size == -1)
103     size = sinfo.size > offset ? sinfo.size - offset : 0;
104
105   /* use the same allocator as the memory we copy  */
106   allocator = mem->allocator;
107   if (GST_OBJECT_FLAG_IS_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC))
108     allocator = NULL;
109   copy = gst_allocator_alloc (allocator, size, &params);
110
111   if (!gst_memory_map (copy, &dinfo, GST_MAP_WRITE)) {
112     GST_CAT_WARNING (GST_CAT_MEMORY, "could not write map memory %p", copy);
113     gst_allocator_free (mem->allocator, copy);
114     gst_memory_unmap (mem, &sinfo);
115     return NULL;
116   }
117
118   GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
119       "memcpy %" G_GSSIZE_FORMAT " memory %p -> %p", size, mem, copy);
120   memcpy (dinfo.data, sinfo.data + offset, size);
121   gst_memory_unmap (copy, &dinfo);
122   gst_memory_unmap (mem, &sinfo);
123
124   return copy;
125 }
126
127 static gboolean
128 _fallback_mem_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
129 {
130   return FALSE;
131 }
132
133 static void
134 gst_allocator_init (GstAllocator * allocator)
135 {
136   allocator->priv = GST_ALLOCATOR_GET_PRIVATE (allocator);
137
138   allocator->mem_copy = _fallback_mem_copy;
139   allocator->mem_is_span = _fallback_mem_is_span;
140 }
141
142 G_DEFINE_BOXED_TYPE (GstAllocationParams, gst_allocation_params,
143     (GBoxedCopyFunc) gst_allocation_params_copy,
144     (GBoxedFreeFunc) gst_allocation_params_free);
145
146 /**
147  * gst_allocation_params_init:
148  * @params: a #GstAllocationParams
149  *
150  * Initialize @params to its default values
151  */
152 void
153 gst_allocation_params_init (GstAllocationParams * params)
154 {
155   g_return_if_fail (params != NULL);
156
157   memset (params, 0, sizeof (GstAllocationParams));
158 }
159
160 /**
161  * gst_allocation_params_copy:
162  * @params: (transfer none): a #GstAllocationParams
163  *
164  * Create a copy of @params.
165  *
166  * Free-function: gst_allocation_params_free
167  *
168  * Returns: (transfer full): a new ##GstAllocationParams, free with
169  * gst_allocation_params_free().
170  */
171 GstAllocationParams *
172 gst_allocation_params_copy (const GstAllocationParams * params)
173 {
174   GstAllocationParams *result = NULL;
175
176   if (params) {
177     result =
178         (GstAllocationParams *) g_slice_copy (sizeof (GstAllocationParams),
179         params);
180   }
181   return result;
182 }
183
184 /**
185  * gst_allocation_params_free:
186  * @params: (in) (transfer full): a #GstAllocationParams
187  *
188  * Free @params
189  */
190 void
191 gst_allocation_params_free (GstAllocationParams * params)
192 {
193   g_slice_free (GstAllocationParams, params);
194 }
195
196 /**
197  * gst_allocator_register:
198  * @name: the name of the allocator
199  * @allocator: (transfer full): #GstAllocator
200  *
201  * Registers the memory @allocator with @name. This function takes ownership of
202  * @allocator.
203  */
204 void
205 gst_allocator_register (const gchar * name, GstAllocator * allocator)
206 {
207   g_return_if_fail (name != NULL);
208   g_return_if_fail (allocator != NULL);
209
210   GST_CAT_DEBUG (GST_CAT_MEMORY, "registering allocator %p with name \"%s\"",
211       allocator, name);
212
213   g_rw_lock_writer_lock (&lock);
214   /* The ref will never be released */
215   GST_OBJECT_FLAG_SET (allocator, GST_OBJECT_FLAG_MAY_BE_LEAKED);
216   g_hash_table_insert (allocators, (gpointer) name, (gpointer) allocator);
217   g_rw_lock_writer_unlock (&lock);
218 }
219
220 /**
221  * gst_allocator_find:
222  * @name: (allow-none): the name of the allocator
223  *
224  * Find a previously registered allocator with @name. When @name is %NULL, the
225  * default allocator will be returned.
226  *
227  * Returns: (transfer full) (nullable): a #GstAllocator or %NULL when
228  * the allocator with @name was not registered. Use gst_object_unref()
229  * to release the allocator after usage.
230  */
231 GstAllocator *
232 gst_allocator_find (const gchar * name)
233 {
234   GstAllocator *allocator;
235
236   g_rw_lock_reader_lock (&lock);
237   if (name) {
238     allocator = g_hash_table_lookup (allocators, (gconstpointer) name);
239   } else {
240     allocator = _default_allocator;
241   }
242   if (allocator)
243     gst_object_ref (allocator);
244   g_rw_lock_reader_unlock (&lock);
245
246   return allocator;
247 }
248
249 /**
250  * gst_allocator_set_default:
251  * @allocator: (transfer full): a #GstAllocator
252  *
253  * Set the default allocator. This function takes ownership of @allocator.
254  */
255 void
256 gst_allocator_set_default (GstAllocator * allocator)
257 {
258   GstAllocator *old;
259
260   g_return_if_fail (GST_IS_ALLOCATOR (allocator));
261
262   g_rw_lock_writer_lock (&lock);
263   old = _default_allocator;
264   _default_allocator = allocator;
265   g_rw_lock_writer_unlock (&lock);
266
267   if (old)
268     gst_object_unref (old);
269 }
270
271 /**
272  * gst_allocator_alloc:
273  * @allocator: (transfer none) (allow-none): a #GstAllocator to use
274  * @size: size of the visible memory area
275  * @params: (transfer none) (allow-none): optional parameters
276  *
277  * Use @allocator to allocate a new memory block with memory that is at least
278  * @size big.
279  *
280  * The optional @params can specify the prefix and padding for the memory. If
281  * %NULL is passed, no flags, no extra prefix/padding and a default alignment is
282  * used.
283  *
284  * The prefix/padding will be filled with 0 if flags contains
285  * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED respectively.
286  *
287  * When @allocator is %NULL, the default allocator will be used.
288  *
289  * The alignment in @params is given as a bitmask so that @align + 1 equals
290  * the amount of bytes to align to. For example, to align to 8 bytes,
291  * use an alignment of 7.
292  *
293  * Returns: (transfer full): a new #GstMemory.
294  */
295 GstMemory *
296 gst_allocator_alloc (GstAllocator * allocator, gsize size,
297     GstAllocationParams * params)
298 {
299   GstMemory *mem;
300   static GstAllocationParams defparams = { 0, 0, 0, 0, };
301   GstAllocatorClass *aclass;
302
303   if (params) {
304     g_return_val_if_fail (((params->align + 1) & params->align) == 0, NULL);
305   } else {
306     params = &defparams;
307   }
308
309   if (allocator == NULL)
310     allocator = _default_allocator;
311
312   aclass = GST_ALLOCATOR_GET_CLASS (allocator);
313   if (aclass->alloc)
314     mem = aclass->alloc (allocator, size, params);
315   else
316     mem = NULL;
317
318   return mem;
319 }
320
321 /**
322  * gst_allocator_free:
323  * @allocator: (transfer none): a #GstAllocator to use
324  * @memory: (transfer full): the memory to free
325  *
326  * Free @memory that was previously allocated with gst_allocator_alloc().
327  */
328 void
329 gst_allocator_free (GstAllocator * allocator, GstMemory * memory)
330 {
331   GstAllocatorClass *aclass;
332
333   g_return_if_fail (GST_IS_ALLOCATOR (allocator));
334   g_return_if_fail (memory != NULL);
335   g_return_if_fail (memory->allocator == allocator);
336
337   aclass = GST_ALLOCATOR_GET_CLASS (allocator);
338   if (aclass->free)
339     aclass->free (allocator, memory);
340 }
341
342 /* default memory implementation */
343 typedef struct
344 {
345   GstMemory mem;
346
347   gsize slice_size;
348   guint8 *data;
349
350   gpointer user_data;
351   GDestroyNotify notify;
352 } GstMemorySystem;
353
354 typedef struct
355 {
356   GstAllocator parent;
357 } GstAllocatorSysmem;
358
359 typedef struct
360 {
361   GstAllocatorClass parent_class;
362 } GstAllocatorSysmemClass;
363
364 GType gst_allocator_sysmem_get_type (void);
365 G_DEFINE_TYPE (GstAllocatorSysmem, gst_allocator_sysmem, GST_TYPE_ALLOCATOR);
366
367 /* initialize the fields */
368 static inline void
369 _sysmem_init (GstMemorySystem * mem, GstMemoryFlags flags,
370     GstMemory * parent, gsize slice_size,
371     gpointer data, gsize maxsize, gsize align, gsize offset, gsize size,
372     gpointer user_data, GDestroyNotify notify)
373 {
374   gst_memory_init (GST_MEMORY_CAST (mem),
375       flags, _sysmem_allocator, parent, maxsize, align, offset, size);
376
377   mem->slice_size = slice_size;
378   mem->data = data;
379   mem->user_data = user_data;
380   mem->notify = notify;
381 }
382
383 /* create a new memory block that manages the given memory */
384 static inline GstMemorySystem *
385 _sysmem_new (GstMemoryFlags flags,
386     GstMemory * parent, gpointer data, gsize maxsize, gsize align, gsize offset,
387     gsize size, gpointer user_data, GDestroyNotify notify)
388 {
389   GstMemorySystem *mem;
390   gsize slice_size;
391
392   slice_size = sizeof (GstMemorySystem);
393
394   mem = g_slice_alloc (slice_size);
395   _sysmem_init (mem, flags, parent, slice_size,
396       data, maxsize, align, offset, size, user_data, notify);
397
398   return mem;
399 }
400
401 /* allocate the memory and structure in one block */
402 static GstMemorySystem *
403 _sysmem_new_block (GstMemoryFlags flags,
404     gsize maxsize, gsize align, gsize offset, gsize size)
405 {
406   GstMemorySystem *mem;
407   gsize aoffset, slice_size, padding;
408   guint8 *data;
409
410   /* ensure configured alignment */
411   align |= gst_memory_alignment;
412   /* allocate more to compensate for alignment */
413   maxsize += align;
414   /* alloc header and data in one block */
415   slice_size = sizeof (GstMemorySystem) + maxsize;
416
417   mem = g_slice_alloc (slice_size);
418   if (mem == NULL)
419     return NULL;
420
421   data = (guint8 *) mem + sizeof (GstMemorySystem);
422
423   /* do alignment */
424   if ((aoffset = ((guintptr) data & align))) {
425     aoffset = (align + 1) - aoffset;
426     data += aoffset;
427     maxsize -= aoffset;
428   }
429
430   if (offset && (flags & GST_MEMORY_FLAG_ZERO_PREFIXED))
431     memset (data, 0, offset);
432
433   padding = maxsize - (offset + size);
434   if (padding && (flags & GST_MEMORY_FLAG_ZERO_PADDED))
435     memset (data + offset + size, 0, padding);
436
437   _sysmem_init (mem, flags, NULL, slice_size, data, maxsize,
438       align, offset, size, NULL, NULL);
439
440   return mem;
441 }
442
443 static gpointer
444 _sysmem_map (GstMemorySystem * mem, gsize maxsize, GstMapFlags flags)
445 {
446   return mem->data;
447 }
448
449 static gboolean
450 _sysmem_unmap (GstMemorySystem * mem)
451 {
452   return TRUE;
453 }
454
455 static GstMemorySystem *
456 _sysmem_copy (GstMemorySystem * mem, gssize offset, gsize size)
457 {
458   GstMemorySystem *copy;
459
460   if (size == -1)
461     size = mem->mem.size > offset ? mem->mem.size - offset : 0;
462
463   copy = _sysmem_new_block (0, size, mem->mem.align, 0, size);
464   GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
465       "memcpy %" G_GSIZE_FORMAT " memory %p -> %p", size, mem, copy);
466   memcpy (copy->data, mem->data + mem->mem.offset + offset, size);
467
468   return copy;
469 }
470
471 static GstMemorySystem *
472 _sysmem_share (GstMemorySystem * mem, gssize offset, gsize size)
473 {
474   GstMemorySystem *sub;
475   GstMemory *parent;
476
477   /* find the real parent */
478   if ((parent = mem->mem.parent) == NULL)
479     parent = (GstMemory *) mem;
480
481   if (size == -1)
482     size = mem->mem.size - offset;
483
484   /* the shared memory is always readonly */
485   sub =
486       _sysmem_new (GST_MINI_OBJECT_FLAGS (parent) |
487       GST_MINI_OBJECT_FLAG_LOCK_READONLY, parent, mem->data, mem->mem.maxsize,
488       mem->mem.align, mem->mem.offset + offset, size, NULL, NULL);
489
490   return sub;
491 }
492
493 static gboolean
494 _sysmem_is_span (GstMemorySystem * mem1, GstMemorySystem * mem2, gsize * offset)
495 {
496
497   if (offset) {
498     GstMemorySystem *parent;
499
500     parent = (GstMemorySystem *) mem1->mem.parent;
501
502     *offset = mem1->mem.offset - parent->mem.offset;
503   }
504
505   /* and memory is contiguous */
506   return mem1->data + mem1->mem.offset + mem1->mem.size ==
507       mem2->data + mem2->mem.offset;
508 }
509
510 static GstMemory *
511 default_alloc (GstAllocator * allocator, gsize size,
512     GstAllocationParams * params)
513 {
514   gsize maxsize = size + params->prefix + params->padding;
515
516   return (GstMemory *) _sysmem_new_block (params->flags,
517       maxsize, params->align, params->prefix, size);
518 }
519
520 static void
521 default_free (GstAllocator * allocator, GstMemory * mem)
522 {
523   GstMemorySystem *dmem = (GstMemorySystem *) mem;
524   gsize slice_size;
525
526   if (dmem->notify)
527     dmem->notify (dmem->user_data);
528
529   slice_size = dmem->slice_size;
530
531 #ifdef USE_POISONING
532   /* just poison the structs, not all the data */
533   memset (mem, 0xff, sizeof (GstMemorySystem));
534 #endif
535
536   g_slice_free1 (slice_size, mem);
537 }
538
539 static void
540 gst_allocator_sysmem_finalize (GObject * obj)
541 {
542   /* Don't raise warnings if we are shutting down */
543   if (_default_allocator)
544     g_warning ("The default memory allocator was freed!");
545
546   ((GObjectClass *) gst_allocator_sysmem_parent_class)->finalize (obj);
547 }
548
549 static void
550 gst_allocator_sysmem_class_init (GstAllocatorSysmemClass * klass)
551 {
552   GObjectClass *gobject_class;
553   GstAllocatorClass *allocator_class;
554
555   gobject_class = (GObjectClass *) klass;
556   allocator_class = (GstAllocatorClass *) klass;
557
558   gobject_class->finalize = gst_allocator_sysmem_finalize;
559
560   allocator_class->alloc = default_alloc;
561   allocator_class->free = default_free;
562 }
563
564 static void
565 gst_allocator_sysmem_init (GstAllocatorSysmem * allocator)
566 {
567   GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
568
569   GST_CAT_DEBUG (GST_CAT_MEMORY, "init allocator %p", allocator);
570
571   alloc->mem_type = GST_ALLOCATOR_SYSMEM;
572   alloc->mem_map = (GstMemoryMapFunction) _sysmem_map;
573   alloc->mem_unmap = (GstMemoryUnmapFunction) _sysmem_unmap;
574   alloc->mem_copy = (GstMemoryCopyFunction) _sysmem_copy;
575   alloc->mem_share = (GstMemoryShareFunction) _sysmem_share;
576   alloc->mem_is_span = (GstMemoryIsSpanFunction) _sysmem_is_span;
577 }
578
579 void
580 _priv_gst_allocator_initialize (void)
581 {
582   g_rw_lock_init (&lock);
583   allocators = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
584       gst_object_unref);
585
586 #ifdef HAVE_GETPAGESIZE
587 #ifdef MEMORY_ALIGNMENT_PAGESIZE
588   gst_memory_alignment = getpagesize () - 1;
589 #endif
590 #endif
591
592   GST_CAT_DEBUG (GST_CAT_MEMORY, "memory alignment: %" G_GSIZE_FORMAT,
593       gst_memory_alignment);
594
595   _sysmem_allocator = g_object_new (gst_allocator_sysmem_get_type (), NULL);
596
597   gst_allocator_register (GST_ALLOCATOR_SYSMEM,
598       gst_object_ref (_sysmem_allocator));
599
600   _default_allocator = gst_object_ref (_sysmem_allocator);
601 }
602
603 void
604 _priv_gst_allocator_cleanup (void)
605 {
606   gst_object_unref (_sysmem_allocator);
607   _sysmem_allocator = NULL;
608
609   gst_object_unref (_default_allocator);
610   _default_allocator = NULL;
611
612   g_clear_pointer (&allocators, g_hash_table_unref);
613 }
614
615 /**
616  * gst_memory_new_wrapped:
617  * @flags: #GstMemoryFlags
618  * @data: (array length=size) (element-type guint8) (transfer none): data to
619  *   wrap
620  * @maxsize: allocated size of @data
621  * @offset: offset in @data
622  * @size: size of valid data
623  * @user_data: (allow-none): user_data
624  * @notify: (allow-none) (scope async) (closure user_data): called with @user_data when the memory is freed
625  *
626  * Allocate a new memory block that wraps the given @data.
627  *
628  * The prefix/padding must be filled with 0 if @flags contains
629  * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED respectively.
630  *
631  * Returns: (transfer full): a new #GstMemory.
632  */
633 GstMemory *
634 gst_memory_new_wrapped (GstMemoryFlags flags, gpointer data,
635     gsize maxsize, gsize offset, gsize size, gpointer user_data,
636     GDestroyNotify notify)
637 {
638   GstMemorySystem *mem;
639
640   g_return_val_if_fail (data != NULL, NULL);
641   g_return_val_if_fail (offset + size <= maxsize, NULL);
642
643   mem =
644       _sysmem_new (flags, NULL, data, maxsize, 0, offset, size, user_data,
645       notify);
646
647   return (GstMemory *) mem;
648 }