Jpp  master_rocky-37-gf0c5bc59d
the software that should make you happy
JObjectAllocator.hh
Go to the documentation of this file.
1 #ifndef __JLANG__JOBJECTALLOCATOR__
2 #define __JLANG__JOBJECTALLOCATOR__
3 
4 #include <cstddef>
5 
6 /**
7  * \file
8  * Auxiliary base class to speed up new/delete operations of any class.
9  * \author mdejong
10  */
11 namespace JLANG {}
12 namespace JPP { using namespace JLANG; }
13 
14 namespace JLANG {
15 
16  /**
17  * Base class for customized new/delete operators.
18  * The first template argument refers to the data type for new/delete operations and
19  * the second to the memory management class.
20  *
21  * Possible syntax:
22  * <pre>
23  * \#include "JLang/JRAM.hh"
24  *
25  * class A : public JLANG::JObjectAllocator<A, JRAM> {
26  * };
27  * </pre>
28  * In this case, the new/delete operations are implemented by the JLANG::JRAM class.
29  */
30  template<class T, class JAllocator_t>
32  {
33  protected:
34  /**
35  * Defaul constructor.
36  */
38  {}
39 
40  public:
41  /**
42  * Get reference to unique instance of this class object.
43  *
44  * \return reference to this class object
45  */
46  static inline JAllocator_t& getInstance()
47  {
48  static JAllocator_t allocator(BLOCK_SIZE);
49 
50  return allocator;
51  }
52 
53 
54  static const std::size_t BLOCK_SIZE = sizeof(T); //!< size of object [Byte]
55 
56 
57  /**
58  * new operator.
59  *
60  * \return pointer to new object (or NULL)
61  */
62  static inline void* operator new(const std::size_t size)
63  {
64  return getInstance().allocate();
65  }
66 
67 
68  /**
69  * delete operator.
70  *
71  * \param p pointer to object to be freed
72  * \param size number of bytes
73  */
74  static inline void operator delete(void* p, const std::size_t size)
75  {
76  getInstance().free(p);
77  }
78  };
79 }
80 
81 #endif
Base class for customized new/delete operators.
static const std::size_t BLOCK_SIZE
size of object [Byte]
JObjectAllocator()
Defaul constructor.
static JAllocator_t & getInstance()
Get reference to unique instance of this class object.
Auxiliary classes and methods for language specific functionality.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).