Tesseract 3.01
/data/source/tesseract-ocr/ccutil/memblk.h
Go to the documentation of this file.
00001 /**********************************************************************
00002  * File:        memblk.h  (Formerly memblock.h)
00003  * Description: Enhanced instrumented memory allocator implemented as a class.
00004  * Author:                                      Ray Smith
00005  * Created:                                     Tue Jan 21 17:13:39 GMT 1992
00006  *
00007  * (C) Copyright 1992, Hewlett-Packard Ltd.
00008  ** Licensed under the Apache License, Version 2.0 (the "License");
00009  ** you may not use this file except in compliance with the License.
00010  ** You may obtain a copy of the License at
00011  ** http://www.apache.org/licenses/LICENSE-2.0
00012  ** Unless required by applicable law or agreed to in writing, software
00013  ** distributed under the License is distributed on an "AS IS" BASIS,
00014  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  ** See the License for the specific language governing permissions and
00016  ** limitations under the License.
00017  *
00018  **********************************************************************/
00019 
00020 #ifndef           MEMBLK_H
00021 #define           MEMBLK_H
00022 
00023 #include          "params.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 class MEMUNION
00040 {
00041   public:
00042     union
00043     {
00044       MEMUNION *ptr;             //next chunk
00045       inT32 size;                //chunk size
00046     };
00047     uinT16 owner;                //owner of chunk
00048     uinT16 age;                  //age of chunk
00049 };
00050 
00051 class MEMBLOCK
00052 {
00053   public:
00054     MEMUNION * blockstart;       /*start of block */
00055     MEMUNION *blockend;          /*end of block */
00056     MEMUNION *freechunk;         /*next free chunk */
00057     MEMUNION *topchunk;          /*top free chunk */
00058     MEMBLOCK *next;              /*next block in chain */
00059     inT32 upperspace;            /*space above freechunk */
00060     inT32 lowerspace;            /*space below freechunk */
00061 
00062     MEMUNION *find_chunk(               //find free chunk
00063                          inT32 count);  //size required
00064 };
00065 
00066 class FREE_CALL
00067 {
00068   public:
00069     void *freeer;                //return addr
00070     inT32 count;                 //no of frees
00071     FREE_CALL() {  //constructor
00072       freeer = NULL;
00073       count = 0;
00074     }
00075 };
00076 class MALLOC_CALL
00077 {
00078   public:
00079     void *caller;                //return addr
00080     FREE_CALL *free_list;        //freeer counts
00081     inT32 *counts;               //no of blocks
00082     inT32 free_bits;             //bits in free table
00083 
00084     MALLOC_CALL() {  //constructor
00085       caller = NULL;
00086       free_list = NULL;
00087       counts = NULL;
00088       free_bits = 0;
00089     }
00090     void count_freeer(              //check a structure
00091                       void *addr);  //return address
00092 
00093     void init_freeers();  //check a structure
00094 };
00095 
00096 class MEM_ALLOCATOR
00097 {
00098   public:
00099     inT16 blockcount;            //blocks in use
00100     uinT16 malloc_serial;        //serial allocation
00101     MEMBLOCK *topblock;          //block for permanents
00102     MEMBLOCK *currblock;         //current block
00103     MALLOC_CALL *callers;        //hash table of callers
00104     void *(*malloc) (inT32);     //external allocator
00105     void (*free) (void *);       //external free
00106     inT32 maxsize;               //biggest block
00107     inT32 biggestblock;          //biggest chunk
00108     inT32 totalmem;              //total free memory
00109     inT32 memsize;               //current block size
00110     uinT32 malloc_div_ratio;     //scaling of malloc_serial
00111     uinT32 malloc_minor_serial;  //scaling counter
00112     uinT32 malloc_auto_count;    //counts auto checks
00113     inT32 call_bits;             //size of table
00114     inT32 entries;               //size of table
00115                                  //all memory blocks
00116     MEMBLOCK memblocks[MAXBLOCKS];
00117 
00118     void init (                  //initialize
00119       void *(*ext_malloc) (inT32),//external source
00120       void (*ext_free) (void *), //external free
00121       inT32 firstsize,           //size of first block
00122       inT32 lastsize,            //size of last block
00123       inT32 maxchunk);           //biggest request
00124 
00125     void *alloc(                //allocator
00126                 inT32 size,     //size of chunk
00127                 void *caller);  //ptr to caller
00128     void *alloc_p(                //allocator
00129                   inT32 size,     //size of chunk
00130                   void *caller);  //ptr to caller
00131     void dealloc(                //deallocator
00132                  void *ptr,      //mem to free
00133                  void *caller);  //ptr to caller
00134     void check(                     //check chunks
00135                const char *string,  //message
00136                inT8 level);         //amount of checks
00137 
00138     void reduce_counts();  //divide by 2
00139     void display_counts();  //count up
00140     MEMBLOCK *new_block(                 //get new big block
00141                         inT32 minsize);  //minimum size
00142     uinT16 hash_caller(              //check a structure
00143                        void *addr);  //return address
00144 
00145   private:
00146     void init_callers();  //check a structure
00147     void set_owner(                       //set owner & date
00148                    MEMUNION *chunkstart,  //chunk to set
00149                    void *caller);         //ptr to caller
00150 };
00151 extern MEM_ALLOCATOR big_mem;
00152 extern MEM_ALLOCATOR main_mem;
00153                                  //heads of freelists
00154 extern MEMUNION *free_structs[MAX_STRUCTS];
00155                                  //number issued
00156 extern inT32 structs_in_use[MAX_STRUCTS];
00157                                  //number issued
00158 extern inT32 blocks_in_use[MAX_STRUCTS];
00159                                  //head of block lists
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(             //trace stack
00172                    inT32 depth  //depth to trace
00173                   );
00174 inT32 identify_struct_owner(                     //get table index
00175                             inT32 struct_count,  //cell size
00176                             const char *name     //name of type
00177                            );
00178 void check_struct(             //check a structure
00179                   inT8 level,  //print control
00180                   inT32 count  //no of bytes
00181                  );
00182 void check_structs(            //count in use on structs
00183                    inT8 level  //print control
00184                   );
00185 void *new_struct_block();  //allocate memory
00186 void old_struct_block(                     //free a structure block
00187                       MEMUNION *deadblock  //block to free
00188                      );
00189 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines