We have detected your current browser version is not the latest one. Xilinx.com uses the latest web technologies to bring you the best online experience possible. Please upgrade to a Xilinx.com supported browser:Chrome, Firefox, Internet Explorer 11, Safari. Thank you!

Page Bookmarked

AR# 12421

MicroBlaze - How is memory managed in MicroBlaze? Should I use malloc/free functions to manage memory?


General Description:
How is memory managed in MicroBlaze? Should I use malloc/free functions to manage my memory?


MicroBlaze currently does not have a memory-management unit (MMU) in hardware or any memory management support in the basic libraries, as a hardware MMU would utilize a great deal more hardware resources. In addition, feature-rich malloc/free functions that are typically found in lib.c are more than 15kB.

Currently, malloc already works properly in that it allocates memory while memory is available and returns NULL if all memory is used. Free is very system-specific and is only an indication to the memory management subsystems that a given set of memory is no longer needed in the given program. It is up to the system to implement the actual functionality of free.

Often, a memory management subsystem does not free memory right away, but rather only performs a freed memory sweep when malloc indicates that the available memory has fallen below a predetermined lower boundary. Only then will the memory manager actually de-allocate all memory that was pre-marked for freeing by calls to free. For code size reasons, free simply does nothing for now (a1.9).

The only safe way to write code that works on any system is to make sure that what malloc returns is greater than NULL. (Please see the example below.) Perform the malloc once, and be certain it is not called out recursively along with free, as this will use up your DS-OCM quickly.

In the future, we will implement full memory management (libraries and hardware MMU) that will make this option available. Remember that this extra functionality will affect performance, in terms of LUTs and of larger code size (larger library functions). However, this option will allow users to write code that news/mallocs and frees storage in various locations.

For now, it is wise to malloc memory only once and reuse this malloced memory as often as possible. You should also write code that always checks to see if malloc has succeeded.

An example of checking the return value of malloc:

digit8 = (char*) malloc(9*sizeof(char));

if (digit8 != NULL) {

for(d=7 ; d>=1 ; d--){
r = n%10;
q = (int)(n/10);

AR# 12421
Date 12/15/2012
Status Active
Type General Article
  • EDK - 10.1 sp2
  • EDK - 10.1
  • EDK - 10.1 sp1
  • More
  • EDK - 10.1 sp3
  • EDK - 11.1
  • EDK - 11.2
  • EDK - 11.3
  • EDK - 11.4
  • EDK - 11.5
  • EDK - 12.1
  • Less
  • Microblaze