Critical section for thread safety#1
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds optional platform-provided critical section callbacks to make the allocator’s core operations thread-safe on embedded targets (e.g., by disabling interrupts or acquiring a lock) without hard-coding a specific RTOS/SDK dependency.
Changes:
- Extend
ESTALLOCwithenter_critical/exit_criticalcallback pointers and exposeest_set_critical_section()to register them. - Wrap allocator operations (
malloc/permalloc/free/reallocand statistics collection) with enter/exit critical section hooks. - Add a few additional internal assertions to detect heap corruption cases earlier.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| estalloc.h | Adds critical section callbacks to ESTALLOC and declares est_set_critical_section() API. |
| estalloc.c | Implements callback registration and applies critical section wrapping to allocator operations. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
d5a7fec to
b3ef41e
Compare
`est_set_critical_section()` to register callbacks for wrapping critical section provided by the platform.
Example for pico-sdk:
```c
#include <pico/critical_section.h>
static critical_section_t heap_critsec;
static void heap_enter_critical(void)
{
critical_section_enter_blocking(&heap_critsec);
}
static void heap_exit_critical(void)
{
critical_section_exit(&heap_critsec);
}
int main(void)
{
ESTALLOC *est = est_init(mem, bytes);
critical_section_init(&heap_critsec); // from pico-sdk
est_set_critical_section(est, heap_enter_critical, heap_exit_critical);
}
```
b3ef41e to
bcafdd9
Compare
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
est_set_critical_section()to register callbacks for wrapping critical section provided by the platform.Example for pico-sdk: