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