Skip to content

Optimize _sbrk_r when no increment is needed #14

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
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion heap_useNewlib_NXP.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ static int heapBytesRemaining = (int)&HEAP_SIZE; // that's (&__HeapLimit)-(&__He
//! _sbrk_r version supporting reentrant newlib (depends upon above symbols defined by linker control file).
void * _sbrk_r(struct _reent *pReent, int incr) {
static char *currentHeapEnd = &__HeapBase;
char *previousHeapEnd = currentHeapEnd;
if(0 == incr) {
// don't suspend all tasks if nothing has to change
return (char *) previousHeapEnd;
}
vTaskSuspendAll(); // Note: safe to use before FreeRTOS scheduler started, but not within an ISR
if (currentHeapEnd + incr > &__HeapLimit) {
// Ooops, no more memory available...
Expand All @@ -159,7 +164,6 @@ void * _sbrk_r(struct _reent *pReent, int incr) {
return (char *)-1; // the malloc-family routine that called sbrk will return 0
}
// 'incr' of memory is available: update accounting and return it.
char *previousHeapEnd = currentHeapEnd;
currentHeapEnd += incr;
heapBytesRemaining -= incr;
#ifndef NDEBUG
Expand Down
6 changes: 5 additions & 1 deletion heap_useNewlib_ST.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ void * _sbrk_r(struct _reent *pReent, int incr) {
char* limit = (xTaskGetSchedulerState()==taskSCHEDULER_NOT_STARTED) ?
stack_ptr : // Before scheduler is started, limit is stack pointer (risky!)
&__HeapLimit-ISR_STACK_LENGTH_BYTES; // Once running, OK to reuse all remaining RAM except ISR stack (MSP) stack
char *previousHeapEnd = currentHeapEnd;
if(0 == incr) {
// don't go into a critical section if nothing has to change
return (char *) previousHeapEnd;
}
DRN_ENTER_CRITICAL_SECTION(usis);
if (currentHeapEnd + incr > limit) {
// Ooops, no more memory available...
Expand All @@ -190,7 +195,6 @@ void * _sbrk_r(struct _reent *pReent, int incr) {
return (char *)-1; // the malloc-family routine that called sbrk will return 0
}
// 'incr' of memory is available: update accounting and return it.
char *previousHeapEnd = currentHeapEnd;
currentHeapEnd += incr;
heapBytesRemaining -= incr;
#ifndef NDEBUG
Expand Down