Skip to content

Limit dynamic memory allocation to stack + reserve #555

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 6 commits 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
4 changes: 2 additions & 2 deletions STM32F1/platform.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ compiler.c.elf.flags={build.flags.optimize} -Wl,--gc-sections {build.flags.ldspe
compiler.S.cmd=arm-none-eabi-gcc
compiler.S.flags=-c -g -x assembler-with-cpp -MMD
compiler.cpp.cmd=arm-none-eabi-g++
compiler.cpp.flags=-c -g {build.flags.optimize} {compiler.warning_flags} -std=gnu++11 -MMD -ffunction-sections -fdata-sections -nostdlib --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -DBOARD_{build.variant} -D{build.vect} -DERROR_LED_PORT={build.error_led_port} -DERROR_LED_PIN={build.error_led_pin}
compiler.cpp.flags=-c -g {build.flags.optimize} {compiler.warning_flags} -std=gnu++11 -MMD -ffunction-sections -fdata-sections -nostdlib --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -fno-use-cxa-atexit -DBOARD_{build.variant} -D{build.vect} -DERROR_LED_PORT={build.error_led_port} -DERROR_LED_PIN={build.error_led_pin}
compiler.ar.cmd=arm-none-eabi-ar
compiler.ar.flags=rcs
compiler.objcopy.cmd=arm-none-eabi-objcopy
Expand Down Expand Up @@ -174,4 +174,4 @@ tools.hid_upload.path.linux={runtime.hardware.path}/tools/linux
tools.hid_upload.path.linux64={runtime.hardware.path}/tools/linux64
tools.hid_upload.upload.params.verbose=-d
tools.hid_upload.upload.params.quiet=n
tools.hid_upload.upload.pattern="{path}/{cmd}" "{build.path}/{build.project_name}.bin" {serial.port.file}
tools.hid_upload.upload.pattern="{path}/{cmd}" "{build.path}/{build.project_name}.bin" {serial.port.file}
9 changes: 8 additions & 1 deletion STM32F1/variants/generic_stm32f103c/wirish/syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,14 @@ extern char _lm_heap_end;
*
* Get incr bytes more RAM (for use by the heap). malloc() and
* friends call this function behind the scenes.
*
* The following can be included in application code to reserve
* a minimum amount of space for future stack growth:
* extern int __sbrk_stack_reserve;
* __sbrk_stack_reserve=10240;
*
*/
volatile int __sbrk_stack_reserve = 256;
void *_sbrk(int incr) {
static void * pbreak = NULL; /* current program break */
void * ret;
Expand All @@ -65,7 +72,7 @@ void *_sbrk(int incr) {
pbreak = CONFIG_HEAP_START;
}

if ((CONFIG_HEAP_END - pbreak < incr) ||
if (((void*)&ret - pbreak < incr + __sbrk_stack_reserve) ||
(pbreak - CONFIG_HEAP_START < -incr)) {
errno = ENOMEM;
return (void *)-1;
Expand Down