1 /********************************************************************** 2 * File: memblk.h (Formerly memblock.h) 3 * Description: Enhanced instrumented memory allocator implemented as a class. 4 * Author: Ray Smith 5 * Created: Tue Jan 21 17:13:39 GMT 1992 6 * 7 * (C) Copyright 1992, Hewlett-Packard Ltd. 8 ** Licensed under the Apache License, Version 2.0 (the "License"); 9 ** you may not use this file except in compliance with the License. 10 ** You may obtain a copy of the License at 11 ** http://www.apache.org/licenses/LICENSE-2.0 12 ** Unless required by applicable law or agreed to in writing, software 13 ** distributed under the License is distributed on an "AS IS" BASIS, 14 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 ** See the License for the specific language governing permissions and 16 ** limitations under the License. 17 * 18 **********************************************************************/ 19 20 #ifndef MEMBLK_H 21 #define MEMBLK_H 22 23 #include "varable.h" 24 25 #define MAXBLOCKS 16 /*max allowed to grab */ 26 #define MAX_STRUCTS 20 //no of units maintained 27 #define MAX_CLASSES 24 //max classes of each size 28 #define MAX_FREE_S_BLOCKS 10 //max free list before all freed 29 #define STRUCT_BLOCK_SIZE 2521 30 #define MAX_CHUNK 262144 //max single chunk 31 #define FIRSTSIZE 16384 //size of first block 32 #define LASTSIZE 262144 //biggest size to use 33 #define BIGSIZE 2100000 //size of big blocks 34 #define MAX_BIGCHUNK 20000000 //max chunk of big mem 35 36 //#define TESTING_BIGSTUFF //define for big tests 37 //#define COUNTING_CLASS_STRUCTURES 38 39 class MEMUNION 40 { 41 public: 42 union 43 { 44 MEMUNION *ptr; //next chunk 45 inT32 size; //chunk size 46 }; 47 uinT16 owner; //owner of chunk 48 uinT16 age; //age of chunk 49 }; 50 51 class MEMBLOCK 52 { 53 public: 54 MEMUNION * blockstart; /*start of block */ 55 MEMUNION *blockend; /*end of block */ 56 MEMUNION *freechunk; /*next free chunk */ 57 MEMUNION *topchunk; /*top free chunk */ 58 MEMBLOCK *next; /*next block in chain */ 59 inT32 upperspace; /*space above freechunk */ 60 inT32 lowerspace; /*space below freechunk */ 61 62 MEMUNION *find_chunk( //find free chunk 63 inT32 count); //size required 64 }; 65 66 class FREE_CALL 67 { 68 public: 69 void *freeer; //return addr 70 inT32 count; //no of frees FREE_CALL()71 FREE_CALL() { //constructor 72 freeer = NULL; 73 count = 0; 74 } 75 }; 76 class MALLOC_CALL 77 { 78 public: 79 void *caller; //return addr 80 FREE_CALL *free_list; //freeer counts 81 inT32 *counts; //no of blocks 82 inT32 free_bits; //bits in free table 83 MALLOC_CALL()84 MALLOC_CALL() { //constructor 85 caller = NULL; 86 free_list = NULL; 87 counts = NULL; 88 free_bits = 0; 89 } 90 void count_freeer( //check a structure 91 void *addr); //return address 92 93 void init_freeers(); //check a structure 94 }; 95 96 class MEM_ALLOCATOR 97 { 98 public: 99 inT16 blockcount; //blocks in use 100 uinT16 malloc_serial; //serial allocation 101 MEMBLOCK *topblock; //block for permanents 102 MEMBLOCK *currblock; //current block 103 MALLOC_CALL *callers; //hash table of callers 104 void *(*malloc) (inT32); //external allocator 105 void (*free) (void *); //external free 106 inT32 maxsize; //biggest block 107 inT32 biggestblock; //biggest chunk 108 inT32 totalmem; //total free memory 109 inT32 memsize; //current block size 110 uinT32 malloc_div_ratio; //scaling of malloc_serial 111 uinT32 malloc_minor_serial; //scaling counter 112 uinT32 malloc_auto_count; //counts auto checks 113 inT32 call_bits; //size of table 114 inT32 entries; //size of table 115 //all memory blocks 116 MEMBLOCK memblocks[MAXBLOCKS]; 117 118 void init ( //initialize 119 void *(*ext_malloc) (inT32),//external source 120 void (*ext_free) (void *), //external free 121 inT32 firstsize, //size of first block 122 inT32 lastsize, //size of last block 123 inT32 maxchunk); //biggest request 124 125 void *alloc( //allocator 126 inT32 size, //size of chunk 127 void *caller); //ptr to caller 128 void *alloc_p( //allocator 129 inT32 size, //size of chunk 130 void *caller); //ptr to caller 131 void dealloc( //deallocator 132 void *ptr, //mem to free 133 void *caller); //ptr to caller 134 void check( //check chunks 135 const char *string, //message 136 inT8 level); //amount of checks 137 138 void reduce_counts(); //divide by 2 139 void display_counts(); //count up 140 MEMBLOCK *new_block( //get new big block 141 inT32 minsize); //minimum size 142 uinT16 hash_caller( //check a structure 143 void *addr); //return address 144 145 private: 146 void init_callers(); //check a structure 147 void set_owner( //set owner & date 148 MEMUNION *chunkstart, //chunk to set 149 void *caller); //ptr to caller 150 }; 151 extern MEM_ALLOCATOR big_mem; 152 extern MEM_ALLOCATOR main_mem; 153 //heads of freelists 154 extern MEMUNION *free_structs[MAX_STRUCTS]; 155 //number issued 156 extern inT32 structs_in_use[MAX_STRUCTS]; 157 //number issued 158 extern inT32 blocks_in_use[MAX_STRUCTS]; 159 //head of block lists 160 extern MEMUNION *struct_blocks[MAX_STRUCTS]; 161 extern inT32 owner_counts[MAX_STRUCTS][MAX_CLASSES]; 162 163 extern INT_VAR_H (mem_mallocdepth, 0, "Malloc stack depth to trace"); 164 extern INT_VAR_H (mem_mallocbits, 8, "Log 2 of hash table size"); 165 extern INT_VAR_H (mem_freedepth, 0, "Free stack dpeth to trace"); 166 extern INT_VAR_H (mem_freebits, 8, "Log 2 of hash table size"); 167 extern INT_VAR_H (mem_countbuckets, 16, "No of buckets for histogram"); 168 extern INT_VAR_H (mem_checkfreq, 0, 169 "Calls to alloc_mem between owner counts"); 170 171 void *trace_caller( //trace stack 172 inT32 depth //depth to trace 173 ); 174 inT32 identify_struct_owner( //get table index 175 inT32 struct_count, //cell size 176 const char *name //name of type 177 ); 178 void check_struct( //check a structure 179 inT8 level, //print control 180 inT32 count //no of bytes 181 ); 182 void check_structs( //count in use on structs 183 inT8 level //print control 184 ); 185 void *new_struct_block(); //allocate memory 186 void old_struct_block( //free a structure block 187 MEMUNION *deadblock //block to free 188 ); 189 #endif 190