00001 00002 #ifndef POOLS_H 00003 #define POOLS_H 00004 00005 00006 /*===============================<o>===================================== 00007 00008 Copyright 1996, 1997, 2004 Ian Kaplan, Bear Products International, 00009 www.bearcave.com. 00010 00011 All Rights Reserved 00012 00013 You may use this software in software components for which you do 00014 not collect money (e.g., non-commercial software). All commercial 00015 use is reserved. 00016 00017 ===============================<o>=====================================*/ 00018 00019 00020 #include "output.h" 00021 #include "blockpool.h" 00022 00023 typedef enum { min_handle = 0}; 00024 00025 00047 template <class pool_type, int max_handle> 00048 class mem_pool { 00049 00050 private: 00051 pool_type *pools[ max_handle ]; 00052 00053 public: 00054 typedef unsigned int pool_handle; 00055 00056 public: 00057 void pool_init(void) 00058 { 00059 int i; 00060 00061 for (i = min_handle; i < max_handle; i++) { 00062 pools[ i ] = NULL; 00063 } 00064 } // pool_init 00065 00067 pool_handle get_new_pool(void) 00068 { 00069 pool_handle handle; 00070 00071 for (handle = min_handle; handle < max_handle; handle++) { 00072 if (pools[handle] == NULL) { 00073 pools[handle] = new pool_type; 00074 break; 00075 } 00076 } 00077 if (handle > max_handle) { 00078 Fatal("big_pool::get_new_pool: no memory pools available"); 00079 } 00080 00081 return handle; 00082 } // get_new_pool 00083 00084 void release_pool( pool_handle handle ) 00085 { 00086 if (handle < max_handle && pools[ handle ] != NULL) { 00087 delete pools[ handle ]; 00088 pools[ handle ] = NULL; 00089 } 00090 else { 00091 Fatal("big_pool::release_pool: bad pool handle"); 00092 } 00093 } // release_pool 00094 00095 void *GetMem( pool_handle handle, unsigned int num_bytes ) 00096 { 00097 void *mem; 00098 00099 mem = NULL; 00100 if (handle < max_handle && pools[ handle ] != NULL) { 00101 // mem = pools[ handle ]->MemAlloc( num_bytes ); 00102 mem = pools[ handle ]->pool_alloc( num_bytes ); 00103 } 00104 else { 00105 Fatal("big_pool::GetMem: bad pool handle"); 00106 } 00107 return mem; 00108 } // GetMem 00109 00110 void pr( FILE *fp, pool_handle handle ) 00111 { 00112 if (handle < max_handle && pools[ handle ] != NULL) { 00113 pools[ handle ]->print_block_pool_info(); 00114 } 00115 else { 00116 fprintf(fp, "big_pool::pr: bad pool handle"); 00117 } 00118 } // pr 00119 }; // mem_pool 00120 00121 00125 #define MAX_POOLS 128 00126 00127 extern mem_pool<big_block_pool, MAX_POOLS> memory_pools; 00128 00136 class pool { 00137 private: 00138 mem_pool<big_block_pool, MAX_POOLS>::pool_handle pool_handle; 00139 00142 pool( const pool &p) {} 00143 00148 Boolean allocated; 00149 00150 public: 00151 pool(void) 00152 { 00153 new_pool(); 00154 } 00155 ~pool(void) 00156 { 00157 delete_pool(); 00158 } 00159 void new_pool(void) 00160 { 00161 allocated = TRUE; 00162 pool_handle = memory_pools.get_new_pool(); 00163 } 00164 void delete_pool(void) 00165 { 00166 allocated = FALSE; 00167 memory_pools.release_pool( pool_handle ); 00168 } 00169 void *GetMem( unsigned int num_bytes ) 00170 { 00171 assert( allocated == TRUE ); 00172 00173 return memory_pools.GetMem( pool_handle, num_bytes ); 00174 } 00175 void pr( FILE *fp = stdout ) 00176 { 00177 memory_pools.pr(fp, pool_handle ); 00178 } 00179 }; 00180 00181 extern pool global_mem; 00182 00183 #endif