ATLAS Offline Software
ArenaHandleBaseAllocT.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3 */
4 /**
5  * @file AthAllocators/ArenaHandleBaseAllocT.icc
6  * @author scott snyder
7  * @date May 2007
8  * @brief Base class for @c Handle classes, containing parts that
9  * depend only on the Allocator.
10  * Inline and template implementations.
11  */
12 
13 
14 namespace SG {
15 
16 
17 /**
18  * @brief Constructor.
19  * @param name Name of the Allocator.
20  * @param makeFunc Function that creates an Allocator given a set of parameters.
21  * @param params Allocator parameters.
22  *
23  * This initializes the @c Creator for creating an Allocator.
24  * The name in @c params will be replaced with the @c name argument.
25  */
26 template <typename ALLOC>
27 ArenaHandleBaseAllocT<ALLOC>::Creator::Creator (const std::string& name,
28  makeFunc_t* makeFunc,
29  const typename ALLOC::Params& params)
30  : m_makeFunc (makeFunc),
31  m_params (params)
32 {
33  m_params.name = name;
34 }
35 
36 
37 /**
38  * @brief Create an allocator instance.
39  */
40 template <typename ALLOC>
41 std::unique_ptr<ArenaAllocatorBase>
42 ArenaHandleBaseAllocT<ALLOC>::Creator::create()
43 {
44  return m_makeFunc (m_params);
45 }
46 
47 
48 /**
49  * @brief Return the name of the Allocator we create.
50  */
51 template <typename ALLOC>
52 const std::string&
53 ArenaHandleBaseAllocT<ALLOC>::Creator::name() const
54 {
55  return m_params.name;
56 }
57 
58 
59 /**
60  * @brief Constructor, passing in an index.
61  * @param header The group of Arenas which this Handle may reference.
62  * May be null to select the global default.
63  * @param index The index of this Handle's Allocator type.
64  */
65 template <typename ALLOC>
66 ArenaHandleBaseAllocT<ALLOC>::ArenaHandleBaseAllocT
67  (ArenaHeader* header, size_t index)
68  : ArenaHandleBase (header, index)
69 {
70 }
71 
72 
73 /**
74  * @brief Constructor, passing in an index, for a specific event slot.
75  * @param header The group of Arenas which this Handle may reference.
76  * May be null to select the global default.
77  * @param ctx Event context identifying the event slot.
78  * @param index The index of this Handle's Allocator type.
79  */
80 template <typename ALLOC>
81 ArenaHandleBaseAllocT<ALLOC>::ArenaHandleBaseAllocT (ArenaHeader* header,
82  const EventContext& ctx,
83  size_t index)
84  : ArenaHandleBase (header, ctx, index)
85 {
86 }
87 
88 
89 /**
90  * @brief Constructor, passing in an index, for a specific Arena.
91  * @param arena The Arena in which to find the allocator.
92  * @param index The index of this Handle's Allocator type.
93  */
94 template <typename ALLOC>
95 ArenaHandleBaseAllocT<ALLOC>::ArenaHandleBaseAllocT (ArenaBase* arena, size_t index)
96  : ArenaHandleBase (arena, index)
97 {
98 }
99 
100 
101 /**
102  * @brief Return our Allocator's parameters.
103  */
104 template <typename ALLOC>
105 const typename ALLOC::Params&
106 ArenaHandleBaseAllocT<ALLOC>::params() const
107 {
108  const ALLOC* alloc = dynamic_cast<const ALLOC*>(this->baseAllocator());
109  if (!alloc) std::abort();
110  return alloc->params();
111 }
112 
113 
114 /**
115  * @brief Return our current Allocator.
116  *
117  * This is on the fast path for allocation. It should be kept
118  * simple and inline.
119  */
120 template <typename ALLOC>
121 inline
122 ALLOC* ArenaHandleBaseAllocT<ALLOC>::allocator()
123 {
124  return reinterpret_cast<ALLOC*> (this->baseAllocator());
125 }
126 
127 
128 /**
129  * @brief Return our current Allocator.
130  *
131  * This is on the fast path for allocation. It should be kept
132  * simple and inline.
133  */
134 template <typename ALLOC>
135 inline
136 const ALLOC* ArenaHandleBaseAllocT<ALLOC>::allocator() const
137 {
138  return reinterpret_cast<const ALLOC*> (this->baseAllocator());
139 }
140 
141 
142 /**
143  * @brief Find the index for creating an allocator.
144  * @param params Pointer to the supplied parameters.
145  * If null, use the result of DEFPARAMS().
146  *
147  * We look up in the registry the Allocator name we get from @c params
148  * (if this is blank, a name is derived from @c ALLOC).
149  * If not found, then we register Allocator and return the new index.
150  */
151 template <typename ALLOC>
152 template <class HANDLE, class DEFPARAMS>
153 size_t
154 ArenaHandleBaseAllocT<ALLOC>::makeIndex (const typename ALLOC::Params* params)
155 {
156  ArenaAllocatorRegistry* reg = ArenaAllocatorRegistry::instance();
157  std::string name;
158  if (params) {
159  name = params->name;
160  }
161  if (name.empty()) {
162  const static std::string sname = System::typeinfoName (typeid (HANDLE));
163  name = sname;
164  }
165  size_t i = reg->lookup (name);
166  if (i != std::string::npos)
167  return i;
168  return reg->registerCreator (name,
169  std::make_unique<Creator> (name,
170  HANDLE::makeAllocator,
171  params ? *params : DEFPARAMS()));
172 }
173 
174 
175 } // namespace SG