ATLAS Offline Software
ArenaBlockAllocatorBase.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 */
15 
16 
17 namespace SG {
18 
19 
26  : m_params (params),
27  m_blocks (nullptr),
28  m_freeblocks (nullptr),
29  m_stats(),
30  m_protected (false)
31 {
32 }
33 
34 
39 {
40  // Should be called by the derived class dtor.
41  // Can't do it from here since it may call reset(), which is
42  // pure virtual in this class.
43  //erase();
44 }
45 
46 
52  : m_params (other.m_params),
53  m_blocks (other.m_blocks),
54  m_freeblocks (other.m_freeblocks),
55  m_stats (other.m_stats),
56  m_protected (other.m_protected)
57 {
58  other.m_blocks = nullptr;
59  other.m_freeblocks = nullptr;
60  other.m_stats.clear();
61  other.m_protected = false;
62 }
63 
64 
69 ArenaBlockAllocatorBase::operator=
71 {
72  if (this != &other) {
73  erase();
74  m_params = other.m_params;
75  m_blocks = other.m_blocks;
76  m_freeblocks = other.m_freeblocks;
77  m_stats = other.m_stats;
78  m_protected = other.m_protected;
79  other.m_blocks = nullptr;
80  other.m_freeblocks = nullptr;
81  other.m_stats.clear();
82  other.m_protected = false;
83  }
84  return *this;
85 }
86 
87 
92 {
93  if (this != &other) {
94  std::swap (m_params, other.m_params);
95  std::swap (m_blocks, other.m_blocks);
96  std::swap (m_freeblocks, other.m_freeblocks);
97  std::swap (m_stats, other.m_stats);
98  std::swap (m_protected, other.m_protected);
99  }
100 }
101 
102 
121 {
122  if (m_protected) {
123  throw SG::ExcProtected();
124  }
125  if (size > m_stats.elts.total) {
126  // Growing the pool.
127  // Make a new block of the required size.
128  size_t sz = size - m_stats.elts.total;
131 
132  // Update statistics (others are derived in stats()).
133  ++m_stats.blocks.free;
134  ++m_stats.blocks.total;
135  m_stats.elts.total += newblock->size();
136 
137  // Add to the free list.
138  newblock->link() = m_freeblocks;
139  m_freeblocks = newblock;
140  }
141  else {
142  // Shrinking the pool.
143  // Loop while we can get rid of the first free block.
144  while (size < m_stats.elts.total &&
145  m_freeblocks &&
147  {
148  // Remove it from the free list.
151 
152  // Update statistics (others are derived in stats()).
153  m_stats.elts.total -= p->size();
154  --m_stats.blocks.free;
155  --m_stats.blocks.total;
156 
157  // Free the block.
159  }
160  }
161 }
162 
163 
173 {
174  if (m_protected) {
175  throw SG::ExcProtected();
176  }
178 }
179 
180 
187 {
188  // Do we need to run clear() on the allocated elements?
189  // If so, do so via reset().
190  if (m_params.mustClear && m_params.clear) {
191  reset();
192  }
193 
194  // Kill the block lists (both free and in use).
197  m_blocks = m_freeblocks = nullptr;
198 
199  // Reset statistics.
200  m_stats.clear();
201 }
202 
203 
208 {
209  // Calculate derived statistics.
210  Stats stats = m_stats;
218  return stats;
219 }
220 
221 
225 const std::string& ArenaBlockAllocatorBase::name() const
226 {
227  return m_params.name;
228 }
229 
230 
236 {
237  return m_params;
238 }
239 
240 
246 {
247  if (m_protected) {
248  throw SG::ExcProtected();
249  }
250 
251  ArenaBlock* newblock = m_freeblocks;
252  if (newblock) {
253  // There's something on the free list. Remove it and update statistics.
254  m_freeblocks = newblock->link();
255  --m_stats.blocks.free;
256  }
257  else {
258  // Otherwise, we need to make a new block.
261  m_stats.elts.total += newblock->size();
262  ++m_stats.blocks.total;
263  }
264  // Finish updating statistics.
265  // (Remaining ones are computed in stats().)
266  ++m_stats.blocks.inuse;
267 
268  // Link it into the in-use list and return.
269  newblock->link() = m_blocks;
270  m_blocks = newblock;
271  return newblock;
272 }
273 
274 
282 {
284  m_protected = true;
285 }
286 
287 
295 {
296  if (m_protected) {
298  m_protected = false;
299  }
300 }
301 
302 
303 } // namespace SG
SG::ArenaBlockAllocatorBase::params
const Params & params() const
Return this Allocator's parameters.
Definition: ArenaBlockAllocatorBase.cxx:235
SG::ArenaBlockAllocatorBase::reserve
virtual void reserve(size_t size) override
Set the total number of elements cached by the allocator.
Definition: ArenaBlockAllocatorBase.cxx:120
SG::ArenaBlockAllocatorBase::m_blocks
ArenaBlock * m_blocks
The list of blocks currently in use.
Definition: ArenaBlockAllocatorBase.h:161
SG::ArenaAllocatorBase::Params::constructor
func_t * constructor
Constructor function for elements.
Definition: ArenaAllocatorBase.h:170
SG::ArenaBlockAllocatorBase::~ArenaBlockAllocatorBase
virtual ~ArenaBlockAllocatorBase()
Destructor.
Definition: ArenaBlockAllocatorBase.cxx:38
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::destroy
static void destroy(ArenaBlock *p, func_t *dtor)
Destroy a block.
Definition: ArenaBlock.cxx:71
SG::ArenaAllocatorBase::Params::nblock
size_t nblock
The number of elements we should allocate in a single block (hint only).
Definition: ArenaAllocatorBase.h:162
SG::ArenaAllocatorBase::Params::mustClear
bool mustClear
If true, the clear call cannot be skipped before destructor.
Definition: ArenaAllocatorBase.h:181
SG::ArenaBlockAllocatorBase::m_params
Params m_params
The parameters for this allocator.
Definition: ArenaBlockAllocatorBase.h:158
SG::ArenaBlock::overhead
static size_t overhead()
Return the per-block memory overhead, in bytes.
Definition: ArenaBlock.cxx:159
SG::ArenaBlock::newBlock
static ArenaBlock * newBlock(size_t n, size_t elt_size, func_t *ctor)
Create a new block.
Definition: ArenaBlock.cxx:42
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
SG::ArenaBlockAllocatorBase::name
virtual const std::string & name() const override
Return the name of this allocator.
Definition: ArenaBlockAllocatorBase.cxx:225
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::ArenaAllocatorBase::Stats::Stat::inuse
size_t inuse
Number of items currently allocated by the application.
Definition: ArenaAllocatorBase.h:202
SG::ArenaAllocatorBase::Stats::blocks
Stat blocks
Counts of blocks.
Definition: ArenaAllocatorBase.h:217
SG::ArenaBlock::unprotectList
static void unprotectList(ArenaBlock *p)
Write-enable all blocks in a list.
Definition: ArenaBlock.cxx:220
SG::ArenaBlock::link
ArenaBlock *& link()
Return the link pointer of the block.
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
SG::ArenaBlockAllocatorBase::ArenaBlockAllocatorBase
ArenaBlockAllocatorBase(const Params &params)
Constructor.
Definition: ArenaBlockAllocatorBase.cxx:25
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:209
SG::ArenaAllocatorBase::Stats::clear
void clear()
Reset to zero.
Definition: ArenaAllocatorBase.cxx:77
SG::ArenaAllocatorBase::Stats
Statistics for an allocator.
Definition: ArenaAllocatorBase.h:188
SG::ArenaBlockAllocatorBase::stats
virtual Stats stats() const override
Return the statistics block for this allocator.
Definition: ArenaBlockAllocatorBase.cxx:207
SG::ArenaBlock::size
size_t size() const
Return the number of elements in the block.
ArenaBlockAllocatorBase.h
Common functionality for block-oriented allocators.
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::ArenaAllocatorBase::Params::name
std::string name
The name of this allocator.
Definition: ArenaAllocatorBase.h:152
SG::ArenaBlockAllocatorBase
Common functionality for block-oriented allocators.
Definition: ArenaBlockAllocatorBase.h:35
SG::ExcProtected
Exception — Attempt to change protected arena.
Definition: Control/AthAllocators/AthAllocators/exceptions.h:58
SG::ArenaAllocatorBase::Stats::bytes
Stat bytes
Counts of bytes.
Definition: ArenaAllocatorBase.h:221
SG::ArenaBlock
A large memory block that gets carved into smaller uniform elements.
Definition: ArenaBlock.h:43
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
SG::ArenaBlockAllocatorBase::m_stats
ArenaAllocatorBase::Stats m_stats
The statistics structure.
Definition: ArenaBlockAllocatorBase.h:167
SG::ArenaBlock::protectList
static void protectList(ArenaBlock *p)
Write-protect all blocks in a list.
Definition: ArenaBlock.cxx:205
SG::ArenaBlock::destroyList
static void destroyList(ArenaBlock *p, func_t *dtor)
Destroy all blocks in a list.
Definition: ArenaBlock.cxx:93
SG::ArenaBlockAllocatorBase::unprotect
void unprotect()
Write-enable the memory managed by this allocator.
Definition: ArenaBlockAllocatorBase.cxx:294
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::ArenaBlockAllocatorBase::protect
void protect()
Write-protect the memory managed by this allocator.
Definition: ArenaBlockAllocatorBase.cxx:281
PowhegControl_ttFCNC_NLO.params
params
Definition: PowhegControl_ttFCNC_NLO.py:226
SG::ArenaAllocatorBase::reset
virtual void reset()=0
Free all allocated elements.
exceptions.h
Exceptions that can be thrown from AthAllocators.
SG::ArenaAllocatorBase::Params
Allocator parameters.
Definition: ArenaAllocatorBase.h:150
ArenaBlock.h
A large memory block that gets carved into smaller uniform elements. See Arena.h for an overview of t...
SG::ArenaAllocatorBase::Params::destructor
func_t * destructor
Destructor function for elements.
Definition: ArenaAllocatorBase.h:172
SG::ArenaAllocatorBase::Stats::elts
Stat elts
Counts of elements.
Definition: ArenaAllocatorBase.h:219