Display used RAM?

Post Reply
EyKcir
Posts: 4
Joined: Sun Mar 18, 2012 3:28 pm

Display used RAM?

Post by EyKcir » Sun Mar 18, 2012 3:42 pm

Is there a function to display the used ram in libnds?

WinterMute
Site Admin
Posts: 2052
Joined: Tue Aug 09, 2005 3:21 am
Location: UK
Contact:

Re: Display used RAM?

Post by WinterMute » Mon Mar 19, 2012 2:23 am

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()); 
}
Help keep devkitPro toolchains free, Donate today

Personal Blog

EyKcir
Posts: 4
Joined: Sun Mar 18, 2012 3:28 pm

Re: Display used RAM?

Post by EyKcir » Mon Mar 19, 2012 5:07 pm

Thank you!

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests