ATLAS Offline Software
ArenaHeapAllocator.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3 */
14 #include <vector>
15 #include <algorithm>
16 #include <cassert>
17 
18 
19 namespace SG {
20 
21 
29  m_freeptr (nullptr)
30 {
31  // Consistency check.
32  assert (params.linkOffset + sizeof (pointer) <= params.eltSize);
33 }
34 
35 
40 {
41  erase();
42 }
43 
44 
50  : ArenaBlockAllocatorBase (std::move (other)),
51  m_freeptr (other.m_freeptr)
52 {
53  // False positives
54  // cppcheck-suppress useInitializationList
55  other.m_freeptr = nullptr;
56 }
57 
58 
63 ArenaHeapAllocator::operator=
65 {
66  if (this != &other) {
68  m_freeptr = other.m_freeptr;
69  other.m_freeptr = nullptr;
70  }
71  return *this;
72 }
73 
74 
79 {
80  if (this != &other) {
82  std::swap (m_freeptr, other.m_freeptr);
83  }
84 }
85 
86 
96 {
97  if (!m_blocks) return;
98  if (m_params.clear) {
99  if (m_params.canReclear || m_freeptr == nullptr) {
100  // Just call clear() on all blocks --- allocated or not.
102  }
103  else {
104  // We can only call clear() on allocated blocks.
105  slowClear();
106  }
107  }
108 
109  // Move all blocks back to the free list.
111 
112  // Reset state.
113  m_blocks = nullptr;
114  m_freeptr = nullptr;
115  m_stats.elts.inuse = 0;
117  m_stats.blocks.inuse = 0;
118 }
119 
120 
130 {
132  m_freeptr = nullptr;
133 }
134 
135 
140 {
141  // Get a new block.
142  ArenaBlock* newblock = getBlock();
143 
144  // Set up the links for the new free elements.
145  pointer lastelt = nullptr;
146  size_t sz = newblock->size();
147  size_t elt_size = newblock->eltSize();
148  for (size_t i=1; i < sz; i++) {
149  pointer elt = newblock->index (i, elt_size);
150  link(elt) = lastelt;
151  lastelt = elt;
152  }
153  // Set the free pointer to the next-to-last one.
154  m_freeptr = newblock->index (sz-1, elt_size);
155 
156  // And return the last one.
157  return newblock->index (0, elt_size);
158 }
159 
160 
165 {
166  // Make a list of all free elements, in sorted order.
167  std::vector<pointer> free_ptrs;
168  free_ptrs.reserve (m_stats.elts.total - m_stats.elts.inuse);
169  for (pointer p = m_freeptr; p; p = link(p)) {
170  free_ptrs.push_back (p);
171  }
172  std::sort (free_ptrs.begin(), free_ptrs.end());
173 
174  // Make a list of all used blocks, in sorted order.
175  std::vector<ArenaBlock*> blocks;
176  for (ArenaBlock* p = m_blocks; p; p = p->link()) {
177  blocks.push_back (p);
178  }
179  std::sort (blocks.begin(), blocks.end());
180 
181  // Walk through both of these lists.
182  // For each block, walk through its elements, and call @c clear
183  // for those not on the free list.
184  std::vector<pointer>::iterator pi = free_ptrs.begin();
185  std::vector<pointer>::iterator pi_end = free_ptrs.end();
186  std::vector<ArenaBlock*>::iterator bi = blocks.begin();
187  std::vector<ArenaBlock*>::iterator bi_end = blocks.end();
189  for (; bi != bi_end; ++bi) {
190  ArenaBlock& bl = **bi;
191  size_t sz = bl.size();
192  size_t elt_size = bl.eltSize();
193  for (size_t i = 0; i < sz; i++) {
194  pointer ptr = bl.index (i, elt_size);
195  if (pi != pi_end && ptr == *pi) {
196  ++pi;
197  }
198  else {
199  clear (ptr);
200  }
201  }
202  }
203 }
204 
205 
206 } // namespace SG
SG::ArenaBlockAllocatorBase::params
const Params & params() const
Return this Allocator's parameters.
Definition: ArenaBlockAllocatorBase.cxx:224
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:153
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:234
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
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:131
SG::ArenaBlockAllocatorBase::m_params
Params m_params
The parameters for this allocator.
Definition: ArenaBlockAllocatorBase.h:150
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
SG::ArenaAllocatorBase::Params::clear
func_t * clear
Clear function for elements.
Definition: ArenaAllocatorBase.h:175
SG::ArenaBlock::appendList
static void appendList(ArenaBlock **headp, ArenaBlock *tail)
Concatenate two lists of blocks.
Definition: ArenaBlock.cxx:110
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
SG::ArenaHeapAllocator::slowClear
void slowClear()
Call clear() for all allocated elements.
Definition: ArenaHeapAllocator.cxx:164
lumiFormat.i
int i
Definition: lumiFormat.py:92
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:129
SG::ArenaHeapAllocator::swap
void swap(ArenaHeapAllocator &other)
Swap.
Definition: ArenaHeapAllocator.cxx:78
SG::ArenaHeapAllocator::reset
virtual void reset() override
Free all allocated elements.
Definition: ArenaHeapAllocator.cxx:95
WriteCalibToCool.swap
swap
Definition: WriteCalibToCool.py:94
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:214
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:159
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::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:156
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:139
PowhegControl_ttFCNC_NLO.params
params
Definition: PowhegControl_ttFCNC_NLO.py:226
SG::ArenaHeapAllocator::ArenaHeapAllocator
ArenaHeapAllocator(const Params &params)
Constructor.
Definition: ArenaHeapAllocator.cxx:27
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:39