ATLAS Offline Software
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 
13 namespace SG {
14 
15 
16 /// Find the address of the function to clear an element,
17 /// or nullptr if it hasn't been set explicitly.
18 template <class VALUE, class CLEAR>
19 class DataPoolClearAddr
20 {
21 public:
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 };
30 template <class VALUE>
31 class DataPoolClearAddr<VALUE, SG::DataPoolNullClear<VALUE> >
32 {
33 public:
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>
44 DATAPOOL_T
45 const typename DataPool<VALUE, CLEAR>::alloc_t::Params DataPool<VALUE, CLEAR>::s_params
46  = DataPool<VALUE, CLEAR>::initParams();
47 
48 
49 DATAPOOL_T
50 typename 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 
60 DATAPOOL_T
61 DataPool<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 
68 DATAPOOL_T
69 DataPool<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 
77 DATAPOOL_T
78 DataPool<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.
88 DATAPOOL_T
89 void DataPool<VALUE, CLEAR>::reset()
90 {
91  m_handle.reset();
92 }
93 
94 /// free all memory in the pool.
95 DATAPOOL_T
96 void 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 
104 DATAPOOL_T
105 void DataPool<VALUE, CLEAR>::reserve(unsigned int size)
106 {
107  m_handle.reserve (size);
108 }
109 
110 
111 DATAPOOL_T
112 void DataPool<VALUE, CLEAR>::prepareToAdd(unsigned int size)
113 {
114  if (this->capacity() - this->allocated() < size) {
115  this->reserve(this->allocated() + size);
116  }
117 }
118 
119 DATAPOOL_T
120 unsigned int DataPool<VALUE, CLEAR>::capacity()
121 {
122  return m_handle.stats().elts.total;
123 }
124 
125 DATAPOOL_T
126 unsigned int DataPool<VALUE, CLEAR>::allocated()
127 {
128  return m_handle.stats().elts.inuse;
129 }
130 
131 
132 //-----------------------------------------------------------
133 /// begin iterators over pool
134 DATAPOOL_T
135 typename DataPool<VALUE, CLEAR>::iterator DataPool<VALUE, CLEAR>::begin()
136 {
137  return iterator (m_handle.begin());
138 }
139 
140 DATAPOOL_T
141 typename 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 
150 DATAPOOL_T
151 typename DataPool<VALUE, CLEAR>::iterator DataPool<VALUE, CLEAR>::end()
152 {
153  return iterator (m_handle.end());
154 }
155 
156 DATAPOOL_T
157 typename DataPool<VALUE, CLEAR>::const_iterator DataPool<VALUE, CLEAR>::end() const {
158  return const_iterator (m_handle.end());
159 }
160 
161 //-----------------------------------------------------------
162 /// typename of pool
163 DATAPOOL_T
164 const 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
172 DATAPOOL_T
173 inline
174 typename DataPool<VALUE, CLEAR>::pointer DataPool<VALUE, CLEAR>::nextElementPtr()
175 {
176  return m_handle.allocate();
177 }
178 
179 
180 #undef DATAPOOL_T