GP32 __heapstats In Ads?


RobertJ

Certified Guru
Joined
Feb 18, 2003
Messages
98
Anyone have any experience of using __heapstats with the ADS compiler?

Whilst using Mirko's SDK gives the wonders of malloc() and free(), there seems to be no easy way under ADS to see how much free space there is in the heap.

It's annoying that I have to write a bleeding dprint function to see how much is free under ADS, when I want to get the answers in ints and not strings.

I need to cache a lot of tables in ram, dumping older unused ones as need be, but in order to know when I should dump them, I need to know how much I have left. :/

TIA
 
Don't know about Mirko's malloc() and free() function as I just use the ADS provided memory functions with my own heap initialisation for the gp32's ram. I wrap up the malloc & free routines with my own which give you the approximate amount of memory used (and therefore you can calculate the approximate amount free) :

Code:
void *tracemalloc(const unsigned char *name, unsigned long len) 
{
  unsigned long *p;

  p = malloc(8 + len);

  if(!p)
  {
    printf("---------------");
    printf("out of memory!");
    printf("tried to allocate: %d bytes", len);
    printf("for: %s", name);
    printf("(in use = %d bytes)", tracemalloc_total);
    for(;;);
  }

  p[0] = len;
  p[1] = 0xFEEDBEEF;

  tracemalloc_total += 8 + len;
  return (void*)(p + 2);
}

/////////////////////////////////////////////////////////////////////////////

void tracefree(void *vp)
{
  unsigned long *p = ((unsigned long*)vp) - 2;

  if (p[1] != 0xFEEDBEEF)
  {
   printf ("*** BAD FREE (double/addr/underflow) -> %X", vp);
   for (;;);
  }

  p[1] = 0; // so we can check for attempts to free same memory twice
  tracemalloc_total -= 8 + p[0];

  free(p);
}

As you can see, I also have my own "printf" routines which output to the gp32's screen, but I'm sure you can either remove these or replace with your equivalents.

In a global header file somewhere, just do this to make sure you never use malloc & free again:

#define malloc DONT_USE_MALLOC
#define free DONT_USE_FREE

(of course it helps if you don't include the header in your file containing the above routines).

I must admit that I have borrowed this idea from someone else, and then modified it to suit my programs/style. I have no idea who it was though.
 
i'm using this template with ADS and mr.mirko's SDK
you can use free and malloc, printf family functions and stdio functions aswell...
stdout and stderr are redirected on gp:\gpetc\stdout.txt or somthing like that... take a look... __user_initial_stackheap() is redefined in crt0.S but dont know how to get the free heap size...
 
Thanks for the replies, I think Squidge's wrapper is the best idea so far, but still not exactly ideal. :/

If I understood how the dprint function was passed params, I could probably write one that instead of formatting for a string pulled the values before and just ignored the string.

Maybe its time to understand how C deals with variable length param lists... :/
 
Normally they are just shoved on the stack, and the format string states how to pull them off (eg. as char, dword, etc)

I prefer my wrapper classes though as the show where you are trying to free null pointers, or trying to do a double free of the same data.
 
Back
Top