#152198 - Rajveer - Tue Mar 11, 2008 7:17 pm
Is there any way to retrieve the amount of stack memory available free? I have a recursive function which I believe to be overloading the stack and crashing my game, before rewriting the functions I thought I'd ask to make sure my assumptions are correct.
#152199 - Rajveer - Tue Mar 11, 2008 7:29 pm
Erm, I had a while loop without incrementing the counter -_-
Still, would be helpful and interesting to see how much stack space I'm using with this function, is it possible?
#152200 - DekuTree64 - Tue Mar 11, 2008 7:33 pm
Make a .s file with this:
Code: |
.global GetStackPointer
.text
.thumb
.thumb_func
GetStackPointer:
mov r0, sp
bx lr |
and prototype as
Code: |
void* GetStackPointer(); |
Then you can print the current stack pointer from your recursive function and see if it goes too far.
Another classic method of stack debugging is to put a bunch of known values near the end of the stack (0xDEADBEEF or whatever) and then in your main loop, check if any of them have been overwritten. That will tell you how large the stack has ever grown. Of course, if it actually overflows, you probably won't make it back to the checking location.
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku
#152201 - simonjhall - Tue Mar 11, 2008 7:48 pm
If you don't want to make a separate assembly file, try:
Code: |
register unsigned int sp asm ("sp");
printf("my stack pointer is, %08x\n", sp); |
...for pure ownage.
Not as robust as deku's, though as the compiler may want to move stuff around. For instance if you want to get the lr and change the asm("sp") bit to "lr" you won't get the lr of the current function, you'll get the line after the printf. In which case, take a copy with a volatile non-register variable, then do the printf.
Or use deku's code :-)
PS: the register name is more target dependent than I would have thought. With the PPC GCC that I use, you can't do "sp" - instead you have to do "1".
_________________
Big thanks to everyone who donated for Quake2
#152248 - Rajveer - Wed Mar 12, 2008 8:02 pm
Cheers guys :)
#154891 - Rajveer - Tue Apr 22, 2008 2:09 pm
Don't mean to bring up an old thread (actually I do, that's why I'm doing it lol) but how would you suggest I check how much free RAM I have too? Keep a count of how much I've allocated, or keep mallocing until it fails, or is there another easier way?
#154893 - eKid - Tue Apr 22, 2008 2:33 pm
#154894 - Noda - Tue Apr 22, 2008 3:25 pm
I didn't get mallinfo to work on DS, unfortunately.
Here's what I'm using, I simply allocate memory to know how much free memory is available:
Code: |
// get amount of free RAM
int getFreeRAM()
{
int q = 2*1024*1024; // 2 Mb
int size = q;
void *ptr;
do {
ptr = malloc(size);
if(ptr) {
free(ptr);
} else {
size -= q;
}
q /= 2;
size += q;
} while(q > 0);
return size;
} |
#154896 - Rajveer - Tue Apr 22, 2008 3:28 pm
Mallinfo worked for me, cheers guys. Now I know it's called mallinfo, searching for it brought up similar posts, sorry!
Noda what was the error, did you include <unistd.h> and
<malloc.h>? For allocated memory I simply used:
Code: |
struct mallinfo mi = mallinfo();
int usedMem = mi.uordblks;
printf("\x1b[22;0HMem: Used %d ", usedMem); |
#154897 - Noda - Tue Apr 22, 2008 3:39 pm
I used that exact same piece of code, but wheras my function returned 2149Ko of free memory, the mallinfo you use told me something around 190Ko.
Maybe it's just a misinterpretation of the result returned.
EDIT: btw the way, I used fordblks to get the number of free memory. Maybe this had been adressed with r22, didn't checked.
#154899 - theli - Tue Apr 22, 2008 3:53 pm
i use this for getting free memory
Code: |
#include <malloc.h>
extern u8 __end__[]; // end of static code and data
extern u8 __eheap_end[]; // farthest point to which the heap will grow
u8* getHeapStart() {
return __end__;
}
u8* getHeapEnd() {
return (u8*)sbrk(0);
}
u8* getHeapLimit() {
return __eheap_end;
}
int getMemUsed() {
struct mallinfo mi = mallinfo();
return mi.uordblks;
}
int getMemFree() {
struct mallinfo mi = mallinfo();
return mi.fordblks + (getHeapLimit() - getHeapEnd());
} |
#154900 - Noda - Tue Apr 22, 2008 4:38 pm
Thanks, that may be handy ;)
#154916 - Cydrak - Tue Apr 22, 2008 11:00 pm
Noda wrote: |
wheras my function returned 2149Ko of free memory, the mallinfo you use told me something around 190Ko. |
theli wrote: |
i use this for getting free memory
... |
Just to clarify (since I use the same thing):
mi.fordblks is right, but it only seems to cover free space inside the heap. The heap itself can shrink and grow, IIRC, through calls to sbrk(). The discrepancy should vanish once you add the space between the heap and the RAM limit.
#154943 - Rajveer - Wed Apr 23, 2008 1:34 am
Thanks for the explanation Cydrak, now I understand :)