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