Standard C heap initialization, rvds4.0 + 6410 initialize C heap malloc
I tried to use the method of initializing the heap through Assembly many times before. It can be used on Keil, but it has never been successful on rvds4.0. Today I found a method to initialize the heap directly in C.
First, the Assembly entry is not processed.
; Skip; jump to the C main function; --------------------------------------------------------------------- importmain blmainb.
Then add the header file and compiler instruction to the C program, and implement a function at the same time.
// Standard C heap support # include "rt_heap.h" # pragma import (_ use_realtime_heap) // This function is declared in rt_heap.h and needs to be implemented by the user, returns any unsigned _ rt_heap_extend (unsigned size, void ** block) {return 0 ;}
Finally, initialize the heap space after entering the main function. I used ok6410 to set the last MB of memory to the heap space.
_ Init_alloc (0x56400000, 0x600000008-8); // initialize the heap range.
The above completes the heap initialization. Now you can use malloc. Of course, do not forget to release it.
In rvds4.0, I also found a simple description, which is concise enough.
Finally, let's test it.
// Request Heap space p1 = (u8 *) malloc (1024); If (INT) P1 = 0) {uart_printf ("malloc error! \ R \ n ");} else {uart_printf (" malloc OK = 0x % 08x \ r \ n ", P1 );} // The second Request Heap space P2 = (u8 *) malloc (1024); If (INT) P2 = 0) {uart_printf ("malloc error! \ R \ n ");} else {uart_printf (" malloc OK = 0x % 08x \ r \ n ", P2 );} // release the heap space free (P1) for the first request; // The Third Request Heap space P3 = (u8 *) malloc (1024); If (INT) p3 = 0) {uart_printf ("malloc error! \ R \ n ");} else {uart_printf (" malloc OK = 0x % 08x \ r \ n ", P3 );}
Result
The result shows that the requested heap is within the defined range. After the first heap space is released, it is applied again for the third time.