MicroBlaze: malloc function dynamically allocates memory overflow

First, the MicroBlaze C library supports standard memory management functions such as malloc(), calloc(), and free(). These standard C libraries are defined in libc.a. Dynamic memory allocation is provided from the heap of the program in memory. So the size of the heap directly affects the size of the memory allocation that is called malloc. The heap size cannot be increased at runtime, so you need to determine the appropriate heap size when the program is compiled. When the size can be EGW under the generate linker script, or directly modify the *.ld file, for example :

_STACK_SIZE = DEFINED(_STACK_SIZE) ? _STACK_SIZE : 0x1000;
_HEAP_SIZE = DEFINED(_HEAP_SIZE) ? _HEAP_SIZE : 0x1000;

If the malloc(int size) function is called, the malloc function returns NULL, which indicates that the memory allocation failed because:

The size of the requested memory may exceed the size of the memory itself.

The size of the application memory exceeds the size of the heap_size

For the above two reasons, you can modify the heap_size and increase the actual physical memory.

One caveat here: every time you call malloc, you must check the return value to ensure the actual memory allocation request.

In practice, I applied for a block of nearly 3MB of memory for storing image data through malloc, but because the heap was set too small, the return value was always NULL and memory allocation failed.

Call malloc code
Void *zalloc(unsigned long size)
{
Void *ret = malloc(size);
If (ret) memset(ret, 0, size);
Return ret;
}

USB Connector

Usb Connector,Micro Usb Sinking Connector,Usb 3.0 Solder Connector,Double Layer Usb Connector

Dongguan Yangyue Metal Technology Co., Ltd , https://www.yyconnector.com

Posted on