1#include "platforms/is_platform.h"
12#include "esp_heap_caps.h"
13#include "esp_system.h"
15#include "platforms/esp/esp_version.h"
30template <
typename Allocator>
33template <
typename Allocator>
41#ifdef FL_VECTOR_PSRAM_ALWAYS_SRAM
43 void *out = heap_caps_malloc(size, MALLOC_CAP_DEFAULT);
45 void *out = heap_caps_malloc(size, MALLOC_CAP_SPIRAM);
48 out = heap_caps_malloc(size, MALLOC_CAP_DEFAULT);
62#if defined(FASTLED_TESTING)
64MallocFreeHook* gMallocFreeHook =
nullptr;
66int& tls_reintrancy_count() {
71 int& reintrancy_count;
72 MemoryGuard()
FL_NOEXCEPT : reintrancy_count(tls_reintrancy_count()) {
78 bool enabled()
const {
79 return reintrancy_count <= 1;
89void init_tls_reentrancy() {
90 (void)tls_reintrancy_count();
93FL_INIT(allocator_init_wrapper, init_tls_reentrancy)
99#if defined(FASTLED_TESTING)
100void SetMallocFreeHook(MallocFreeHook* hook) {
104 (void)tls_reintrancy_count();
106 gMallocFreeHook = hook;
109void ClearMallocFreeHook() {
110 gMallocFreeHook =
nullptr;
121 void *ptr = Alloc(size);
126#if defined(FASTLED_TESTING)
127 if (gMallocFreeHook && ptr) {
128 MemoryGuard allows_hook;
129 if (allows_hook.enabled()) {
130 gMallocFreeHook->onMalloc(ptr, size);
139#if defined(FASTLED_TESTING)
140 if (gMallocFreeHook && ptr) {
142 MemoryGuard allows_hook;
143 if (allows_hook.enabled()) {
144 gMallocFreeHook->onFree(ptr);
153 void* ptr = Alloc(size);
155#if defined(FASTLED_TESTING)
156 if (gMallocFreeHook && ptr) {
157 MemoryGuard allows_hook;
158 if (allows_hook.enabled()) {
159 gMallocFreeHook->onMalloc(ptr, size);
168#if defined(FASTLED_TESTING)
169 if (gMallocFreeHook && ptr) {
170 MemoryGuard allows_hook;
171 if (allows_hook.enabled()) {
172 gMallocFreeHook->onFree(ptr);
184void* InternalAlloc(fl::size size) {
187 void* ptr = heap_caps_malloc(size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
194void* InternalRealloc(
void* ptr, fl::size size) {
196 void* new_ptr = heap_caps_realloc(ptr, size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
206void InternalFree(
void* ptr) {
210void* DMAAlloc(fl::size size) {
220 fl::size aligned_size = ((size + 63) / 64) * 64;
222#if ESP_IDF_VERSION_4_OR_HIGHER
224 void* ptr = heap_caps_aligned_alloc(64, aligned_size, MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL);
227 void* ptr = heap_caps_malloc(aligned_size, MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL);
235void DMAFree(
void* ptr) {
257 for (
int i = 0; i < slab_registry_count; i++) {
258 if (slab_registry[i].block_size == block_size &&
259 slab_registry[i].slab_size == slab_size) {
268 for (
int i = 0; i < slab_registry_count; i++) {
269 if (slab_registry[i].block_size == block_size &&
270 slab_registry[i].slab_size == slab_size) {
276 if (slab_registry_count < SLAB_REGISTRY_MAX) {
277 slab_registry[slab_registry_count++] = {block_size, slab_size,
allocator};
static T & instance() FL_NOEXCEPT
void DefaultFree(void *ptr)
void * DefaultAlloc(fl::size size)
static SlabRegistryEntry slab_registry[SLAB_REGISTRY_MAX]
static constexpr int SLAB_REGISTRY_MAX
static int slab_registry_count
void * slab_allocator_registry_get(fl::size block_size, fl::size slab_size)
void slab_allocator_registry_set(fl::size block_size, fl::size slab_size, void *allocator)
Compile-time linker keep-alive hook for a single fl::Bus.
void * memset(void *s, int c, size_t n) FL_NOEXCEPT
void * malloc(size_t size)
void SetPSRamAllocator(void *(*alloc)(fl::size), void(*free)(void *))
void PSRamDeallocate(void *ptr)
void * Malloc(fl::size size)
void * PSRamAllocate(fl::size size, bool zero)
Base definition for an LED controller.
static constexpr bool has_allocate_at_least_v
static constexpr bool has_reallocate_v