Skip to content

Commit 3c8e3ed

Browse files
c1728p9adbridge
authored andcommitted
Fix memory allocation on STM32L4 devices
Depending on initial size allocated on STM32L4 devices with TWO_RAM_REGIONS set a crash may occur. This is because there is a mismatch between the size newlib is expecting and the size actually returned by _sbrk. This is because the STM32L4 implementation of _sbrk is performing alignment internally. This patch fixes this problem by removing the code in __wrap__sbrk which performs the alignment.
1 parent 564483e commit 3c8e3ed

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

targets/TARGET_STM/TARGET_STM32L4/l4_retarget.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@
3838
extern uint32_t __mbed_sbrk_start;
3939
extern uint32_t __mbed_krbs_start;
4040

41-
#define STM32L4_HEAP_ALIGN 32
42-
#define STM32L4_ALIGN_UP(X, ALIGN) (((X) + (ALIGN) - 1) & ~((ALIGN) - 1))
4341
/**
4442
* The default implementation of _sbrk() (in platform/mbed_retarget.cpp) for GCC_ARM requires one-region model (heap and
4543
* stack share one region), which doesn't fit two-region model (heap and stack are two distinct regions), for example,
@@ -50,8 +48,8 @@ extern uint32_t __mbed_krbs_start;
5048
void *__wrap__sbrk(int incr)
5149
{
5250
static uint32_t heap_ind = (uint32_t) &__mbed_sbrk_start;
53-
uint32_t heap_ind_old = STM32L4_ALIGN_UP(heap_ind, STM32L4_HEAP_ALIGN);
54-
uint32_t heap_ind_new = STM32L4_ALIGN_UP(heap_ind_old + incr, STM32L4_HEAP_ALIGN);
51+
uint32_t heap_ind_old = heap_ind;
52+
uint32_t heap_ind_new = heap_ind_old + incr;
5553

5654
if (heap_ind_new > &__mbed_krbs_start) {
5755
errno = ENOMEM;

0 commit comments

Comments
 (0)