nulldc-360/nullDC/debug_mem.cpp
2022-02-11 13:27:24 +01:00

73 lines
No EOL
1.2 KiB
C++

#include "types.h"
//If the MEM_ALLOC_CHECK define is on , we check for allocation errors
//and report/break
//Notice for future:
//We can add a check witch mallocs were not freed ect
//What bout calloc and other shit ?
#ifdef malloc
#undef malloc
#endif
#ifdef realloc
#undef realloc
#endif
#ifdef free
#undef free
#endif
#ifdef MEM_ALLOC_CHECK
void * debug_malloc(size_t size)
{
if (size==0)
{
dlog("debug_malloc warning : malloc with size=0");
MEM_ERROR_BREAK;
}
void *rv= malloc(size);
if (rv==0)
{
dlog("debug_malloc warning : malloc (%d) failed",size);
MEM_ERROR_BREAK;
}
//if (size==1024*128)
// memset(rv,0,size);
//dlog("malloc %X\n",*(u8*)rv);
return rv;
}
void * debug_realloc(void* mem,size_t size)
{
if (size==0)
{
dlog("debug_realloc warning : malloc with size=0 called \n");
MEM_ERROR_BREAK;
}
if (mem==0)
dlog("debug_realloc warning : malloc with ptr=0 called \n");
void *rv= realloc(mem,size);
if (rv==0)
{
dlog("debug_realloc warning : realloc (0x%p,%d) failed",mem,size);
MEM_ERROR_BREAK;
}
return rv;
}
void debug_free(void* ptr)
{
if (ptr==0)
{
dlog("debug_free warning : malloc with ptr==0 called \n");
MEM_ERROR_BREAK;
}
free(ptr);
}
#endif