Skip to content

Commit 0a0b326

Browse files
committed
[NUC472/M453] Change sbrk() allocation to be 32-byte aligned
1 parent 453f60e commit 0a0b326

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111

1212
#include "M451Series.h"
1313
#include <errno.h>
14+
#include "nu_miscutil.h"
1415

1516
extern uint32_t __mbed_sbrk_start;
1617
extern uint32_t __mbed_krbs_start;
1718

19+
#define NU_HEAP_ALIGN 32
20+
1821
/**
1922
* 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
2023
* 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
@@ -23,8 +26,8 @@ extern uint32_t __mbed_krbs_start;
2326
void *__wrap__sbrk(int incr)
2427
{
2528
static uint32_t heap_ind = (uint32_t) &__mbed_sbrk_start;
26-
uint32_t heap_ind_old = heap_ind;
27-
uint32_t heap_ind_new = (heap_ind_old + incr + 7) & ~7;
29+
uint32_t heap_ind_old = NU_ALIGN_UP(heap_ind, NU_HEAP_ALIGN);
30+
uint32_t heap_ind_new = NU_ALIGN_UP(heap_ind_old + incr, NU_HEAP_ALIGN);
2831

2932
if (heap_ind_new > &__mbed_krbs_start) {
3033
errno = ENOMEM;

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111

1212
#include "NUC472_442.h"
1313
#include <errno.h>
14+
#include "nu_miscutil.h"
1415

1516
extern uint32_t __mbed_sbrk_start;
1617
extern uint32_t __mbed_krbs_start;
1718

19+
#define NU_HEAP_ALIGN 32
20+
1821
/**
1922
* 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
2023
* 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
@@ -23,8 +26,8 @@ extern uint32_t __mbed_krbs_start;
2326
void *__wrap__sbrk(int incr)
2427
{
2528
static uint32_t heap_ind = (uint32_t) &__mbed_sbrk_start;
26-
uint32_t heap_ind_old = heap_ind;
27-
uint32_t heap_ind_new = (heap_ind_old + incr + 7) & ~7;
29+
uint32_t heap_ind_old = NU_ALIGN_UP(heap_ind, NU_HEAP_ALIGN);
30+
uint32_t heap_ind_new = NU_ALIGN_UP(heap_ind_old + incr, NU_HEAP_ALIGN);
2831

2932
if (heap_ind_new > &__mbed_krbs_start) {
3033
errno = ENOMEM;

targets/TARGET_NUVOTON/nu_miscutil.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ extern "C" {
2424
#define NU_MAX(a,b) ((a)>(b)?(a):(b))
2525
#define NU_MIN(a,b) ((a)<(b)?(a):(b))
2626
#define NU_CLAMP(x, min, max) NU_MIN(NU_MAX((x), (min)), (max))
27+
#define NU_ALIGN_DOWN(X, ALIGN) ((X) & ~((ALIGN) - 1))
28+
#define NU_ALIGN_UP(X, ALIGN) (((X) + (ALIGN) - 1) & ~((ALIGN) - 1))
2729

2830
void nu_nop(uint32_t n);
2931

0 commit comments

Comments
 (0)