tesseract++ 0.0.1
N-dimensional tensor library for embedded systems
Loading...
Searching...
No Matches
dynamic_storage.h
Go to the documentation of this file.
1#ifndef DYNAMIC_STORAGE_H
2#define DYNAMIC_STORAGE_H
3
4#include <cstdlib> // for aligned_alloc, free
5#include <new> // for std::bad_alloc (not <stdexcept>)
6#include "config.h"
8
9template <typename T, my_size_t N>
11{
12 T *_data = nullptr;
13
14public:
15 // Constructor: allocate memory for N elements
17 {
18 // _data = static_cast<T *>(std::malloc(N * sizeof(T)));
19 _data = static_cast<T *>(std::aligned_alloc(DATA_ALIGNAS, N * sizeof(T)));
20
21 if (!_data)
22 {
23 throw std::bad_alloc();
24 }
25 }
26
27 // Destructor
29 {
30 std::free(_data);
31 }
32
33 // Copy constructor
35 {
36 // _data = static_cast<T *>(std::malloc(N * sizeof(T)));
37 _data = static_cast<T *>(std::aligned_alloc(DATA_ALIGNAS, N * sizeof(T)));
38 if (!_data)
39 throw std::bad_alloc();
40 __builtin_memcpy(_data, other._data, N * sizeof(T));
41 }
42
43 // Move constructor
44 DynamicStorage(DynamicStorage &&other) noexcept : _data(other._data)
45 {
46 other._data = nullptr;
47 }
48
49 // Copy assignment
51 {
52 if (this != &other)
53 {
54 if (!_data)
55 {
56 _data = static_cast<T *>(std::aligned_alloc(DATA_ALIGNAS, N * sizeof(T)));
57 }
58 __builtin_memcpy(_data, other._data, N * sizeof(T));
59 }
60 return *this;
61 }
62
63 // Move assignment
65 {
66 if (this != &other)
67 {
68 std::free(_data);
69 _data = other._data;
70 other._data = nullptr;
71 }
72 return *this;
73 }
74
75 // Element access
76 FORCE_INLINE constexpr T &operator[](my_size_t idx) noexcept { return _data[idx]; }
77 FORCE_INLINE constexpr const T &operator[](my_size_t idx) const noexcept { return _data[idx]; }
78
79 FORCE_INLINE constexpr T *data() noexcept { return _data; }
80 FORCE_INLINE constexpr const T *data() const noexcept { return _data; }
81
82 FORCE_INLINE constexpr T *begin() noexcept { return _data; }
83 FORCE_INLINE constexpr const T *begin() const noexcept { return _data; }
84
85 FORCE_INLINE constexpr T *end() noexcept { return _data + N; }
86 FORCE_INLINE constexpr const T *end() const noexcept { return _data + N; }
87};
88
89#endif // DYNAMIC_STORAGE_H
Definition dynamic_storage.h:11
FORCE_INLINE constexpr T * end() noexcept
Definition dynamic_storage.h:85
FORCE_INLINE constexpr T & operator[](my_size_t idx) noexcept
Definition dynamic_storage.h:76
FORCE_INLINE constexpr const T * end() const noexcept
Definition dynamic_storage.h:86
DynamicStorage(const DynamicStorage &other)
Definition dynamic_storage.h:34
FORCE_INLINE constexpr const T * begin() const noexcept
Definition dynamic_storage.h:83
DynamicStorage & operator=(const DynamicStorage &other)
Definition dynamic_storage.h:50
DynamicStorage(DynamicStorage &&other) noexcept
Definition dynamic_storage.h:44
DynamicStorage & operator=(DynamicStorage &&other) noexcept
Definition dynamic_storage.h:64
~DynamicStorage()
Definition dynamic_storage.h:28
FORCE_INLINE constexpr T * begin() noexcept
Definition dynamic_storage.h:82
FORCE_INLINE constexpr T * data() noexcept
Definition dynamic_storage.h:79
DynamicStorage()
Definition dynamic_storage.h:16
FORCE_INLINE constexpr const T & operator[](my_size_t idx) const noexcept
Definition dynamic_storage.h:77
FORCE_INLINE constexpr const T * data() const noexcept
Definition dynamic_storage.h:80
Global configuration for the tesseract tensor library.
#define my_size_t
Size/index type used throughout the library.
Definition config.h:126
#define FORCE_INLINE
Hint the compiler to always inline a function.
Definition config.h:26
constexpr my_size_t DATA_ALIGNAS
Definition microkernel_base.h:145