ATLAS Offline Software
DataPool.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 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 #define DATAPOOL_T template <typename VALUE, DataPoolClearFuncPtr_t<VALUE> clear>
14 DATAPOOL_T
15 const typename DataPool<VALUE, clear>::alloc_t::Params DataPool<VALUE, clear>::s_params
16  = DataPool<VALUE, clear>::initParams();
17 
18 
19 #if __GNUC__ >= 12
20 # pragma GCC diagnostic push
21 # pragma GCC diagnostic ignored "-Waddress"
22 #endif
23 DATAPOOL_T
24 typename DataPool<VALUE, clear>::alloc_t::Params DataPool<VALUE, clear>::initParams()
25 {
26  typename alloc_t::Params params = alloc_t::initParams<VALUE> (s_minRefCount);
27  if (clear != nullptr) {
28  params.clear = callClear;
29  }
30  return params;
31 }
32 #if __GNUC__ >= 12
33 # pragma GCC diagnostic pop
34 #endif
35 
36 
37 DATAPOOL_T
38 void DataPool<VALUE, clear>::callClear (SG::ArenaAllocatorBase::pointer p)
39 {
40  clear (reinterpret_cast<VALUE*> (p));
41 }
42 
43 
44 //-----------------------------------------------------------
45 
46 DATAPOOL_T
47 DataPool<VALUE, clear>::DataPool(size_type n /*= 0*/)
48  : m_handle (&s_params)
49 {
50  if (n > 0)
51  m_handle.reserve (std::max (n, s_minRefCount));
52 }
53 
54 DATAPOOL_T
55 DataPool<VALUE, clear>::DataPool(const EventContext& ctx,
56  size_type n /*= 0*/)
57  : m_handle (static_cast<SG::ArenaHeader*>(nullptr), ctx, &s_params)
58 {
59  if (n > 0)
60  m_handle.reserve (std::max (n, s_minRefCount));
61 }
62 
63 DATAPOOL_T
64 DataPool<VALUE, clear>::DataPool(SG::Arena* arena,
65  size_type n /*= 0*/)
66  : m_handle (arena, &s_params)
67 {
68  if (n > 0)
69  m_handle.reserve (std::max (n, s_minRefCount));
70 }
71 
72 //-----------------------------------------------------------
73 /// release all elements in the pool.
74 DATAPOOL_T
75 void DataPool<VALUE, clear>::reset()
76 {
77  m_handle.reset();
78 }
79 
80 /// free all memory in the pool.
81 DATAPOOL_T
82 void DataPool<VALUE, clear>::erase()
83 {
84  m_handle.erase();
85 }
86 //-----------------------------------------------------------
87 // reserve space for the pool
88 // allocated elements will not be deleted.
89 
90 DATAPOOL_T
91 void DataPool<VALUE, clear>::reserve(unsigned int size)
92 {
93  m_handle.reserve (size);
94 }
95 
96 
97 DATAPOOL_T
98 void DataPool<VALUE, clear>::prepareToAdd(unsigned int size)
99 {
100  if (this->capacity() - this->allocated() < size) {
101  this->reserve(this->allocated() + size);
102  }
103 }
104 
105 DATAPOOL_T
106 unsigned int DataPool<VALUE, clear>::capacity()
107 {
108  return m_handle.stats().elts.total;
109 }
110 
111 DATAPOOL_T
112 unsigned int DataPool<VALUE, clear>::allocated()
113 {
114  return m_handle.stats().elts.inuse;
115 }
116 
117 
118 //-----------------------------------------------------------
119 /// begin iterators over pool
120 DATAPOOL_T
121 typename DataPool<VALUE, clear>::iterator DataPool<VALUE, clear>::begin()
122 {
123  return iterator (m_handle.begin());
124 }
125 
126 DATAPOOL_T
127 typename DataPool<VALUE, clear>::const_iterator DataPool<VALUE, clear>::begin() const
128 {
129  return const_Iterator (m_handle.begin());
130 }
131 
132 //-----------------------------------------------------------
133 /// the end() method will allow looping over only valid elements
134 /// and not over ALL elements of the pool
135 
136 DATAPOOL_T
137 typename DataPool<VALUE, clear>::iterator DataPool<VALUE, clear>::end()
138 {
139  return iterator (m_handle.end());
140 }
141 
142 DATAPOOL_T
143 typename DataPool<VALUE, clear>::const_iterator DataPool<VALUE, clear>::end() const {
144  return const_iterator (m_handle.end());
145 }
146 
147 //-----------------------------------------------------------
148 /// typename of pool
149 DATAPOOL_T
150 const std::string& DataPool<VALUE, clear>::typeName() {
151  static std::string name = System::typeinfoName (typeid (VALUE));
152  return name;
153 }
154 
155 //-----------------------------------------------------------
156 /// obtain the next available element in pool by pointer
157 /// pool is resized if reached its limit
158 DATAPOOL_T
159 inline
160 typename DataPool<VALUE, clear>::pointer DataPool<VALUE, clear>::nextElementPtr()
161 {
162  return m_handle.allocate();
163 }
164 
165 
166 #undef DATAPOOL_T