4
5
6
17#include "esp_heap_caps.h"
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53template <
typename T = uint8_t>
60
61
62
63
64 PSRAMAllocator()
noexcept {}
67
68
69
70
71
72
73
74
76 PSRAMAllocator(
const PSRAMAllocator<U>&)
noexcept {}
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95 T* allocate(std::size_t n) {
96 if (n == 0)
return nullptr;
98 std::size_t bytes = n *
sizeof(T);
102#if defined(MALLOC_CAP_SPIRAM)
104 p = heap_caps_aligned_calloc(
alignof(T), n,
sizeof(T),
105 (
unsigned)MALLOC_CAP_SPIRAM);
109#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L
)
111 std::size_t align =
alignof(T);
112 std::size_t alloc_size = bytes;
114 std::size_t rem = alloc_size % align;
115 if (rem) alloc_size += (align - rem);
117 p = aligned_alloc(align, alloc_size);
120 std::memset(p, 0, bytes);
125 p = std::calloc(n,
sizeof(T));
129 if (!p)
throw std::bad_alloc();
130 return static_cast<T*>(p);
134
135
136
137
138
139
140
141
142
143
144
145 void deallocate(T* p, std::size_t )
noexcept {
151
152
153
154
155
156
157
161 typedef PSRAMAllocator<U> other;