// ----------------------------- // C Headers / Local Headers // ----------------------------- #include #include #include "memory.h" // ----------------------------- // doNothing // ----------------------------- static void doNothing(const char *fmt, ...) { (void)fmt; } // ----------------------------- // showPointer // ----------------------------- static void showPointer( const char *which, void *ptr, int line, const char *file ) { #if M_DEBUG == 1 # define showPointerOutput printf #else # define showPointerOutput doNothing #endif showPointerOutput("%p (%s) %d %s\n", ptr, which, line, file); } // ----------------------------- // m_allocateFileLine // ----------------------------- static void *m_allocateFileLine(int bytes, const char *file, int line) { void *ptr = malloc(bytes); if(!ptr) { printf( "Could not allocate: %d bytes (%s | %d)\n", bytes, file, line ); exit(1); } showPointer("allocate", ptr, line, file); return(ptr); } // ----------------------------- // m_reallocateFileLine // ----------------------------- static void *m_reallocateFileLine(void *ptr, int bytes, const char *file, int line) { void *thePointer = realloc(ptr, bytes); if(!thePointer) { printf( "Could not reallocate to: %d bytes (%s | %d)\n", bytes, file, line ); exit(1); } if(thePointer != ptr) { showPointer("deallocate", ptr, line, file); showPointer("allocate", thePointer, line, file); } return(thePointer); } // ----------------------------- // m_deallocateFileLine // ----------------------------- void m_deallocateFileLine(void *ptr, const char *file, int line) { showPointer("deallocate", ptr, line, file); free(ptr); } MemoryFunctions Memory = { m_allocateFileLine, m_reallocateFileLine, m_deallocateFileLine };