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