Xalan-C++ API Reference  1.12.0
XalanMemoryManagement.hpp
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 #if !defined(XALANMEMORYMANAGEMENT_HEADER_GUARD_1357924680)
19 #define XALANMEMORYMANAGEMENT_HEADER_GUARD_1357924680
20 
21 
22 // Base include file. Must be first.
24 
25 
26 
27 #include <cassert>
28 #include <cstddef>
29 #include <new>
30 
31 
32 
33 #include <xercesc/framework/MemoryManager.hpp>
34 
35 
36 
37 
38 namespace XALAN_CPP_NAMESPACE {
39 
40 
41 
42 using xercesc::MemoryManager;
43 typedef MemoryManager MemoryManagerType;
44 
45 
46 class XALAN_PLATFORM_EXPORT XalanMemoryManager : public MemoryManager
47 {
48 public:
49 
50 #if XERCES_VERSION_MAJOR < 3
51  typedef std::size_t size_type;
52 #else
53  typedef XalanSize_t size_type;
54 #endif
55 
56 
58 
59  virtual
61 
62  virtual void*
63  allocate(size_type size) = 0;
64 
65  virtual void
66  deallocate(void* pointer) = 0;
67 
68  virtual MemoryManager*
69  getExceptionMemoryManager() = 0;
70 
71  static MemoryManager&
72  getExceptionMemoryManager(MemoryManager& theMemoryManager)
73  {
74 #if XERCES_VERSION_MAJOR < 3
75  return theMemoryManager;
76 #else
77  assert(theMemoryManager.getExceptionMemoryManager() != 0);
78 
79  return *theMemoryManager.getExceptionMemoryManager();
80 #endif
81  }
82 
83 protected:
84 
85  XalanMemoryManager(const XalanMemoryManager& theSource);
86 
88  operator=(const XalanMemoryManager& /* theRHS */)
89  {
90  return *this;
91  }
92 };
93 
94 
95 
97 {
98 public:
99 
100  typedef std::size_t size_type;
101 
103  MemoryManager& theMemoryManager,
104  void* thePointer) :
105  m_memoryManager(theMemoryManager),
106  m_pointer(thePointer)
107  {
108  }
109 
111  MemoryManager& theMemoryManager,
112  size_type theSize) :
113  m_memoryManager(theMemoryManager),
114  m_pointer(theMemoryManager.allocate(theSize))
115  {
116  }
117 
119  {
120  if (m_pointer != 0)
121  {
122  m_memoryManager.deallocate(m_pointer);
123  }
124  }
125 
126  void*
127  get() const
128  {
129  return m_pointer;
130  }
131 
132  void
134  {
135  m_pointer = 0;
136  }
137 
138 private:
139 
140  // Data members...
141  MemoryManager& m_memoryManager;
142 
143  void* m_pointer;
144 };
145 
146 
147 
148 template<class Type>
149 void
150 XalanDestroy(Type& theArg)
151 {
152  theArg.~Type();
153 }
154 
155 
156 
157 template<class Type>
158 void
159 XalanDestroy(Type* theArg)
160 {
161  if (theArg != 0)
162  {
163  theArg->~Type();
164  }
165 }
166 
167 
168 
169 template<class Type>
170 void
172  MemoryManager& theMemoryManager,
173  Type* theArg)
174 {
175  if (theArg != 0)
176  {
177  XalanDestroy(*theArg);
178 
179  theMemoryManager.deallocate(theArg);
180  }
181 }
182 
183 
184 
185 template<class Type>
186 void
188  MemoryManager& theMemoryManager,
189  Type& theArg)
190 {
191  XalanDestroy(theArg);
192 
193  theMemoryManager.deallocate(&theArg);
194 }
195 
196 
197 
198 template<class Type>
199 Type*
201  MemoryManager& theMemoryManager,
202  Type*& theInstance)
203 {
204  XalanAllocationGuard theGuard(
205  theMemoryManager,
206  sizeof(Type));
207 
208  theInstance =
209  new (theGuard.get()) Type;
210 
211  theGuard.release();
212 
213  return theInstance;
214 }
215 
216 
217 
218 template<
219  class Type,
220  class Param1Type>
221 Type*
223  MemoryManager& theMemoryManager,
224  Type*& theInstance,
225  const Param1Type& theParam1)
226 {
227  XalanAllocationGuard theGuard(
228  theMemoryManager,
229  sizeof(Type));
230 
231  theInstance =
232  new (theGuard.get()) Type(theParam1);
233 
234  theGuard.release();
235 
236  return theInstance;
237 }
238 
239 
240 
241 template<
242  class Type,
243  class Param1Type>
244 Type*
246  MemoryManager& theMemoryManager,
247  Type*& theInstance,
248  Param1Type& theParam1)
249 {
250  XalanAllocationGuard theGuard(
251  theMemoryManager,
252  sizeof(Type));
253 
254  theInstance =
255  new (theGuard.get()) Type(theParam1);
256 
257  theGuard.release();
258 
259  return theInstance;
260 }
261 
262 
263 
264 template<
265  class Type,
266  class Param1Type,
267  class Param2Type>
268 Type*
270  MemoryManager& theMemoryManager,
271  Type*& theInstance,
272  Param1Type& theParam1,
273  const Param2Type& theParam2)
274 {
275  XalanAllocationGuard theGuard(
276  theMemoryManager,
277  sizeof(Type));
278 
279  theInstance =
280  new (theGuard.get()) Type(theParam1, theParam2);
281 
282  theGuard.release();
283 
284  return theInstance;
285 }
286 
287 
288 
289 template<
290  class Type,
291  class Param1Type,
292  class Param2Type,
293  class Param3Type,
294  class Param4Type>
295 Type*
297  MemoryManager& theMemoryManager,
298  Type*& theInstance,
299  const Param1Type* theParam1,
300  const Param2Type* theParam2,
301  const Param3Type* theParam3,
302  Param4Type& theParam4)
303 {
304  XalanAllocationGuard theGuard(
305  theMemoryManager,
306  sizeof(Type));
307 
308  theInstance =
309  new (theGuard.get()) Type(theParam1, theParam2, theParam3, theParam4);
310 
311  theGuard.release();
312 
313  return theInstance;
314 }
315 
316 
317 
318 template<
319  class Type,
320  class Param1Type,
321  class Param2Type,
322  class Param3Type,
323  class Param4Type,
324  class Param5Type,
325  class Param6Type>
326 Type*
328  MemoryManager& theMemoryManager,
329  Type*& theInstance,
330  const Param1Type* theParam1,
331  const Param2Type* theParam2,
332  const Param3Type* theParam3,
333  const Param4Type* theParam4,
334  const Param5Type* theParam5,
335  Param6Type& theParam6)
336 {
337  XalanAllocationGuard theGuard(
338  theMemoryManager,
339  sizeof(Type));
340 
341  theInstance =
342  new (theGuard.get()) Type(
343  theParam1,
344  theParam2,
345  theParam3,
346  theParam4,
347  theParam5,
348  theParam6);
349 
350  theGuard.release();
351 
352  return theInstance;
353 }
354 
355 
356 
357 template<
358  class Type,
359  class Param1Type,
360  class Param2Type,
361  class Param3Type>
362 Type*
364  MemoryManager& theMemoryManager,
365  Type*& theInstance,
366  Param1Type& theParam1,
367  const Param2Type& theParam2,
368  Param3Type& theParam3)
369 {
370  XalanAllocationGuard theGuard(
371  theMemoryManager,
372  sizeof(Type));
373 
374  theInstance =
375  new (theGuard.get()) Type(theParam1, theParam2, theParam3);
376 
377  theGuard.release();
378 
379  return theInstance;
380 }
381 
382 
383 
384 template<
385  class Type,
386  class Param1Type,
387  class Param2Type,
388  class Param3Type,
389  class Param4Type,
390  class Param5Type>
391 Type*
393  MemoryManager& theMemoryManager,
394  Type*& theInstance,
395  Param1Type& theParam1,
396  Param2Type& theParam2,
397  const Param3Type& theParam3,
398  const Param4Type& theParam4,
399  const Param5Type& theParam5)
400 {
401  XalanAllocationGuard theGuard(
402  theMemoryManager,
403  sizeof(Type));
404 
405  theInstance =
406  new (theGuard.get()) Type(theParam1, theParam2, theParam3, theParam4, theParam5);
407 
408  theGuard.release();
409 
410  return theInstance;
411 }
412 
413 
414 
415 template<
416  class Type,
417  class Param1Type,
418  class Param2Type,
419  class Param3Type,
420  class Param4Type,
421  class Param5Type,
422  class Param6Type>
423 Type*
425  MemoryManager& theMemoryManager,
426  Type*& theInstance,
427  Param1Type& theParam1,
428  Param2Type& theParam2,
429  const Param3Type& theParam3,
430  const Param4Type& theParam4,
431  const Param5Type& theParam5,
432  const Param6Type& theParam6)
433 {
434  XalanAllocationGuard theGuard(
435  theMemoryManager,
436  sizeof(Type));
437 
438  theInstance =
439  new (theGuard.get()) Type(theParam1, theParam2, theParam3, theParam4, theParam5, theParam6);
440 
441  theGuard.release();
442 
443  return theInstance;
444 }
445 
446 
447 
448 template<class Type>
449 Type*
451  MemoryManager& theMemoryManager,
452  const Type& theSource)
453 {
454  XalanAllocationGuard theGuard(
455  theMemoryManager,
456  sizeof(Type));
457 
458  Type* const theInstance =
459  new (theGuard.get()) Type(theSource);
460 
461  theGuard.release();
462 
463  return theInstance;
464 }
465 
466 
467 
468 template<
469  class Type,
470  class Param1Type>
471 Type*
473  MemoryManager& theMemoryManager,
474  const Type& theSource,
475  Param1Type& theParam1)
476 {
477  XalanAllocationGuard theGuard(
478  theMemoryManager,
479  sizeof(Type));
480 
481  Type* const theInstance =
482  new (theGuard.get()) Type(theSource, theParam1);
483 
484  theGuard.release();
485 
486  return theInstance;
487 }
488 
489 
490 
492 {
493 public:
494 
495  static MemoryManager&
496  getDummyMemMgr();
497 
498  static MemoryManager&
499  getDefaultXercesMemMgr();
500 
501  static MemoryManager&
503  {
504  return getDefaultXercesMemMgr();
505  }
506 };
507 
508 
509 
510 
511 #if defined (XALAN_DEVELOPMENT)
512 #define XALAN_DEFAULT_CONSTRUCTOR_MEMMGR
513 #define XALAN_DEFAULT_MEMMGR = XalanMemMgrs::getDummyMemMgr()
514 #else
515 #define XALAN_DEFAULT_CONSTRUCTOR_MEMMGR = XalanMemMgrs::getDefaultXercesMemMgr()
516 #define XALAN_DEFAULT_MEMMGR = XalanMemMgrs::getDefaultXercesMemMgr()
517 #endif
518 
519 
520 
521 template <class C>
523 {
524  ConstructValueWithNoMemoryManager(MemoryManager& /*mgr*/) :
525  value()
526  {
527  }
528 
529  C value;
530 };
531 
532 template <class C>
534 {
535  ConstructValueWithMemoryManager(MemoryManager& mgr) :
536  value(mgr)
537  {
538  }
539 
540  C value;
541 };
542 
543 template <class C>
545 {
547 
548  static C* construct(C* address, MemoryManager& /* mgr */)
549  {
550  return (C*) new (address) C();
551  }
552 
553  static C* construct(C* address, const C& theRhs, MemoryManager& /* mgr */)
554  {
555  return (C*) new (address) C(theRhs);
556  }
557 };
558 
559 template <class C>
561 {
563 
564  static C* construct(C* address, MemoryManager& mgr)
565  {
566  return (C*) new (address) C(mgr);
567  }
568 
569  static C* construct(C* address, const C& theRhs, MemoryManager& mgr)
570  {
571  return (C*) new (address) C(theRhs, mgr);
572  }
573 };
574 
575 template <class C>
577 {
579 
580 };
581 
582 template <class C>
584 {
586 
587 };
588 
589 #define XALAN_USES_MEMORY_MANAGER(Type) \
590 template<> \
591 struct MemoryManagedConstructionTraits<Type> \
592  { \
593  typedef ConstructWithMemoryManager<Type> Constructor; \
594  };
595 
596 template <class C>
598 {
600 };
601 
602 template <class C>
604 {
606 };
607 
608 
609 
610 }
611 
612 
613 
614 #endif // XALANMEMORYMANAGEMENT_HEADER_GUARD_1357924680
xalanc::ConstructWithMemoryManager
Definition: XalanMemoryManagement.hpp:560
xalanc::ExplicitMemoryManagedConstructionTraits::Constructor
ConstructWithMemoryManager< C > Constructor
Definition: XalanMemoryManagement.hpp:585
XALAN_CPP_NAMESPACE
#define XALAN_CPP_NAMESPACE
Xalan-C++ namespace, including major and minor version.
Definition: XalanVersion.hpp:76
xalanc::XalanAllocationGuard::release
void release()
Definition: XalanMemoryManagement.hpp:133
xalanc::XalanAllocationGuard::XalanAllocationGuard
XalanAllocationGuard(MemoryManager &theMemoryManager, size_type theSize)
Definition: XalanMemoryManagement.hpp:110
xalanc::ConstructWithNoMemoryManager::ConstructableType
ConstructValueWithNoMemoryManager< C > ConstructableType
Definition: XalanMemoryManagement.hpp:546
xalanc::ConstructWithMemoryManager::construct
static C * construct(C *address, MemoryManager &mgr)
Definition: XalanMemoryManagement.hpp:564
xalanc::XalanMemoryManager::size_type
std::size_t size_type
Definition: XalanMemoryManagement.hpp:51
xalanc::ExplicitMemoryManagedConstructionTraits
Definition: XalanMemoryManagement.hpp:583
xalanc::MemoryManagedConstructionTraits
Definition: XalanMemoryManagement.hpp:576
xalanc::XalanMemoryManager
Definition: XalanMemoryManagement.hpp:46
xalanc::size_type
size_t size_type
Definition: XalanMap.hpp:46
xalanc::ConstructWithMemoryManagerTraits
Definition: XalanMemoryManagement.hpp:597
xalanc::ConstructWithMemoryManagerTraits::Constructor
ConstructWithMemoryManager< C > Constructor
Definition: XalanMemoryManagement.hpp:599
xalanc::XalanAllocationGuard::~XalanAllocationGuard
~XalanAllocationGuard()
Definition: XalanMemoryManagement.hpp:118
xalanc::ConstructWithNoMemoryManager::construct
static C * construct(C *address, const C &theRhs, MemoryManager &)
Definition: XalanMemoryManagement.hpp:553
xalanc::ConstructValueWithNoMemoryManager
Definition: XalanMemoryManagement.hpp:522
xalanc::ConstructValueWithNoMemoryManager::value
C value
Definition: XalanMemoryManagement.hpp:529
xalanc::MemoryManagerType
MemoryManager MemoryManagerType
Definition: XalanMemoryManagement.hpp:43
xalanc::XalanMemMgrs::getDefault
static MemoryManager & getDefault()
Definition: XalanMemoryManagement.hpp:502
xalanc::XalanAllocationGuard
Definition: XalanMemoryManagement.hpp:96
xalanc::ConstructValueWithMemoryManager::ConstructValueWithMemoryManager
ConstructValueWithMemoryManager(MemoryManager &mgr)
Definition: XalanMemoryManagement.hpp:535
xalanc::ConstructValueWithMemoryManager
Definition: XalanMemoryManagement.hpp:533
xalanc::ConstructWithMemoryManager::construct
static C * construct(C *address, const C &theRhs, MemoryManager &mgr)
Definition: XalanMemoryManagement.hpp:569
xalanc::XalanConstruct
Type * XalanConstruct(MemoryManager &theMemoryManager, Type *&theInstance, Param1Type &theParam1, Param2Type &theParam2, const Param3Type &theParam3, const Param4Type &theParam4, const Param5Type &theParam5, const Param6Type &theParam6)
Definition: XalanMemoryManagement.hpp:424
xalanc::XalanMemoryManager::getExceptionMemoryManager
static MemoryManager & getExceptionMemoryManager(MemoryManager &theMemoryManager)
Definition: XalanMemoryManagement.hpp:72
xalanc::XalanAllocationGuard::XalanAllocationGuard
XalanAllocationGuard(MemoryManager &theMemoryManager, void *thePointer)
Definition: XalanMemoryManagement.hpp:102
xalanc::XalanDestroy
void XalanDestroy(MemoryManager &theMemoryManager, Type &theArg)
Definition: XalanMemoryManagement.hpp:187
xalanc::XalanAllocationGuard::size_type
std::size_t size_type
Definition: XalanMemoryManagement.hpp:100
XALAN_PLATFORM_EXPORT
#define XALAN_PLATFORM_EXPORT
Definition: PlatformDefinitions.hpp:68
xalanc::ConstructWithNoMemoryManagerTraits
Definition: XalanMemoryManagement.hpp:603
xalanc::ConstructWithMemoryManager::ConstructableType
ConstructValueWithMemoryManager< C > ConstructableType
Definition: XalanMemoryManagement.hpp:562
xalanc::XalanAllocationGuard::get
void * get() const
Definition: XalanMemoryManagement.hpp:127
xalanc::ConstructWithNoMemoryManager
Definition: XalanMemoryManagement.hpp:544
xalanc::ConstructWithNoMemoryManager::construct
static C * construct(C *address, MemoryManager &)
Definition: XalanMemoryManagement.hpp:548
xalanc::XalanCopyConstruct
Type * XalanCopyConstruct(MemoryManager &theMemoryManager, const Type &theSource, Param1Type &theParam1)
Definition: XalanMemoryManagement.hpp:472
xalanc::XalanMemoryManager::operator=
XalanMemoryManager & operator=(const XalanMemoryManager &)
Definition: XalanMemoryManagement.hpp:88
PlatformDefinitions.hpp
xalanc::MemoryManagedConstructionTraits::Constructor
ConstructWithNoMemoryManager< C > Constructor
Definition: XalanMemoryManagement.hpp:578
xalanc::XalanMemMgrs
Definition: XalanMemoryManagement.hpp:491
xalanc::ConstructValueWithMemoryManager::value
C value
Definition: XalanMemoryManagement.hpp:540
xalanc::ConstructWithNoMemoryManagerTraits::Constructor
ConstructWithNoMemoryManager< C > Constructor
Definition: XalanMemoryManagement.hpp:605
xalanc::ConstructValueWithNoMemoryManager::ConstructValueWithNoMemoryManager
ConstructValueWithNoMemoryManager(MemoryManager &)
Definition: XalanMemoryManagement.hpp:524