2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
5//-----------------------------------------------------------
6// .icc file for DataPool
7//-----------------------------------------------------------
10#include "GaudiKernel/System.h"
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
22 // Generic version --- a function that will call CLEAR::operator(),
23 // casting the argument appropriately.
24 static void callClear (SG::ArenaAllocatorBase::pointer p)
26 CLEAR::clear (reinterpret_cast<VALUE*> (p));
28 static SG::ArenaAllocatorBase::func_t* addr() { return callClear; }
31class DataPoolClearAddr<VALUE, SG::DataPoolNullClear<VALUE> >
34 // Specialized version of the default DataPoolNullClear.
35 // Just return nullptr.
36 static SG::ArenaAllocatorBase::func_t* addr() { return nullptr; }
43#define DATAPOOL_T template <typename VALUE, typename CLEAR>
45const typename DataPool<VALUE, CLEAR>::alloc_t::Params DataPool<VALUE, CLEAR>::s_params
46 = DataPool<VALUE, CLEAR>::initParams();
50typename DataPool<VALUE, CLEAR>::alloc_t::Params DataPool<VALUE, CLEAR>::initParams()
52 typename alloc_t::Params params = alloc_t::initParams<VALUE> (s_minRefCount);
53 params.clear = SG::DataPoolClearAddr<VALUE, CLEAR>::addr();
58//-----------------------------------------------------------
61DataPool<VALUE, CLEAR>::DataPool(size_type n /*= 0*/)
62 : m_handle (&s_params)
65 m_handle.reserve (std::max (n, s_minRefCount));
69DataPool<VALUE, CLEAR>::DataPool(const EventContext& ctx,
71 : m_handle (static_cast<SG::ArenaHeader*>(nullptr), ctx, &s_params)
74 m_handle.reserve (std::max (n, s_minRefCount));
78DataPool<VALUE, CLEAR>::DataPool(SG::Arena* arena,
80 : m_handle (arena, &s_params)
83 m_handle.reserve (std::max (n, s_minRefCount));
86//-----------------------------------------------------------
87/// release all elements in the pool.
89void DataPool<VALUE, CLEAR>::reset()
94/// free all memory in the pool.
96void DataPool<VALUE, CLEAR>::erase()
100//-----------------------------------------------------------
101// reserve space for the pool
102// allocated elements will not be deleted.
105void DataPool<VALUE, CLEAR>::reserve(unsigned int size)
107 m_handle.reserve (size);
112void DataPool<VALUE, CLEAR>::prepareToAdd(unsigned int size)
114 if (this->capacity() - this->allocated() < size) {
115 this->reserve(this->allocated() + size);
120unsigned int DataPool<VALUE, CLEAR>::capacity()
122 return m_handle.stats().elts.total;
126unsigned int DataPool<VALUE, CLEAR>::allocated()
128 return m_handle.stats().elts.inuse;
132//-----------------------------------------------------------
133/// begin iterators over pool
135typename DataPool<VALUE, CLEAR>::iterator DataPool<VALUE, CLEAR>::begin()
137 return iterator (m_handle.begin());
141typename DataPool<VALUE, CLEAR>::const_iterator DataPool<VALUE, CLEAR>::begin() const
143 return const_Iterator (m_handle.begin());
146//-----------------------------------------------------------
147/// the end() method will allow looping over only valid elements
148/// and not over ALL elements of the pool
151typename DataPool<VALUE, CLEAR>::iterator DataPool<VALUE, CLEAR>::end()
153 return iterator (m_handle.end());
157typename DataPool<VALUE, CLEAR>::const_iterator DataPool<VALUE, CLEAR>::end() const {
158 return const_iterator (m_handle.end());
161//-----------------------------------------------------------
164const std::string& DataPool<VALUE, CLEAR>::typeName() {
165 static std::string name = System::typeinfoName (typeid (VALUE));
169//-----------------------------------------------------------
170/// obtain the next available element in pool by pointer
171/// pool is resized if reached its limit
174typename DataPool<VALUE, CLEAR>::pointer DataPool<VALUE, CLEAR>::nextElementPtr()
176 return m_handle.allocate();