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