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