Skip to content

Commit 23af842

Browse files
authored
Merge pull request #10078 from OpenNuvoton/nuvoton_fix-heap-in-rtosless
Fix heap init error in rtos-less code
2 parents 536da47 + 64515c0 commit 23af842

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

platform/mbed_retarget.cpp

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -920,25 +920,33 @@ __asm(".global __use_no_semihosting\n\t");
920920
#endif
921921
#endif
922922

923-
#if !defined(HEAP_START)
923+
// Through weak-reference, we can check if ARM_LIB_HEAP is defined at run-time.
924+
// If ARM_LIB_HEAP is defined, we can fix heap allocation.
925+
extern MBED_WEAK uint32_t Image$$ARM_LIB_HEAP$$ZI$$Base[];
926+
extern MBED_WEAK uint32_t Image$$ARM_LIB_HEAP$$ZI$$Length[];
927+
extern MBED_WEAK uint32_t Image$$ARM_LIB_HEAP$$ZI$$Limit[];
928+
924929
// Heap here is considered starting after ZI ends to Stack start
925930
extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Base[];
926931
extern uint32_t Image$$RW_IRAM1$$ZI$$Limit[];
927-
#define HEAP_START Image$$RW_IRAM1$$ZI$$Limit
928-
#define HEAP_SIZE ((uint32_t)((uint32_t) Image$$ARM_LIB_STACK$$ZI$$Base - (uint32_t) HEAP_START))
929-
#endif
930-
931-
#define HEAP_LIMIT ((uint32_t)((uint32_t)HEAP_START + (uint32_t)HEAP_SIZE))
932932

933933
extern "C" MBED_WEAK __value_in_regs struct __initial_stackheap _mbed_user_setup_stackheap(uint32_t R0, uint32_t R1, uint32_t R2, uint32_t R3)
934934
{
935-
uint32_t heap_base = (uint32_t)HEAP_START;
935+
// Define heap by assuming one-region
936+
uint32_t heap_base = (uint32_t)Image$$RW_IRAM1$$ZI$$Limit;
937+
uint32_t heap_limit = (uint32_t)Image$$ARM_LIB_STACK$$ZI$$Base;
936938
struct __initial_stackheap r;
937939

940+
// Fix heap if ARM_LIB_HEAP is defined
941+
if (Image$$ARM_LIB_HEAP$$ZI$$Length) {
942+
heap_base = (uint32_t) Image$$ARM_LIB_HEAP$$ZI$$Base;
943+
heap_limit = (uint32_t) Image$$ARM_LIB_HEAP$$ZI$$Limit;
944+
}
945+
938946
// Ensure heap_base is 8-byte aligned
939947
heap_base = (heap_base + 7) & ~0x7;
940-
r.heap_base = (uint32_t)heap_base;
941-
r.heap_limit = (uint32_t)HEAP_LIMIT;
948+
r.heap_base = heap_base;
949+
r.heap_limit = heap_limit;
942950

943951
return r;
944952
}
@@ -948,7 +956,17 @@ extern "C" __value_in_regs struct __argc_argv $Super$$__rt_lib_init(unsigned hea
948956

949957
extern "C" __value_in_regs struct __argc_argv $Sub$$__rt_lib_init(unsigned heapbase, unsigned heaptop)
950958
{
951-
return $Super$$__rt_lib_init((unsigned)HEAP_START, (unsigned)HEAP_LIMIT);
959+
// Define heap by assuming one-region
960+
uint32_t heap_base = (uint32_t)Image$$RW_IRAM1$$ZI$$Limit;
961+
uint32_t heap_limit = (uint32_t)Image$$ARM_LIB_STACK$$ZI$$Base;
962+
963+
// Fix heap if ARM_LIB_HEAP is defined
964+
if (Image$$ARM_LIB_HEAP$$ZI$$Length) {
965+
heap_base = (uint32_t) Image$$ARM_LIB_HEAP$$ZI$$Base;
966+
heap_limit = (uint32_t) Image$$ARM_LIB_HEAP$$ZI$$Limit;
967+
}
968+
969+
return $Super$$__rt_lib_init((unsigned)heap_base, (unsigned)heap_limit);
952970
}
953971
#endif
954972

0 commit comments

Comments
 (0)