SourceForge.net Logo
XPath2MemoryManager.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2008
3  * DecisionSoft Limited. All rights reserved.
4  * Copyright (c) 2004-2008
5  * Oracle. All rights reserved.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * $Id$
20  */
21 
22 #ifndef __XPATH2MEMORYMANAGER_HPP
23 #define __XPATH2MEMORYMANAGER_HPP
24 
25 #include <algorithm>
26 #include <assert.h>
27 #include <cstddef>
28 
29 #include <xqilla/framework/XQillaExport.hpp>
30 
32 
33 XERCES_CPP_NAMESPACE_BEGIN
34 class DOMNode;
35 class XMLGrammarPool;
36 XERCES_CPP_NAMESPACE_END
37 
38 class VariableStore;
39 class VariableTypeStore;
40 class DynamicContext;
41 class Collation;
42 class CollationHelper;
43 class XQillaNSResolver;
44 class ATDecimalOrDerived;
45 class StringPool;
46 
47 class XQILLA_API XPath2MemoryManager : public XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager
48 {
49 public:
50  virtual ~XPath2MemoryManager() {}
51 
53  virtual void reset() = 0;
54 
56  virtual const XMLCh* getPooledString(const XMLCh *src) = 0;
57  virtual const XMLCh* getPooledString(const XMLCh *src, unsigned int length) = 0;
58  virtual const XMLCh* getPooledString(const char *src) = 0;
59 
60  // from MemoryManager
61 #if _XERCES_VERSION >= 30000
62  virtual void* allocate(XMLSize_t numElements) = 0;
63 #else
64  virtual void* allocate(size_t numElements) = 0;
65 #endif
66  virtual void deallocate(void* p) = 0;
67 
69  virtual Collation* createCollation(CollationHelper* helper) = 0;
70 
72  virtual XQillaNSResolver* createNSResolver(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *resolverNode) = 0;
73 
75  virtual VariableTypeStore* createVariableTypeStore() = 0;
76 
78  virtual ATDecimalOrDerived* createInteger(int value) = 0;
79 
80  virtual void dumpStatistics() const = 0;
81  virtual size_t getAllocatedObjectCount() const = 0;
82  virtual size_t getTotalAllocatedMemory() const = 0;
83  virtual const StringPool *getStringPool() const = 0;
84 };//XPath2MemoryManager
85 
86 template <class _Tp>
88 {
89 public:
90  typedef size_t size_type;
91  typedef ptrdiff_t difference_type;
92  typedef _Tp* pointer;
93  typedef const _Tp* const_pointer;
94  typedef _Tp& reference;
95  typedef const _Tp& const_reference;
96  typedef _Tp value_type;
97 
98  template <class _Tp1> struct rebind {
100  };
101 
102  // Should never be used - for compiling on AIX only
104  {
105  assert(false);
106  }
107 
108  XQillaAllocator(XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager* memMgr)
109  {
110  _memMgr=memMgr;
111  }
112 
113  // define a copy constructor, because we don't want to copy the singleton object
115  {
116  _memMgr=o._memMgr;
117  }
118 #if _MSC_VER >= 1500
119  // Needed for Visual Studio 2008
120  template<class _Tp1> XQillaAllocator(const XQillaAllocator<_Tp1>& o)
121  {
122  _memMgr = o._memMgr;
123  }
124 #endif
125  pointer allocate(size_t _n, const void* = 0)
126  {
127  if(_n==1)
128  return (pointer)_singleton;
129  //std::cout << "XQillaAllocator::allocate(" << _n << ")" << std::endl;
130  if(_memMgr)
131  return _n != 0 ? static_cast<pointer>(_memMgr->allocate(_n*sizeof(_Tp))) : 0;
132  else
133  return _n != 0 ? static_cast<pointer>(malloc(_n*sizeof(_Tp))) : 0;
134  }
135 
136  void deallocate(void* _p, size_t _n)
137  {
138  //std::cout << "XQillaAllocator::deallocate(" << _n << ")" << std::endl;
139  if(_p) {
140  if(_p!=_singleton) {
141  if(_memMgr)
142  _memMgr->deallocate(_p);
143  else
144  free(_p);
145  }
146  }
147  }
148 
150  {
151  new ((void *)_p) _Tp(_v);
152  }
153 
154  void destroy(pointer _p)
155  {
156  _p->~_Tp();
157  }
158 
160  {
161  return 0xFFFFFFFF;
162  }
163 
165  {
166  return 0xFFFFFFFF;
167  }
168 
169  char _singleton[sizeof(_Tp)];
170  XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager* _memMgr;
171 };
172 
173 // ---------------------------------------------------------------------------
174 //
175 // Operator new. Global overloaded version, lets any object be allocated on
176 // the heap owned by a MemoryManager.
177 //
178 // ---------------------------------------------------------------------------
179 inline void * operator new(size_t amt, XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager* memMgr)
180 {
181  void *p = memMgr->allocate(amt);
182  return p;
183 }
184 
185 inline void operator delete(void* ptr, XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager* memMgr)
186 {
187  memMgr->deallocate(ptr);
188 }
189 
190 template<class TYPE>
192 {
193 public:
194  AutoRelease(TYPE *p)
195  : p_(p) {}
197  {
198  if(p_ != 0)
199  p_->release();
200  }
201 
202  TYPE &operator*() const
203  {
204  return *p_;
205  }
206  TYPE *operator->() const
207  {
208  return p_;
209  }
210  operator TYPE*() const
211  {
212  return p_;
213  }
214  TYPE *get() const
215  {
216  return p_;
217  }
218  TYPE *adopt()
219  {
220  TYPE *tmp = p_;
221  p_ = 0;
222  return tmp;
223  }
224  TYPE *swap(TYPE *p)
225  {
226  TYPE *tmp = p_;
227  p_ = p;
228  return tmp;
229  }
230  void set(TYPE *p)
231  {
232  if(p_ != 0)
233  p_->release();
234  p_ = p;
235  }
236 
237 private:
239  AutoRelease<TYPE> &operator=(const AutoRelease<TYPE> &);
240 
241  TYPE *p_;
242 };
243 
244 template<class TYPE>
246 {
247 public:
248  AutoDelete(TYPE *p)
249  : p_(p) {}
251  {
252  delete p_;
253  }
254 
255  TYPE &operator*() const
256  {
257  return *p_;
258  }
259  TYPE *operator->() const
260  {
261  return p_;
262  }
263  operator TYPE*() const
264  {
265  return p_;
266  }
267  TYPE *get() const
268  {
269  return p_;
270  }
271  TYPE *adopt()
272  {
273  TYPE *tmp = p_;
274  p_ = 0;
275  return tmp;
276  }
277  TYPE *swap(TYPE *p)
278  {
279  TYPE *tmp = p_;
280  p_ = p;
281  return tmp;
282  }
283  void set(TYPE *p)
284  {
285  delete p_;
286  p_ = p;
287  }
288 
289 private:
290  AutoDelete(const AutoDelete<TYPE> &);
291  AutoDelete<TYPE> &operator=(const AutoDelete<TYPE> &);
292 
293  TYPE *p_;
294 };
295 
296 template<class TYPE>
298 {
299 public:
301  : p_(p) {}
303  {
304  delete [] p_;
305  }
306 
307  TYPE &operator*() const
308  {
309  return *p_;
310  }
311  TYPE *operator->() const
312  {
313  return p_;
314  }
315  operator TYPE*() const
316  {
317  return p_;
318  }
319  TYPE *get() const
320  {
321  return p_;
322  }
323  TYPE *adopt()
324  {
325  TYPE *tmp = p_;
326  p_ = 0;
327  return tmp;
328  }
329  TYPE *swap(TYPE *p)
330  {
331  TYPE *tmp = p_;
332  p_ = p;
333  return tmp;
334  }
335  void set(TYPE *p)
336  {
337  delete [] p_;
338  p_ = p;
339  }
340 
341 private:
343  AutoDeleteArray<TYPE> &operator=(const AutoDeleteArray<TYPE> &);
344 
345  TYPE *p_;
346 };
347 
348 template<class TYPE>
350 {
351 public:
352  AutoDeallocate(XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager *mmgr, size_t size = sizeof(TYPE))
353  : p_(0), mmgr_(mmgr) {
354  p_ = (TYPE*)mmgr_->allocate(size);
355  }
356  AutoDeallocate(TYPE *p, XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager *mmgr)
357  : p_(p), mmgr_(mmgr) {}
359  {
360  if(p_ != 0)
361  mmgr_->deallocate((void*)p_);
362  }
363 
364  TYPE &operator*() const
365  {
366  return *p_;
367  }
368  TYPE *operator->() const
369  {
370  return p_;
371  }
372  operator TYPE*() const
373  {
374  return p_;
375  }
376  TYPE *get() const
377  {
378  return p_;
379  }
380  TYPE *adopt()
381  {
382  TYPE *tmp = p_;
383  p_ = 0;
384  return tmp;
385  }
386  TYPE *swap(TYPE *p)
387  {
388  TYPE *tmp = p_;
389  p_ = p;
390  return tmp;
391  }
392  void set(TYPE *p)
393  {
394  if(p_ != 0)
395  mmgr_->deallocate((void*)p_);
396  p_ = p;
397  }
398 
399 private:
401  AutoDeallocate<TYPE> &operator=(const AutoDeallocate<TYPE> &);
402 
403  TYPE *p_;
404  XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager *mmgr_;
405 };
406 
407 #endif //__XPATH2MEMORYMANAGER_HPP
408 
TYPE & operator*() const
Definition: XPath2MemoryManager.hpp:255
XQillaAllocator(xercesc::MemoryManager *memMgr)
Definition: XPath2MemoryManager.hpp:108
Definition: XPath2MemoryManager.hpp:349
TYPE * swap(TYPE *p)
Definition: XPath2MemoryManager.hpp:386
TYPE * adopt()
Definition: XPath2MemoryManager.hpp:323
_Tp value_type
Definition: XPath2MemoryManager.hpp:96
char _singleton[sizeof(_Tp)]
Definition: XPath2MemoryManager.hpp:169
Definition: XPath2MemoryManager.hpp:47
~AutoDeleteArray()
Definition: XPath2MemoryManager.hpp:302
TYPE * operator->() const
Definition: XPath2MemoryManager.hpp:259
This is the wrapper class for the variable store, which implements the lookup and scoping of simple v...
Definition: VariableTypeStore.hpp:35
virtual void deallocate(void *p)=0
This method deallocates memory.
TYPE * adopt()
Definition: XPath2MemoryManager.hpp:271
void deallocate(void *_p, size_t _n)
Definition: XPath2MemoryManager.hpp:136
Definition: XPath2MemoryManager.hpp:87
size_type max_size(size_type) const
Definition: XPath2MemoryManager.hpp:164
Definition: XPath2MemoryManager.hpp:297
~AutoDelete()
Definition: XPath2MemoryManager.hpp:250
void construct(pointer _p, const_reference _v)
Definition: XPath2MemoryManager.hpp:149
xercesc::MemoryManager * _memMgr
Definition: XPath2MemoryManager.hpp:170
TYPE * swap(TYPE *p)
Definition: XPath2MemoryManager.hpp:224
TYPE * swap(TYPE *p)
Definition: XPath2MemoryManager.hpp:329
_Tp & reference
Definition: XPath2MemoryManager.hpp:94
AutoRelease(TYPE *p)
Definition: XPath2MemoryManager.hpp:194
virtual ~XPath2MemoryManager()
Definition: XPath2MemoryManager.hpp:50
virtual void * allocate(XMLSize_t size)=0
This method allocates requested memory.
AutoDeleteArray(TYPE *p)
Definition: XPath2MemoryManager.hpp:300
TYPE & operator*() const
Definition: XPath2MemoryManager.hpp:307
AutoDeallocate(TYPE *p, xercesc::MemoryManager *mmgr)
Definition: XPath2MemoryManager.hpp:356
TYPE * operator->() const
Definition: XPath2MemoryManager.hpp:311
void destroy(pointer _p)
Definition: XPath2MemoryManager.hpp:154
Definition: StringPool.hpp:34
Definition: Collation.hpp:31
size_t size_type
Definition: XPath2MemoryManager.hpp:90
const _Tp & const_reference
Definition: XPath2MemoryManager.hpp:95
_Tp * pointer
Definition: XPath2MemoryManager.hpp:92
void set(TYPE *p)
Definition: XPath2MemoryManager.hpp:230
ptrdiff_t difference_type
Definition: XPath2MemoryManager.hpp:91
Definition: XPath2MemoryManager.hpp:191
void set(TYPE *p)
Definition: XPath2MemoryManager.hpp:392
TYPE * adopt()
Definition: XPath2MemoryManager.hpp:218
The execution time dynamic context interface.
Definition: DynamicContext.hpp:39
The pure virtual base class for accessing variables at runtime.
Definition: VariableStore.hpp:33
TYPE * swap(TYPE *p)
Definition: XPath2MemoryManager.hpp:277
TYPE * adopt()
Definition: XPath2MemoryManager.hpp:380
TYPE & operator*() const
Definition: XPath2MemoryManager.hpp:202
size_type max_size() const
Definition: XPath2MemoryManager.hpp:159
void set(TYPE *p)
Definition: XPath2MemoryManager.hpp:283
void set(TYPE *p)
Definition: XPath2MemoryManager.hpp:335
pointer allocate(size_t _n, const void *=0)
Definition: XPath2MemoryManager.hpp:125
TYPE * operator->() const
Definition: XPath2MemoryManager.hpp:368
Definition: XPath2MemoryManager.hpp:98
TYPE & operator*() const
Definition: XPath2MemoryManager.hpp:364
~AutoDeallocate()
Definition: XPath2MemoryManager.hpp:358
~AutoRelease()
Definition: XPath2MemoryManager.hpp:196
XQillaAllocator(const XQillaAllocator< _Tp > &o)
Definition: XPath2MemoryManager.hpp:114
XQillaAllocator()
Definition: XPath2MemoryManager.hpp:103
const _Tp * const_pointer
Definition: XPath2MemoryManager.hpp:93
Definition: ATDecimalOrDerived.hpp:31
AutoDelete(TYPE *p)
Definition: XPath2MemoryManager.hpp:248
XQillaAllocator< _Tp1 > other
Definition: XPath2MemoryManager.hpp:99
Definition: XPath2MemoryManager.hpp:245
TYPE * operator->() const
Definition: XPath2MemoryManager.hpp:206