This is the mail archive of the
newlib@sourceware.org
mailing list for the newlib project.
[PATCH][ARM]Honour __heap_limit__ during _sbrk() memory allocation checking.
- From: Renlin Li <renlin dot li at arm dot com>
- To: "newlib at sourceware dot org" <newlib at sourceware dot org>
- Cc: marcus Shawcroft <Marcus dot Shawcroft at arm dot com>
- Date: Fri, 13 Feb 2015 16:38:04 +0000
- Subject: [PATCH][ARM]Honour __heap_limit__ during _sbrk() memory allocation checking.
- Authentication-results: sourceware.org; auth=none
Hi all,
HeapInfo is returned when SYS_HEAPINFO Angel semihost call is triggered,
the following data will be returned.
struct block {
int heap_base;
int heap_limit;
int stack_base;
int stack_limit;
};
the heap_limit should be honoured while doing dynamic memory allocation.
Currently, _sbrk() does a simple check by comparing the heap end against
stack pointer. This is not enough, if __heap_limit is valid, it also
should be used to predicate whether enough heap memory is there to be
allocated.
libstdc++v3, gcc regression tests are checked using arm-none-eabi
toolchain, on new issues.
Okay to commit?
Actually, aarch64 target also has the same problem. In addition,
__heap_base__ is not used either. I will come up with a patch later.
libgloss/ChangeLog:
2015-02-13 Renlin Li <renlin.li@arm.com>
* arm/crt0.S: Initialise __heap_limit when ARM_RDI_MONITOR is defined.
* arm/syscalls.c: define __heap_limit global symbol.
* arm/syscalls.c (_sbrk): Honour __heap_limit.
diff --git a/libgloss/arm/crt0.S b/libgloss/arm/crt0.S
index cc1c370..cb9021d 100644
--- a/libgloss/arm/crt0.S
+++ b/libgloss/arm/crt0.S
@@ -123,6 +123,11 @@
#endif
ldr r0, .LC0 /* point at values read */
+ /* Set __heap_limit. */
+ ldr r1, [r0, #4]
+ ldr r2, =__heap_limit
+ str r1, [r2]
+
ldr r1, [r0, #0]
cmp r1, #0
bne .LC32
diff --git a/libgloss/arm/syscalls.c b/libgloss/arm/syscalls.c
index 54e3d74..0ccad21 100644
--- a/libgloss/arm/syscalls.c
+++ b/libgloss/arm/syscalls.c
@@ -587,6 +587,9 @@ _getpid (int n __attribute__ ((unused)))
return 1;
}
+/* Heap limit returned from SYS_HEAPINFO Angel semihost call. */
+uint __heap_limit = 0xcafedead;
+
caddr_t __attribute__((weak))
_sbrk (int incr)
{
@@ -599,7 +602,9 @@ _sbrk (int incr)
prev_heap_end = heap_end;
- if (heap_end + incr > stack_ptr)
+ if ((heap_end + incr > stack_ptr)
+ /* Honour heap limit if it's valid. */
+ || (__heap_limit != 0xcafedead && heap_end + incr > __heap_limit))
{
/* Some of the libstdc++-v3 tests rely upon detecting
out of memory errors, so do not abort here. */