00001
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
00044 class MEMUNION
00045 {
00046 public:
00047 union
00048 {
00049 MEMUNION *ptr;
00050 INT32 size;
00051 };
00052 UINT16 owner;
00053 UINT16 age;
00054 };
00055
00060 class MEMBLOCK
00061 {
00062 public:
00063 MEMUNION * blockstart;
00064 MEMUNION *blockend;
00065 MEMUNION *freechunk;
00066 MEMUNION *topchunk;
00067 MEMBLOCK *next;
00068 INT32 upperspace;
00069 INT32 lowerspace;
00070
00071 MEMUNION *find_chunk(
00072 INT32 count);
00073 };
00074
00079 class FREE_CALL
00080 {
00081 public:
00082 void *freeer;
00083 INT32 count;
00084 FREE_CALL() {
00085 freeer = NULL;
00086 count = 0;
00087 }
00088 };
00089
00094 class MALLOC_CALL
00095 {
00096 public:
00097 void *caller;
00098 FREE_CALL *free_list;
00099 INT32 *counts;
00100 INT32 free_bits;
00101
00102 MALLOC_CALL() {
00103 caller = NULL;
00104 free_list = NULL;
00105 counts = NULL;
00106 free_bits = 0;
00107 }
00108 void count_freeer(
00109 void *addr);
00110
00111 void init_freeers();
00112 };
00113
00118 class MEM_ALLOCATOR
00119 {
00120 public:
00121 INT16 blockcount;
00122 UINT16 malloc_serial;
00123 MEMBLOCK *topblock;
00124 MEMBLOCK *currblock;
00125 MALLOC_CALL *callers;
00126 void *(*malloc) (INT32);
00127 void (*free) (void *);
00128 INT32 maxsize;
00129 INT32 biggestblock;
00130 INT32 totalmem;
00131 INT32 memsize;
00132 UINT32 malloc_div_ratio;
00133 UINT32 malloc_minor_serial;
00134 UINT32 malloc_auto_count;
00135 INT32 call_bits;
00136 INT32 entries;
00137
00138 MEMBLOCK memblocks[MAXBLOCKS];
00139
00140 void init (
00141 void *(*ext_malloc) (INT32),
00142 void (*ext_free) (void *),
00143 INT32 firstsize,
00144 INT32 lastsize,
00145 INT32 maxchunk);
00146
00147 void *alloc(
00148 INT32 size,
00149 void *caller);
00150 void *alloc_p(
00151 INT32 size,
00152 void *caller);
00153 void dealloc(
00154 void *ptr,
00155 void *caller);
00156 void check(
00157 const char *string,
00158 INT8 level);
00159
00160 void reduce_counts();
00161 void display_counts();
00162 MEMBLOCK *new_block(
00163 INT32 minsize);
00164 UINT16 hash_caller(
00165 void *addr);
00166
00167 private:
00168 void init_callers();
00169 void set_owner(
00170 MEMUNION *chunkstart,
00171 void *caller);
00172 };
00173
00174 extern MEM_ALLOCATOR big_mem;
00175 extern MEM_ALLOCATOR main_mem;
00176
00177 extern MEMUNION *free_structs[MAX_STRUCTS];
00178
00179 extern INT32 structs_in_use[MAX_STRUCTS];
00180
00181 extern INT32 blocks_in_use[MAX_STRUCTS];
00182
00183 extern MEMUNION *struct_blocks[MAX_STRUCTS];
00184 extern INT32 owner_counts[MAX_STRUCTS][MAX_CLASSES];
00185
00186
00189 extern INT_VAR_H (mem_mallocdepth, 0, "Malloc stack depth to trace");
00190 extern INT_VAR_H (mem_mallocbits, 8, "Log 2 of hash table size");
00191 extern INT_VAR_H (mem_freedepth, 0, "Free stack dpeth to trace");
00192 extern INT_VAR_H (mem_freebits, 8, "Log 2 of hash table size");
00193 extern INT_VAR_H (mem_countbuckets, 16, "No of buckets for histogram");
00194 extern INT_VAR_H (mem_checkfreq, 0,
00195 "Calls to alloc_mem between owner counts");
00198 void *trace_caller(
00199 INT32 depth
00200 );
00201 INT32 identify_struct_owner(
00202 INT32 struct_count,
00203 const char *name
00204 );
00205 void check_struct(
00206 INT8 level,
00207 INT32 count
00208 );
00209 void check_structs(
00210 INT8 level
00211 );
00212 void *new_struct_block();
00213 void old_struct_block(
00214 MEMUNION *deadblock
00215 );
00216 #endif