ATLAS Offline Software
ArenaAllocatorBase.h
Go to the documentation of this file.
1 // This file's extension implies that it's C, but it's really -*- C++ -*-.
2 
3 /*
4  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
5 */
6 
16 #ifndef ATLALLOCATORS_ARENAALLOCATORBASE_H
17 #define ATLALLOCATORS_ARENAALLOCATORBASE_H
18 
19 
20 #include <cstdlib>
21 #include <new>
22 #include <string>
23 #include <iosfwd>
24 #include <type_traits>
26 
27 namespace SG {
28 
29 
134 {
135 public:
137  typedef char* pointer;
138 
140  typedef const char* const_pointer;
141 
143  typedef void func_t (pointer);
144 
145 
149  struct Params
150  {
152  std::string name;
153 
155  size_t eltSize;
156 
158  size_t minSize;
159 
162  size_t nblock;
163 
166  size_t linkOffset;
167 
169 
173 
176 
179 
181  bool mustClear;
182  };
183 
184 
188  struct Stats {
189  // Strictly unneeded, but without it we get a cppcheck false positive.
190  Stats() = default;
191 
193  struct Stat {
195  Stat();
197  void clear();
199  Stat& operator+= (const Stat& other);
200 
202  size_t inuse;
205  size_t free;
207  size_t total;
208  };
210  void clear();
212  Stats& operator+= (const Stats& other);
214  static void header (std::ostream& os);
215 
222  };
223 
224 
228  virtual ~ArenaAllocatorBase() {}
229 
230 
239  virtual void reset() = 0;
240 
241 
250  virtual void erase() = 0;
251 
252 
270  virtual void reserve (size_t size) = 0;
271 
272 
276  virtual Stats stats() const = 0;
277 
278 
282  virtual const std::string& name() const = 0;
283 
284 
289  virtual void report (std::ostream& os) const;
290 
291 
292  //=========================================================================
311  template <typename T,
312  bool clear = false,
313  bool no_ctor = false,
314  bool no_dtor = false>
315  struct initParams
316  {
317 
318  static_assert(
320  "Requested Alignment larger than the one that can be provided");
328  initParams (size_t nblock = 1000, const std::string& name = "");
329 
331  Params params() const;
332 
334  // Note: gcc 3.2.3 doesn't allow defining this out-of-line.
335  operator Params() const { return params(); }
336 
337 
338  private:
340  size_t m_nblock;
341 
343  std::string m_name;
344  };
345 
346 
348  template <class T>
349  static func_t* makeConstructor (const std::false_type&);
350 
352  template <class T>
353  static func_t* makeConstructor (const std::true_type&);
354 
356  template <class T>
357  static func_t* makeDestructor (const std::false_type&);
358 
360  template <class T>
361  static func_t* makeDestructor (const std::true_type&);
362 
364  template <class T>
365  static func_t* makeClear (const std::false_type&);
366 
368  template <class T>
369  static func_t* makeClear (const std::true_type&);
370 
371 private:
376  template <typename T>
377  static void construct_fcn (pointer p);
378 
379 
384  template <typename T>
385  static void destroy_fcn (pointer p);
386 
387 
392  template <typename T>
393  static void clear_fcn (pointer p);
394 };
395 
396 
402 std::ostream& operator<< (std::ostream& os,
404 
405 
411 std::ostream& operator<< (std::ostream& os,
413 
414 } // namespace SG
415 
416 
418 
419 
420 #endif // not ATLALLOCATORS_ARENAALLOCATORBASE_H
SG::ArenaAllocatorBase::Params::constructor
func_t * constructor
Constructor function for elements.
Definition: ArenaAllocatorBase.h:170
SG::ArenaAllocatorBase::stats
virtual Stats stats() const =0
Return the statistics block for this allocator.
SG::ArenaAllocatorBase::initParams::initParams
initParams(size_t nblock=1000, const std::string &name="")
Constructor.
SG
Forward declaration.
Definition: CaloCellPacker_400_500.h:32
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::initParams::m_nblock
size_t m_nblock
Saved value of the number of elements to allocate per block.
Definition: ArenaAllocatorBase.h:340
SG::ArenaAllocatorBase::Params::mustClear
bool mustClear
If true, the clear call cannot be skipped before destructor.
Definition: ArenaAllocatorBase.h:181
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
athena.value
value
Definition: athena.py:124
SG::ArenaAllocatorBase::reserve
virtual void reserve(size_t size)=0
Set the total number of elements cached by the allocator.
SG::ArenaAllocatorBase::Params::clear
func_t * clear
Clear function for elements.
Definition: ArenaAllocatorBase.h:175
trigbs_dumpHLTContentInBS.stats
stats
Definition: trigbs_dumpHLTContentInBS.py:91
SG::ArenaAllocatorBase::erase
virtual void erase()=0
Free all allocated elements and release memory back to the system.
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
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
SG::ArenaAllocatorBase::Stats::Stat::clear
void clear()
Reset to zero.
Definition: ArenaAllocatorBase.cxx:36
SG::ArenaAllocatorBase::Stats::Stat
A single statistic.
Definition: ArenaAllocatorBase.h:193
SG::ArenaAllocatorBase::func_t
void func_t(pointer)
Type of functions for constructor, etc.
Definition: ArenaAllocatorBase.h:143
SG::ArenaAllocatorBase::makeConstructor
static func_t * makeConstructor(const std::true_type &)
Make a constructor function pointer for a trivial constructor.
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
SG::ArenaAllocatorBase::Stats::clear
void clear()
Reset to zero.
Definition: ArenaAllocatorBase.cxx:77
SG::ArenaAllocatorBase::initParams::m_name
std::string m_name
Saved value of the allocator name.
Definition: ArenaAllocatorBase.h:343
SG::ArenaAllocatorBase::Stats
Statistics for an allocator.
Definition: ArenaAllocatorBase.h:188
SG::ArenaAllocatorBase::Stats::Stat::operator+=
Stat & operator+=(const Stat &other)
Accumulate.
Definition: ArenaAllocatorBase.cxx:47
SG::ArenaAllocatorBase
Common base class for arena allocator classes.
Definition: ArenaAllocatorBase.h:134
SG::ArenaAllocatorBase::makeDestructor
static func_t * makeDestructor(const std::true_type &)
Make a constructor function pointer for a trivial destructor.
SG::ArenaAllocatorBase::construct_fcn
static void construct_fcn(pointer p)
Call T's default constructor on the object at p.
SG::ArenaAllocatorBase::report
virtual void report(std::ostream &os) const
Generate a report on the memory usage of this allocator.
Definition: ArenaAllocatorBase.cxx:130
SG::ArenaBlockAlignDetail::alignment
constexpr size_t alignment
Definition: ArenaBlockAlignDetail.h:39
SG::ArenaAllocatorBase::destroy_fcn
static void destroy_fcn(pointer p)
Call T's destructor on the object at p.
SG::ArenaAllocatorBase::makeClear
static func_t * makeClear(const std::true_type &)
Make a dummy clear function pointer.
beamspotman.stat
stat
Definition: beamspotman.py:266
SG::ArenaAllocatorBase::Params::name
std::string name
The name of this allocator.
Definition: ArenaAllocatorBase.h:152
ReadFromCoolCompare.os
os
Definition: ReadFromCoolCompare.py:231
SG::ArenaAllocatorBase::Stats::bytes
Stat bytes
Counts of bytes.
Definition: ArenaAllocatorBase.h:221
SG::ArenaAllocatorBase::clear_fcn
static void clear_fcn(pointer p)
Call T::clear on the object at p.
ArenaAllocatorBase.icc
SG::ArenaAllocatorBase::Stats::Stat::Stat
Stat()
Default constructor.
Definition: ArenaAllocatorBase.cxx:25
SG::ArenaAllocatorBase::Stats::Stat::total
size_t total
Total number of items held by the allocator.
Definition: ArenaAllocatorBase.h:207
SG::ArenaAllocatorBase::const_pointer
const char * const_pointer
And a const version of the pointer.
Definition: ArenaAllocatorBase.h:140
InDetDD::other
@ other
Definition: InDetDD_Defs.h:16
VKalVrtAthena::varHolder_detail::clear
void clear(T &var)
Definition: NtupleVars.h:48
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::initParams::params
Params params() const
Return an initialized parameters structure.
SG::ArenaAllocatorBase::makeDestructor
static func_t * makeDestructor(const std::false_type &)
Make a constructor function pointer for a non-trivial destructor.
SG::ArenaAllocatorBase::makeConstructor
static func_t * makeConstructor(const std::false_type &)
Make a constructor function pointer for a non-trivial constructor.
SG::ArenaAllocatorBase::Stats::header
static void header(std::ostream &os)
Print report header.
Definition: ArenaAllocatorBase.cxx:116
SG::ArenaAllocatorBase::~ArenaAllocatorBase
virtual ~ArenaAllocatorBase()
Destructor.
Definition: ArenaAllocatorBase.h:228
SG::ArenaAllocatorBase::Params::canReclear
bool canReclear
If true, clear can be called more than once on a given element.
Definition: ArenaAllocatorBase.h:178
SG::ArenaAllocatorBase::Params::eltSize
size_t eltSize
The size in bytes of the individual elements we're allocating.
Definition: ArenaAllocatorBase.h:155
SG::ArenaAllocatorBase::initParams
Helper to initialize a parameters structure.
Definition: ArenaAllocatorBase.h:316
SG::ArenaAllocatorBase::pointer
char * pointer
Type for pointers to elements.
Definition: ArenaAllocatorBase.h:137
ArenaBlockAlignDetail.h
A dummy pad struct to put at the end of the ArenaBlock header to ensure the alignment of the elements...
SG::ArenaAllocatorBase::reset
virtual void reset()=0
Free all allocated elements.
SG::ArenaAllocatorBase::Params::minSize
size_t minSize
The minimum size that this Allocator allows for an element.
Definition: ArenaAllocatorBase.h:158
SG::ArenaAllocatorBase::Params
Allocator parameters.
Definition: ArenaAllocatorBase.h:150
SG::ArenaAllocatorBase::makeClear
static func_t * makeClear(const std::false_type &)
Make a function pointer for a clear function.
SG::operator<<
std::ostream & operator<<(std::ostream &os, const ArenaAllocatorBase::Stats::Stat &stat)
Format a statistic structure.
Definition: ArenaAllocatorBase.cxx:61
SG::ArenaAllocatorBase::Stats::operator+=
Stats & operator+=(const Stats &other)
Accumulate.
Definition: ArenaAllocatorBase.cxx:90
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
SG::ArenaAllocatorBase::name
virtual const std::string & name() const =0
Return the name of this allocator.
SG::ArenaAllocatorBase::Stats::Stats
Stats()=default