2 Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
5 // $Id: ArenaHandle.icc 470529 2011-11-24 23:54:22Z ssnyder $
7 * @file AthAllocators/ArenaHandle.icc
10 * @brief User interface for allocating memory.
11 * Inline and template implementations.
19 * @brief Constructor, passing in an index. (For internal/testing use.)
20 * @param header The group of Arenas which this Handle may reference.
21 * May be null to select the global default.
22 * @param index The index of this Handle's Allocator type.
24 template <class T, class ALLOC>
25 ArenaHandle<T, ALLOC>::ArenaHandle (ArenaHeader* header,
27 : Base (header, index)
33 * @brief Constructor, passing in an optional parameter set.
34 * @param params Parameters to pass to the Allocator,
35 * or nullptr to use the defaults.
37 template <class T, class ALLOC>
38 ArenaHandle<T, ALLOC>::ArenaHandle
39 (const typename ALLOC::Params* params /*= nullptr*/)
40 : Base (static_cast<SG::ArenaHeader*>(nullptr),
41 Base::template makeIndex<ArenaHandle, defaultParams_t> (params))
47 * @brief Constructor, passing in a Header and an optional parameter set.
48 * @param header The group of Arenas which this Handle may reference.
49 * May be null to select the global default.
50 * @param params Parameters to pass to the Allocator,
51 * or nullptr to use the defaults.
53 template <class T, class ALLOC>
54 ArenaHandle<T, ALLOC>::ArenaHandle
56 const typename ALLOC::Params* params /*= nullptr*/)
58 Base::template makeIndex<ArenaHandle, defaultParams_t> (params))
64 * @brief Constructor, passing in a Header, context, and an optional parameter set.
65 * @param header The group of Arenas which this Handle may reference.
66 * May be null to select the global default.
67 * @param ctx Event context identifying the event slot.
68 * @param params Parameters to pass to the Allocator,
69 * or nullptr to use the defaults.
71 template <class T, class ALLOC>
72 ArenaHandle<T, ALLOC>::ArenaHandle
74 const EventContext& ctx,
75 const typename ALLOC::Params* params /*= nullptr*/)
78 Base::template makeIndex<ArenaHandle, defaultParams_t> (params))
84 * @brief Constructor, passing in an Arena and an optional parameter set.
85 * @param arena The Arena in which to create the Allocator.
86 * @param params Parameters to pass to the Allocator,
87 * or nullptr to use the defaults.
89 template <class T, class ALLOC>
90 ArenaHandle<T, ALLOC>::ArenaHandle
92 const typename ALLOC::Params* params /*= nullptr*/)
94 Base::template makeIndex<ArenaHandle, defaultParams_t> (params))
100 * @brief Allocate a new element.
102 * The element's constructor will not be called; thus, the memory
103 * is returned as a @c void*.
105 * This is on the fast path for element allocation, so keep it small
108 template <class T, class ALLOC>
110 void* ArenaHandle<T, ALLOC>::allocate()
112 return this->allocator()->allocate();
117 * @brief Internal helper: create a new Allocator instance.
118 * @param params The parameters for the Allocator.
120 template <class T, class ALLOC>
121 std::unique_ptr<ArenaAllocatorBase> ArenaHandle<T, ALLOC>::makeAllocator
122 (const typename ALLOC::Params& params)
124 typename ALLOC::Params newparams = params;
126 // We don't call the element constructor.
127 newparams.constructor = 0;
129 // The destructor is called when we free an element --- not when
130 // we return it to the system.
131 newparams.clear = newparams.destructor;
132 newparams.destructor = 0;
134 // We can't skip running the destructor.
135 newparams.mustClear = true;
137 // We can't call the destructor twice.
138 newparams.canReclear = false;
140 // If we need a link, it can overlap the element.
141 newparams.eltSize = std::max (sizeof(T), newparams.minSize);
142 newparams.linkOffset = 0;
144 // Make the Allocator.
145 return std::make_unique <ALLOC> (newparams);