SourceForge.net Logo

XPath2MemoryManager.hpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2001-2007
00003  *     DecisionSoft Limited. All rights reserved.
00004  * Copyright (c) 2004-2007
00005  *     Oracle. All rights reserved.
00006  *
00007  * Licensed under the Apache License, Version 2.0 (the "License");
00008  * you may not use this file except in compliance with the License.
00009  * You may obtain a copy of the License at
00010  *
00011  *     http://www.apache.org/licenses/LICENSE-2.0
00012  *
00013  * Unless required by applicable law or agreed to in writing, software
00014  * distributed under the License is distributed on an "AS IS" BASIS,
00015  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016  * See the License for the specific language governing permissions and
00017  * limitations under the License.
00018  *
00019  * $Id: XPath2MemoryManager.hpp,v 1.20 2007/11/28 13:13:22 jpcs Exp $
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   // from MemoryManager
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 };//XPath2MemoryManager
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   // Should never be used - for compiling on AIX only
00098   XQillaAllocator()
00099   {
00100     assert(false);
00101   }
00102 
00103   XQillaAllocator(XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager* memMgr)
00104   {
00105     _memMgr=memMgr;
00106   }
00107 
00108   // define a copy constructor, because we don't want to copy the singleton object
00109   XQillaAllocator(const XQillaAllocator<_Tp>& o)
00110   {
00111     _memMgr=o._memMgr;
00112   }
00113 
00114   pointer allocate(size_t _n, const void* = 0)
00115   {
00116     if(_n==1)
00117       return (pointer)_singleton;
00118     //std::cout << "XQillaAllocator::allocate(" << _n << ")" << std::endl;
00119     if(_memMgr)
00120       return _n != 0 ? static_cast<pointer>(_memMgr->allocate(_n*sizeof(_Tp))) : 0;
00121     else
00122       return _n != 0 ? static_cast<pointer>(malloc(_n*sizeof(_Tp))) : 0;        
00123   }
00124 
00125   void deallocate(void* _p, size_t _n)
00126   {
00127     //std::cout << "XQillaAllocator::deallocate(" << _n << ")" << std::endl;
00128     if(_p) {
00129       if(_p!=_singleton) {
00130         if(_memMgr)
00131           _memMgr->deallocate(_p);
00132         else
00133           free(_p);
00134       }
00135   }
00136   }
00137 
00138   void construct(pointer _p, const_reference _v)
00139   {
00140     new ((void *)_p) _Tp(_v); 
00141   }
00142 
00143   void destroy(pointer _p)
00144   {
00145     _p->~_Tp();
00146   }
00147 
00148   size_type max_size() const
00149   {
00150     return 0xFFFFFFFF;
00151   }
00152     
00153   size_type max_size(size_type) const
00154   {
00155     return 0xFFFFFFFF;
00156   }
00157     
00158 protected:
00159   char _singleton[sizeof(_Tp)];
00160   XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager* _memMgr;
00161 };
00162 
00163 // ---------------------------------------------------------------------------
00164 //
00165 //  Operator new.  Global overloaded version, lets any object be allocated on
00166 //                 the heap owned by a MemoryManager.
00167 //
00168 // ---------------------------------------------------------------------------
00169 inline void * operator new(size_t amt, XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager* memMgr)
00170 {
00171     void *p = memMgr->allocate(amt);
00172     return p;
00173 }
00174 
00175 inline void operator delete(void* ptr, XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager* memMgr)
00176 {
00177   memMgr->deallocate(ptr);
00178 }
00179 
00180 template<class TYPE>
00181 class AutoRelease
00182 {
00183 public:
00184   AutoRelease(TYPE *p)
00185     : p_(p) {}
00186   ~AutoRelease()
00187   {
00188     if(p_ != 0)
00189       p_->release();
00190   }
00191 
00192   TYPE &operator*() const
00193   {
00194     return *p_;
00195   }
00196   TYPE *operator->() const
00197   {
00198     return p_;
00199   }
00200   operator TYPE*() const
00201   {
00202     return p_;
00203   }
00204   TYPE *get() const
00205   {
00206     return p_;
00207   }
00208   TYPE *adopt()
00209   {
00210     TYPE *tmp = p_;
00211     p_ = 0;
00212     return tmp;
00213   }
00214   TYPE *swap(TYPE *p)
00215   {
00216     TYPE *tmp = p_;
00217     p_ = p;
00218     return tmp;
00219   }
00220   void set(TYPE *p)
00221   {
00222     if(p_ != 0)
00223       p_->release();
00224     p_ = p;
00225   }
00226 
00227 private:
00228   AutoRelease(const AutoRelease<TYPE> &);
00229   AutoRelease<TYPE> &operator=(const AutoRelease<TYPE> &);
00230 
00231   TYPE *p_;
00232 };
00233 
00234 template<class TYPE>
00235 class AutoDelete
00236 {
00237 public:
00238   AutoDelete(TYPE *p)
00239     : p_(p) {}
00240   ~AutoDelete()
00241   {
00242     delete p_;
00243   }
00244 
00245   TYPE &operator*() const
00246   {
00247     return *p_;
00248   }
00249   TYPE *operator->() const
00250   {
00251     return p_;
00252   }
00253   operator TYPE*() const
00254   {
00255     return p_;
00256   }
00257   TYPE *get() const
00258   {
00259     return p_;
00260   }
00261   TYPE *adopt()
00262   {
00263     TYPE *tmp = p_;
00264     p_ = 0;
00265     return tmp;
00266   }
00267   TYPE *swap(TYPE *p)
00268   {
00269     TYPE *tmp = p_;
00270     p_ = p;
00271     return tmp;
00272   }
00273   void set(TYPE *p)
00274   {
00275     delete p_;
00276     p_ = p;
00277   }
00278 
00279 private:
00280   AutoDelete(const AutoDelete<TYPE> &);
00281   AutoDelete<TYPE> &operator=(const AutoDelete<TYPE> &);
00282 
00283   TYPE *p_;
00284 };
00285 
00286 template<class TYPE>
00287 class AutoDeleteArray
00288 {
00289 public:
00290   AutoDeleteArray(TYPE *p)
00291     : p_(p) {}
00292   ~AutoDeleteArray()
00293   {
00294     delete [] p_;
00295   }
00296 
00297   TYPE &operator*() const
00298   {
00299     return *p_;
00300   }
00301   TYPE *operator->() const
00302   {
00303     return p_;
00304   }
00305   operator TYPE*() const
00306   {
00307     return p_;
00308   }
00309   TYPE *get() const
00310   {
00311     return p_;
00312   }
00313   TYPE *adopt()
00314   {
00315     TYPE *tmp = p_;
00316     p_ = 0;
00317     return tmp;
00318   }
00319   TYPE *swap(TYPE *p)
00320   {
00321     TYPE *tmp = p_;
00322     p_ = p;
00323     return tmp;
00324   }
00325   void set(TYPE *p)
00326   {
00327     delete [] p_;
00328     p_ = p;
00329   }
00330 
00331 private:
00332   AutoDeleteArray(const AutoDeleteArray<TYPE> &);
00333   AutoDeleteArray<TYPE> &operator=(const AutoDeleteArray<TYPE> &);
00334 
00335   TYPE *p_;
00336 };
00337 
00338 template<class TYPE>
00339 class AutoDeallocate
00340 {
00341 public:
00342   AutoDeallocate(XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager *mmgr, size_t size = sizeof(TYPE))
00343     : p_(0), mmgr_(mmgr) {
00344     p_ = (TYPE*)mmgr_->allocate(size);
00345   }
00346   AutoDeallocate(TYPE *p, XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager *mmgr)
00347     : p_(p), mmgr_(mmgr) {}
00348   ~AutoDeallocate()
00349   {
00350     if(p_ != 0)
00351       mmgr_->deallocate((void*)p_);
00352   }
00353 
00354   TYPE &operator*() const
00355   {
00356     return *p_;
00357   }
00358   TYPE *operator->() const
00359   {
00360     return p_;
00361   }
00362   operator TYPE*() const
00363   {
00364     return p_;
00365   }
00366   TYPE *get() const
00367   {
00368     return p_;
00369   }
00370   TYPE *adopt()
00371   {
00372     TYPE *tmp = p_;
00373     p_ = 0;
00374     return tmp;
00375   }
00376   TYPE *swap(TYPE *p)
00377   {
00378     TYPE *tmp = p_;
00379     p_ = p;
00380     return tmp;
00381   }
00382   void set(TYPE *p)
00383   {
00384     if(p_ != 0)
00385       mmgr_->deallocate((void*)p_);
00386     p_ = p;
00387   }
00388 
00389 private:
00390   AutoDeallocate(const AutoDeallocate<TYPE> &);
00391   AutoDeallocate<TYPE> &operator=(const AutoDeallocate<TYPE> &);
00392 
00393   TYPE *p_;
00394   XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager *mmgr_;
00395 };
00396 
00397 #endif //__XPATH2MEMORYMANAGER_HPP
00398 

Generated on Wed Dec 12 20:16:43 2007 for XQilla Simple API by  doxygen 1.5.1