Sound Open Firmware
Loading...
Searching...
No Matches
array.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: BSD-3-Clause
2 *
3 * Copyright(c) 2022 Intel Corporation. All rights reserved.
4 */
5
6#ifndef DSP_FW_FW_ARRAY_H_
7#define DSP_FW_FW_ARRAY_H_
8
9#include <stdint.h>
10#include <stddef.h>
11
12#include <rtos/panic.h>
13
14#ifdef __cplusplus
15extern "C" {
16#endif
17
19typedef struct byte_array {
21 uint8_t *data;
23 size_t size;
25
26static inline uint8_t *array_get_data(const byte_array_t *ba)
27{
28 return (uint8_t *)ba->data;
29}
30
31static inline uint8_t *array_get_data_end(const byte_array_t *ba)
32{
33 return array_get_data(ba) + ba->size;
34}
35
36
37static inline size_t array_get_size(const byte_array_t *ba)
38{
39 return ba->size;
40}
41
42static inline uint8_t *array_alloc_from(byte_array_t *ba,
43 size_t required_size)
44{
45 /* TODO: add alignment */
46 /* TODO: validate inputs */
47 /* TODO: move this to more appropriate file */
48
49 uint8_t *cached_data = array_get_data(ba);
50
51 ba->data += required_size;
52 ba->size -= required_size;
53
54 return cached_data;
55}
56#ifdef __cplusplus
57} /* extern "C" */
58#endif
59
60#ifdef __cplusplus
61
62static inline uint8_t *array_get_data(const byte_array_t &ba)
63{
64 return (uint8_t *)ba.data;
65}
66
67static inline size_t array_get_size(const byte_array_t &ba)
68{
69 return ba.size;
70}
71
72namespace dsp_fw
73{
89template < class T >
90class Array /*: public byte_array*/
91{
92public:
93
97Array()
98{
99 data_ = NULL;
100 size_ = 0;
101}
102
110Array(T * ptr, size_t size)
111{
112 data_ = (T *)ptr;
113 size_ = size;
114}
115
123void Init(T *ptr, size_t size)
124{
125 data_ = (T *)ptr;
126 size_ = size;
127}
128
136void InitWithRange(T *ptr, T *ptr_end)
137{
138 assert(ptr_end >= ptr);
139 data_ = (T *)ptr;
140 size_ = ptr_end - ptr;
141}
142
147void Detach(void) { data_ = NULL; size_ = 0; }
154size_t size(void) const { return size_; }
158size_t alloc_size(void) const { return size()*sizeof(T); }
162void Resize(size_t new_size) { size_ = new_size; }
168const T *data(void) const { return (const T *)data_; }
175T *data(void) { return (T *)data_; }
180const T *data_end(void) const { return data() + size_; }
185T *data_end(void) { return data() + size_; }
192const T & operator[](size_t idx) const {
193 /*assert(idx < size()); //TODO: no exceptions, release err handling req */
194 return data()[idx];
195}
196
203T & operator[](size_t idx) {
204 /*assert(idx < size()); //TODO: no exceptions, release err handling req */
205 return data()[idx];
206}
207
216void copyFrom(const T *src, size_t srcSize, size_t dst_offset = 0)
217{
218 assert(data() != NULL);
219 assert(size() > dst_offset);
220 assert((size() - dst_offset) >= srcSize);
221 memcpy_s(data() + dst_offset, alloc_size() - dst_offset * sizeof(T),
222 src, srcSize * sizeof(T));
223}
224
232void copyFrom(const Array < T > &src, size_t dst_offset = 0)
233{
234 assert(data() != 0);
235 assert(size() > dst_offset);
236 assert((size() - dst_offset) >= src.size());
237 memcpy_s(data() + dst_offset, alloc_size() - dst_offset * sizeof(T),
238 src.data(), src.alloc_size());
239}
240
246void copyTo(T *dst, size_t dstSize) const {
247 assert(data() != 0);
248 assert(size() <= dstSize);
249 memcpy_s(dst, dstSize * sizeof(T), data(), alloc_size());
250}
251
256void copyTo(Array < T > &dst) const {
257 assert(data() != 0);
258 assert(size() <= dst.size());
259 memcpy_s(dst.data(), dst.alloc_size(), data(), alloc_size());
260}
261
268void copyFragmentTo(Array < T > &dst, size_t copy_size, size_t src_offset = 0) const {
269 assert(data() != 0);
270 assert((src_offset + copy_size) <= size());
271 assert(copy_size <= dst.size());
272 memcpy_s(dst.data(), dst.alloc_size(), data() + src_offset, copy_size * sizeof(T));
273}
274
281template < class TC >
282TC *dataAs(void)
283{
284 if (alloc_size() < sizeof(TC)) {
285 assert(false);
286 return NULL;
287 }
288 return reinterpret_cast < TC * > (data());
289}
290
291template < class TC >
292TC *dataAsArray(size_t size)
293{
294 if (alloc_size() < sizeof(TC)*size) {
295 assert(false);
296 return NULL;
297 }
298 return reinterpret_cast < TC * > (data());
299}
300
306template < class TC >
307const TC *dataAs(void) const {
308 if (alloc_size() < sizeof(TC)) {
309 assert(false);
310 return NULL;
311 }
312 return reinterpret_cast < const TC * > (data());
313}
314
319template < class TC >
320void setDataAs(const TC & d)
321{
322 TC *t = dataAs < TC > ();
323
324 if (t == NULL) {
325 assert(false);
326 return;
327 }
328 *t = d; /* using operator=, not shallow copy */
329 Resize(sizeof(TC));
330}
331
335void clear(void)
336{
337 memset(data(), 0x00, alloc_size());
338}
339
340void Swap(Array < T > *array)
341{
342 Array < T > temp(*array);
343 array->data_ = data_;
344 array->size_ = size_;
345 this->data_ = temp.data_;
346 this->size_ = temp.size_;
347}
348
349void Swap(Array < T > &array)
350{
351 Array < T > temp(array);
352 array.data_ = data_;
353 array.size_ = size_;
354 this->data_ = temp.data_;
355 this->size_ = temp.size_;
356}
357private:
358 T *data_;
359 size_t size_;
360};
361
365typedef Array < uint8_t > ByteArray;
366
370typedef Array < uint32_t > DwordArray;
371
372} /* end of namespace dsp_fw */
373
374#endif /* __cplusplus */
375/* clang-format on */
376#endif /* #ifndef DSP_FW_FW_ARRAY_H_ */
static uint8_t * array_get_data(const byte_array_t *ba)
Definition array.h:26
static size_t array_get_size(const byte_array_t *ba)
Definition array.h:37
static uint8_t * array_alloc_from(byte_array_t *ba, size_t required_size)
Definition array.h:42
static uint8_t * array_get_data_end(const byte_array_t *ba)
Definition array.h:31
uint32_t data[]
Definition debug_stream_slot.h:2
Definition module_initial_settings_concrete.h:46
#define assert(x)
Definition panic.h:19
static int memcpy_s(void *dest, size_t dest_size, const void *src, size_t count)
Definition string.h:38
Definition array.h:19
size_t size
Definition array.h:23
uint8_t * data
Definition array.h:21