FastLED 3.9.15
Loading...
Searching...
No Matches
allocator.h
Go to the documentation of this file.
1
2#pragma once
3
4#include <stddef.h>
5#include <stdlib.h>
6#include <string.h>
7#include "fl/inplacenew.h"
8#include "fl/type_traits.h"
9#include "fl/unused.h"
10
11namespace fl {
12
13void SetPSRamAllocator(void *(*alloc)(size_t), void (*free)(void *));
14void *PSRamAllocate(size_t size, bool zero = true);
15void PSRamDeallocate(void *ptr);
16void Malloc(size_t size);
17void Free(void *ptr);
18
19template <typename T> class PSRamAllocator {
20 public:
21 static T *Alloc(size_t n) {
22 void *ptr = PSRamAllocate(sizeof(T) * n, true);
23 return reinterpret_cast<T *>(ptr);
24 }
25
26 static void Free(T *p) {
27 if (p == nullptr) {
28 return;
29 }
31 }
32};
33
34// std compatible allocator.
35template <typename T> class allocator {
36 public:
37 // Type definitions required by STL
38 using value_type = T;
39 using pointer = T*;
40 using const_pointer = const T*;
41 using reference = T&;
42 using const_reference = const T&;
43 using size_type = size_t;
44 using difference_type = ptrdiff_t;
45
46 // Rebind allocator to type U
47 template <typename U>
48 struct rebind {
50 };
51
52 // Default constructor
53 allocator() noexcept {}
54
55 // Copy constructor
56 template <typename U>
57 allocator(const allocator<U>&) noexcept {}
58
59 // Destructor
60 ~allocator() noexcept {}
61
62 // Use this to allocate large blocks of memory for T.
63 // This is useful for large arrays or objects that need to be allocated
64 // in a single block.
65 T* allocate(size_t n) {
66 if (n == 0) {
67 return nullptr; // Handle zero allocation
68 }
69 void *ptr = malloc(sizeof(T) * n);
70 if (ptr == nullptr) {
71 return nullptr; // Handle allocation failure
72 }
73 memset(ptr, 0, sizeof(T) * n); // Zero-initialize the memory
74 return static_cast<T*>(ptr);
75 }
76
77 void deallocate(T* p, size_t n) {
79 if (p == nullptr) {
80 return; // Handle null pointer
81 }
82 free(p); // Free the allocated memory
83 }
84
85 // Construct an object at the specified address
86 template <typename U, typename... Args>
87 void construct(U* p, Args&&... args) {
88 new(static_cast<void*>(p)) U(fl::forward<Args>(args)...);
89 }
90
91 // Destroy an object at the specified address
92 template <typename U>
93 void destroy(U* p) {
94 p->~U();
95 }
96};
97
98template <typename T> class allocator_psram {
99 public:
100 // Type definitions required by STL
101 using value_type = T;
102 using pointer = T*;
103 using const_pointer = const T*;
104 using reference = T&;
105 using const_reference = const T&;
106 using size_type = size_t;
107 using difference_type = ptrdiff_t;
108
109 // Rebind allocator to type U
110 template <typename U>
111 struct rebind {
113 };
114
115 // Default constructor
116 allocator_psram() noexcept {}
117
118 // Copy constructor
119 template <typename U>
121
122 // Destructor
123 ~allocator_psram() noexcept {}
124
125 // Allocate memory for n objects of type T
126 T* allocate(size_t n) {
127 return PSRamAllocator<T>::Alloc(n);
128 }
129
130 // Deallocate memory for n objects of type T
131 void deallocate(T* p, size_t n) {
134 }
135
136 // Construct an object at the specified address
137 template <typename U, typename... Args>
138 void construct(U* p, Args&&... args) {
139 new(static_cast<void*>(p)) U(fl::forward<Args>(args)...);
140 }
141
142 // Destroy an object at the specified address
143 template <typename U>
144 void destroy(U* p) {
145 p->~U();
146 }
147};
148
149
150// Macro to specialize the Allocator for a specific type to use PSRam
151// Usage: FL_USE_PSRAM_ALLOCATOR(MyClass)
152#define FL_USE_PSRAM_ALLOCATOR(TYPE) \
153template <> \
154class allocator<TYPE> { \
155 public: \
156 using value_type = TYPE; \
157 using pointer = TYPE*; \
158 using const_pointer = const TYPE*; \
159 using reference = TYPE&; \
160 using const_reference = const TYPE&; \
161 using size_type = size_t; \
162 using difference_type = ptrdiff_t; \
163 \
164 template <typename U> \
165 struct rebind { \
166 using other = allocator<U>; \
167 }; \
168 \
169 allocator() noexcept {} \
170 \
171 template <typename U> \
172 allocator(const allocator<U>&) noexcept {} \
173 \
174 ~allocator() noexcept {} \
175 \
176 TYPE *allocate(size_t n) { \
177 return reinterpret_cast<TYPE *>(PSRamAllocate(sizeof(TYPE) * n, true)); \
178 } \
179 void deallocate(TYPE *p, size_t n) { \
180 if (p == nullptr) { \
181 return; \
182 } \
183 PSRamDeallocate(p); \
184 } \
185 \
186 template <typename U, typename... Args> \
187 void construct(U* p, Args&&... args) { \
188 new(static_cast<void*>(p)) U(fl::forward<Args>(args)...); \
189 } \
190 \
191 template <typename U> \
192 void destroy(U* p) { \
193 p->~U(); \
194 } \
195};
196
197} // namespace fl
static T * Alloc(size_t n)
Definition allocator.h:21
static void Free(T *p)
Definition allocator.h:26
ptrdiff_t difference_type
Definition allocator.h:107
void deallocate(T *p, size_t n)
Definition allocator.h:131
void construct(U *p, Args &&... args)
Definition allocator.h:138
allocator_psram(const allocator_psram< U > &) noexcept
Definition allocator.h:120
const T & const_reference
Definition allocator.h:105
const T * const_pointer
Definition allocator.h:103
T * allocate(size_t n)
Definition allocator.h:126
void destroy(U *p)
Definition allocator.h:144
allocator_psram() noexcept
Definition allocator.h:116
~allocator_psram() noexcept
Definition allocator.h:123
allocator_psram< U > other
Definition allocator.h:112
void deallocate(T *p, size_t n)
Definition allocator.h:77
T * allocate(size_t n)
Definition allocator.h:65
const T * const_pointer
Definition allocator.h:40
ptrdiff_t difference_type
Definition allocator.h:44
allocator() noexcept
Definition allocator.h:53
void construct(U *p, Args &&... args)
Definition allocator.h:87
allocator(const allocator< U > &) noexcept
Definition allocator.h:57
void destroy(U *p)
Definition allocator.h:93
const T & const_reference
Definition allocator.h:42
~allocator() noexcept
Definition allocator.h:60
size_t size_type
Definition allocator.h:43
allocator< U > other
Definition allocator.h:49
void Free(void *ptr)
Definition allocator.cpp:61
void SetPSRamAllocator(void *(*alloc)(size_t), void(*free)(void *))
Definition allocator.cpp:36
void * PSRamAllocate(size_t size, bool zero)
Definition allocator.cpp:41
void Malloc(size_t size)
Definition allocator.cpp:51
void PSRamDeallocate(void *ptr)
Definition allocator.cpp:49
constexpr T && forward(typename remove_reference< T >::type &t) noexcept
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
static FASTLED_NAMESPACE_BEGIN uint8_t const p[]
Definition noise.cpp:30
corkscrew_args args
Definition old.h:142
#define FASTLED_UNUSED(x)
Definition unused.h:3