ATLAS Offline Software
ArenaCachingHandle.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // $Id: ArenaCachingHandle.icc 470529 2011-11-24 23:54:22Z ssnyder $
6 /**
7  * @file AthAllocators/ArenaCachingHandle.icc
8  * @author scott snyder
9  * @date May 2007
10  * @brief User interface for allocating memory that caches constructed objects.
11  * Inline and template implementations.
12  */
13 
14 
15 namespace SG {
16 
17 
18 /**
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.
23  */
24 template <class T, class ALLOC>
25 ArenaCachingHandle<T, ALLOC>::ArenaCachingHandle (ArenaHeader* header,
26  size_t index)
27  : Base (header, index)
28 {
29 }
30 
31 
32 /**
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.
36  */
37 template <class T, class ALLOC>
38 ArenaCachingHandle<T, ALLOC>::ArenaCachingHandle
39  (const typename ALLOC::Params* params /*= nullptr*/)
40  : Base (static_cast<ArenaHeader*>(nullptr),
41  Base::template makeIndex<ArenaCachingHandle, defaultParams_t> (params))
42 {
43 }
44 
45 
46 /**
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.
52  */
53 template <class T, class ALLOC>
54 ArenaCachingHandle<T, ALLOC>::ArenaCachingHandle
55  (ArenaHeader* header,
56  const typename ALLOC::Params* params /*=nullptr */)
57  : Base (header,
58  Base::template makeIndex<ArenaCachingHandle, defaultParams_t> (params))
59 {
60 }
61 
62 
63 /**
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.
70  */
71 template <class T, class ALLOC>
72 ArenaCachingHandle<T, ALLOC>::ArenaCachingHandle
73  (ArenaHeader* header,
74  const EventContext& ctx,
75  const typename ALLOC::Params* params /*= nullptr */)
76  : Base (header,
77  ctx,
78  Base::template makeIndex<ArenaCachingHandle, defaultParams_t> (params))
79 {
80 }
81 
82 
83 /**
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.
88  */
89 template <class T, class ALLOC>
90 ArenaCachingHandle<T, ALLOC>::ArenaCachingHandle
91  (Arena* arena,
92  const typename ALLOC::Params* params /*= nullptr */)
93  : Base (arena,
94  Base::template makeIndex<ArenaCachingHandle, defaultParams_t> (params))
95 {
96 }
97 
98 
99 /**
100  * @brief Allocate a new element.
101  *
102  * This returns an already-initialized element.
103  *
104  * This is on the fast path for element allocation, so keep it small
105  * and inline.
106  */
107 template <class T, class ALLOC>
108 inline
109 typename ArenaCachingHandle<T, ALLOC>::pointer
110 ArenaCachingHandle<T, ALLOC>::allocate()
111 {
112  return reinterpret_cast<pointer> (this->allocator()->allocate());
113 }
114 
115 
116 /**
117  * @brief Internal helper: create a new Allocator instance.
118  * @param params The parameters for the Allocator.
119  */
120 template <class T, class ALLOC>
121 std::unique_ptr<ArenaAllocatorBase> ArenaCachingHandle<T, ALLOC>::makeAllocator
122  (const typename ALLOC::Params& params)
123 {
124  return std::make_unique<ALLOC> (params);
125 }
126 
127 
128 } // namespace SG