How does Optlib initialize the heap?

From GNU Tools - Wiki
Jump to: navigation, search

How does Optlib initialize the heap? (RL78, RX, ARM targets)

Using the Optimized Library, the heap space is set between 2 pointers when malloc() is first called:

 _heap_of_memory
 _top_of_heap()

_top_of_heap() returns a pointer to the stack pointer:

 char* _top_of_heap(void)
 {
 	return stack_ptr;
 }

_heap_of memory is set as the address of the label _end. The label is found by default in the project linker script (Debug/projectName_Debug_auto.gsi) as the end of the .bss section. The Optlib uses it to initialize the start of the heap:

 if(_heap_of_memory == NULL)
 {
 	_heap_of_memory = &end;
 	_last_heap_object = &end;
 }

If an user wants to define his own heap space with a desired length, he can do that by overwriting the address that the _heap_of_memory and _last_heap_object get and the address that _top_of_heap() returns.

NOTE: Make sure that the given addresses are not reserved and they are part of the RAM section!