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