SourceForge.net Logo
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Examples

XPath2MemoryManager.hpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2001-2008
00003  *     DecisionSoft Limited. All rights reserved.
00004  * Copyright (c) 2004-2008
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 688 2008-11-27 02:45:23Z caiying $
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 #if _XERCES_VERSION >= 30000
00061   virtual void* allocate(XMLSize_t numElements) = 0;
00062 #else
00063   virtual void* allocate(size_t numElements) = 0;       
00064 #endif  
00065   virtual void deallocate(void* p) = 0;
00066   
00068   virtual Collation* createCollation(CollationHelper* helper) = 0;
00069 
00071   virtual XQillaNSResolver* createNSResolver(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *resolverNode) = 0;
00072   
00074   virtual VariableTypeStore* createVariableTypeStore() = 0;
00075 
00077   virtual ATDecimalOrDerived* createInteger(int value) = 0;
00078 
00079   virtual void dumpStatistics() const = 0;
00080   virtual size_t getAllocatedObjectCount() const = 0;
00081   virtual size_t getTotalAllocatedMemory() const = 0;
00082   virtual const StringPool *getStringPool() const = 0;
00083 };//XPath2MemoryManager
00084 
00085 template <class _Tp>
00086 class XQillaAllocator
00087 {
00088 public:
00089   typedef size_t size_type;
00090   typedef ptrdiff_t difference_type;
00091   typedef _Tp* pointer;
00092   typedef const _Tp* const_pointer;
00093   typedef _Tp& reference;
00094   typedef const _Tp& const_reference;
00095   typedef _Tp value_type;
00096 
00097   template <class _Tp1> struct rebind {
00098     typedef XQillaAllocator<_Tp1> other;
00099   };
00100 
00101   // Should never be used - for compiling on AIX only
00102   XQillaAllocator()
00103   {
00104     assert(false);
00105   }
00106 
00107   XQillaAllocator(XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager* memMgr)
00108   {
00109     _memMgr=memMgr;
00110   }
00111 
00112   // define a copy constructor, because we don't want to copy the singleton object
00113   XQillaAllocator(const XQillaAllocator<_Tp>& o)
00114   {
00115     _memMgr=o._memMgr;
00116   }
00117 #if _MSC_VER >= 1500
00118   // Needed for Visual Studio 2008      
00119   template<class _Tp1> XQillaAllocator(const XQillaAllocator<_Tp1>& o)
00120   {
00121     _memMgr = o._memMgr;
00122   }
00123 #endif
00124   pointer allocate(size_t _n, const void* = 0)
00125   {
00126     if(_n==1)
00127       return (pointer)_singleton;
00128     //std::cout << "XQillaAllocator::allocate(" << _n << ")" << std::endl;
00129     if(_memMgr)
00130       return _n != 0 ? static_cast<pointer>(_memMgr->allocate(_n*sizeof(_Tp))) : 0;
00131     else
00132       return _n != 0 ? static_cast<pointer>(malloc(_n*sizeof(_Tp))) : 0;        
00133   }
00134 
00135   void deallocate(void* _p, size_t _n)
00136   {
00137     //std::cout << "XQillaAllocator::deallocate(" << _n << ")" << std::endl;
00138     if(_p) {
00139       if(_p!=_singleton) {
00140         if(_memMgr)
00141           _memMgr->deallocate(_p);
00142         else
00143           free(_p);
00144       }
00145   }
00146   }
00147 
00148   void construct(pointer _p, const_reference _v)
00149   {
00150     new ((void *)_p) _Tp(_v); 
00151   }
00152 
00153   void destroy(pointer _p)
00154   {
00155     _p->~_Tp();
00156   }
00157 
00158   size_type max_size() const
00159   {
00160     return 0xFFFFFFFF;
00161   }
00162     
00163   size_type max_size(size_type) const
00164   {
00165     return 0xFFFFFFFF;
00166   }
00167 
00168   char _singleton[sizeof(_Tp)];
00169   XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager* _memMgr;
00170 };
00171 
00172 // ---------------------------------------------------------------------------
00173 //
00174 //  Operator new.  Global overloaded version, lets any object be allocated on
00175 //                 the heap owned by a MemoryManager.
00176 //
00177 // ---------------------------------------------------------------------------
00178 inline void * operator new(size_t amt, XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager* memMgr)
00179 {
00180     void *p = memMgr->allocate(amt);
00181     return p;
00182 }
00183 
00184 inline void operator delete(void* ptr, XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager* memMgr)
00185 {
00186   memMgr->deallocate(ptr);
00187 }
00188 
00189 template<class TYPE>
00190 class AutoRelease
00191 {
00192 public:
00193   AutoRelease(TYPE *p)
00194     : p_(p) {}
00195   ~AutoRelease()
00196   {
00197     if(p_ != 0)
00198       p_->release();
00199   }
00200 
00201   TYPE &operator*() const
00202   {
00203     return *p_;
00204   }
00205   TYPE *operator->() const
00206   {
00207     return p_;
00208   }
00209   operator TYPE*() const
00210   {
00211     return p_;
00212   }
00213   TYPE *get() const
00214   {
00215     return p_;
00216   }
00217   TYPE *adopt()
00218   {
00219     TYPE *tmp = p_;
00220     p_ = 0;
00221     return tmp;
00222   }
00223   TYPE *swap(TYPE *p)
00224   {
00225     TYPE *tmp = p_;
00226     p_ = p;
00227     return tmp;
00228   }
00229   void set(TYPE *p)
00230   {
00231     if(p_ != 0)
00232       p_->release();
00233     p_ = p;
00234   }
00235 
00236 private:
00237   AutoRelease(const AutoRelease<TYPE> &);
00238   AutoRelease<TYPE> &operator=(const AutoRelease<TYPE> &);
00239 
00240   TYPE *p_;
00241 };
00242 
00243 template<class TYPE>
00244 class AutoDelete
00245 {
00246 public:
00247   AutoDelete(TYPE *p)
00248     : p_(p) {}
00249   ~AutoDelete()
00250   {
00251     delete p_;
00252   }
00253 
00254   TYPE &operator*() const
00255   {
00256     return *p_;
00257   }
00258   TYPE *operator->() const
00259   {
00260     return p_;
00261   }
00262   operator TYPE*() const
00263   {
00264     return p_;
00265   }
00266   TYPE *get() const
00267   {
00268     return p_;
00269   }
00270   TYPE *adopt()
00271   {
00272     TYPE *tmp = p_;
00273     p_ = 0;
00274     return tmp;
00275   }
00276   TYPE *swap(TYPE *p)
00277   {
00278     TYPE *tmp = p_;
00279     p_ = p;
00280     return tmp;
00281   }
00282   void set(TYPE *p)
00283   {
00284     delete p_;
00285     p_ = p;
00286   }
00287 
00288 private:
00289   AutoDelete(const AutoDelete<TYPE> &);
00290   AutoDelete<TYPE> &operator=(const AutoDelete<TYPE> &);
00291 
00292   TYPE *p_;
00293 };
00294 
00295 template<class TYPE>
00296 class AutoDeleteArray
00297 {
00298 public:
00299   AutoDeleteArray(TYPE *p)
00300     : p_(p) {}
00301   ~AutoDeleteArray()
00302   {
00303     delete [] p_;
00304   }
00305 
00306   TYPE &operator*() const
00307   {
00308     return *p_;
00309   }
00310   TYPE *operator->() const
00311   {
00312     return p_;
00313   }
00314   operator TYPE*() const
00315   {
00316     return p_;
00317   }
00318   TYPE *get() const
00319   {
00320     return p_;
00321   }
00322   TYPE *adopt()
00323   {
00324     TYPE *tmp = p_;
00325     p_ = 0;
00326     return tmp;
00327   }
00328   TYPE *swap(TYPE *p)
00329   {
00330     TYPE *tmp = p_;
00331     p_ = p;
00332     return tmp;
00333   }
00334   void set(TYPE *p)
00335   {
00336     delete [] p_;
00337     p_ = p;
00338   }
00339 
00340 private:
00341   AutoDeleteArray(const AutoDeleteArray<TYPE> &);
00342   AutoDeleteArray<TYPE> &operator=(const AutoDeleteArray<TYPE> &);
00343 
00344   TYPE *p_;
00345 };
00346 
00347 template<class TYPE>
00348 class AutoDeallocate
00349 {
00350 public:
00351   AutoDeallocate(XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager *mmgr, size_t size = sizeof(TYPE))
00352     : p_(0), mmgr_(mmgr) {
00353     p_ = (TYPE*)mmgr_->allocate(size);
00354   }
00355   AutoDeallocate(TYPE *p, XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager *mmgr)
00356     : p_(p), mmgr_(mmgr) {}
00357   ~AutoDeallocate()
00358   {
00359     if(p_ != 0)
00360       mmgr_->deallocate((void*)p_);
00361   }
00362 
00363   TYPE &operator*() const
00364   {
00365     return *p_;
00366   }
00367   TYPE *operator->() const
00368   {
00369     return p_;
00370   }
00371   operator TYPE*() const
00372   {
00373     return p_;
00374   }
00375   TYPE *get() const
00376   {
00377     return p_;
00378   }
00379   TYPE *adopt()
00380   {
00381     TYPE *tmp = p_;
00382     p_ = 0;
00383     return tmp;
00384   }
00385   TYPE *swap(TYPE *p)
00386   {
00387     TYPE *tmp = p_;
00388     p_ = p;
00389     return tmp;
00390   }
00391   void set(TYPE *p)
00392   {
00393     if(p_ != 0)
00394       mmgr_->deallocate((void*)p_);
00395     p_ = p;
00396   }
00397 
00398 private:
00399   AutoDeallocate(const AutoDeallocate<TYPE> &);
00400   AutoDeallocate<TYPE> &operator=(const AutoDeallocate<TYPE> &);
00401 
00402   TYPE *p_;
00403   XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager *mmgr_;
00404 };
00405 
00406 #endif //__XPATH2MEMORYMANAGER_HPP
00407 

Generated on Fri Sep 25 06:55:26 2009 for XQilla Simple API by  doxygen 1.3.9.1