Skip to content

Commit a4224ed

Browse files
VladimirUmekJonatanAntoni
authored andcommitted
GCC: disable default heap limit checking in sbrk
- __HeapLimit symbol was used in sbrk implementation to check for out of heap condition. In many cases linker scripts do not provide this symbol, hence by default this check is now disabled. - One can re-enable heap limit check by using define SBRK_HEAP_LIMIT_CHECK and provide symbol __HeapLimit which designates the end of heap section.
1 parent d768446 commit a4224ed

1 file changed

Lines changed: 13 additions & 11 deletions

File tree

source/gcc/retarget_syscalls.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,16 @@
4646
#endif
4747

4848
#ifndef STDIN_ECHO
49-
#define STDIN_ECHO 0 /* STDIN: echo to STDOUT */
49+
#define STDIN_ECHO 0 /* STDIN: echo to STDOUT */
5050
#endif
5151
#ifndef STDOUT_CR_LF
52-
#define STDOUT_CR_LF 0 /* STDOUT: add CR for LF */
52+
#define STDOUT_CR_LF 0 /* STDOUT: add CR for LF */
5353
#endif
5454
#ifndef STDERR_CR_LF
55-
#define STDERR_CR_LF 0 /* STDERR: add CR for LF */
55+
#define STDERR_CR_LF 0 /* STDERR: add CR for LF */
56+
#endif
57+
#ifndef SBRK_HEAP_LIMIT_CHECK
58+
#define SBRK_HEAP_LIMIT_CHECK 0 /* SBRK: use __HeapLimit symbol to check for out of heap condition */
5659
#endif
5760

5861
/* Forward prototypes. */
@@ -613,11 +616,12 @@ pid_t _wait (int *stat_loc) {
613616
/* Extend heap space by incr bytes (reentrant version) */
614617
__attribute__((weak))
615618
void *_sbrk_r (struct _reent *reent, ptrdiff_t incr) {
619+
#if (SBRK_HEAP_LIMIT_CHECK != 0)
616620
extern char __HeapLimit;
621+
#endif
617622
extern char __HeapStart __ASM("end");
618623
static char *heap;
619624
char *heap_prev;
620-
void *p;
621625

622626
if (heap == NULL) {
623627
/* Initialize current heap memory address */
@@ -626,18 +630,16 @@ void *_sbrk_r (struct _reent *reent, ptrdiff_t incr) {
626630

627631
heap_prev = heap;
628632

633+
#if (SBRK_HEAP_LIMIT_CHECK != 0)
629634
if ((heap + incr) > &__HeapLimit) {
630635
/* Out of heap memory */
631636
reent->_errno = ENOMEM;
632637

633-
p = (void *)-1;
638+
return ((void *)-1);
634639
}
635-
else {
636-
/* All good, extend heap space */
637-
heap += incr;
640+
#endif
638641

639-
p = (void *)heap_prev;
640-
}
642+
heap += incr;
641643

642-
return (p);
644+
return ((void *)heap_prev);
643645
}

0 commit comments

Comments
 (0)