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