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.
pull/7631/head
ccli8 2018-07-26 14:42:09 +08:00 committed by Cruz Monrreal II
parent 5e8fa8b394
commit f0c4e949c5
7 changed files with 48 additions and 40 deletions

View File

@ -1196,12 +1196,10 @@ extern "C" uint32_t __HeapLimit;
extern "C" int errno;
// Dynamic memory allocation related syscall.
#if (defined(TARGET_NUVOTON) || defined(TWO_RAM_REGIONS))
#if defined(TWO_RAM_REGIONS)
// Overwrite _sbrk() to support two region model (heap and stack are two distinct regions).
// __wrap__sbrk() is implemented in:
// TARGET_NUMAKER_PFM_NUC472 targets/TARGET_NUVOTON/TARGET_NUC472/TARGET_NUMAKER_PFM_NUC472/TOOLCHAIN_GCC_ARM/nuc472_retarget.c
// TARGET_NUMAKER_PFM_M453 targets/TARGET_NUVOTON/TARGET_M451/TARGET_NUMAKER_PFM_M453/TOOLCHAIN_GCC_ARM/m451_retarget.c
// TARGET_STM32L4 targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L4/l4_retarget.c
extern "C" void *__wrap__sbrk(int incr);
extern "C" caddr_t _sbrk(int incr)

View File

@ -18,18 +18,20 @@ extern uint32_t __mbed_krbs_start;
#define NU_HEAP_ALIGN 4
/**
* 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
* 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
* override the default _sbrk(). It is expected to get called through gcc hooking mechanism ('-Wl,--wrap,_sbrk') or in _sbrk().
/* Support heap with two-region model
*
* The default implementation of _sbrk() (in mbed_retarget.cpp) for GCC_ARM requires one-region
* model (heap and stack share one region), which doesn't fit two-region model (heap and stack
* are two distinct regions), e.g., stack in internal SRAM/heap in external SRAM on NUMAKER_PFM_NUC472.
* Hence, override _sbrk() here to support heap with two-region model.
*/
void *__wrap__sbrk(int incr)
void *_sbrk(int incr)
{
static uint32_t heap_ind = (uint32_t) &__mbed_sbrk_start;
uint32_t heap_ind_old = NU_ALIGN_UP(heap_ind, NU_HEAP_ALIGN);
uint32_t heap_ind_new = NU_ALIGN_UP(heap_ind_old + incr, NU_HEAP_ALIGN);
if (heap_ind_new > &__mbed_krbs_start) {
if (heap_ind_new > (uint32_t) &__mbed_krbs_start) {
errno = ENOMEM;
return (void *) -1;
}

View File

@ -18,18 +18,20 @@ extern uint32_t __mbed_krbs_start;
#define NU_HEAP_ALIGN 32
/**
* 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
* 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
* override the default _sbrk(). It is expected to get called through gcc hooking mechanism ('-Wl,--wrap,_sbrk') or in _sbrk().
/* Support heap with two-region model
*
* The default implementation of _sbrk() (in mbed_retarget.cpp) for GCC_ARM requires one-region
* model (heap and stack share one region), which doesn't fit two-region model (heap and stack
* are two distinct regions), e.g., stack in internal SRAM/heap in external SRAM on NUMAKER_PFM_NUC472.
* Hence, override _sbrk() here to support heap with two-region model.
*/
void *__wrap__sbrk(int incr)
void *_sbrk(int incr)
{
static uint32_t heap_ind = (uint32_t) &__mbed_sbrk_start;
uint32_t heap_ind_old = NU_ALIGN_UP(heap_ind, NU_HEAP_ALIGN);
uint32_t heap_ind_new = NU_ALIGN_UP(heap_ind_old + incr, NU_HEAP_ALIGN);
if (heap_ind_new > &__mbed_krbs_start) {
if (heap_ind_new > (uint32_t) &__mbed_krbs_start) {
errno = ENOMEM;
return (void *) -1;
}

View File

@ -18,12 +18,14 @@ extern uint32_t __mbed_krbs_start;
#define NU_HEAP_ALIGN 32
/**
* 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
* 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
* override the default _sbrk(). It is expected to get called through gcc hooking mechanism ('-Wl,--wrap,_sbrk') or in _sbrk().
/* Support heap with two-region model
*
* The default implementation of _sbrk() (in mbed_retarget.cpp) for GCC_ARM requires one-region
* model (heap and stack share one region), which doesn't fit two-region model (heap and stack
* are two distinct regions), e.g., stack in internal SRAM/heap in external SRAM on NUMAKER_PFM_NUC472.
* Hence, override _sbrk() here to support heap with two-region model.
*/
void *__wrap__sbrk(int incr)
void *_sbrk(int incr)
{
static uint32_t heap_ind = (uint32_t) &__mbed_sbrk_start;
uint32_t heap_ind_old = NU_ALIGN_UP(heap_ind, NU_HEAP_ALIGN);

View File

@ -18,18 +18,20 @@ extern uint32_t __mbed_krbs_start;
#define NU_HEAP_ALIGN 4
/**
* 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
* 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
* override the default _sbrk(). It is expected to get called through gcc hooking mechanism ('-Wl,--wrap,_sbrk') or in _sbrk().
/* Support heap with two-region model
*
* The default implementation of _sbrk() (in mbed_retarget.cpp) for GCC_ARM requires one-region
* model (heap and stack share one region), which doesn't fit two-region model (heap and stack
* are two distinct regions), e.g., stack in internal SRAM/heap in external SRAM on NUMAKER_PFM_NUC472.
* Hence, override _sbrk() here to support heap with two-region model.
*/
void *__wrap__sbrk(int incr)
void *_sbrk(int incr)
{
static uint32_t heap_ind = (uint32_t) &__mbed_sbrk_start;
uint32_t heap_ind_old = NU_ALIGN_UP(heap_ind, NU_HEAP_ALIGN);
uint32_t heap_ind_new = NU_ALIGN_UP(heap_ind_old + incr, NU_HEAP_ALIGN);
if (heap_ind_new > &__mbed_krbs_start) {
if (heap_ind_new > (uint32_t) &__mbed_krbs_start) {
errno = ENOMEM;
return (void *) -1;
}

View File

@ -18,18 +18,20 @@ extern uint32_t __mbed_krbs_start;
#define NU_HEAP_ALIGN 32
/**
* 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
* 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
* override the default _sbrk(). It is expected to get called through gcc hooking mechanism ('-Wl,--wrap,_sbrk') or in _sbrk().
/* Support heap with two-region model
*
* The default implementation of _sbrk() (in mbed_retarget.cpp) for GCC_ARM requires one-region
* model (heap and stack share one region), which doesn't fit two-region model (heap and stack
* are two distinct regions), e.g., stack in internal SRAM/heap in external SRAM on NUMAKER_PFM_NUC472.
* Hence, override _sbrk() here to support heap with two-region model.
*/
void *__wrap__sbrk(int incr)
void *_sbrk(int incr)
{
static uint32_t heap_ind = (uint32_t) &__mbed_sbrk_start;
uint32_t heap_ind_old = NU_ALIGN_UP(heap_ind, NU_HEAP_ALIGN);
uint32_t heap_ind_new = NU_ALIGN_UP(heap_ind_old + incr, NU_HEAP_ALIGN);
if (heap_ind_new > &__mbed_krbs_start) {
if (heap_ind_new > (uint32_t) &__mbed_krbs_start) {
errno = ENOMEM;
return (void *) -1;
}

View File

@ -31,14 +31,14 @@
#define ISR_STACK_START ((unsigned char*)Image$$ARM_LIB_STACK$$ZI$$Base)
#define ISR_STACK_SIZE ((uint32_t)Image$$ARM_LIB_STACK$$ZI$$Length)
#elif defined(__GNUC__)
extern uint32_t __StackTop[];
extern uint32_t __StackLimit[];
extern uint32_t __end__[];
extern uint32_t __HeapLimit[];
#define HEAP_START ((unsigned char*)__end__)
#define HEAP_SIZE ((uint32_t)((uint32_t)__HeapLimit - (uint32_t)HEAP_START))
#define ISR_STACK_START ((unsigned char*)__StackLimit)
#define ISR_STACK_SIZE ((uint32_t)((uint32_t)__StackTop - (uint32_t)__StackLimit))
extern uint32_t __StackTop;
extern uint32_t __StackLimit;
extern uint32_t __end__;
extern uint32_t __HeapLimit;
#define HEAP_START ((unsigned char*) &__end__)
#define HEAP_SIZE ((uint32_t) ((uint32_t) &__HeapLimit - (uint32_t) HEAP_START))
#define ISR_STACK_START ((unsigned char*) &__StackLimit)
#define ISR_STACK_SIZE ((uint32_t)((uint32_t) &__StackTop - (uint32_t) &__StackLimit))
#elif defined(__ICCARM__)
/* No region declarations needed */
#else