-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Improvements to heap-memory and PSRAM handling #4791
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DedeHai
wants to merge
8
commits into
wled:main
Choose a base branch
from
DedeHai:heap_handling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+342
−206
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b96a950
Improved heap and PSRAM handling
DedeHai 516aa62
Added new functions for allocation and heap checking
DedeHai 48fbb1d
code formatting, removed now unused function declaration
DedeHai 9bd9d32
Fixes and clearer comments
DedeHai ec29161
Merge branch 'main' into heap_handling
DedeHai 32743ab
Merge branch 'main' into heap_handling
DedeHai f9d0c19
Merge branch 'main' into heap_handling
DedeHai 39870f8
removed "psramsafe", updated allocation functions
DedeHai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,35 +38,29 @@ uint32_t colorBalanceFromKelvin(uint16_t kelvin, uint32_t rgb); | |
uint8_t realtimeBroadcast(uint8_t type, IPAddress client, uint16_t length, const byte *buffer, uint8_t bri=255, bool isRGBW=false); | ||
|
||
//util.cpp | ||
// PSRAM allocation wrappers | ||
#if !defined(ESP8266) && !defined(CONFIG_IDF_TARGET_ESP32C3) | ||
// memory allocation wrappers | ||
extern "C" { | ||
void *p_malloc(size_t); // prefer PSRAM over DRAM | ||
void *p_calloc(size_t, size_t); // prefer PSRAM over DRAM | ||
void *p_realloc(void *, size_t); // prefer PSRAM over DRAM | ||
void *p_realloc_malloc(void *ptr, size_t size); // realloc with malloc fallback, prefer PSRAM over DRAM | ||
inline void p_free(void *ptr) { heap_caps_free(ptr); } | ||
void *d_malloc(size_t); // prefer DRAM over PSRAM | ||
void *d_calloc(size_t, size_t); // prefer DRAM over PSRAM | ||
void *d_realloc(void *, size_t); // prefer DRAM over PSRAM | ||
void *d_realloc_malloc(void *ptr, size_t size); // realloc with malloc fallback, prefer DRAM over PSRAM | ||
// prefer DRAM over PSRAM (if available) in d_ alloc functions | ||
void *d_malloc(size_t); | ||
void *d_calloc(size_t, size_t); | ||
void *d_realloc_malloc(void *ptr, size_t size); | ||
#ifndef ESP8266 | ||
inline void d_free(void *ptr) { heap_caps_free(ptr); } | ||
#else | ||
inline void d_free(void *ptr) { free(ptr); } | ||
#endif | ||
#if defined(BOARD_HAS_PSRAM) | ||
// prefer PSRAM over DRAM in p_ alloc functions | ||
void *p_malloc(size_t); | ||
void *p_calloc(size_t, size_t); | ||
void *p_realloc_malloc(void *ptr, size_t size); | ||
inline void p_free(void *ptr) { heap_caps_free(ptr); } | ||
#else | ||
#define p_malloc d_malloc | ||
#define p_calloc d_calloc | ||
#define p_free d_free | ||
#endif | ||
} | ||
Comment on lines
+41
to
63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don’t redeclare alloc wrappers here; fix missing p_realloc mapping. This duplicates fcn_declare.h and risks drift. Also, the non-PSRAM path lacks p_realloc mapping. Apply: -//util.cpp
-// memory allocation wrappers
-extern "C" {
- // prefer DRAM over PSRAM (if available) in d_ alloc functions
- void *d_malloc(size_t);
- void *d_calloc(size_t, size_t);
- void *d_realloc_malloc(void *ptr, size_t size);
- #ifndef ESP8266
- inline void d_free(void *ptr) { heap_caps_free(ptr); }
- #else
- inline void d_free(void *ptr) { free(ptr); }
- #endif
- #if defined(BOARD_HAS_PSRAM)
- // prefer PSRAM over DRAM in p_ alloc functions
- void *p_malloc(size_t);
- void *p_calloc(size_t, size_t);
- void *p_realloc_malloc(void *ptr, size_t size);
- inline void p_free(void *ptr) { heap_caps_free(ptr); }
- #else
- #define p_malloc d_malloc
- #define p_calloc d_calloc
- #define p_free d_free
- #endif
-}
+#include "fcn_declare.h" // use canonical allocator declarations
+// (If keeping local fallbacks, ensure:)
+// #define p_realloc_malloc d_realloc_malloc
|
||
#else | ||
extern "C" { | ||
void *realloc_malloc(void *ptr, size_t size); | ||
} | ||
#define p_malloc malloc | ||
#define p_calloc calloc | ||
#define p_realloc realloc | ||
#define p_realloc_malloc realloc_malloc | ||
#define p_free free | ||
#define d_malloc malloc | ||
#define d_calloc calloc | ||
#define d_realloc realloc | ||
#define d_realloc_malloc realloc_malloc | ||
#define d_free free | ||
#endif | ||
|
||
//color mangling macros | ||
#define RGBW32(r,g,b,w) (uint32_t((byte(w) << 24) | (byte(r) << 16) | (byte(g) << 8) | (byte(b)))) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
finalizeInit(): enforce-PSRAM pixel buffer needs graceful fallback
If PSRAM is defined but unavailable at runtime (or temporarily exhausted), ENFORCE_PSRAM fails and the build becomes unusable. Attempt DRAM/IRAM as a fallback before erroring out.
Also applies to: 1221-1226
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the allocate_buffer() function already takes care of a fallback if PSRAM is not available. If PSRAM is available, this will not fail as it is done at setup() stage and available PSRAM at that point is vast.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Length of output: 8856
🏁 Script executed:
Length of output: 9403
@DedeHai Thank you for the correction! You're absolutely right. I can now see from the
allocate_buffer()
implementation that my concern was misplaced.Looking at the code, I can see that:
BFRALLOC_ENFORCE_PSRAM already has fallback logic: The comment in
fcn_declare.h
clearly states "use PSRAM if available, otherwise fall back to DRAM"validateFreeHeap() provides additional safety: Even when
BFRALLOC_ENFORCE_PSRAM
callsheap_caps_malloc(size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT)
directly (line 777), the result still goes throughvalidateFreeHeap()
which will free the buffer and returnnullptr
if minimum heap requirements aren't metESP32 classic has even more sophisticated fallback: On CONFIG_IDF_TARGET_ESP32 with BFRALLOC_NOBYTEACCESS, it uses
heap_caps_malloc_prefer()
with a 3-tier preference: 32BIT → SPIRAM → 8BIT, providing automatic fallbackSetup timing: As you mentioned, during the setup() stage when
finalizeInit()
is called, PSRAM has vast available memory, making failures highly unlikelyThe allocate_buffer() function is indeed well-designed with multiple fallback mechanisms, and my suggestion for manual fallback logic was unnecessary. The current implementation already handles PSRAM unavailability gracefully.
🐰
/| /|
( . .)
) (
( v )
^^ ^ ^^
✏️ Learnings added
🧠 Learnings used