3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
17 * Files for debugging memory allocator
26 // Warning( "MEM: Too many malloc's!" );
27 // Warning( "MEM: Malloc returnd an already alloced block!" );
28 // Warning( "MEM: Malloc Failed!" );
29 // Warning( "MEM: Freeing the NULL pointer!" );
30 // Warning( "MEM: Freeing a non-malloced pointer!" );
31 // Warning( "MEM: %d/%d check bytes were overwritten at the end of %8x", ec, CHECKSIZE, buffer );
32 // Warning( "MEM: %d blocks were left allocated!", numleft );
38 #include <sys/malloc.h>
50 #include <Processes.h>
51 ubyte virtual_memory_on = 0;
53 #if defined(RELEASE) || defined(NDEBUG)
61 static int sMemStatsFileInitialized = false;
62 static FILE* sMemStatsFile = NULL;
63 static char sMemStatsFileName[32] = "memstats.txt";
64 #endif // end of if MEMSTATS
66 #else // no memstats on pc
70 #define FULL_MEM_CHECKING 1
72 #if defined(FULL_MEM_CHECKING) && !defined(NDEBUG)
75 #define CHECKBYTE 0xFC
77 #define MAX_INDEX 10000
79 static void *MallocBase[MAX_INDEX];
80 static unsigned int MallocSize[MAX_INDEX];
81 static unsigned char Present[MAX_INDEX];
82 static char * Filename[MAX_INDEX];
83 static char * Varname[MAX_INDEX];
84 static int LineNum[MAX_INDEX];
85 static int BytesMalloced = 0;
87 int show_mem_info = 1;
89 static int free_list[MAX_INDEX];
91 static int num_blocks = 0;
93 static int Initialized = 0;
95 static int LargestIndex = 0;
97 int out_of_memory = 0;
99 void mem_display_blocks();
107 for (i=0; i<MAX_INDEX; i++ )
121 atexit(mem_display_blocks);
125 // need to check for virtual memory since we will need to do some
126 // tricks based on whether it is on or not
130 THz theD2PartionPtr = nil;
131 unsigned long thePartitionSize = 0;
134 MoreMasters(); // allocate 240 more master pointers (mainly for snd handles)
138 virtual_memory_on = 0;
139 if(Gestalt(gestaltVMAttr, &mem_type) == noErr)
141 virtual_memory_on = 1;
144 sMemStatsFile = fopen(sMemStatsFileName, "wt");
146 if (sMemStatsFile != NULL)
148 sMemStatsFileInitialized = true;
150 theD2PartionPtr = ApplicationZone();
151 thePartitionSize = ((unsigned long) theD2PartionPtr->bkLim) - ((unsigned long) theD2PartionPtr);
152 fprintf(sMemStatsFile, "\nMemory Stats File Initialized.");
153 fprintf(sMemStatsFile, "\nDescent 2 launched in partition of %u bytes.\n",
156 #endif // end of ifdef MEMSTATS
159 #endif // end of ifdef macintosh
163 void PrintInfo( int id )
165 fprintf( stderr, "\tBlock '%s' created in %s, line %d.\n", Varname[id], Filename[id], LineNum[id] );
169 void * mem_malloc( unsigned int size, char * var, char * filename, int line, int fill_zero )
178 // printf("malloc: %d %s %d\n", size, filename, line);
182 unsigned long theFreeMem = 0;
184 if (sMemStatsFileInitialized)
186 theFreeMem = FreeMem();
188 fprintf(sMemStatsFile,
189 "\n%9u bytes free before attempting: MALLOC %9u bytes.",
194 #endif // end of ifdef memstats
196 if ( num_blocks >= MAX_INDEX ) {
197 fprintf( stderr,"\nMEM_OUT_OF_SLOTS: Not enough space in mem.c to hold all the mallocs.\n" );
198 fprintf( stderr, "\tBlock '%s' created in %s, line %d.\n", var, filename, line );
199 Error( "MEM_OUT_OF_SLOTS" );
202 id = free_list[ num_blocks++ ];
204 if (id > LargestIndex ) LargestIndex = id;
208 fprintf( stderr,"\nMEM_OUT_OF_SLOTS: Not enough space in mem.c to hold all the mallocs.\n" );
209 fprintf( stderr, "\tBlock '%s' created in %s, line %d.\n", Varname[id], Filename[id], LineNum[id] );
210 Error( "MEM_OUT_OF_SLOTS" );
214 ptr = malloc( size+CHECKSIZE );
216 ptr = (void *)NewPtrClear( size+CHECKSIZE );
220 for (j=0; j<=LargestIndex; j++ )
222 if (Present[j] && MallocBase[j] == (unsigned int)ptr )
224 fprintf( stderr,"\nMEM_SPACE_USED: Malloc returned a block that is already marked as preset.\n" );
225 fprintf( stderr, "\tBlock '%s' created in %s, line %d.\n", Varname[id], Filename[id], Line[id] );
226 Warning( "MEM_SPACE_USED" );
235 fprintf( stderr, "\nMEM_OUT_OF_MEMORY: Malloc returned NULL\n" );
236 fprintf( stderr, "\tBlock '%s' created in %s, line %d.\n", Varname[id], Filename[id], LineNum[id] );
237 Error( "MEM_OUT_OF_MEMORY" );
240 MallocBase[id] = ptr;
241 MallocSize[id] = size;
243 Filename[id] = filename;
249 BytesMalloced += size;
251 for (i=0; i<CHECKSIZE; i++ )
252 pc[size+i] = CHECKBYTE;
255 memset( ptr, 0, size );
261 int mem_find_id( void * buffer )
265 for (i=0; i<=LargestIndex; i++ )
267 if (MallocBase[i] == buffer )
274 int mem_check_integrity( int block_number )
279 CheckData = (char *)(MallocBase[block_number] + MallocSize[block_number]);
283 for (i=0; i<CHECKSIZE; i++ )
284 if (CheckData[i] != CHECKBYTE ) {
286 fprintf( stderr, "OA: %p ", &CheckData[i] );
289 if (ErrorCount && (!out_of_memory)) {
290 fprintf( stderr, "\nMEM_OVERWRITE: Memory after the end of allocated block overwritten.\n" );
291 PrintInfo( block_number );
292 fprintf( stderr, "\t%d/%d check bytes were overwritten.\n", ErrorCount, CHECKSIZE );
300 void mem_free( void * buffer )
309 unsigned long theFreeMem = 0;
311 if (sMemStatsFileInitialized)
313 theFreeMem = FreeMem();
315 fprintf(sMemStatsFile,
316 "\n%9u bytes free before attempting: FREE", theFreeMem);
319 #endif // end of ifdef memstats
321 if (buffer==NULL && (!out_of_memory))
323 fprintf( stderr, "\nMEM_FREE_NULL: An attempt was made to free the null pointer.\n" );
324 Warning( "MEM: Freeing the NULL pointer!" );
329 id = mem_find_id( buffer );
331 if (id==-1 && (!out_of_memory))
333 fprintf( stderr, "\nMEM_FREE_NOMALLOC: An attempt was made to free a ptr that wasn't\nallocated with mem.h included.\n" );
334 Warning( "MEM: Freeing a non-malloced pointer!" );
339 mem_check_integrity( id );
341 BytesMalloced -= MallocSize[id];
346 DisposePtr( (Ptr)buffer );
354 free_list[ --num_blocks ] = id;
357 void *mem_realloc(void * buffer, unsigned int size, char * var, char * filename, int line)
368 } else if (buffer == NULL) {
369 newbuffer = mem_malloc(size, var, filename, line, 0);
371 newbuffer = mem_malloc(size, var, filename, line, 0);
372 if (newbuffer != NULL) {
373 id = mem_find_id(buffer);
374 if (MallocSize[id] < size)
375 size = MallocSize[id];
376 memcpy(newbuffer, buffer, size);
383 void mem_display_blocks()
387 if (Initialized==0) return;
391 if (sMemStatsFileInitialized)
393 unsigned long theFreeMem = 0;
395 theFreeMem = FreeMem();
397 fprintf(sMemStatsFile,
398 "\n%9u bytes free before closing MEMSTATS file.", theFreeMem);
399 fprintf(sMemStatsFile, "\nMemory Stats File Closed.");
400 fclose(sMemStatsFile);
403 #endif // end of ifdef memstats
406 for (i=0; i<=LargestIndex; i++ )
408 if (Present[i]==1 && (!out_of_memory))
412 fprintf( stderr, "\nMEM_LEAKAGE: Memory block has not been freed.\n" );
415 mem_free( (void *)MallocBase[i] );
419 if (numleft && (!out_of_memory))
421 Warning( "MEM: %d blocks were left allocated!", numleft );
426 void mem_validate_heap()
430 for (i=0; i<LargestIndex; i++ )
432 mem_check_integrity( i );
440 ef = fopen( "DESCENT.MEM", "wt" );
442 for (i=0; i<LargestIndex; i++ )
443 if (Present[i]==1 ) {
444 size += MallocSize[i];
445 //fprintf( ef, "Var:%s\t File:%s\t Line:%d\t Size:%d Base:%x\n", Varname[i], Filename[i], Line[i], MallocSize[i], MallocBase[i] );
446 fprintf( ef, "%12d bytes in %s declared in %s, line %d\n", MallocSize[i], Varname[i], Filename[i], LineNum[i] );
448 fprintf( ef, "%d bytes (%d Kbytes) allocated.\n", size, size/1024 );
454 static int Initialized = 0;
455 static unsigned int SmallestAddress = 0xFFFFFFF;
456 static unsigned int LargestAddress = 0x0;
457 static unsigned int BytesMalloced = 0;
459 void mem_display_blocks();
462 #define CHECKBYTE 0xFC
464 int show_mem_info = 0;
470 SmallestAddress = 0xFFFFFFF;
471 LargestAddress = 0x0;
473 atexit(mem_display_blocks);
477 // need to check for virtual memory since we will need to do some
478 // tricks based on whether it is on or not
482 THz theD2PartionPtr = nil;
483 unsigned long thePartitionSize = 0;
486 MoreMasters(); // allocate 240 more master pointers (mainly for snd handles)
490 virtual_memory_on = 0;
491 if(Gestalt(gestaltVMAttr, &mem_type) == noErr)
493 virtual_memory_on = 1;
496 sMemStatsFile = fopen(sMemStatsFileName, "wt");
498 if (sMemStatsFile != NULL)
500 sMemStatsFileInitialized = true;
502 theD2PartionPtr = ApplicationZone();
503 thePartitionSize = ((unsigned long) theD2PartionPtr->bkLim) - ((unsigned long) theD2PartionPtr);
504 fprintf(sMemStatsFile, "\nMemory Stats File Initialized.");
505 fprintf(sMemStatsFile, "\nDescent 2 launched in partition of %u bytes.\n",
508 #endif // end of ifdef memstats
511 #endif // end of ifdef macintosh
515 void * mem_malloc( unsigned int size, char * var, char * filename, int line, int fill_zero )
526 unsigned long theFreeMem = 0;
528 if (sMemStatsFileInitialized)
530 theFreeMem = FreeMem();
532 fprintf(sMemStatsFile,
533 "\n%9u bytes free before attempting: MALLOC %9u bytes.",
538 #endif // end of ifdef memstats
541 fprintf( stderr, "\nMEM_MALLOC_ZERO: Attempting to malloc 0 bytes.\n" );
542 fprintf( stderr, "\tVar %s, file %s, line %d.\n", var, filename, line );
543 Error( "MEM_MALLOC_ZERO" );
548 ptr = malloc( size + CHECKSIZE );
550 ptr = (void *)NewPtrClear( size+CHECKSIZE );
554 fprintf( stderr, "\nMEM_OUT_OF_MEMORY: Malloc returned NULL\n" );
555 fprintf( stderr, "\tVar %s, file %s, line %d.\n", var, filename, line );
556 Error( "MEM_OUT_OF_MEMORY" );
560 base = (unsigned int)ptr;
561 if ( base < SmallestAddress ) SmallestAddress = base;
562 if ( (base+size) > LargestAddress ) LargestAddress = base+size;
567 BytesMalloced += *psize;
570 memset( ptr, 0, size );
575 void mem_free( void * buffer )
577 int * psize = (int *)buffer;
585 unsigned long theFreeMem = 0;
587 if (sMemStatsFileInitialized)
589 theFreeMem = FreeMem();
591 fprintf(sMemStatsFile,
592 "\n%9u bytes free before attempting: FREE", theFreeMem);
595 #endif // end of ifdef memstats
598 fprintf( stderr, "\nMEM_FREE_NULL: An attempt was made to free the null pointer.\n" );
599 Warning( "MEM: Freeing the NULL pointer!" );
604 BytesMalloced -= *psize;
609 DisposePtr( (Ptr)buffer );
613 void mem_display_blocks()
615 if (Initialized==0) return;
619 if (sMemStatsFileInitialized)
621 unsigned long theFreeMem = 0;
623 theFreeMem = FreeMem();
625 fprintf(sMemStatsFile,
626 "\n%9u bytes free before closing MEMSTATS file.", theFreeMem);
627 fprintf(sMemStatsFile, "\nMemory Stats File Closed.");
628 fclose(sMemStatsFile);
631 #endif // end of ifdef memstats
633 if (BytesMalloced != 0 ) {
634 fprintf( stderr, "\nMEM_LEAKAGE: %d bytes of memory have not been freed.\n", BytesMalloced );
638 fprintf( stderr, "\n\nMEMORY USAGE:\n" );
639 fprintf( stderr, " %u Kbytes dynamic data\n", (LargestAddress-SmallestAddress+512)/1024 );
640 fprintf( stderr, " %u Kbytes code/static data.\n", (SmallestAddress-(4*1024*1024)+512)/1024 );
641 fprintf( stderr, " ---------------------------\n" );
642 fprintf( stderr, " %u Kbytes required.\n", (LargestAddress-(4*1024*1024)+512)/1024 );
646 void mem_validate_heap()
659 // routine to try and compact and purge the process manager zone to squeeze
660 // some temporary memory out of it for QT purposes.
666 THz appZone, processZone;
669 // compact the system zone to try and squeeze some temporary memory out of it
670 MaxMemSys(&heapSize);
672 // compact the Process Manager zone to get more temporary memory
673 appZone = ApplicationZone();
674 tempHandle = TempNewHandle(10, &err); // temporary allocation may fail
675 if (!err && (tempHandle != NULL) ) {
676 processZone = HandleZone(tempHandle);
677 if ( MemError() || (processZone == NULL) ) {
678 DisposeHandle(tempHandle);
681 SetZone(processZone);
682 MaxMem(&heapSize); // purge and compact the Process Manager Zone.
684 DisposeHandle(tempHandle);