gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > Memory Profilers

#167337 - albinofrenchy - Mon Mar 09, 2009 11:38 am

Are there any memory debugger tools ported to the DS for leak detection?

If not, anyone know a lightweight one? All I've used is valgrind and there is no way that will port.

#167339 - samel - Mon Mar 09, 2009 1:23 pm

I've one ... i'll send it by mail

#167413 - paladine - Wed Mar 11, 2009 4:51 am

I wrote my own by implementing new and delete. If you're interested, I could provide it.

#167748 - hacker013 - Wed Mar 25, 2009 4:28 pm

Could you post it here?
_________________
Website / Blog

Let the nds be with you.

#167768 - a128 - Thu Mar 26, 2009 5:35 pm

hacker013 wrote:
Could you post it here?


Yes, please post it here

#167852 - paladine - Tue Mar 31, 2009 5:43 pm

Quick and dirty implemention. Tracks total number of objects and entire allocated memory size. Inserts a guard value at the end of the memory and checks against it when the memory is finally freed. The only thing you'd have to implement to use this code is PrintStatusMessage(const char *fmt,...);

Code:

#ifdef _DEBUG
#define GUARD_VALUE 0xDEEDBEEF
#define NEXT4ALIGN(x) ( (x + 3) & ~0x3)
static u32 total = 0;
static u32 objects = 0;
static u32 oid = 0;

void *operator new(size_t sz)
{
    u8 *mem = reinterpret_cast<u8*>(malloc(sz+12+3));
    if (mem)
    {
        reinterpret_cast<u32*>(mem)[0] = sz; // insert size before data area
        reinterpret_cast<u32*>(mem)[1] = ++oid; // insert allocation id before data area
        mem += 8;
        *reinterpret_cast<u32*>(mem+NEXT4ALIGN(sz)) = GUARD_VALUE; // insert guard value at end of memory
        total += sz; // increment total size of allocations
        ++objects;
        PrintStatusMessage("Alloc: %p,%u,%u,%u,%u,%p\n",mem,oid,sz,objects,total,__builtin_return_address(0));
    }
    else
    {
        PrintStatusMessage("Failed allocate %u\n",sz);
    }
    return mem;

}

void operator delete(void *p) {
    u32 sz = reinterpret_cast<u32*>(p)[-2]; // retrieve size from memory
    u32 id = reinterpret_cast<u32*>(p)[-1]; // retrieve id
    if (*reinterpret_cast<u32*>(reinterpret_cast<u8*>(p)+NEXT4ALIGN(sz)) != GUARD_VALUE) // check against guard value
    {
        PrintStatusMessage("MALLOC OVERFLOW!\n");
    }
    total -= sz; // update counter
    --objects;
    PrintStatusMessage("Free:  %p,%u,%u,%u,%u\n",p,id,sz,objects,total);
    free(reinterpret_cast<u8*>(p)-8);
}

void PrintMemoryStatistics()
{
    PrintStatusMessage("Objects: %u, sz: %u\n",objects,total);
}
#endif

#167869 - hacker013 - Wed Apr 01, 2009 3:23 pm

can you also release a c version ?
_________________
Website / Blog

Let the nds be with you.

#167871 - paladine - Wed Apr 01, 2009 3:45 pm

If you can't convert that from C++ to C, then you've got some problems.

I'll leave that exercise up to the reader.