You copied the Doc URL to your clipboard.

How do I retrieve stack and heap information in C language when __user_initial_stackheap() or __user_setup_stackheap() is used?

Article ID: 156363815

Published date: 31 Jan 2018

Last updated: -

Applies to: Compilers

Problem/Question

How do I retrieve stack and heap information in C language when __user_initial_stackheap() or __user_setup_stackheap() is used?

Scenario

N/A

Answer

Suppose that you use the function __user_initial_stackheap() to get the stack and heap information in a project. If the C micro-library (microLib) is not selected at link time, you get the following code segment in the assembly source file, say startup_xxxxxx.s:

;***********************************************************************

; User Stack and Heap initialization

;***********************************************************************

IF :DEF:__MICROLIB

EXPORT __initial_sp

EXPORT __heap_base

EXPORT __heap_limit

ELSE

IMPORT __use_two_region_memory

EXPORT __user_initial_stackheap

__user_initial_stackheap

LDR R0, = Heap_Mem

LDR R1, =(Stack_Mem + Stack_Size)

LDR R2, = (Heap_Mem + Heap_Size)

LDR R3, = Stack_Mem

BX LR

ALIGN

ENDIF

The conditional compilation in the code indicates that you cannot use the symbol __initial_sp, __heap_base, and __heap_limit in the C language directly. To retrieve the heap and stack information, you must use a wrapper function.

In the assembly source file startup_xxxxxx.s, add the following function:

EXPORT get_heap_stack_info

IMPORT __user_initial_stackheap

get_heap_stack_info

PUSH {R4, LR} ; preserve R4 and LR

MOVS R4, R0 ; use R4 as base address

; retrieve stack and heap info

BL __user_initial_stackheap

STMIA R4, {R0-R3} ; store information with R4

POP {R4, LR} ; restore the R4 and LR

BX LR

In any C source file, add the following code:

/* Definition of the stack and heap information structure */

typedef struct {

uint32_t wHeapBase;

uint32_t wStackBase;

uint32_t wHeapLimit;

uint32_t wStackLimit;

} stack_ heap_info_t;

extern void get_heap_stack_info(stack_heap_info_t *ptInfo);

When the stack and heap information is needed, you can use the function get_heap_stack_info to load the information to a specific stack_heap_info_t object. For example:

stack_heap_info_t s_tInfo;

get_heap_stack_info((stack_heap_info_t *)&s_tInfo);

When using __user_setup_stackheap() rather than __user_initial_stackheap(), you can use a similar method. In such a case, the get_heap_stack_info can be implemented as below:

EXPORT get_heap_stack_info

IMPORT __user_setup_stackheap

get_heap_stack_info

PUSH {R4, LR} ; preserve R4 and LR

MOVS R4, R0 ; use R4 as base address

; retrieve stack and heap info

BL __user_setup_stackheap

MOVS R1, SP ; copy SP to R1

STMIA R4, {R0-R2} ; store information with R4

POP {R4, LR} ; restore the R4 and LR

BX LR

The stack_heap_info_t can be updated as below:

/* Definition of the stack and heap information structure */

typedef struct {

uint32_t wHeapBase;

uint32_t wStackBase;

uint32_t wHeapLimit;

} stack_heap_info_t;

Workaround

N/A

Example

N/A

Was this page helpful? Yes No