00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef MEMBLK_H
00021 #define MEMBLK_H
00022
00023 #include "varable.h"
00024
00025 #define MAXBLOCKS 16
00026 #define MAX_STRUCTS 20 //no of units maintained
00027 #define MAX_CLASSES 24 //max classes of each size
00028 #define MAX_FREE_S_BLOCKS 10 //max free list before all freed
00029 #define STRUCT_BLOCK_SIZE 2521
00030 #define MAX_CHUNK 262144 //max single chunk
00031 #define FIRSTSIZE 16384 //size of first block
00032 #define LASTSIZE 262144 //biggest size to use
00033 #define BIGSIZE 2100000 //size of big blocks
00034 #define MAX_BIGCHUNK 20000000 //max chunk of big mem
00035
00036
00037
00038
00039 class MEMUNION
00040 {
00041 public:
00042 union
00043 {
00044 MEMUNION *ptr;
00045 inT32 size;
00046 };
00047 uinT16 owner;
00048 uinT16 age;
00049 };
00050
00051 class MEMBLOCK
00052 {
00053 public:
00054 MEMUNION * blockstart;
00055 MEMUNION *blockend;
00056 MEMUNION *freechunk;
00057 MEMUNION *topchunk;
00058 MEMBLOCK *next;
00059 inT32 upperspace;
00060 inT32 lowerspace;
00061
00062 MEMUNION *find_chunk(
00063 inT32 count);
00064 };
00065
00066 class FREE_CALL
00067 {
00068 public:
00069 void *freeer;
00070 inT32 count;
00071 FREE_CALL() {
00072 freeer = NULL;
00073 count = 0;
00074 }
00075 };
00076 class MALLOC_CALL
00077 {
00078 public:
00079 void *caller;
00080 FREE_CALL *free_list;
00081 inT32 *counts;
00082 inT32 free_bits;
00083
00084 MALLOC_CALL() {
00085 caller = NULL;
00086 free_list = NULL;
00087 counts = NULL;
00088 free_bits = 0;
00089 }
00090 void count_freeer(
00091 void *addr);
00092
00093 void init_freeers();
00094 };
00095
00096 class MEM_ALLOCATOR
00097 {
00098 public:
00099 inT16 blockcount;
00100 uinT16 malloc_serial;
00101 MEMBLOCK *topblock;
00102 MEMBLOCK *currblock;
00103 MALLOC_CALL *callers;
00104 void *(*malloc) (inT32);
00105 void (*free) (void *);
00106 inT32 maxsize;
00107 inT32 biggestblock;
00108 inT32 totalmem;
00109 inT32 memsize;
00110 uinT32 malloc_div_ratio;
00111 uinT32 malloc_minor_serial;
00112 uinT32 malloc_auto_count;
00113 inT32 call_bits;
00114 inT32 entries;
00115
00116 MEMBLOCK memblocks[MAXBLOCKS];
00117
00118 void init (
00119 void *(*ext_malloc) (inT32),
00120 void (*ext_free) (void *),
00121 inT32 firstsize,
00122 inT32 lastsize,
00123 inT32 maxchunk);
00124
00125 void *alloc(
00126 inT32 size,
00127 void *caller);
00128 void *alloc_p(
00129 inT32 size,
00130 void *caller);
00131 void dealloc(
00132 void *ptr,
00133 void *caller);
00134 void check(
00135 const char *string,
00136 inT8 level);
00137
00138 void reduce_counts();
00139 void display_counts();
00140 MEMBLOCK *new_block(
00141 inT32 minsize);
00142 uinT16 hash_caller(
00143 void *addr);
00144
00145 private:
00146 void init_callers();
00147 void set_owner(
00148 MEMUNION *chunkstart,
00149 void *caller);
00150 };
00151 extern MEM_ALLOCATOR big_mem;
00152 extern MEM_ALLOCATOR main_mem;
00153
00154 extern MEMUNION *free_structs[MAX_STRUCTS];
00155
00156 extern inT32 structs_in_use[MAX_STRUCTS];
00157
00158 extern inT32 blocks_in_use[MAX_STRUCTS];
00159
00160 extern MEMUNION *struct_blocks[MAX_STRUCTS];
00161 extern inT32 owner_counts[MAX_STRUCTS][MAX_CLASSES];
00162
00163 extern INT_VAR_H (mem_mallocdepth, 0, "Malloc stack depth to trace");
00164 extern INT_VAR_H (mem_mallocbits, 8, "Log 2 of hash table size");
00165 extern INT_VAR_H (mem_freedepth, 0, "Free stack dpeth to trace");
00166 extern INT_VAR_H (mem_freebits, 8, "Log 2 of hash table size");
00167 extern INT_VAR_H (mem_countbuckets, 16, "No of buckets for histogram");
00168 extern INT_VAR_H (mem_checkfreq, 0,
00169 "Calls to alloc_mem between owner counts");
00170
00171 void *trace_caller(
00172 inT32 depth
00173 );
00174 inT32 identify_struct_owner(
00175 inT32 struct_count,
00176 const char *name
00177 );
00178 void check_struct(
00179 inT8 level,
00180 inT32 count
00181 );
00182 void check_structs(
00183 inT8 level
00184 );
00185 void *new_struct_block();
00186 void old_struct_block(
00187 MEMUNION *deadblock
00188 );
00189 #endif