ATLAS Offline Software
ArenaHeapAllocator.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 */
15 #include <vector>
16 #include <algorithm>
17 #include <cassert>
18 
19 
20 namespace SG {
21 
22 
30  m_freeptr (nullptr)
31 {
32  // Consistency check.
33  assert (params.linkOffset + sizeof (pointer) <= params.eltSize);
34 }
35 
36 
41 {
42  if (m_protected) {
43  try {
44  unprotect();
45  }
46  catch (const SG::ExcProtection&) {
47  // Got an error from mprotect...
48  std::abort();
49  }
50  }
52 }
53 
54 
60  : ArenaBlockAllocatorBase (std::move (other)),
61  m_freeptr (other.m_freeptr)
62 {
63  // False positives
64  // cppcheck-suppress useInitializationList
65  other.m_freeptr = nullptr;
66 }
67 
68 
73 ArenaHeapAllocator::operator=
75 {
76  if (this != &other) {
78  m_freeptr = other.m_freeptr;
79  other.m_freeptr = nullptr;
80  }
81  return *this;
82 }
83 
84 
89 {
90  if (this != &other) {
92  std::swap (m_freeptr, other.m_freeptr);
93  }
94 }
95 
96 
106 {
107  if (!m_blocks) return;
108  if (m_params.clear) {
109  if (m_params.canReclear || m_freeptr == nullptr) {
110  // Just call clear() on all blocks --- allocated or not.
112  }
113  else {
114  // We can only call clear() on allocated blocks.
115  slowClear();
116  }
117  }
118 
119  // Move all blocks back to the free list.
121 
122  // Reset state.
123  m_blocks = nullptr;
124  m_freeptr = nullptr;
125  m_stats.elts.inuse = 0;
127  m_stats.blocks.inuse = 0;
128 }
129 
130 
140 {
142  m_freeptr = nullptr;
143 }
144 
145 
150 {
151  // Get a new block.
152  ArenaBlock* newblock = getBlock();
153 
154  // Set up the links for the new free elements.
155  pointer lastelt = nullptr;
156  size_t sz = newblock->size();
157  size_t elt_size = newblock->eltSize();
158  for (size_t i=1; i < sz; i++) {
159  pointer elt = newblock->index (i, elt_size);
160  link(elt) = lastelt;
161  lastelt = elt;
162  }
163  // Set the free pointer to the next-to-last one.
164  m_freeptr = newblock->index (sz-1, elt_size);
165 
166  // And return the last one.
167  return newblock->index (0, elt_size);
168 }
169 
170 
175 {
176  // Make a list of all free elements, in sorted order.
177  std::vector<pointer> free_ptrs;
178  free_ptrs.reserve (m_stats.elts.total - m_stats.elts.inuse);
179  for (pointer p = m_freeptr; p; p = link(p)) {
180  free_ptrs.push_back (p);
181  }
182  std::sort (free_ptrs.begin(), free_ptrs.end());
183 
184  // Make a list of all used blocks, in sorted order.
185  std::vector<ArenaBlock*> blocks;
186  for (ArenaBlock* p = m_blocks; p; p = p->link()) {
187  blocks.push_back (p);
188  }
189  std::sort (blocks.begin(), blocks.end());
190 
191  // Walk through both of these lists.
192  // For each block, walk through its elements, and call @c clear
193  // for those not on the free list.
194  std::vector<pointer>::iterator pi = free_ptrs.begin();
195  std::vector<pointer>::iterator pi_end = free_ptrs.end();
196  std::vector<ArenaBlock*>::iterator bi = blocks.begin();
197  std::vector<ArenaBlock*>::iterator bi_end = blocks.end();
199  for (; bi != bi_end; ++bi) {
200  ArenaBlock& bl = **bi;
201  size_t sz = bl.size();
202  size_t elt_size = bl.eltSize();
203  for (size_t i = 0; i < sz; i++) {
204  pointer ptr = bl.index (i, elt_size);
205  if (pi != pi_end && ptr == *pi) {
206  ++pi;
207  }
208  else {
209  clear (ptr);
210  }
211  }
212  }
213 }
214 
215 
216 } // namespace SG
SG::ArenaBlockAllocatorBase::params
const Params & params() const
Return this Allocator's parameters.
Definition: ArenaBlockAllocatorBase.cxx:235
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
SG::ArenaBlockAllocatorBase::m_blocks
ArenaBlock * m_blocks
The list of blocks currently in use.
Definition: ArenaBlockAllocatorBase.h:161
SG::ExcProtection
Exception — Attempt to change memory protection failed.
Definition: Control/AthAllocators/AthAllocators/exceptions.h:43
SG::ArenaBlock::index
pointer index(size_t i)
Return a pointer to element i in the block.
fitman.sz
sz
Definition: fitman.py:527
SG::ArenaBlockAllocatorBase::getBlock
ArenaBlock * getBlock()
Return an empty block, either newly-allocated or from the free list.
Definition: ArenaBlockAllocatorBase.cxx:245
SG
Forward declaration.
Definition: CaloCellPacker_400_500.h:32
SG::ArenaBlock::applyList
static void applyList(ArenaBlock *p, func_t *func, size_t n)
Call a function on elements in a list of blocks.
Definition: ArenaBlock.cxx:133
SG::ArenaBlockAllocatorBase::m_params
Params m_params
The parameters for this allocator.
Definition: ArenaBlockAllocatorBase.h:158
SG::ArenaHeapAllocator
Heap-based allocator.
Definition: ArenaHeapAllocator.h:64
SG::ArenaBlock::eltSize
size_t eltSize() const
Return the size of the elements in the block.
SG::ArenaBlockAllocatorBase::erase
virtual void erase() override
Free all allocated elements and release memory back to the system.
Definition: ArenaBlockAllocatorBase.cxx:172
SG::ArenaAllocatorBase::Stats::Stat::free
size_t free
Number of items currently not allocated by the application but cached by the allocator.
Definition: ArenaAllocatorBase.h:205
dbg::ptr
void * ptr(T *p)
Definition: SGImplSvc.cxx:74
SG::ArenaAllocatorBase::Params::clear
func_t * clear
Clear function for elements.
Definition: ArenaAllocatorBase.h:175
SG::ArenaBlockAllocatorBase::m_protected
bool m_protected
Flag whether the arena has been protected.
Definition: ArenaBlockAllocatorBase.h:170
SG::ArenaBlock::appendList
static void appendList(ArenaBlock **headp, ArenaBlock *tail)
Concatenate two lists of blocks.
Definition: ArenaBlock.cxx:112
SG::ArenaAllocatorBase::Stats::Stat::inuse
size_t inuse
Number of items currently allocated by the application.
Definition: ArenaAllocatorBase.h:202
pi
#define pi
Definition: TileMuonFitter.cxx:65
SG::ArenaAllocatorBase::Stats::blocks
Stat blocks
Counts of blocks.
Definition: ArenaAllocatorBase.h:217
SG::ArenaAllocatorBase::func_t
void func_t(pointer)
Type of functions for constructor, etc.
Definition: ArenaAllocatorBase.h:143
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:209
SG::ArenaHeapAllocator::slowClear
void slowClear()
Call clear() for all allocated elements.
Definition: ArenaHeapAllocator.cxx:174
lumiFormat.i
int i
Definition: lumiFormat.py:85
SG::ArenaBlock::size
size_t size() const
Return the number of elements in the block.
SG::ArenaHeapAllocator::erase
virtual void erase() override final
Free all allocated elements and release memory back to the system.
Definition: ArenaHeapAllocator.cxx:139
SG::ArenaHeapAllocator::swap
void swap(ArenaHeapAllocator &other)
Swap.
Definition: ArenaHeapAllocator.cxx:88
SG::ArenaHeapAllocator::reset
virtual void reset() override
Free all allocated elements.
Definition: ArenaHeapAllocator.cxx:105
WriteCalibToCool.swap
swap
Definition: WriteCalibToCool.py:94
SG::ArenaBlockAllocatorBase::eraseUnprotected
void eraseUnprotected()
Free all allocated elements and release memory back to the system.
Definition: ArenaBlockAllocatorBase.cxx:186
SG::ArenaBlockAllocatorBase
Common functionality for block-oriented allocators.
Definition: ArenaBlockAllocatorBase.h:35
SG::ArenaBlock
A large memory block that gets carved into smaller uniform elements.
Definition: ArenaBlock.h:43
SG::ArenaBlockAllocatorBase::operator=
ArenaBlockAllocatorBase & operator=(const ArenaBlockAllocatorBase &)=delete
SG::ArenaHeapAllocator::m_freeptr
pointer m_freeptr
Pointer to the next free element.
Definition: ArenaHeapAllocator.h:213
SG::ArenaAllocatorBase::Stats::Stat::total
size_t total
Total number of items held by the allocator.
Definition: ArenaAllocatorBase.h:207
InDetDD::other
@ other
Definition: InDetDD_Defs.h:16
VKalVrtAthena::varHolder_detail::clear
void clear(T &var)
Definition: NtupleVars.h:48
SG::ArenaBlockAllocatorBase::m_stats
ArenaAllocatorBase::Stats m_stats
The statistics structure.
Definition: ArenaBlockAllocatorBase.h:167
SG::ArenaAllocatorBase::Params::linkOffset
size_t linkOffset
Offset from the start of a free element to a pointer to be used by the allocator.
Definition: ArenaAllocatorBase.h:166
SG::ArenaBlockAllocatorBase::unprotect
void unprotect()
Write-enable the memory managed by this allocator.
Definition: ArenaBlockAllocatorBase.cxx:294
SG::ArenaAllocatorBase::Params::canReclear
bool canReclear
If true, clear can be called more than once on a given element.
Definition: ArenaAllocatorBase.h:178
SG::ArenaBlockAllocatorBase::swap
void swap(ArenaBlockAllocatorBase &other)
Swap.
Definition: ArenaBlockAllocatorBase.cxx:91
SG::ArenaBlockAllocatorBase::m_freeblocks
ArenaBlock * m_freeblocks
The list of free blocks.
Definition: ArenaBlockAllocatorBase.h:164
SG::ArenaAllocatorBase::Params::eltSize
size_t eltSize
The size in bytes of the individual elements we're allocating.
Definition: ArenaAllocatorBase.h:155
SG::ArenaAllocatorBase::pointer
char * pointer
Type for pointers to elements.
Definition: ArenaAllocatorBase.h:137
SG::ArenaHeapAllocator::refill
pointer refill()
Add more free elements to the pool, and allocate a new element.
Definition: ArenaHeapAllocator.cxx:149
PowhegControl_ttFCNC_NLO.params
params
Definition: PowhegControl_ttFCNC_NLO.py:226
exceptions.h
Exceptions that can be thrown from AthAllocators.
SG::ArenaHeapAllocator::ArenaHeapAllocator
ArenaHeapAllocator(const Params &params)
Constructor.
Definition: ArenaHeapAllocator.cxx:28
SG::ArenaAllocatorBase::Params
Allocator parameters.
Definition: ArenaAllocatorBase.h:150
SG::ArenaHeapAllocator::link
pointer & link(pointer p) const
Return a reference to the link for an element.
ArenaHeapAllocator.h
Heap-based allocator. See Arena.h for an overview of the arena-based memory allocators.
ArenaBlock.h
A large memory block that gets carved into smaller uniform elements. See Arena.h for an overview of t...
SG::ArenaAllocatorBase::Stats::elts
Stat elts
Counts of elements.
Definition: ArenaAllocatorBase.h:219
SG::ArenaHeapAllocator::~ArenaHeapAllocator
virtual ~ArenaHeapAllocator()
Destructor.
Definition: ArenaHeapAllocator.cxx:40