The world's most popular open source database
00001 /* Copyright (C) 2003 MySQL AB 00002 00003 This program is free software; you can redistribute it and/or modify 00004 it under the terms of the GNU General Public License as published by 00005 the Free Software Foundation; either version 2 of the License, or 00006 (at your option) any later version. 00007 00008 This program is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00011 GNU General Public License for more details. 00012 00013 You should have received a copy of the GNU General Public License 00014 along with this program; if not, write to the Free Software 00015 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 00016 00017 #ifndef NDB_POOL_HPP 00018 #define NDB_POOL_HPP 00019 00020 #include <ndb_global.h> 00021 #include <kernel_types.h> 00022 00033 #define RG_BITS 5 00034 #define RG_MASK ((1 << RG_BITS) - 1) 00035 #define MAKE_TID(TID,RG) ((TID << RG_BITS) | RG) 00036 00040 #define POOL_RECORD_BITS 13 00041 #define POOL_RECORD_MASK ((1 << POOL_RECORD_BITS) - 1) 00042 00047 struct Record_info 00048 { 00049 Uint16 m_size; 00050 Uint16 m_type_id; 00051 Uint16 m_offset_next_pool; 00052 Uint16 m_offset_magic; 00053 }; 00054 00058 struct Resource_limit 00059 { 00060 Uint32 m_min; 00061 Uint32 m_max; 00062 Uint32 m_curr; 00063 Uint32 m_resource_id; 00064 }; 00065 00066 struct Pool_context 00067 { 00068 class SimulatedBlock* m_block; 00069 00073 void* get_memroot(); 00074 00083 void* alloc_page(Uint32 type_id, Uint32 *i); 00084 00091 void release_page(Uint32 type_id, Uint32 i); 00092 00105 void* alloc_pages(Uint32 type_id, Uint32 *i, Uint32 *cnt, Uint32 min =1); 00106 00114 void release_pages(Uint32 type_id, Uint32 i, Uint32 cnt); 00115 00119 void handleAbort(int code, const char* msg); 00120 }; 00121 00122 template <typename T> 00123 struct Ptr 00124 { 00125 T * p; 00126 Uint32 i; 00127 inline bool isNull() const { return i == RNIL; } 00128 inline void setNull() { i = RNIL; } 00129 }; 00130 00131 template <typename T> 00132 struct ConstPtr 00133 { 00134 const T * p; 00135 Uint32 i; 00136 inline bool isNull() const { return i == RNIL; } 00137 inline void setNull() { i = RNIL; } 00138 }; 00139 00140 #ifdef XX_DOCUMENTATION_XX 00141 00144 struct PoolImpl 00145 { 00146 Pool_context m_ctx; 00147 Record_info m_record_info; 00148 00149 void init(const Record_info& ri, const Pool_context& pc); 00150 void init(const Record_info& ri, const Pool_context& pc); 00151 00152 bool seize(Ptr<void>&); 00153 void release(Ptr<void>); 00154 void * getPtr(Uint32 i); 00155 }; 00156 #endif 00157 00158 template <typename T, typename P> 00159 class RecordPool { 00160 public: 00161 RecordPool(); 00162 ~RecordPool(); 00163 00164 void init(Uint32 type_id, const Pool_context& pc); 00165 void wo_pool_init(Uint32 type_id, const Pool_context& pc); 00166 00170 void getPtr(Ptr<T> &); 00171 void getPtr(ConstPtr<T> &) const; 00172 00176 T * getPtr(Uint32 i); 00177 const T * getConstPtr(Uint32 i) const; 00178 00182 void getPtr(Ptr<T> &, Uint32 i); 00183 void getPtr(ConstPtr<T> &, Uint32 i) const; 00184 00190 bool seize(Ptr<T> &); 00191 00195 void release(Uint32 i); 00196 00200 void release(Ptr<T>); 00201 private: 00202 P m_pool; 00203 }; 00204 00205 template <typename T, typename P> 00206 inline 00207 RecordPool<T, P>::RecordPool() 00208 { 00209 } 00210 00211 template <typename T, typename P> 00212 inline 00213 void 00214 RecordPool<T, P>::init(Uint32 type_id, const Pool_context& pc) 00215 { 00216 T tmp; 00217 const char * off_base = (char*)&tmp; 00218 const char * off_next = (char*)&tmp.nextPool; 00219 const char * off_magic = (char*)&tmp.m_magic; 00220 00221 Record_info ri; 00222 ri.m_size = sizeof(T); 00223 ri.m_offset_next_pool = Uint32(off_next - off_base); 00224 ri.m_offset_magic = Uint32(off_magic - off_base); 00225 ri.m_type_id = type_id; 00226 m_pool.init(ri, pc); 00227 } 00228 00229 template <typename T, typename P> 00230 inline 00231 void 00232 RecordPool<T, P>::wo_pool_init(Uint32 type_id, const Pool_context& pc) 00233 { 00234 T tmp; 00235 const char * off_base = (char*)&tmp; 00236 const char * off_magic = (char*)&tmp.m_magic; 00237 00238 Record_info ri; 00239 ri.m_size = sizeof(T); 00240 ri.m_offset_next_pool = 0; 00241 ri.m_offset_magic = Uint32(off_magic - off_base); 00242 ri.m_type_id = type_id; 00243 m_pool.init(ri, pc); 00244 } 00245 00246 template <typename T, typename P> 00247 inline 00248 RecordPool<T, P>::~RecordPool() 00249 { 00250 } 00251 00252 00253 template <typename T, typename P> 00254 inline 00255 void 00256 RecordPool<T, P>::getPtr(Ptr<T> & ptr) 00257 { 00258 ptr.p = static_cast<T*>(m_pool.getPtr(ptr.i)); 00259 } 00260 00261 template <typename T, typename P> 00262 inline 00263 void 00264 RecordPool<T, P>::getPtr(ConstPtr<T> & ptr) const 00265 { 00266 ptr.p = static_cast<const T*>(m_pool.getPtr(ptr.i)); 00267 } 00268 00269 template <typename T, typename P> 00270 inline 00271 void 00272 RecordPool<T, P>::getPtr(Ptr<T> & ptr, Uint32 i) 00273 { 00274 ptr.i = i; 00275 ptr.p = static_cast<T*>(m_pool.getPtr(ptr.i)); 00276 } 00277 00278 template <typename T, typename P> 00279 inline 00280 void 00281 RecordPool<T, P>::getPtr(ConstPtr<T> & ptr, Uint32 i) const 00282 { 00283 ptr.i = i; 00284 ptr.p = static_cast<const T*>(m_pool.getPtr(ptr.i)); 00285 } 00286 00287 template <typename T, typename P> 00288 inline 00289 T * 00290 RecordPool<T, P>::getPtr(Uint32 i) 00291 { 00292 return static_cast<T*>(m_pool.getPtr(i)); 00293 } 00294 00295 template <typename T, typename P> 00296 inline 00297 const T * 00298 RecordPool<T, P>::getConstPtr(Uint32 i) const 00299 { 00300 return static_cast<const T*>(m_pool.getPtr(i)); 00301 } 00302 00303 template <typename T, typename P> 00304 inline 00305 bool 00306 RecordPool<T, P>::seize(Ptr<T> & ptr) 00307 { 00308 return m_pool.seize(*(Ptr<void>*)&ptr); 00309 } 00310 00311 template <typename T, typename P> 00312 inline 00313 void 00314 RecordPool<T, P>::release(Uint32 i) 00315 { 00316 Ptr<void> ptr; 00317 ptr.i = i; 00318 ptr.p = m_pool.getPtr(i); 00319 m_pool.release(ptr); 00320 } 00321 00322 template <typename T, typename P> 00323 inline 00324 void 00325 RecordPool<T, P>::release(Ptr<T> ptr) 00326 { 00327 m_pool.release(*(Ptr<void>*)&ptr); 00328 } 00329 00330 #endif
1.4.7

