SD library: reduce stack size requirement by 4k by moving work buffer to heap #6745
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.
Description of Change
The stack requirement of this library is huge due to a work buffer allocated for f_mkfs in sdcard_mount. The work buffer is only needed if argument format_if_empty is set true (which is by default false) and the card was empty.
This change is quite important if you plan to use this library in a task. as now it increases the tasks stacks size by 4k, even this work memory is never used (due to format_if_empty is set false). If users are not aware of the large stack requirement during init this library they may have seen various side effects caused by the original code. the other variables in this function may have written addresses that were outside its valid stack range.
Tests scenarios
This issue was detected ESP32 when SD.begin was called from a task with a 2k stack (with format_if_empty set false).
Before this change either the stack of the task would have to been increased to 6k. Before it would show random crashed depending on time SD.begin was executed.