FastLED 3.10.8
Loading...
Searching...
No Matches
heap.cpp.hpp
Go to the documentation of this file.
1#include "fl/system/heap.h"
2#include "fl/stl/noexcept.h"
3
4// Platform-specific headers
5#include "platforms/is_platform.h" // IWYU pragma: keep (needed for FL_IS_* macros)
6#ifdef FL_IS_ESP32
7// IWYU pragma: begin_keep
8#include "fl/system/arduino.h"
9// IWYU pragma: end_keep // For ESP object
10#include "esp_heap_caps.h" // For heap_caps_get_free_size()
11#elif defined(FL_IS_ESP8266)
12// IWYU pragma: begin_keep
13#include "fl/system/arduino.h"
14// IWYU pragma: end_keep // For ESP object
15#elif defined(FL_IS_AVR)
16// AVR doesn't have a built-in getFreeHeap, but we can compute it
17// by measuring the gap between heap and stack pointers
18extern "C" char *__brkval; // Heap top pointer (set by malloc)
19extern "C" char *__malloc_heap_start; // Heap start (linker symbol)
20#endif
21
22namespace fl {
23
24#ifdef FL_IS_ESP32
25// ESP32 implementation - report both SRAM and PSRAM
27 HeapInfo info;
28
29 // MALLOC_CAP_INTERNAL: Internal SRAM (fast, always available)
30 info.free_sram = static_cast<fl::size>(heap_caps_get_free_size(MALLOC_CAP_INTERNAL));
31
32 // MALLOC_CAP_SPIRAM: External PSRAM (slower, may not be available)
33 // Returns 0 if PSRAM is not installed or not enabled
34 info.free_psram = static_cast<fl::size>(heap_caps_get_free_size(MALLOC_CAP_SPIRAM));
35
36 return info;
37}
38
39#elif defined(FL_IS_ESP8266)
40// ESP8266 implementation - SRAM only (no PSRAM support)
42 HeapInfo info;
43 info.free_sram = static_cast<fl::size>(ESP.getFreeHeap());
44 info.free_psram = 0; // No PSRAM on ESP8266
45 return info;
46}
47
48#elif defined(FL_IS_AVR)
49// AVR implementation - compute free RAM between heap and stack (SRAM only)
51 HeapInfo info;
52
53 // Stack grows downward from top of RAM, heap grows upward
54 // Free memory is the gap between them
55 char stack_top;
56 char *heap_top = __brkval ? __brkval : __malloc_heap_start;
57
58 // Distance from heap top to current stack position
59 info.free_sram = static_cast<fl::size>(&stack_top - heap_top);
60 info.free_psram = 0; // No PSRAM on AVR
61
62 return info;
63}
64
65#else
66// Default implementation for platforms without heap introspection
67// (Native/Stub, WASM, ARM variants without malloc_stats, etc.)
69 HeapInfo info;
70 info.free_sram = 0; // Not available
71 info.free_psram = 0; // Not available
72 return info;
73}
74
75#endif
76
77} // namespace fl
Platform-abstracted heap memory query functions.
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
HeapInfo getFreeHeap() FL_NO_EXCEPT
Query available heap memory.
Definition heap.cpp.hpp:68
Base definition for an LED controller.
Definition crgb.hpp:179
fl::size free_psram
Free PSRAM in bytes (external slower memory, 0 if not available)
Definition heap.h:19
fl::size free_sram
Free SRAM in bytes (internal fast memory)
Definition heap.h:18
Heap memory information.
Definition heap.h:17