ccutil/memblk.h

Go to the documentation of this file.
00001 
00020 #ifndef           MEMBLK_H
00021 #define           MEMBLK_H
00022 
00023 #include          "varable.h"
00024 
00025 #define MAXBLOCKS     16         /*max allowed to grab */
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 //#define TESTING_BIGSTUFF                                                                                      //define for big tests
00037 //#define COUNTING_CLASS_STRUCTURES
00038 
00039 
00044 class MEMUNION
00045 {
00046   public:
00047     union
00048     {
00049       MEMUNION *ptr;             //next chunk
00050       INT32 size;                //chunk size
00051     };
00052     UINT16 owner;                //owner of chunk
00053     UINT16 age;                  //age of chunk
00054 };
00055 
00060 class MEMBLOCK
00061 {
00062   public:
00063     MEMUNION * blockstart;       /*start of block */
00064     MEMUNION *blockend;          /*end of block */
00065     MEMUNION *freechunk;         /*next free chunk */
00066     MEMUNION *topchunk;          /*top free chunk */
00067     MEMBLOCK *next;              /*next block in chain */
00068     INT32 upperspace;            /*space above freechunk */
00069     INT32 lowerspace;            /*space below freechunk */
00070 
00071     MEMUNION *find_chunk(               //find free chunk
00072                          INT32 count);  //size required
00073 };
00074 
00079 class FREE_CALL
00080 {
00081   public:
00082     void *freeer;                //return addr
00083     INT32 count;                 //no of frees
00084     FREE_CALL() {  //constructor
00085       freeer = NULL;
00086       count = 0;
00087     }
00088 };
00089 
00094 class MALLOC_CALL
00095 {
00096   public:
00097     void *caller;                //return addr
00098     FREE_CALL *free_list;        //freeer counts
00099     INT32 *counts;               //no of blocks
00100     INT32 free_bits;             //bits in free table
00101 
00102     MALLOC_CALL() {  //constructor
00103       caller = NULL;
00104       free_list = NULL;
00105       counts = NULL;
00106       free_bits = 0;
00107     }
00108     void count_freeer(              //check a structure
00109                       void *addr);  //return address
00110 
00111     void init_freeers();  //check a structure
00112 };
00113 
00118 class MEM_ALLOCATOR
00119 {
00120   public:
00121     INT16 blockcount;            //blocks in use
00122     UINT16 malloc_serial;        //serial allocation
00123     MEMBLOCK *topblock;          //block for permanents
00124     MEMBLOCK *currblock;         //current block
00125     MALLOC_CALL *callers;        //hash table of callers
00126     void *(*malloc) (INT32);     //external allocator
00127     void (*free) (void *);       //external free
00128     INT32 maxsize;               //biggest block
00129     INT32 biggestblock;          //biggest chunk
00130     INT32 totalmem;              //total free memory
00131     INT32 memsize;               //current block size
00132     UINT32 malloc_div_ratio;     //scaling of malloc_serial
00133     UINT32 malloc_minor_serial;  //scaling counter
00134     UINT32 malloc_auto_count;    //counts auto checks
00135     INT32 call_bits;             //size of table
00136     INT32 entries;               //size of table
00137                                  //all memory blocks
00138     MEMBLOCK memblocks[MAXBLOCKS];
00139 
00140     void init (                  //initialize
00141       void *(*ext_malloc) (INT32),//external source
00142       void (*ext_free) (void *), //external free
00143       INT32 firstsize,           //size of first block
00144       INT32 lastsize,            //size of last block
00145       INT32 maxchunk);           //biggest request
00146 
00147     void *alloc(                //allocator
00148                 INT32 size,     //size of chunk
00149                 void *caller);  //ptr to caller
00150     void *alloc_p(                //allocator
00151                   INT32 size,     //size of chunk
00152                   void *caller);  //ptr to caller
00153     void dealloc(                //deallocator
00154                  void *ptr,      //mem to free
00155                  void *caller);  //ptr to caller
00156     void check(                     //check chunks
00157                const char *string,  //message
00158                INT8 level);         //amount of checks
00159 
00160     void reduce_counts();  //divide by 2
00161     void display_counts();  //count up
00162     MEMBLOCK *new_block(                 //get new big block
00163                         INT32 minsize);  //minimum size
00164     UINT16 hash_caller(              //check a structure
00165                        void *addr);  //return address
00166 
00167   private:
00168     void init_callers();  //check a structure
00169     void set_owner(                       //set owner & date
00170                    MEMUNION *chunkstart,  //chunk to set
00171                    void *caller);         //ptr to caller
00172 };
00173 
00174 extern MEM_ALLOCATOR big_mem;
00175 extern MEM_ALLOCATOR main_mem;
00176                                  //heads of freelists
00177 extern MEMUNION *free_structs[MAX_STRUCTS];
00178                                  //number issued
00179 extern INT32 structs_in_use[MAX_STRUCTS];
00180                                  //number issued
00181 extern INT32 blocks_in_use[MAX_STRUCTS];
00182                                  //head of block lists
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(             //trace stack
00199                    INT32 depth  //depth to trace
00200                   );
00201 INT32 identify_struct_owner(                     //get table index
00202                             INT32 struct_count,  //cell size
00203                             const char *name     //name of type
00204                            );
00205 void check_struct(             //check a structure
00206                   INT8 level,  //print control
00207                   INT32 count  //no of bytes
00208                  );
00209 void check_structs(            //count in use on structs
00210                    INT8 level  //print control
00211                   );
00212 void *new_struct_block();  //allocate memory
00213 void old_struct_block(                     //free a structure block
00214                       MEMUNION *deadblock  //block to free
00215                      );
00216 #endif

Generated on Wed Feb 28 19:49:09 2007 for Tesseract by  doxygen 1.5.1