/* $Id: mem.c,v 1.1.1.1 2006/03/17 19:52:29 zicodxx Exp $ */ /* THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE. COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED. */ /* * * Files for debugging memory allocator * * */ #include #include #include #include "pstypes.h" #include "error.h" #include "args.h" #include "console.h" #define MEMSTATS 0 #define FULL_MEM_CHECKING 1 #if defined(FULL_MEM_CHECKING) && !defined(NDEBUG) #define CHECKSIZE 16 #define CHECKBYTE 0xFC #define MAX_INDEX 10000 static void *MallocBase[MAX_INDEX]; static unsigned int MallocSize[MAX_INDEX]; static unsigned char Present[MAX_INDEX]; static char * Filename[MAX_INDEX]; static char * Varname[MAX_INDEX]; static int LineNum[MAX_INDEX]; static int BytesMalloced = 0; static int free_list[MAX_INDEX]; static int num_blocks = 0; static int Initialized = 0; static int LargestIndex = 0; int out_of_memory = 0; void mem_display_blocks(); void mem_init() { int i; Initialized = 1; for (i=0; i= MAX_INDEX ) { con_printf(CON_CRITICAL,"\nMEM_OUT_OF_SLOTS: Not enough space in mem.c to hold all the mallocs.\n" ); con_printf(CON_CRITICAL, "\tBlock '%s' created in %s, line %d.\n", var, filename, line ); Error( "MEM_OUT_OF_SLOTS" ); } id = free_list[ num_blocks++ ]; if (id > LargestIndex ) LargestIndex = id; if (id==-1) { con_printf(CON_CRITICAL,"\nMEM_OUT_OF_SLOTS: Not enough space in mem.c to hold all the mallocs.\n" ); con_printf(CON_CRITICAL, "\tBlock '%s' created in %s, line %d.\n", Varname[id], Filename[id], LineNum[id] ); Error( "MEM_OUT_OF_SLOTS" ); } ptr = malloc( size+CHECKSIZE ); if (ptr==NULL) { out_of_memory = 1; con_printf(CON_CRITICAL, "\nMEM_OUT_OF_MEMORY: Malloc returned NULL\n" ); con_printf(CON_CRITICAL, "\tBlock '%s' created in %s, line %d.\n", Varname[id], Filename[id], LineNum[id] ); Error( "MEM_OUT_OF_MEMORY" ); } MallocBase[id] = ptr; MallocSize[id] = size; Varname[id] = var; Filename[id] = filename; LineNum[id] = line; Present[id] = 1; pc = (char *)ptr; BytesMalloced += size; for (i=0; i LargestAddress ) LargestAddress = base+size; psize = (int *)ptr; psize--; BytesMalloced += *psize; if (fill_zero) memset( ptr, 0, size ); return ptr; } void mem_free( void * buffer ) { int * psize = (int *)buffer; psize--; if (Initialized==0) mem_init(); #if MEMSTATS { unsigned long theFreeMem = 0; if (sMemStatsFileInitialized) { theFreeMem = FreeMem(); fprintf(sMemStatsFile, "\n%9u bytes free before attempting: FREE", theFreeMem); } } #endif // end of ifdef memstats if (buffer==NULL) { con_printf(CON_CRITICAL, "\nMEM_FREE_NULL: An attempt was made to free the null pointer.\n" ); Warning( "MEM: Freeing the NULL pointer!" ); Int3(); return; } BytesMalloced -= *psize; free( buffer ); } void mem_display_blocks() { if (Initialized==0) return; #if MEMSTATS { if (sMemStatsFileInitialized) { unsigned long theFreeMem = 0; theFreeMem = FreeMem(); fprintf(sMemStatsFile, "\n%9u bytes free before closing MEMSTATS file.", theFreeMem); fprintf(sMemStatsFile, "\nMemory Stats File Closed."); fclose(sMemStatsFile); } } #endif // end of ifdef memstats if (BytesMalloced != 0 ) { con_printf(CON_CRITICAL, "\nMEM_LEAKAGE: %d bytes of memory have not been freed.\n", BytesMalloced ); } if (GameArg.DbgShowMemInfo) { con_printf(CON_CRITICAL, "\n\nMEMORY USAGE:\n" ); con_printf(CON_CRITICAL, " %u Kbytes dynamic data\n", (LargestAddress-SmallestAddress+512)/1024 ); con_printf(CON_CRITICAL, " %u Kbytes code/static data.\n", (SmallestAddress-(4*1024*1024)+512)/1024 ); con_printf(CON_CRITICAL, " ---------------------------\n" ); con_printf(CON_CRITICAL, " %u Kbytes required.\n", (LargestAddress-(4*1024*1024)+512)/1024 ); } } void mem_validate_heap() { } void mem_print_all() { } #endif