00001 00002 #ifndef BLOCKPOOL_H 00003 #define BLOCKPOOL_H 00004 00005 #include <assert.h> 00006 00049 /*===============================<o>===================================== 00050 00051 Copyright 1996, 1997, 2004 Ian Kaplan, Bear Products International, 00052 www.bearcave.com. 00053 00054 All Rights Reserved 00055 00056 You may use this software in software components for which you do 00057 not collect money (e.g., non-commercial software). All commercial 00058 use is reserved. 00059 00060 ===============================<o>=====================================*/ 00061 00062 00068 class big_block_alloc { 00069 private: 00070 unsigned int page_size; 00071 unsigned int alloc_gran; 00072 00073 private: 00074 void GetSysInfo(void); 00075 void alloc_error(void); 00076 00077 public: 00078 void get_info(unsigned int &pg_size, unsigned int &gran ) 00079 { 00080 assert((page_size != 0 && alloc_gran != 0)); 00081 00082 pg_size = page_size; 00083 gran = alloc_gran; 00084 } 00085 void *MemoryAlloc( unsigned int num_bytes ); 00086 void MemoryFree( void *address ); 00087 big_block_alloc(void) { GetSysInfo(); } 00088 }; // big_block_alloc 00089 00090 00091 00095 class block_pool { 00096 private: // typedefs and variables 00099 typedef enum { max_block_multiple = 256 } bogus; 00100 00101 typedef struct block_chain_struct { 00103 void *block; 00105 unsigned int bytes_used; 00107 unsigned int block_size; 00109 block_chain_struct *next_block; 00110 } block_chain; 00111 00112 unsigned int page_size; 00113 unsigned int alloc_gran; 00114 00116 block_chain *block_list_start; 00118 block_chain *current_block; 00119 00120 public: 00126 typedef block_chain blk_chain; 00127 00128 private: // class functions 00129 blk_chain *new_block( unsigned int block_size ); 00130 void *add_block( unsigned int block_size ); 00131 void init_pool(void); 00132 00133 public: // class functions 00134 block_pool(void); 00135 void free_pool(void); 00136 void *pool_alloc( unsigned int block_size ); 00137 void print_block_pool_info( FILE *fp = stdout ); 00138 00139 virtual void getinfo(unsigned int &p_size, unsigned int &a_gran ) = 0; 00140 virtual void *MemAlloc( unsigned int n_bytes ) = 0; 00141 virtual void MemFree( void *addr ) = 0; 00142 }; // class block_pool 00143 00144 00145 /* macros for block_chain pointers */ 00146 #define Chain_block(p) ((p)->block) 00147 #define Chain_bytes_used(p) ((p)->bytes_used) 00148 #define Chain_block_size(p) ((p)->block_size) 00149 #define Chain_next(p) ((p)->next_block) 00150 00151 00156 class big_block_pool : public block_pool { 00157 private: 00158 big_block_alloc block_alloc; 00159 00160 protected: 00161 void getinfo(unsigned int &p_size, unsigned int &a_gran ) 00162 { 00163 block_alloc.get_info( p_size, a_gran ); 00164 } 00165 00166 public: 00167 big_block_pool(void) : block_pool() {} 00168 ~big_block_pool(void) 00169 { 00170 free_pool(); // inherited from block_pool 00171 } 00172 00177 void *MemAlloc( unsigned int num_bytes ) 00178 { 00179 return block_alloc.MemoryAlloc( num_bytes ); 00180 } 00181 00182 00186 void MemFree( void *address ) 00187 { 00188 block_alloc.MemoryFree( address ); 00189 } 00190 00191 }; // big_block_pool 00192 00193 00194 #endif