ATLAS Offline Software
Loading...
Searching...
No Matches
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
14
15
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
27namespace SG {
28
29
134{
135public:
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
167
169
173
176
179
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 };
209
210 void clear();
212 Stats& operator+= (const Stats& other);
214 static void header (std::ostream& os);
215
222 };
223
224
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>
316 {
317
318 static_assert(
319 std::alignment_of<T>::value <= SG::ArenaBlockAlignDetail::alignment,
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
371private:
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
402std::ostream& operator<< (std::ostream& os,
404
405
411std::ostream& operator<< (std::ostream& os,
412 const ArenaAllocatorBase::Stats& stats);
413
414} // namespace SG
415
416
418
419
420#endif // not ATLALLOCATORS_ARENAALLOCATORBASE_H
A dummy pad struct to put at the end of the ArenaBlock header to ensure the alignment of the elements...
Common base class for arena allocator classes.
char * pointer
Type for pointers to elements.
void func_t(pointer)
Type of functions for constructor, etc.
virtual ~ArenaAllocatorBase()
Destructor.
virtual void reset()=0
Free all allocated elements.
virtual void reserve(size_t size)=0
Set the total number of elements cached by the allocator.
virtual const std::string & name() const =0
Return the name of this allocator.
static func_t * makeConstructor(const std::false_type &)
Make a constructor function pointer for a non-trivial constructor.
static func_t * makeDestructor(const std::true_type &)
Make a constructor function pointer for a trivial destructor.
static func_t * makeClear(const std::false_type &)
Make a function pointer for a clear function.
static void clear_fcn(pointer p)
Call T::clear on the object at p.
static void construct_fcn(pointer p)
Call T's default constructor on the object at p.
const char * const_pointer
And a const version of the pointer.
virtual void erase()=0
Free all allocated elements and release memory back to the system.
virtual void report(std::ostream &os) const
Generate a report on the memory usage of this allocator.
virtual Stats stats() const =0
Return the statistics block for this allocator.
static void destroy_fcn(pointer p)
Call T's destructor on the object at p.
static func_t * makeClear(const std::true_type &)
Make a dummy clear function pointer.
static func_t * makeConstructor(const std::true_type &)
Make a constructor function pointer for a trivial constructor.
static func_t * makeDestructor(const std::false_type &)
Make a constructor function pointer for a non-trivial destructor.
Forward declaration.
std::ostream & operator<<(std::ostream &os, const ArenaAllocatorBase::Stats::Stat &stat)
Format a statistic structure.
func_t * constructor
Constructor function for elements.
func_t * clear
Clear function for elements.
size_t eltSize
The size in bytes of the individual elements we're allocating.
size_t linkOffset
Offset from the start of a free element to a pointer to be used by the allocator.
size_t nblock
The number of elements we should allocate in a single block (hint only).
func_t * destructor
Destructor function for elements.
size_t minSize
The minimum size that this Allocator allows for an element.
bool mustClear
If true, the clear call cannot be skipped before destructor.
bool canReclear
If true, clear can be called more than once on a given element.
std::string name
The name of this allocator.
Stat & operator+=(const Stat &other)
Accumulate.
size_t free
Number of items currently not allocated by the application but cached by the allocator.
size_t inuse
Number of items currently allocated by the application.
size_t total
Total number of items held by the allocator.
Statistics for an allocator.
static void header(std::ostream &os)
Print report header.
Stats & operator+=(const Stats &other)
Accumulate.
initParams(size_t nblock=1000, const std::string &name="")
Constructor.
Params params() const
Return an initialized parameters structure.