FastLED 3.9.15
Loading...
Searching...
No Matches
allocator.cpp
Go to the documentation of this file.
1
2#include <stdlib.h>
3
4#include "fl/allocator.h"
5#include "fl/namespace.h"
6
7#ifdef ESP32
8#include "esp_heap_caps.h"
9#include "esp_system.h"
10#endif
11
12namespace fl {
13
14namespace {
15
16#ifdef ESP32
17// On esp32, attempt to always allocate in psram first.
18void *DefaultAlloc(size_t size) {
19 void *out = heap_caps_malloc(size, MALLOC_CAP_SPIRAM);
20 if (out == nullptr) {
21 // Fallback to default allocator.
22 out = heap_caps_malloc(size, MALLOC_CAP_DEFAULT);
23 }
24 return out;
25}
26void DefaultFree(void *ptr) { heap_caps_free(ptr); }
27#else
28void *DefaultAlloc(size_t size) { return malloc(size); }
29void DefaultFree(void *ptr) { free(ptr); }
30#endif
31
32void *(*Alloc)(size_t) = DefaultAlloc;
33void (*Free)(void *) = DefaultFree;
34} // namespace
35
36void SetLargeBlockAllocator(void *(*alloc)(size_t), void (*free)(void *)) {
37 Alloc = alloc;
38 Free = free;
39}
40
41void *LargeBlockAllocate(size_t size, bool zero) {
42 void *ptr = Alloc(size);
43 if (zero) {
44 memset(ptr, 0, size);
45 }
46 return ptr;
47}
48
49void LargeBlockDeallocate(void *ptr) { Free(ptr); }
50
51} // namespace fl
Implements the FastLED namespace macros.
void SetLargeBlockAllocator(void *(*alloc)(size_t), void(*free)(void *))
Definition allocator.cpp:36
void LargeBlockDeallocate(void *ptr)
Definition allocator.cpp:49
void * LargeBlockAllocate(size_t size, bool zero)
Definition allocator.cpp:41
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16