ATLAS Offline Software
ArenaAllocatorBase.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 /**
5  * @file AthAllocators/ArenaAllocatorBase.icc
6  * @author scott snyder
7  * @date May 2007
8  * @brief Common base class for arena allocator classes.
9  * Inline/template implementations.
10  */
11 
12 
13 namespace SG {
14 
15 
16 /**
17  * @brief Make a constructor function pointer for a non-trivial constructor.
18  */
19 template <class T>
20 ArenaAllocatorBase::func_t*
21 ArenaAllocatorBase::makeConstructor (const std::false_type&)
22 {
23  return &construct_fcn<T>;
24 }
25 
26 
27 /**
28  * @brief Make a constructor function pointer for a trivial constructor.
29  */
30 template <class T>
31 ArenaAllocatorBase::func_t*
32 ArenaAllocatorBase::makeConstructor (const std::true_type&)
33 {
34  return 0;
35 }
36 
37 
38 /**
39  * @brief Make a constructor function pointer for a non-trivial destructor.
40  */
41 template <class T>
42 ArenaAllocatorBase::func_t*
43 ArenaAllocatorBase::makeDestructor (const std::false_type&)
44 {
45  return &destroy_fcn<T>;
46 }
47 
48 
49 /**
50  * @brief Make a constructor function pointer for a trivial destructor.
51  */
52 template <class T>
53 ArenaAllocatorBase::func_t*
54 ArenaAllocatorBase::makeDestructor (const std::true_type&)
55 {
56  return 0;
57 }
58 
59 
60 /**
61  * @brief Make a function pointer for a @c clear function.
62  */
63 template <class T>
64 ArenaAllocatorBase::func_t*
65 ArenaAllocatorBase::makeClear (const std::false_type&)
66 {
67  return &clear_fcn<T>;
68 }
69 
70 
71 /**
72  * @brief Make a dummy @c clear function pointer.
73  */
74 template <class T>
75 ArenaAllocatorBase::func_t*
76 ArenaAllocatorBase::makeClear (const std::true_type&)
77 {
78  return 0;
79 }
80 
81 
82 /**
83  * @brief Call @c T's default constructor on the object at @c p.
84  * @param p The object on which to run the constructor.
85  */
86 template <typename T>
87 void ArenaAllocatorBase::construct_fcn (pointer p)
88 {
89  new(p) T;
90 }
91 
92 
93 /**
94  * @brief Call @c T's destructor on the object at @c p.
95  * @param p The object on which to run the destructor.
96  */
97 template <typename T>
98 void ArenaAllocatorBase::destroy_fcn (pointer p)
99 {
100  reinterpret_cast<T*>(p)->~T();
101 }
102 
103 
104 /**
105  * @brief Call @c T::clear on the object at @c p.
106  * @param p The object on which to run the @c clear.
107  */
108 template <typename T>
109 void ArenaAllocatorBase::clear_fcn (pointer p)
110 {
111  reinterpret_cast<T*>(p)->clear();
112 }
113 
114 
115 /**
116  * @brief Constructor.
117  * @param nblock Value to set in the parameters structure for the
118  * number of elements to allocate per block.
119  * @param name Value to set in the parameters structure for the
120  * allocator name.
121  */
122 template <typename T, bool clear, bool no_ctor, bool no_dtor>
123 ArenaAllocatorBase::initParams<T, clear, no_ctor, no_dtor>::initParams
124  (size_t nblock /*=1000*/,
125  const std::string& name /*= ""*/)
126  : m_nblock (nblock),
127  m_name (name)
128 {
129 }
130 
131 
132 /**
133  * @brief Return an initialized parameters structure.
134  */
135 template <typename T, bool clear, bool no_ctor, bool no_dtor>
136 ArenaAllocatorBase::Params
137 ArenaAllocatorBase::initParams<T, clear, no_ctor, no_dtor>::params() const
138 {
139  Params params;
140 
141  // Fill in the parameters that were passed to our constructor.
142  params.nblock = m_nblock;
143  params.name = m_name;
144 
145  // We're not setting up a link.
146  params.linkOffset = 0;
147  params.eltSize = sizeof (T);
148  params.minSize = 1;
149 
150  // Defaults for these.
151  params.canReclear = true;
152  params.mustClear = false;
153 
154  // Set up the constructor/destructor.
155  // We want the pointers to be null if they're trivial.
156  params.constructor =
157  makeConstructor<T> (::std::integral_constant<bool,
158  ::std::is_trivially_constructible<T>::value ||
159  no_ctor>());
160  params.destructor =
161  makeDestructor<T> (::std::integral_constant<bool,
162  ::std::is_trivially_destructible<T>::value ||
163  no_dtor>());
164 
165  // Set up the clear function --- only if the flag is set!
166  params.clear = makeClear<T> (::std::integral_constant<bool, !clear>());
167 
168  return params;
169 }
170 
171 
172 } // namespace SG