2 Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
6 #ifdef LW_DEBUG_POOLS_DEBUG_USAGE
12 //____________________________________________________________________
13 class LWPools::PoolList {
14 //basically an array of pools which inits to null
16 std::atomic<LWPool*>& operator[](unsigned i) { return m_poolarr[i]; }
17 const LWPool * operator[](unsigned i) const { return m_poolarr[i].load(); }
20 #ifdef LW_DEBUG_POOLS_DEBUG_USAGE
21 std::map<char*,unsigned> m_memoryHandedOut;
26 std::atomic<LWPool*> m_poolarr[LWPoolSelector::numberOfPools];
29 //____________________________________________________________________
30 inline LWPool * LWPools::getPool(unsigned length)
33 unsigned idx = LWPoolSelector::poolIndex(length);
36 LWPool * pool = s_pools[idx];
37 return pool ? pool : initPool(idx,length);
40 //____________________________________________________________________
41 inline char * LWPools::acquire(unsigned length)
45 #ifdef LW_DEBUG_POOLS_USE_NEW
46 s_bytesDynAlloc += length;
49 LWPool * pool = getPool(length);
53 s_bytesDynAlloc += length;
58 #ifdef LW_DEBUG_POOLS_DEBUG_USAGE
59 std::scoped_lock lock (m_mutex);
60 std::map<char*,unsigned>::const_iterator it = s_pools.m_memoryHandedOut.find(c);
61 if (it!=s_pools.m_memoryHandedOut.end()) {
62 std::cout<<"LWPools::acquire("<<length<<") INTERNAL ERROR: Handing out the same address ("<<(void*)c<<") twice!!"<<std::endl;
65 s_pools.m_memoryHandedOut[c] = length;
72 //____________________________________________________________________
73 inline void LWPools::release(char*c,unsigned length)
75 #ifdef LW_DEBUG_POOLS_DEBUG_USAGE
76 std::scoped_lock lock (m_mutex);
78 std::cout<<"LWPools::release("<<(void*)c<<", "<<length<<")"<<std::endl;
79 std::map<char*,unsigned>::iterator it = s_pools.m_memoryHandedOut.find(c);
80 if (it==s_pools.m_memoryHandedOut.end()) {
81 std::cout<<"LWPools::release("<<(void*)c<<", "<<length<<") ERROR:"
82 <<" Releasing address which was never acquired"<<std::endl;
85 if (length!=it->second) {
86 std::cout<<"LWPools::release("<<(void*)c<<", "<<length<<") ERROR:"
87 <<" Releasing address of length "<<length
88 <<" which was previously acquired with length "<<it->second<<std::endl;
91 s_pools.m_memoryHandedOut.erase(it);
95 #ifdef LW_DEBUG_POOLS_USE_NEW
96 s_bytesDynAlloc -= length;
100 LWPool * pool = getPool(length);
104 s_bytesDynAlloc -= length;
109 template<class T, unsigned length>
110 inline T* LWPools::acquire()
112 T* t = reinterpret_cast<T*>(acquire<sizeof(T)*length>());
116 template<class T, unsigned length>
117 inline void LWPools::release(T*t)
119 release<sizeof(T)*length>(reinterpret_cast<char*>(t));
123 inline T * LWPools::acquire(unsigned length)
125 return reinterpret_cast<T*>(acquire(sizeof(T)*length));
129 inline void LWPools::release(T*t,unsigned length)
131 release( reinterpret_cast<char*>(t),sizeof(T)*length);
134 template<class T> inline void LWPools::deleteObject(T*t)
138 release<sizeof(T)>(reinterpret_cast<char*>(t));