1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
/***************************************************************************/
/* */
/* memdebug.c */
/* */
/* Memory debugging functions (body only). */
/* */
/* Copyright 1996-1999 by */
/* David Turner, Robert Wilhelm, and Werner Lemberg. */
/* */
/* This file is part of the FreeType project, and may only be used */
/* modified and distributed under the terms of the FreeType project */
/* license, LICENSE.TXT. By continuing to use, modify, or distribute */
/* this file you indicate that you have read the license and */
/* understand and accept it fully. */
/* */
/***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
typedef struct TBlockRec_
{
char* base;
long size;
} TBlockRec;
static TBlockRec* mem_blocks;
static int num_mem_blocks;
static int max_mem_blocks;
void DM_Init_Mem()
{
num_mem_blocks = 0;
max_mem_blocks = 4096;
mem_blocks = (TBlockRec*)malloc( max_mem_blocks *
sizeof ( *mem_blocks ) );
}
void DM_Done_Mem()
{
/* Now print the remaining blocks */
if ( num_mem_blocks == 0 )
{
fprintf( stderr, "No memory leaked!\n" );
}
else
{
int i;
fprintf( stderr, "There were %d leaked memory blocks\n\n",
num_mem_blocks );
fprintf( stderr, "base size\n" );
fprintf( stderr, "------------------\n" );
for ( i = 0; i < num_mem_blocks; i++ )
{
fprintf( stderr, "%08lx %04lx\n",
(long)mem_blocks[i].base, mem_blocks[i].size );
}
}
free( mem_blocks );
}
void DM_Record( char* base,
long size )
{
TBlockRec* block;
#if 0
/* First, check that the block is not located within one of the */
/* recorded blocks */
for ( i = 0; i < num_mem_blocks; i++ )
{
char *start, *end, *_limit, *_base;
_base = mem_blocks[i].base;
_limit = _base + mem_blocks[i].size;
start = base;
end = base + size - 1;
if ( ( start >= base && start < limit ) ||
( end >= base && end < limit ) )
{
fprintf( stderr, "Warning: Recording an invalid block!\n" );
}
}
#endif
/* Add block to list */
if ( num_mem_blocks >= max_mem_blocks )
{
max_mem_blocks *= 2;
mem_blocks = realloc( mem_blocks,
max_mem_blocks * sizeof ( *mem_blocks ) );
}
block = mem_blocks + num_mem_blocks;
block->base = base;
block->size = size;
num_mem_blocks++;
}
void DM_Forget( char* base )
{
TBlockRec* block = mem_blocks;
int i;
for ( i = 0; i < num_mem_blocks; i++, block++ )
{
if ( block->base == base )
{
/* simply move last block to the current position */
if ( num_mem_blocks > 1 )
*block = mem_blocks[num_mem_blocks - 1];
num_mem_blocks--;
return;
}
#if 1
if ( base >= block->base && base < block->base + block->size )
{
fprintf( stderr, "Invalid block forgotten!\n" );
}
#endif
}
}
/* END */