Skip to content

Commit b4d8c24

Browse files
ccli8Cruz Monrreal II
authored andcommitted
Replace __wrap__sbrk with overriding _sbrk
With _sbrk being weak, we can override it directly rather than #if to support heap with two-region model.
1 parent 4937918 commit b4d8c24

File tree

7 files changed

+48
-40
lines changed

7 files changed

+48
-40
lines changed

platform/mbed_retarget.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,12 +1196,10 @@ extern "C" uint32_t __HeapLimit;
11961196
extern "C" int errno;
11971197

11981198
// Dynamic memory allocation related syscall.
1199-
#if (defined(TARGET_NUVOTON) || defined(TWO_RAM_REGIONS))
1199+
#if defined(TWO_RAM_REGIONS)
12001200

12011201
// Overwrite _sbrk() to support two region model (heap and stack are two distinct regions).
12021202
// __wrap__sbrk() is implemented in:
1203-
// TARGET_NUMAKER_PFM_NUC472 targets/TARGET_NUVOTON/TARGET_NUC472/TARGET_NUMAKER_PFM_NUC472/TOOLCHAIN_GCC_ARM/nuc472_retarget.c
1204-
// TARGET_NUMAKER_PFM_M453 targets/TARGET_NUVOTON/TARGET_M451/TARGET_NUMAKER_PFM_M453/TOOLCHAIN_GCC_ARM/m451_retarget.c
12051203
// TARGET_STM32L4 targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L4/l4_retarget.c
12061204
extern "C" void *__wrap__sbrk(int incr);
12071205
extern "C" caddr_t _sbrk(int incr)

targets/TARGET_NUVOTON/TARGET_M2351/device/TOOLCHAIN_GCC_ARM/m2351_retarget.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,20 @@ extern uint32_t __mbed_krbs_start;
1818

1919
#define NU_HEAP_ALIGN 4
2020

21-
/**
22-
* The default implementation of _sbrk() (in common/retarget.cpp) for GCC_ARM requires one-region model (heap and stack share one region), which doesn't
23-
* fit two-region model (heap and stack are two distinct regions), for example, NUMAKER-PFM-NUC472 locates heap on external SRAM. Define __wrap__sbrk() to
24-
* override the default _sbrk(). It is expected to get called through gcc hooking mechanism ('-Wl,--wrap,_sbrk') or in _sbrk().
21+
/* Support heap with two-region model
22+
*
23+
* The default implementation of _sbrk() (in mbed_retarget.cpp) for GCC_ARM requires one-region
24+
* model (heap and stack share one region), which doesn't fit two-region model (heap and stack
25+
* are two distinct regions), e.g., stack in internal SRAM/heap in external SRAM on NUMAKER_PFM_NUC472.
26+
* Hence, override _sbrk() here to support heap with two-region model.
2527
*/
26-
void *__wrap__sbrk(int incr)
28+
void *_sbrk(int incr)
2729
{
2830
static uint32_t heap_ind = (uint32_t) &__mbed_sbrk_start;
2931
uint32_t heap_ind_old = NU_ALIGN_UP(heap_ind, NU_HEAP_ALIGN);
3032
uint32_t heap_ind_new = NU_ALIGN_UP(heap_ind_old + incr, NU_HEAP_ALIGN);
3133

32-
if (heap_ind_new > &__mbed_krbs_start) {
34+
if (heap_ind_new > (uint32_t) &__mbed_krbs_start) {
3335
errno = ENOMEM;
3436
return (void *) -1;
3537
}

targets/TARGET_NUVOTON/TARGET_M451/device/TOOLCHAIN_GCC_ARM/m451_retarget.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,20 @@ extern uint32_t __mbed_krbs_start;
1818

1919
#define NU_HEAP_ALIGN 32
2020

21-
/**
22-
* The default implementation of _sbrk() (in common/retarget.cpp) for GCC_ARM requires one-region model (heap and stack share one region), which doesn't
23-
* fit two-region model (heap and stack are two distinct regions), for example, NUMAKER-PFM-NUC472 locates heap on external SRAM. Define __wrap__sbrk() to
24-
* override the default _sbrk(). It is expected to get called through gcc hooking mechanism ('-Wl,--wrap,_sbrk') or in _sbrk().
21+
/* Support heap with two-region model
22+
*
23+
* The default implementation of _sbrk() (in mbed_retarget.cpp) for GCC_ARM requires one-region
24+
* model (heap and stack share one region), which doesn't fit two-region model (heap and stack
25+
* are two distinct regions), e.g., stack in internal SRAM/heap in external SRAM on NUMAKER_PFM_NUC472.
26+
* Hence, override _sbrk() here to support heap with two-region model.
2527
*/
26-
void *__wrap__sbrk(int incr)
28+
void *_sbrk(int incr)
2729
{
2830
static uint32_t heap_ind = (uint32_t) &__mbed_sbrk_start;
2931
uint32_t heap_ind_old = NU_ALIGN_UP(heap_ind, NU_HEAP_ALIGN);
3032
uint32_t heap_ind_new = NU_ALIGN_UP(heap_ind_old + incr, NU_HEAP_ALIGN);
3133

32-
if (heap_ind_new > &__mbed_krbs_start) {
34+
if (heap_ind_new > (uint32_t) &__mbed_krbs_start) {
3335
errno = ENOMEM;
3436
return (void *) -1;
3537
}

targets/TARGET_NUVOTON/TARGET_M480/device/TOOLCHAIN_GCC_ARM/m480_retarget.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ extern uint32_t __mbed_krbs_start;
1818

1919
#define NU_HEAP_ALIGN 32
2020

21-
/**
22-
* The default implementation of _sbrk() (in common/retarget.cpp) for GCC_ARM requires one-region model (heap and stack share one region), which doesn't
23-
* fit two-region model (heap and stack are two distinct regions), for example, NUMAKER-PFM-NUC472 locates heap on external SRAM. Define __wrap__sbrk() to
24-
* override the default _sbrk(). It is expected to get called through gcc hooking mechanism ('-Wl,--wrap,_sbrk') or in _sbrk().
21+
/* Support heap with two-region model
22+
*
23+
* The default implementation of _sbrk() (in mbed_retarget.cpp) for GCC_ARM requires one-region
24+
* model (heap and stack share one region), which doesn't fit two-region model (heap and stack
25+
* are two distinct regions), e.g., stack in internal SRAM/heap in external SRAM on NUMAKER_PFM_NUC472.
26+
* Hence, override _sbrk() here to support heap with two-region model.
2527
*/
26-
void *__wrap__sbrk(int incr)
28+
void *_sbrk(int incr)
2729
{
2830
static uint32_t heap_ind = (uint32_t) &__mbed_sbrk_start;
2931
uint32_t heap_ind_old = NU_ALIGN_UP(heap_ind, NU_HEAP_ALIGN);

targets/TARGET_NUVOTON/TARGET_NANO100/device/TOOLCHAIN_GCC_ARM/nano100_retarget.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,20 @@ extern uint32_t __mbed_krbs_start;
1818

1919
#define NU_HEAP_ALIGN 4
2020

21-
/**
22-
* The default implementation of _sbrk() (in common/retarget.cpp) for GCC_ARM requires one-region model (heap and stack share one region), which doesn't
23-
* fit two-region model (heap and stack are two distinct regions), for example, NUMAKER-PFM-NUC472 locates heap on external SRAM. Define __wrap__sbrk() to
24-
* override the default _sbrk(). It is expected to get called through gcc hooking mechanism ('-Wl,--wrap,_sbrk') or in _sbrk().
21+
/* Support heap with two-region model
22+
*
23+
* The default implementation of _sbrk() (in mbed_retarget.cpp) for GCC_ARM requires one-region
24+
* model (heap and stack share one region), which doesn't fit two-region model (heap and stack
25+
* are two distinct regions), e.g., stack in internal SRAM/heap in external SRAM on NUMAKER_PFM_NUC472.
26+
* Hence, override _sbrk() here to support heap with two-region model.
2527
*/
26-
void *__wrap__sbrk(int incr)
28+
void *_sbrk(int incr)
2729
{
2830
static uint32_t heap_ind = (uint32_t) &__mbed_sbrk_start;
2931
uint32_t heap_ind_old = NU_ALIGN_UP(heap_ind, NU_HEAP_ALIGN);
3032
uint32_t heap_ind_new = NU_ALIGN_UP(heap_ind_old + incr, NU_HEAP_ALIGN);
3133

32-
if (heap_ind_new > &__mbed_krbs_start) {
34+
if (heap_ind_new > (uint32_t) &__mbed_krbs_start) {
3335
errno = ENOMEM;
3436
return (void *) -1;
3537
}

targets/TARGET_NUVOTON/TARGET_NUC472/device/TOOLCHAIN_GCC_ARM/nuc472_retarget.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,20 @@ extern uint32_t __mbed_krbs_start;
1818

1919
#define NU_HEAP_ALIGN 32
2020

21-
/**
22-
* The default implementation of _sbrk() (in common/retarget.cpp) for GCC_ARM requires one-region model (heap and stack share one region), which doesn't
23-
* fit two-region model (heap and stack are two distinct regions), for example, NUMAKER-PFM-NUC472 locates heap on external SRAM. Define __wrap__sbrk() to
24-
* override the default _sbrk(). It is expected to get called through gcc hooking mechanism ('-Wl,--wrap,_sbrk') or in _sbrk().
21+
/* Support heap with two-region model
22+
*
23+
* The default implementation of _sbrk() (in mbed_retarget.cpp) for GCC_ARM requires one-region
24+
* model (heap and stack share one region), which doesn't fit two-region model (heap and stack
25+
* are two distinct regions), e.g., stack in internal SRAM/heap in external SRAM on NUMAKER_PFM_NUC472.
26+
* Hence, override _sbrk() here to support heap with two-region model.
2527
*/
26-
void *__wrap__sbrk(int incr)
28+
void *_sbrk(int incr)
2729
{
2830
static uint32_t heap_ind = (uint32_t) &__mbed_sbrk_start;
2931
uint32_t heap_ind_old = NU_ALIGN_UP(heap_ind, NU_HEAP_ALIGN);
3032
uint32_t heap_ind_new = NU_ALIGN_UP(heap_ind_old + incr, NU_HEAP_ALIGN);
3133

32-
if (heap_ind_new > &__mbed_krbs_start) {
34+
if (heap_ind_new > (uint32_t) &__mbed_krbs_start) {
3335
errno = ENOMEM;
3436
return (void *) -1;
3537
}

targets/TARGET_NUVOTON/mbed_rtx.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@
3131
#define ISR_STACK_START ((unsigned char*)Image$$ARM_LIB_STACK$$ZI$$Base)
3232
#define ISR_STACK_SIZE ((uint32_t)Image$$ARM_LIB_STACK$$ZI$$Length)
3333
#elif defined(__GNUC__)
34-
extern uint32_t __StackTop[];
35-
extern uint32_t __StackLimit[];
36-
extern uint32_t __end__[];
37-
extern uint32_t __HeapLimit[];
38-
#define HEAP_START ((unsigned char*)__end__)
39-
#define HEAP_SIZE ((uint32_t)((uint32_t)__HeapLimit - (uint32_t)HEAP_START))
40-
#define ISR_STACK_START ((unsigned char*)__StackLimit)
41-
#define ISR_STACK_SIZE ((uint32_t)((uint32_t)__StackTop - (uint32_t)__StackLimit))
34+
extern uint32_t __StackTop;
35+
extern uint32_t __StackLimit;
36+
extern uint32_t __end__;
37+
extern uint32_t __HeapLimit;
38+
#define HEAP_START ((unsigned char*) &__end__)
39+
#define HEAP_SIZE ((uint32_t) ((uint32_t) &__HeapLimit - (uint32_t) HEAP_START))
40+
#define ISR_STACK_START ((unsigned char*) &__StackLimit)
41+
#define ISR_STACK_SIZE ((uint32_t)((uint32_t) &__StackTop - (uint32_t) &__StackLimit))
4242
#elif defined(__ICCARM__)
4343
/* No region declarations needed */
4444
#else

0 commit comments

Comments
 (0)