home *** CD-ROM | disk | FTP | other *** search
/ Amiga ACS 1998 #6 / amigaacscoverdisc1998-061998.iso / games / descent / source / mem / mem1.c < prev   
C/C++ Source or Header  |  1998-06-08  |  4KB  |  217 lines

  1. /*
  2. THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
  3. SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
  4. END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
  5. ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
  6. IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
  7. SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
  8. FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
  9. CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
  10. AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
  11. COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
  12. */
  13. /*
  14.  * $Source: $
  15.  * $Revision: $
  16.  * $Author: $
  17.  * $Date: $
  18.  * 
  19.  * Files for debugging memory allocator
  20.  * 
  21.  * $Log: $
  22.  * 
  23.  */
  24.  
  25.  
  26. #pragma off (unreferenced)
  27. static char rcsid[] = "$Id: $";
  28. #pragma on (unreferenced)
  29.  
  30.  
  31. // Warning( "MEM: Too many malloc's!" );
  32. // Warning( "MEM: Malloc returnd an already alloced block!" );
  33. // Warning( "MEM: Malloc Failed!" );
  34. // Warning( "MEM: Freeing the NULL pointer!" );
  35. // Warning( "MEM: Freeing a non-malloced pointer!" );
  36. // Warning( "MEM: %d/%d check bytes were overwritten at the end of %8x", ec, CHECKSIZE, buffer  );
  37. // Warning( "MEM: %d blocks were left allocated!", numleft );
  38.  
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <stdarg.h>
  42. #include <string.h>
  43. #include <dos.h>
  44.  
  45. #include "mono.h"
  46. #include "error.h"
  47.  
  48. #define CHECKSIZE 16
  49.  
  50. static unsigned int MallocBase[10000];
  51. static unsigned int MallocSize[10000];
  52. static unsigned char Present[10000];
  53. static char * Filename[10000];
  54. static char * Varname[10000];
  55. static int Line[10000];
  56. static int BytesMalloced = 0;
  57.  
  58. static int Initialized = 0;
  59.  
  60. void mem_display_blocks();
  61.  
  62. void mem_init()
  63. {
  64.     int i;
  65.  
  66.     Initialized = 1;
  67.  
  68.     for (i=0; i<10000; i++ )
  69.     {
  70.         MallocBase[i] = 0;
  71.         MallocSize[i] = 0;
  72.         Present[i] = 0;
  73.         Filename[i] = NULL;
  74.         Varname[i] = NULL;
  75.         Line[i] = 0;
  76.     }
  77.  
  78.     atexit(mem_display_blocks);
  79.  
  80. }
  81.  
  82. void * mem_malloc( unsigned int size, char * var, char * filename, int line )
  83. {
  84.     int i,j;
  85.     void * ptr;
  86.     char * pc;
  87.  
  88.     if (Initialized==0)
  89.         mem_init();
  90.  
  91.     for (i=0; i<10000; i++ )
  92.         if (Present[i]==0) break;
  93.  
  94.     if (i==10000)
  95.     {
  96.         Warning( "MEM: Too many malloc's!" );
  97.         Int3();
  98.     }
  99.  
  100.     ptr = malloc( size+CHECKSIZE );
  101.  
  102.     for (j=0; j<10000; j++ )
  103.     {
  104.         if (Present[j] && MallocBase[j] == (unsigned int)ptr )
  105.         {
  106.             Warning( "MEM: Malloc returnd an already alloced block!" );
  107.             Int3();
  108.         }
  109.     }
  110.  
  111.  
  112.     if (ptr==NULL)
  113.     {
  114.         Error( "MEM: Malloc Failed! (Var: %s, File: %s, Line: %d Size: %d bytes)",var,filename,line,size );
  115.         Int3();
  116.     }
  117.  
  118.     MallocBase[i] = (unsigned int)ptr;
  119.     MallocSize[i] = (unsigned int)size;
  120.     Varname[i] = var;
  121.     Filename[i] = filename;
  122.     Line[i] = line;
  123.     Present[i]    = 1;
  124.  
  125.     pc = (char *)ptr;
  126.  
  127.     BytesMalloced += size;
  128.  
  129.     for (i=0; i<CHECKSIZE; i++ )
  130.         pc[size+i] = 0xC;
  131.  
  132.     return ptr;
  133.  
  134. }
  135.  
  136. void mem_free( void * buffer )
  137. {
  138.     int i;
  139.     char * pc;
  140.     int n, ec;
  141.  
  142.     if (Initialized==0)
  143.         mem_init();
  144.  
  145.     // mprintf( 0, "Freeing pointer: %8x\n", buffer );
  146.  
  147.     if (buffer==NULL )
  148.     {
  149.         Warning( "MEM: Freeing the NULL pointer!" );
  150.         Int3();
  151.     }
  152.  
  153.     for (i=0; i<10000; i++ )
  154.     {
  155.         if (Present[i]==1)
  156.         {
  157.             if (MallocBase[i] == (unsigned int)buffer )
  158.                 break;
  159.         }
  160.     }
  161.  
  162.     if (i==10000)
  163.     {
  164.         Warning( "MEM: Freeing a non-malloced pointer!" );
  165.         Int3();
  166.     }
  167.     else {
  168.  
  169.         n = i;
  170.         pc = (char *)buffer;
  171.         ec = 0;
  172.     
  173.         for (i=0; i<CHECKSIZE; i++ )
  174.         {
  175.             if (pc[MallocSize[n]+i] != 0xC ) ec++;
  176.         }
  177.     
  178.         if (ec > 0)
  179.         {
  180.             Warning( "MEM: %d/%d check bytes were overwritten at the end of %8x", ec, CHECKSIZE, buffer  );
  181.             Int3();
  182.         }
  183.     
  184.         BytesMalloced -= MallocSize[n];
  185.     
  186.         free( buffer );
  187.     
  188.         Present[n] = 0;
  189.         MallocBase[n] = 0;
  190.         MallocSize[n] = 0;
  191.     }
  192. }
  193.  
  194. void mem_display_blocks()
  195. {
  196.     int i, numleft;
  197.  
  198.     if (Initialized==0) return;
  199.  
  200.     numleft = 0;
  201.     for (i=0; i<10000; i++ )
  202.     {
  203.         if (Present[i]==1)
  204.         {
  205.             numleft++;
  206.             printf( "Block: %d, Var:%s = %x File: %s, Line: %d Size: %d bytes.\n", i, Varname[i], MallocBase[i], Filename[i], Line[i], MallocSize[i] );
  207.             mem_free( (void *)MallocBase[i] );
  208.         }
  209.     }
  210.  
  211.     if (numleft )
  212.     {
  213.         Warning( "MEM: %d blocks were left allocated!", numleft );
  214.     }
  215. }
  216.  
  217.