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, 2018 Oracle and/or its affiliates. All rights reserved.
5  *
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 
20 #ifndef __XPATH2MEMORYMANAGER_HPP
21 #define __XPATH2MEMORYMANAGER_HPP
22 
23 #include <algorithm>
24 #include <assert.h>
25 #include <cstddef>
26 
27 #include <xqilla/framework/XQillaExport.hpp>
28 
30 
31 XERCES_CPP_NAMESPACE_BEGIN
32 class DOMNode;
33 class XMLGrammarPool;
34 XERCES_CPP_NAMESPACE_END
35 
36 class VariableStore;
37 class VariableTypeStore;
38 class DynamicContext;
39 class Collation;
40 class CollationHelper;
41 class XQillaNSResolver;
42 class ATDecimalOrDerived;
43 class StringPool;
44 
45 class XQILLA_API XPath2MemoryManager : public XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager
46 {
47 public:
48  virtual ~XPath2MemoryManager() {}
49 
51  virtual void reset() = 0;
52 
54  virtual const XMLCh* getPooledString(const XMLCh *src) = 0;
55  virtual const XMLCh* getPooledString(const XMLCh *src, unsigned int length) = 0;
56  virtual const XMLCh* getPooledString(const char *src) = 0;
57 
58  // from MemoryManager
59 #if _XERCES_VERSION >= 30000
60  virtual void* allocate(XMLSize_t numElements) = 0;
61 #else
62  virtual void* allocate(size_t numElements) = 0;
63 #endif
64  virtual void deallocate(void* p) = 0;
65 
67  virtual Collation* createCollation(CollationHelper* helper) = 0;
68 
70  virtual XQillaNSResolver* createNSResolver(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *resolverNode) = 0;
71 
73  virtual VariableTypeStore* createVariableTypeStore() = 0;
74 
76  virtual ATDecimalOrDerived* createInteger(int value) = 0;
77 
78  virtual void dumpStatistics() const = 0;
79  virtual size_t getAllocatedObjectCount() const = 0;
80  virtual size_t getTotalAllocatedMemory() const = 0;
81  virtual const StringPool *getStringPool() const = 0;
82 };//XPath2MemoryManager
83 
84 template <class _Tp>
86 {
87 public:
88  typedef size_t size_type;
89  typedef ptrdiff_t difference_type;
90  typedef _Tp* pointer;
91  typedef const _Tp* const_pointer;
92  typedef _Tp& reference;
93  typedef const _Tp& const_reference;
94  typedef _Tp value_type;
95 
96  template <class _Tp1> struct rebind {
98  };
99 
100  // Should never be used - for compiling on AIX only
102  {
103  assert(false);
104  }
105 
106  XQillaAllocator(XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager* memMgr)
107  {
108  _memMgr=memMgr;
109  }
110 
111  // define a copy constructor, because we don't want to copy the singleton object
113  {
114  _memMgr=o._memMgr;
115  }
116 #if _MSC_VER >= 1500
117  // Needed for Visual Studio 2008
118  template<class _Tp1> XQillaAllocator(const XQillaAllocator<_Tp1>& o)
119  {
120  _memMgr = o._memMgr;
121  }
122 #endif
123  pointer allocate(size_t _n, const void* = 0)
124  {
125 #ifndef _MSC_VER
126  if(_n==1)
127  return (pointer)_singleton;
128 #endif
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  bool operator==(const XQillaAllocator<_Tp>& o) const
170  {
171  return &o == this;
172  }
173 
174  bool operator!=(const XQillaAllocator<_Tp>& o) const
175  {
176  return &o != this;
177  }
178 
180  {
181  return _memMgr != o._memMgr;
182  }
183 
184  char _singleton[sizeof(_Tp)];
185  XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager* _memMgr;
186 };
187 
188 // ---------------------------------------------------------------------------
189 //
190 // Operator new. Global overloaded version, lets any object be allocated on
191 // the heap owned by a MemoryManager.
192 //
193 // ---------------------------------------------------------------------------
194 inline void * operator new(size_t amt, XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager* memMgr)
195 {
196  void *p = memMgr->allocate(amt);
197  return p;
198 }
199 
200 inline void operator delete(void* ptr, XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager* memMgr)
201 {
202  memMgr->deallocate(ptr);
203 }
204 
205 template<class TYPE>
207 {
208 public:
209  AutoRelease(TYPE *p)
210  : p_(p) {}
212  {
213  if(p_ != 0)
214  p_->release();
215  }
216 
217  TYPE &operator*() const
218  {
219  return *p_;
220  }
221  TYPE *operator->() const
222  {
223  return p_;
224  }
225  operator TYPE*() const
226  {
227  return p_;
228  }
229  TYPE *get() const
230  {
231  return p_;
232  }
233  TYPE *adopt()
234  {
235  TYPE *tmp = p_;
236  p_ = 0;
237  return tmp;
238  }
239  TYPE *swap(TYPE *p)
240  {
241  TYPE *tmp = p_;
242  p_ = p;
243  return tmp;
244  }
245  void set(TYPE *p)
246  {
247  if(p_ != 0)
248  p_->release();
249  p_ = p;
250  }
251 
252 private:
254  AutoRelease<TYPE> &operator=(const AutoRelease<TYPE> &);
255 
256  TYPE *p_;
257 };
258 
259 template<class TYPE>
261 {
262 public:
263  AutoDelete(TYPE *p)
264  : p_(p) {}
266  {
267  delete p_;
268  }
269 
270  TYPE &operator*() const
271  {
272  return *p_;
273  }
274  TYPE *operator->() const
275  {
276  return p_;
277  }
278  operator TYPE*() const
279  {
280  return p_;
281  }
282  TYPE *get() const
283  {
284  return p_;
285  }
286  TYPE *adopt()
287  {
288  TYPE *tmp = p_;
289  p_ = 0;
290  return tmp;
291  }
292  TYPE *swap(TYPE *p)
293  {
294  TYPE *tmp = p_;
295  p_ = p;
296  return tmp;
297  }
298  void set(TYPE *p)
299  {
300  delete p_;
301  p_ = p;
302  }
303 
304 private:
305  AutoDelete(const AutoDelete<TYPE> &);
306  AutoDelete<TYPE> &operator=(const AutoDelete<TYPE> &);
307 
308  TYPE *p_;
309 };
310 
311 template<class TYPE>
313 {
314 public:
316  : p_(p) {}
318  {
319  delete [] p_;
320  }
321 
322  TYPE &operator*() const
323  {
324  return *p_;
325  }
326  TYPE *operator->() const
327  {
328  return p_;
329  }
330  operator TYPE*() const
331  {
332  return p_;
333  }
334  TYPE *get() const
335  {
336  return p_;
337  }
338  TYPE *adopt()
339  {
340  TYPE *tmp = p_;
341  p_ = 0;
342  return tmp;
343  }
344  TYPE *swap(TYPE *p)
345  {
346  TYPE *tmp = p_;
347  p_ = p;
348  return tmp;
349  }
350  void set(TYPE *p)
351  {
352  delete [] p_;
353  p_ = p;
354  }
355 
356 private:
358  AutoDeleteArray<TYPE> &operator=(const AutoDeleteArray<TYPE> &);
359 
360  TYPE *p_;
361 };
362 
363 template<class TYPE>
365 {
366 public:
367  AutoDeallocate(XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager *mmgr, size_t size = sizeof(TYPE))
368  : p_(0), mmgr_(mmgr) {
369  p_ = (TYPE*)mmgr_->allocate(size);
370  }
371  AutoDeallocate(TYPE *p, XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager *mmgr)
372  : p_(p), mmgr_(mmgr) {}
374  {
375  if(p_ != 0)
376  mmgr_->deallocate((void*)p_);
377  }
378 
379  TYPE &operator*() const
380  {
381  return *p_;
382  }
383  TYPE *operator->() const
384  {
385  return p_;
386  }
387  operator TYPE*() const
388  {
389  return p_;
390  }
391  TYPE *get() const
392  {
393  return p_;
394  }
395  TYPE *adopt()
396  {
397  TYPE *tmp = p_;
398  p_ = 0;
399  return tmp;
400  }
401  TYPE *swap(TYPE *p)
402  {
403  TYPE *tmp = p_;
404  p_ = p;
405  return tmp;
406  }
407  void set(TYPE *p)
408  {
409  if(p_ != 0)
410  mmgr_->deallocate((void*)p_);
411  p_ = p;
412  }
413 
414 private:
416  AutoDeallocate<TYPE> &operator=(const AutoDeallocate<TYPE> &);
417 
418  TYPE *p_;
419  XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager *mmgr_;
420 };
421 
422 #endif //__XPATH2MEMORYMANAGER_HPP
423 
TYPE & operator*() const
Definition: XPath2MemoryManager.hpp:270
XQillaAllocator(xercesc::MemoryManager *memMgr)
Definition: XPath2MemoryManager.hpp:106
bool operator==(const XQillaAllocator< _Tp > &o) const
Definition: XPath2MemoryManager.hpp:169
Definition: XPath2MemoryManager.hpp:364
TYPE * swap(TYPE *p)
Definition: XPath2MemoryManager.hpp:401
TYPE * adopt()
Definition: XPath2MemoryManager.hpp:338
_Tp value_type
Definition: XPath2MemoryManager.hpp:94
char _singleton[sizeof(_Tp)]
Definition: XPath2MemoryManager.hpp:184
Definition: XPath2MemoryManager.hpp:45
~AutoDeleteArray()
Definition: XPath2MemoryManager.hpp:317
TYPE * operator->() const
Definition: XPath2MemoryManager.hpp:274
virtual void deallocate(void *p)=0
This method deallocates memory.
TYPE * adopt()
Definition: XPath2MemoryManager.hpp:286
void deallocate(void *_p, size_t _n)
Definition: XPath2MemoryManager.hpp:136
Definition: XPath2MemoryManager.hpp:85
size_type max_size(size_type) const
Definition: XPath2MemoryManager.hpp:164
Definition: XPath2MemoryManager.hpp:312
~AutoDelete()
Definition: XPath2MemoryManager.hpp:265
void construct(pointer _p, const_reference _v)
Definition: XPath2MemoryManager.hpp:149
xercesc::MemoryManager * _memMgr
Definition: XPath2MemoryManager.hpp:185
TYPE * swap(TYPE *p)
Definition: XPath2MemoryManager.hpp:239
TYPE * swap(TYPE *p)
Definition: XPath2MemoryManager.hpp:344
_Tp & reference
Definition: XPath2MemoryManager.hpp:92
AutoRelease(TYPE *p)
Definition: XPath2MemoryManager.hpp:209
virtual ~XPath2MemoryManager()
Definition: XPath2MemoryManager.hpp:48
virtual void * allocate(XMLSize_t size)=0
This method allocates requested memory.
AutoDeleteArray(TYPE *p)
Definition: XPath2MemoryManager.hpp:315
TYPE & operator*() const
Definition: XPath2MemoryManager.hpp:322
The XQillaNSResolver interface extends the DOMXPathNSResolver providing the ability to add additional...
Definition: XQillaNSResolver.hpp:38
AutoDeallocate(TYPE *p, xercesc::MemoryManager *mmgr)
Definition: XPath2MemoryManager.hpp:371
TYPE * operator->() const
Definition: XPath2MemoryManager.hpp:326
void destroy(pointer _p)
Definition: XPath2MemoryManager.hpp:154
size_t size_type
Definition: XPath2MemoryManager.hpp:88
const _Tp & const_reference
Definition: XPath2MemoryManager.hpp:93
_Tp * pointer
Definition: XPath2MemoryManager.hpp:90
void set(TYPE *p)
Definition: XPath2MemoryManager.hpp:245
ptrdiff_t difference_type
Definition: XPath2MemoryManager.hpp:89
Definition: XPath2MemoryManager.hpp:206
void set(TYPE *p)
Definition: XPath2MemoryManager.hpp:407
TYPE * adopt()
Definition: XPath2MemoryManager.hpp:233
TYPE * swap(TYPE *p)
Definition: XPath2MemoryManager.hpp:292
TYPE * adopt()
Definition: XPath2MemoryManager.hpp:395
bool operator!=(XQillaAllocator< _Tp > &o)
Definition: XPath2MemoryManager.hpp:179
TYPE & operator*() const
Definition: XPath2MemoryManager.hpp:217
size_type max_size() const
Definition: XPath2MemoryManager.hpp:159
void set(TYPE *p)
Definition: XPath2MemoryManager.hpp:298
void set(TYPE *p)
Definition: XPath2MemoryManager.hpp:350
bool operator!=(const XQillaAllocator< _Tp > &o) const
Definition: XPath2MemoryManager.hpp:174
pointer allocate(size_t _n, const void *=0)
Definition: XPath2MemoryManager.hpp:123
TYPE * operator->() const
Definition: XPath2MemoryManager.hpp:383
Definition: XPath2MemoryManager.hpp:96
TYPE & operator*() const
Definition: XPath2MemoryManager.hpp:379
~AutoDeallocate()
Definition: XPath2MemoryManager.hpp:373
~AutoRelease()
Definition: XPath2MemoryManager.hpp:211
XQillaAllocator(const XQillaAllocator< _Tp > &o)
Definition: XPath2MemoryManager.hpp:112
XQillaAllocator()
Definition: XPath2MemoryManager.hpp:101
const _Tp * const_pointer
Definition: XPath2MemoryManager.hpp:91
AutoDelete(TYPE *p)
Definition: XPath2MemoryManager.hpp:263
XQillaAllocator< _Tp1 > other
Definition: XPath2MemoryManager.hpp:97
Definition: XPath2MemoryManager.hpp:260
TYPE * operator->() const
Definition: XPath2MemoryManager.hpp:221