ATLAS Offline Software
Loading...
Searching...
No Matches
DataPool.icc
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
4
5//-----------------------------------------------------------
6// .icc file for DataPool
7//-----------------------------------------------------------
8// includes:
9#include <algorithm>
10#include "GaudiKernel/System.h"
11
12
13namespace SG {
14
15
16/// Find the address of the function to clear an element,
17/// or nullptr if it hasn't been set explicitly.
18template <class VALUE, class CLEAR>
19class DataPoolClearAddr
20{
21public:
22 // Generic version --- a function that will call CLEAR::operator(),
23 // casting the argument appropriately.
24 static void callClear (SG::ArenaAllocatorBase::pointer p)
25 {
26 CLEAR::clear (reinterpret_cast<VALUE*> (p));
27 }
28 static SG::ArenaAllocatorBase::func_t* addr() { return callClear; }
29};
30template <class VALUE>
31class DataPoolClearAddr<VALUE, SG::DataPoolNullClear<VALUE> >
32{
33public:
34 // Specialized version of the default DataPoolNullClear.
35 // Just return nullptr.
36 static SG::ArenaAllocatorBase::func_t* addr() { return nullptr; }
37};
38
39
40} // namespace SG
41
42
43#define DATAPOOL_T template <typename VALUE, typename CLEAR>
44DATAPOOL_T
45const typename DataPool<VALUE, CLEAR>::alloc_t::Params DataPool<VALUE, CLEAR>::s_params
46 = DataPool<VALUE, CLEAR>::initParams();
47
48
49DATAPOOL_T
50typename DataPool<VALUE, CLEAR>::alloc_t::Params DataPool<VALUE, CLEAR>::initParams()
51{
52 typename alloc_t::Params params = alloc_t::initParams<VALUE> (s_minRefCount);
53 params.clear = SG::DataPoolClearAddr<VALUE, CLEAR>::addr();
54 return params;
55}
56
57
58//-----------------------------------------------------------
59
60DATAPOOL_T
61DataPool<VALUE, CLEAR>::DataPool(size_type n /*= 0*/)
62 : m_handle (&s_params)
63{
64 if (n > 0)
65 m_handle.reserve (std::max (n, s_minRefCount));
66}
67
68DATAPOOL_T
69DataPool<VALUE, CLEAR>::DataPool(const EventContext& ctx,
70 size_type n /*= 0*/)
71 : m_handle (static_cast<SG::ArenaHeader*>(nullptr), ctx, &s_params)
72{
73 if (n > 0)
74 m_handle.reserve (std::max (n, s_minRefCount));
75}
76
77DATAPOOL_T
78DataPool<VALUE, CLEAR>::DataPool(SG::Arena* arena,
79 size_type n /*= 0*/)
80 : m_handle (arena, &s_params)
81{
82 if (n > 0)
83 m_handle.reserve (std::max (n, s_minRefCount));
84}
85
86//-----------------------------------------------------------
87/// release all elements in the pool.
88DATAPOOL_T
89void DataPool<VALUE, CLEAR>::reset()
90{
91 m_handle.reset();
92}
93
94/// free all memory in the pool.
95DATAPOOL_T
96void DataPool<VALUE, CLEAR>::erase()
97{
98 m_handle.erase();
99}
100//-----------------------------------------------------------
101// reserve space for the pool
102// allocated elements will not be deleted.
103
104DATAPOOL_T
105void DataPool<VALUE, CLEAR>::reserve(unsigned int size)
106{
107 m_handle.reserve (size);
108}
109
110
111DATAPOOL_T
112void DataPool<VALUE, CLEAR>::prepareToAdd(unsigned int size)
113{
114 if (this->capacity() - this->allocated() < size) {
115 this->reserve(this->allocated() + size);
116 }
117}
118
119DATAPOOL_T
120unsigned int DataPool<VALUE, CLEAR>::capacity()
121{
122 return m_handle.stats().elts.total;
123}
124
125DATAPOOL_T
126unsigned int DataPool<VALUE, CLEAR>::allocated()
127{
128 return m_handle.stats().elts.inuse;
129}
130
131
132//-----------------------------------------------------------
133/// begin iterators over pool
134DATAPOOL_T
135typename DataPool<VALUE, CLEAR>::iterator DataPool<VALUE, CLEAR>::begin()
136{
137 return iterator (m_handle.begin());
138}
139
140DATAPOOL_T
141typename DataPool<VALUE, CLEAR>::const_iterator DataPool<VALUE, CLEAR>::begin() const
142{
143 return const_Iterator (m_handle.begin());
144}
145
146//-----------------------------------------------------------
147/// the end() method will allow looping over only valid elements
148/// and not over ALL elements of the pool
149
150DATAPOOL_T
151typename DataPool<VALUE, CLEAR>::iterator DataPool<VALUE, CLEAR>::end()
152{
153 return iterator (m_handle.end());
154}
155
156DATAPOOL_T
157typename DataPool<VALUE, CLEAR>::const_iterator DataPool<VALUE, CLEAR>::end() const {
158 return const_iterator (m_handle.end());
159}
160
161//-----------------------------------------------------------
162/// typename of pool
163DATAPOOL_T
164const std::string& DataPool<VALUE, CLEAR>::typeName() {
165 static std::string name = System::typeinfoName (typeid (VALUE));
166 return name;
167}
168
169//-----------------------------------------------------------
170/// obtain the next available element in pool by pointer
171/// pool is resized if reached its limit
172DATAPOOL_T
173inline
174typename DataPool<VALUE, CLEAR>::pointer DataPool<VALUE, CLEAR>::nextElementPtr()
175{
176 return m_handle.allocate();
177}
178
179
180#undef DATAPOOL_T