Page 1 of 1

Display used RAM?

Posted: Sun Mar 18, 2012 3:42 pm
by EyKcir
Is there a function to display the used ram in libnds?

Re: Display used RAM?

Posted: Mon Mar 19, 2012 2:23 am
by WinterMute
Short answer, no.

Longer answer :-

There is code in newlib which can help determine how much RAM is left to allocate and some internal variables used by the code I added to newlib for allocation. This should help a bit but mostly there's very little need to check this directly and this method won't necessarily tell you if sufficient RAM is left to make an allocation. The memory can become fragmented if you're not careful and the largest block available may not be the total RAM left.

Code: Select all

#include <malloc.h>    // for mallinfo() 
#include <unistd.h>    // for sbrk() 

extern u8 *fake_heap_end;   // current heap start 
extern u8 *fake_heap_start;   // current heap end 

u8* getHeapStart() { 
   return fake_heap_start; 
} 

u8* getHeapEnd() { 
   return (u8*)sbrk(0); 
} 

u8* getHeapLimit() { 
   return fake_heap_end; 
} 

int getMemUsed() { // returns the amount of used memory in bytes 
   struct mallinfo mi = mallinfo(); 
   return mi.uordblks; 
} 

int getMemFree() { // returns the amount of free memory in bytes 
   struct mallinfo mi = mallinfo(); 
   return mi.fordblks + (getHeapLimit() - getHeapEnd()); 
}

Re: Display used RAM?

Posted: Mon Mar 19, 2012 5:07 pm
by EyKcir
Thank you!