Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1-only */
2 : /**
3 : * Copyright (C) 2021 Junhwan Kim <jejudo.kim@samsung.com>
4 : *
5 : * @file tensor_allocator.c
6 : * @date 12 May 2021
7 : * @brief Allocator for memory alignment
8 : * @author Junhwan Kim <jejudo.kim@samsung.com>
9 : * @see http://github.com/nnstreamer/nnstreamer
10 : * @bug No known bugs
11 : *
12 : */
13 :
14 : #include <gst/gst.h>
15 : #include "nnstreamer_plugin_api.h"
16 :
17 : #define GST_TENSOR_ALLOCATOR "GstTensorAllocator"
18 :
19 : static gsize gst_tensor_allocator_alignment = 0;
20 :
21 : /**
22 : * @brief struct for type GstTensorAllocator
23 : */
24 : typedef struct
25 : {
26 : GstAllocator parent;
27 : } GstTensorAllocator;
28 :
29 : /**
30 : * @brief struct for class GstTensorAllocatorClass
31 : */
32 : typedef struct
33 : {
34 : GstAllocatorClass parent_class;
35 : } GstTensorAllocatorClass;
36 :
37 :
38 : static GType gst_tensor_allocator_get_type (void);
39 3 : G_DEFINE_TYPE (GstTensorAllocator, gst_tensor_allocator, GST_TYPE_ALLOCATOR);
40 :
41 : /**
42 : * @brief allocation wrapper that binds alignment parameter
43 : */
44 : static GstMemory *
45 3 : _alloc (GstAllocator * allocator, gsize size, GstAllocationParams * params)
46 : {
47 : GstAllocator *sysmem_alloc;
48 : GstAllocatorClass *sysmem_aclass;
49 : GstAllocationParams *_params;
50 : GstMemory *mem;
51 :
52 3 : sysmem_alloc = gst_allocator_find (GST_ALLOCATOR_SYSMEM);
53 3 : sysmem_aclass = GST_ALLOCATOR_GET_CLASS (sysmem_alloc);
54 3 : _params = gst_allocation_params_copy (params);
55 3 : _params->align = gst_tensor_allocator_alignment;
56 :
57 3 : mem = sysmem_aclass->alloc (allocator, size, _params);
58 :
59 3 : gst_allocation_params_free (_params);
60 3 : gst_object_unref (sysmem_alloc);
61 3 : return mem;
62 : }
63 :
64 : /**
65 : * @brief class initization for GstTensorAllocatorClass
66 : */
67 : static void
68 1 : gst_tensor_allocator_class_init (GstTensorAllocatorClass * klass)
69 : {
70 : GstAllocatorClass *allocator_class, *sysmem_aclass;
71 : GstAllocator *sysmem_alloc;
72 :
73 1 : allocator_class = (GstAllocatorClass *) klass;
74 1 : sysmem_alloc = gst_allocator_find (GST_ALLOCATOR_SYSMEM);
75 1 : sysmem_aclass = GST_ALLOCATOR_GET_CLASS (sysmem_alloc);
76 :
77 1 : allocator_class->alloc = _alloc;
78 1 : allocator_class->free = sysmem_aclass->free;
79 :
80 1 : gst_object_unref (sysmem_alloc);
81 1 : }
82 :
83 : /**
84 : * @brief initialization for GstTensorAllocator
85 : */
86 : static void
87 1 : gst_tensor_allocator_init (GstTensorAllocator * allocator)
88 : {
89 : GstAllocator *sysmem_alloc, *alloc;
90 :
91 1 : alloc = GST_ALLOCATOR_CAST (allocator);
92 1 : sysmem_alloc = gst_allocator_find (GST_ALLOCATOR_SYSMEM);
93 :
94 1 : alloc->mem_type = sysmem_alloc->mem_type;
95 1 : alloc->mem_map = sysmem_alloc->mem_map;
96 1 : alloc->mem_unmap = sysmem_alloc->mem_unmap;
97 1 : alloc->mem_copy = sysmem_alloc->mem_copy;
98 1 : alloc->mem_share = sysmem_alloc->mem_share;
99 1 : alloc->mem_is_span = sysmem_alloc->mem_is_span;
100 :
101 1 : gst_object_unref (sysmem_alloc);
102 1 : }
103 :
104 : /**
105 : * @brief set alignment that default allocator would align to
106 : * @param alignment bytes of alignment
107 : */
108 : void
109 1 : gst_tensor_alloc_init (gsize alignment)
110 : {
111 : GstAllocator *allocator;
112 :
113 1 : gst_tensor_allocator_alignment = alignment;
114 :
115 : /* no alignment */
116 1 : if (alignment == 0) {
117 0 : gst_allocator_set_default (gst_allocator_find (GST_ALLOCATOR_SYSMEM));
118 0 : return;
119 : }
120 :
121 1 : allocator = gst_allocator_find (GST_TENSOR_ALLOCATOR);
122 : /* allocator already set */
123 1 : if (allocator == NULL) {
124 1 : allocator = g_object_new (gst_tensor_allocator_get_type (), NULL);
125 1 : gst_allocator_register (GST_TENSOR_ALLOCATOR, gst_object_ref (allocator));
126 : }
127 1 : gst_allocator_set_default (allocator);
128 : }
|