00001 /* 00002 * Copyright (c) 2001-2006 00003 * DecisionSoft Limited. All rights reserved. 00004 * Copyright (c) 2004-2006 00005 * Progress Software Corporation. All rights reserved. 00006 * Copyright (c) 2004-2006 00007 * Oracle. All rights reserved. 00008 * 00009 * See the file LICENSE for redistribution information. 00010 * 00011 * $Id: XPath2MemoryManager.hpp,v 1.17 2007/07/10 16:28:58 jpcs Exp $ 00012 */ 00013 00014 #ifndef __XPATH2MEMORYMANAGER_HPP 00015 #define __XPATH2MEMORYMANAGER_HPP 00016 00017 #include <algorithm> 00018 #include <assert.h> 00019 00020 #include <xqilla/framework/XQillaExport.hpp> 00021 00022 #include <xercesc/framework/MemoryManager.hpp> 00023 00024 XERCES_CPP_NAMESPACE_BEGIN 00025 class DOMNode; 00026 class XMLGrammarPool; 00027 XERCES_CPP_NAMESPACE_END 00028 00029 class VariableStore; 00030 class VariableTypeStore; 00031 class DynamicContext; 00032 class Collation; 00033 class CollationHelper; 00034 class XQillaNSResolver; 00035 class ATDecimalOrDerived; 00036 class StringPool; 00037 00038 class XQILLA_API XPath2MemoryManager : public XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager 00039 { 00040 public: 00041 virtual ~XPath2MemoryManager() {} 00042 00044 virtual void reset() = 0; 00045 00047 virtual const XMLCh* getPooledString(const XMLCh *src) = 0; 00048 virtual const XMLCh* getPooledString(const char *src) = 0; 00049 00050 // from MemoryManager 00051 virtual void* allocate(size_t numElements) = 0; 00052 virtual void deallocate(void* p) = 0; 00053 00055 virtual Collation* createCollation(CollationHelper* helper) = 0; 00056 00058 virtual XQillaNSResolver* createNSResolver(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *resolverNode) = 0; 00059 00061 virtual VariableTypeStore* createVariableTypeStore() = 0; 00062 00064 virtual ATDecimalOrDerived* createInteger(int value) = 0; 00065 00066 virtual void dumpStatistics() const = 0; 00067 virtual unsigned long getAllocatedObjectCount() const = 0; 00068 virtual unsigned long getTotalAllocatedMemory() const = 0; 00069 virtual const StringPool *getStringPool() const = 0; 00070 };//XPath2MemoryManager 00071 00072 template <class _Tp> 00073 class XQillaAllocator 00074 { 00075 public: 00076 typedef size_t size_type; 00077 typedef ptrdiff_t difference_type; 00078 typedef _Tp* pointer; 00079 typedef const _Tp* const_pointer; 00080 typedef _Tp& reference; 00081 typedef const _Tp& const_reference; 00082 typedef _Tp value_type; 00083 00084 template <class _Tp1> struct rebind { 00085 typedef XQillaAllocator<_Tp1> other; 00086 }; 00087 00088 // Should never be used - for compiling on AIX only 00089 XQillaAllocator() 00090 { 00091 assert(false); 00092 } 00093 00094 XQillaAllocator(XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager* memMgr) 00095 { 00096 _memMgr=memMgr; 00097 } 00098 00099 // define a copy constructor, because we don't want to copy the singleton object 00100 XQillaAllocator(const XQillaAllocator<_Tp>& o) 00101 { 00102 _memMgr=o._memMgr; 00103 } 00104 00105 pointer allocate(size_t _n, const void* = 0) 00106 { 00107 if(_n==1) 00108 return (pointer)_singleton; 00109 //std::cout << "XQillaAllocator::allocate(" << _n << ")" << std::endl; 00110 if(_memMgr) 00111 return _n != 0 ? static_cast<pointer>(_memMgr->allocate(_n*sizeof(_Tp))) : 0; 00112 else 00113 return _n != 0 ? static_cast<pointer>(malloc(_n*sizeof(_Tp))) : 0; 00114 } 00115 00116 void deallocate(void* _p, size_t _n) 00117 { 00118 //std::cout << "XQillaAllocator::deallocate(" << _n << ")" << std::endl; 00119 if(_p) { 00120 if(_p!=_singleton) { 00121 if(_memMgr) 00122 _memMgr->deallocate(_p); 00123 else 00124 free(_p); 00125 } 00126 } 00127 } 00128 00129 void construct(pointer _p, const_reference _v) 00130 { 00131 new ((void *)_p) _Tp(_v); 00132 } 00133 00134 void destroy(pointer _p) 00135 { 00136 _p->~_Tp(); 00137 } 00138 00139 size_type max_size() const 00140 { 00141 return 0xFFFFFFFF; 00142 } 00143 00144 size_type max_size(size_type) const 00145 { 00146 return 0xFFFFFFFF; 00147 } 00148 00149 protected: 00150 char _singleton[sizeof(_Tp)]; 00151 XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager* _memMgr; 00152 }; 00153 00154 // --------------------------------------------------------------------------- 00155 // 00156 // Operator new. Global overloaded version, lets any object be allocated on 00157 // the heap owned by a MemoryManager. 00158 // 00159 // --------------------------------------------------------------------------- 00160 inline void * operator new(size_t amt, XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager* memMgr) 00161 { 00162 void *p = memMgr->allocate(amt); 00163 return p; 00164 } 00165 00166 inline void operator delete(void* ptr, XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager* memMgr) 00167 { 00168 memMgr->deallocate(ptr); 00169 } 00170 00171 template<class TYPE> 00172 class AutoRelease 00173 { 00174 public: 00175 AutoRelease(TYPE *p) 00176 : p_(p) {} 00177 ~AutoRelease() 00178 { 00179 if(p_ != 0) 00180 p_->release(); 00181 } 00182 00183 TYPE &operator*() const 00184 { 00185 return *p_; 00186 } 00187 TYPE *operator->() const 00188 { 00189 return p_; 00190 } 00191 operator TYPE*() const 00192 { 00193 return p_; 00194 } 00195 TYPE *get() const 00196 { 00197 return p_; 00198 } 00199 TYPE *adopt() 00200 { 00201 TYPE *tmp = p_; 00202 p_ = 0; 00203 return tmp; 00204 } 00205 TYPE *swap(TYPE *p) 00206 { 00207 TYPE *tmp = p_; 00208 p_ = p; 00209 return tmp; 00210 } 00211 void set(TYPE *p) 00212 { 00213 if(p_ != 0) 00214 p_->release(); 00215 p_ = p; 00216 } 00217 00218 private: 00219 AutoRelease(const AutoRelease<TYPE> &); 00220 AutoRelease<TYPE> &operator=(const AutoRelease<TYPE> &); 00221 00222 TYPE *p_; 00223 }; 00224 00225 template<class TYPE> 00226 class AutoDelete 00227 { 00228 public: 00229 AutoDelete(TYPE *p) 00230 : p_(p) {} 00231 ~AutoDelete() 00232 { 00233 delete p_; 00234 } 00235 00236 TYPE &operator*() const 00237 { 00238 return *p_; 00239 } 00240 TYPE *operator->() const 00241 { 00242 return p_; 00243 } 00244 operator TYPE*() const 00245 { 00246 return p_; 00247 } 00248 TYPE *get() const 00249 { 00250 return p_; 00251 } 00252 TYPE *adopt() 00253 { 00254 TYPE *tmp = p_; 00255 p_ = 0; 00256 return tmp; 00257 } 00258 TYPE *swap(TYPE *p) 00259 { 00260 TYPE *tmp = p_; 00261 p_ = p; 00262 return tmp; 00263 } 00264 void set(TYPE *p) 00265 { 00266 delete p_; 00267 p_ = p; 00268 } 00269 00270 private: 00271 AutoDelete(const AutoDelete<TYPE> &); 00272 AutoDelete<TYPE> &operator=(const AutoDelete<TYPE> &); 00273 00274 TYPE *p_; 00275 }; 00276 00277 template<class TYPE> 00278 class AutoDeleteArray 00279 { 00280 public: 00281 AutoDeleteArray(TYPE *p) 00282 : p_(p) {} 00283 ~AutoDeleteArray() 00284 { 00285 delete [] p_; 00286 } 00287 00288 TYPE &operator*() const 00289 { 00290 return *p_; 00291 } 00292 TYPE *operator->() const 00293 { 00294 return p_; 00295 } 00296 operator TYPE*() const 00297 { 00298 return p_; 00299 } 00300 TYPE *get() const 00301 { 00302 return p_; 00303 } 00304 TYPE *adopt() 00305 { 00306 TYPE *tmp = p_; 00307 p_ = 0; 00308 return tmp; 00309 } 00310 TYPE *swap(TYPE *p) 00311 { 00312 TYPE *tmp = p_; 00313 p_ = p; 00314 return tmp; 00315 } 00316 void set(TYPE *p) 00317 { 00318 delete [] p_; 00319 p_ = p; 00320 } 00321 00322 private: 00323 AutoDeleteArray(const AutoDeleteArray<TYPE> &); 00324 AutoDeleteArray<TYPE> &operator=(const AutoDeleteArray<TYPE> &); 00325 00326 TYPE *p_; 00327 }; 00328 00329 template<class TYPE> 00330 class AutoDeallocate 00331 { 00332 public: 00333 AutoDeallocate(XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager *mmgr, size_t size = sizeof(TYPE)) 00334 : p_(0), mmgr_(mmgr) { 00335 p_ = (TYPE*)mmgr_->allocate(size); 00336 } 00337 AutoDeallocate(TYPE *p, XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager *mmgr) 00338 : p_(p), mmgr_(mmgr) {} 00339 ~AutoDeallocate() 00340 { 00341 if(p_ != 0) 00342 mmgr_->deallocate((void*)p_); 00343 } 00344 00345 TYPE &operator*() const 00346 { 00347 return *p_; 00348 } 00349 TYPE *operator->() const 00350 { 00351 return p_; 00352 } 00353 operator TYPE*() const 00354 { 00355 return p_; 00356 } 00357 TYPE *get() const 00358 { 00359 return p_; 00360 } 00361 TYPE *adopt() 00362 { 00363 TYPE *tmp = p_; 00364 p_ = 0; 00365 return tmp; 00366 } 00367 TYPE *swap(TYPE *p) 00368 { 00369 TYPE *tmp = p_; 00370 p_ = p; 00371 return tmp; 00372 } 00373 void set(TYPE *p) 00374 { 00375 if(p_ != 0) 00376 mmgr_->deallocate((void*)p_); 00377 p_ = p; 00378 } 00379 00380 private: 00381 AutoDeallocate(const AutoDeallocate<TYPE> &); 00382 AutoDeallocate<TYPE> &operator=(const AutoDeallocate<TYPE> &); 00383 00384 TYPE *p_; 00385 XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager *mmgr_; 00386 }; 00387 00388 #endif //__XPATH2MEMORYMANAGER_HPP 00389