Dawn Framework 1.0
Universal data acquisition framework for embedded systems
ddata.hxx
1// dawn/include/dawn/io/ddata.hxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#pragma once
7
8#include <cassert>
9#include <cstddef>
10#include <cstdlib>
11#include <new>
12
13#include "dawn/io/idata.hxx"
14#include "dawn/porting/config.hxx"
15
16namespace dawn
17{
20struct io_ddata_t : public io_data_cmn_t
21{
22 uint8_t *data; //< Heap-allocated data buffer.
23 size_t T; //< Element size in bytes (1, 2, 4, or 8).
24 size_t N; //< Number of elements per batch.
25 size_t M; //< Number of batches.
26 size_t len; //< Total buffer length in bytes.
27 size_t off; //< Aligned batch offset in bytes.
28 uint8_t dtype; //< Data type enum for introspection.
29 bool hasTs; //< Whether this buffer has per-batch timestamp storage.
30 io_ts_t dummyTs; //< Dummy timestamp for API compatibility when hasTs is false.
31
34 constexpr size_t ALIGN_TO_WORD_SIZE(size_t size, size_t word_size)
35 {
36 return (size + word_size - 1) & ~(word_size - 1);
37 };
38
49 explicit io_ddata_t(size_t t, size_t n = 1, size_t m = 1, uint8_t dt = 0, bool ts = false)
50 : dummyTs(0)
51 {
52 size_t tsSize = ts ? sizeof(io_ts_t) : 0;
53
54 assert(t == 1 || t == 2 || t == 4 || t == 8);
55
56 this->T = t;
57 this->N = n;
58 this->M = m;
59 this->dtype = dt;
60 this->hasTs = ts;
61 this->off = ts ? ALIGN_TO_WORD_SIZE(t * n + tsSize, sizeof(uintptr_t)) : t * n;
62 this->len = this->off * m;
63
64 /* Allocate data buffer */
65
66 this->data = new (std::nothrow) uint8_t[this->len]();
67 }
68
69 io_ddata_t(const io_ddata_t &) = delete;
70 io_ddata_t &operator=(const io_ddata_t &) = delete;
71
74 ~io_ddata_t() override
75 {
76 delete[] data;
77 };
78
79 bool isAllocated() const
80 {
81 return data != nullptr;
82 }
83
92 template<typename T>
93 constexpr T &get(size_t index, size_t batch = 0)
94 {
95 size_t tsOff = this->hasTs ? sizeof(io_ts_t) : 0;
96
97 assert(index < this->N);
98 assert(batch < this->M);
99
100 size_t i = this->off * batch + index * sizeof(T) + tsOff;
101 T *ptr = reinterpret_cast<T *>(&data[i]);
102
103 return *ptr;
104 }
105
113 io_ts_t &operator[](size_t batch)
114 {
115 assert(batch < this->M);
116 return this->getTs(batch);
117 }
118
121 constexpr size_t getSize() const
122 {
123 return this->T;
124 }
125
128 constexpr size_t getItems() override
129 {
130 return this->N;
131 }
132
135 constexpr size_t getBatch() const
136 {
137 return this->M;
138 }
139
142 constexpr size_t getBufferSize() const
143 {
144 return this->len;
145 }
146
153 constexpr size_t getDataSize() override
154 {
155 return this->T * this->N;
156 }
157
168 constexpr void *getPtr(size_t batch = 0) override
169 {
170 return (void *)&data[this->off * batch];
171 }
172
180 constexpr void *getDataPtr(size_t batch = 0) override
181 {
182 size_t tsOff = this->hasTs ? sizeof(io_ts_t) : 0;
183 return (void *)&data[this->off * batch + tsOff];
184 }
185
188 bool hasTimestamp() override
189 {
190 return this->hasTs;
191 }
192
203 io_ts_t &getTs(size_t batch = 0) override
204 {
205 if (!this->hasTs)
206 {
207 return dummyTs;
208 }
209
210 size_t i = this->off * batch;
211 io_ts_t *ptr = reinterpret_cast<io_ts_t *>(&this->data[i]);
212 return *ptr;
213 };
214
217 constexpr uint8_t getDtype() const
218 {
219 return this->dtype;
220 }
221};
222
223} // Namespace dawn
Out-of-tree user-extension hooks for Dawn.
Definition bindable.hxx:13
uint64_t io_ts_t
Timestamp data type (uint64_t, typically microseconds since boot).
Definition idata.hxx:16
Heap-allocated dynamic I/O data buffer.
Definition ddata.hxx:21
uint8_t getDtype() const
Get data type enum for introspection.
Definition ddata.hxx:217
size_t getBufferSize() const
Get total buffer size in bytes.
Definition ddata.hxx:142
size_t getBatch() const
Get number of batches.
Definition ddata.hxx:135
io_ts_t & operator[](size_t batch)
Get timestamp reference via bracket operator.
Definition ddata.hxx:113
size_t getDataSize()
Get data size in bytes.
Definition ddata.hxx:153
void * getDataPtr(size_t batch=0)
Get pointer to data only (skips timestamp if present).
Definition ddata.hxx:180
bool hasTimestamp()
Check if this buffer has per-batch timestamp storage.
Definition ddata.hxx:188
size_t getSize() const
Get element size in bytes.
Definition ddata.hxx:121
size_t getItems()
Get number of items per batch.
Definition ddata.hxx:128
T & get(size_t index, size_t batch=0)
Get data element by index and batch (type-safe).
Definition ddata.hxx:93
~io_ddata_t()
Destructor - deallocate heap buffer.
Definition ddata.hxx:74
size_t ALIGN_TO_WORD_SIZE(size_t size, size_t word_size)
Align size to word boundary.
Definition ddata.hxx:34
io_ddata_t(size_t t, size_t n=1, size_t m=1, uint8_t dt=0, bool ts=false)
Constructor - allocate heap buffer with M batches.
Definition ddata.hxx:49
io_ts_t & getTs(size_t batch=0)
Get timestamp reference for batch.
Definition ddata.hxx:203
void * getPtr(size_t batch=0)
Get pointer to batch.
Definition ddata.hxx:168