Doxygen Book
tensor_allocator.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: LGPL-2.1-only */
14 #include <gst/gst.h>
15 #include "nnstreamer_plugin_api.h"
16 
17 #define GST_TENSOR_ALLOCATOR "GstTensorAllocator"
18 
20 
24 typedef struct
25 {
26  GstAllocator parent;
28 
32 typedef struct
33 {
34  GstAllocatorClass parent_class;
36 
37 
38 static GType gst_tensor_allocator_get_type (void);
39 G_DEFINE_TYPE (GstTensorAllocator, gst_tensor_allocator, GST_TYPE_ALLOCATOR);
40 
44 static GstMemory *
45 _alloc (GstAllocator * allocator, gsize size, GstAllocationParams * params)
46 {
47  GstAllocator *sysmem_alloc;
48  GstAllocatorClass *sysmem_aclass;
49  GstAllocationParams *_params;
50  GstMemory *mem;
51 
52  sysmem_alloc = gst_allocator_find (GST_ALLOCATOR_SYSMEM);
53  sysmem_aclass = GST_ALLOCATOR_GET_CLASS (sysmem_alloc);
54  _params = gst_allocation_params_copy (params);
55  _params->align = gst_tensor_allocator_alignment;
56 
57  mem = sysmem_aclass->alloc (allocator, size, _params);
58 
59  gst_allocation_params_free (_params);
60  gst_object_unref (sysmem_alloc);
61  return mem;
62 }
63 
67 static void
69 {
70  GstAllocatorClass *allocator_class, *sysmem_aclass;
71  GstAllocator *sysmem_alloc;
72 
73  allocator_class = (GstAllocatorClass *) klass;
74  sysmem_alloc = gst_allocator_find (GST_ALLOCATOR_SYSMEM);
75  sysmem_aclass = GST_ALLOCATOR_GET_CLASS (sysmem_alloc);
76 
77  allocator_class->alloc = _alloc;
78  allocator_class->free = sysmem_aclass->free;
79 
80  gst_object_unref (sysmem_alloc);
81 }
82 
86 static void
88 {
89  GstAllocator *sysmem_alloc, *alloc;
90 
91  alloc = GST_ALLOCATOR_CAST (allocator);
92  sysmem_alloc = gst_allocator_find (GST_ALLOCATOR_SYSMEM);
93 
94  alloc->mem_type = sysmem_alloc->mem_type;
95  alloc->mem_map = sysmem_alloc->mem_map;
96  alloc->mem_unmap = sysmem_alloc->mem_unmap;
97  alloc->mem_copy = sysmem_alloc->mem_copy;
98  alloc->mem_share = sysmem_alloc->mem_share;
99  alloc->mem_is_span = sysmem_alloc->mem_is_span;
100 
101  gst_object_unref (sysmem_alloc);
102 }
103 
108 void
109 gst_tensor_alloc_init (gsize alignment)
110 {
111  GstAllocator *allocator;
112 
113  gst_tensor_allocator_alignment = alignment;
114 
115  /* no alignment */
116  if (alignment == 0) {
117  gst_allocator_set_default (gst_allocator_find (GST_ALLOCATOR_SYSMEM));
118  return;
119  }
120 
121  allocator = gst_allocator_find (GST_TENSOR_ALLOCATOR);
122  /* allocator already set */
123  if (allocator == NULL) {
124  allocator = g_object_new (gst_tensor_allocator_get_type (), NULL);
125  gst_allocator_register (GST_TENSOR_ALLOCATOR, gst_object_ref (allocator));
126  }
127  gst_allocator_set_default (allocator);
128 }
GstTensorAllocatorClass
struct for class GstTensorAllocatorClass
Definition: tensor_allocator.c:32
GstTensorAllocatorClass::parent_class
GstAllocatorClass parent_class
Definition: tensor_allocator.c:34
GST_TENSOR_ALLOCATOR
#define GST_TENSOR_ALLOCATOR
Definition: tensor_allocator.c:17
G_DEFINE_TYPE
G_DEFINE_TYPE(GstTensorAllocator, gst_tensor_allocator, GST_TYPE_ALLOCATOR)
gst_tensor_allocator_init
static void gst_tensor_allocator_init(GstTensorAllocator *allocator)
initialization for GstTensorAllocator
Definition: tensor_allocator.c:87
gst_tensor_allocator_get_type
static GType gst_tensor_allocator_get_type(void)
GstTensorAllocator::parent
GstAllocator parent
Definition: tensor_allocator.c:26
gst_tensor_allocator_class_init
static void gst_tensor_allocator_class_init(GstTensorAllocatorClass *klass)
class initization for GstTensorAllocatorClass
Definition: tensor_allocator.c:68
_alloc
static GstMemory * _alloc(GstAllocator *allocator, gsize size, GstAllocationParams *params)
allocation wrapper that binds alignment parameter
Definition: tensor_allocator.c:45
gst_tensor_allocator_alignment
static gsize gst_tensor_allocator_alignment
Definition: tensor_allocator.c:19
gst_tensor_alloc_init
void gst_tensor_alloc_init(gsize alignment)
set alignment that default allocator would align to
Definition: tensor_allocator.c:109
GstTensorAllocator
struct for type GstTensorAllocator
Definition: tensor_allocator.c:24
nnstreamer_plugin_api.h
Optional/Additional NNStreamer APIs for sub-plugin writers. (Need Gst devel)