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-2026 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>
25#include <format>
27
28namespace SG {
29
30
135{
136public:
138 typedef char* pointer;
139
141 typedef const char* const_pointer;
142
144 typedef void func_t (pointer);
145
146
150 struct Params
151 {
153 std::string name;
154
156 size_t eltSize = 0;
157
159 size_t minSize = 0;
160
163 size_t nblock = 0;
164
167 size_t linkOffset = 0;
168
170
171 func_t* constructor = nullptr;
173 func_t* destructor = nullptr;
174
176 func_t* clear = nullptr;
177
179 bool canReclear = false;
180
182 bool mustClear = false;
183 };
184
185
189 struct Stats {
190 // Strictly unneeded, but without it we get a cppcheck false positive.
191 Stats() = default;
192
194 struct Stat {
196 Stat();
198 void clear();
200 Stat& operator+= (const Stat& other);
201
203 size_t inuse;
206 size_t free;
208 size_t total;
209 };
210
211 void clear();
213 Stats& operator+= (const Stats& other);
215 static void header (std::ostream& os);
216
223 };
224
225
230
231
240 virtual void reset() = 0;
241
242
251 virtual void erase() = 0;
252
253
271 virtual void reserve (size_t size) = 0;
272
273
277 virtual Stats stats() const = 0;
278
279
283 virtual const std::string& name() const = 0;
284
285
290 virtual void report (std::ostream& os) const;
291
292
293 //=========================================================================
312 template <typename T,
313 bool clear = false,
314 bool no_ctor = false,
315 bool no_dtor = false>
317 {
318
319 static_assert(
320 std::alignment_of<T>::value <= SG::ArenaBlockAlignDetail::alignment,
321 "Requested Alignment larger than the one that can be provided");
329 initParams (size_t nblock = 1000, const std::string& name = "");
330
332 Params params() const;
333
335 // Note: gcc 3.2.3 doesn't allow defining this out-of-line.
336 operator Params() const { return params(); }
337
338
339 private:
341 size_t m_nblock;
342
344 std::string m_name;
345 };
346
347
349 template <class T>
350 static func_t* makeConstructor (const std::false_type&);
351
353 template <class T>
354 static func_t* makeConstructor (const std::true_type&);
355
357 template <class T>
358 static func_t* makeDestructor (const std::false_type&);
359
361 template <class T>
362 static func_t* makeDestructor (const std::true_type&);
363
365 template <class T>
366 static func_t* makeClear (const std::false_type&);
367
369 template <class T>
370 static func_t* makeClear (const std::true_type&);
371
372private:
377 template <typename T>
378 static void construct_fcn (pointer p);
379
380
385 template <typename T>
386 static void destroy_fcn (pointer p);
387
388
393 template <typename T>
394 static void clear_fcn (pointer p);
395};
396
397
403std::ostream& operator<< (std::ostream& os,
405
406
412std::ostream& operator<< (std::ostream& os,
413 const ArenaAllocatorBase::Stats& stats);
414
415} // namespace SG
416
417
419namespace std {
420template <>
421struct formatter<SG::ArenaAllocatorBase::Stats::Stat>
422 : public formatter<string_view>
423{
424 template <class FmtContext>
425 FmtContext::iterator format(const SG::ArenaAllocatorBase::Stats::Stat& stat, FmtContext& ctx) const;
426};
427
428template <>
429struct formatter<SG::ArenaAllocatorBase::Stats>
430 : public formatter<string_view>
431{
432 template <class FmtContext>
433 FmtContext::iterator format(const SG::ArenaAllocatorBase::Stats& stats, FmtContext& ctx) const;
434};
435} // namespace std
436
437
439
440
441#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...
void clear()
Empty the pool.
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.
virtual size_t size() const override
Return the number of elements in the store.
std::ostream & operator<<(std::ostream &os, const ArenaAllocatorBase::Stats::Stat &stat)
Format a statistic structure.
STL namespace.
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.
FmtContext::iterator format(const SG::ArenaAllocatorBase::Stats &stats, FmtContext &ctx) const
C++20 style formatters.
FmtContext::iterator format(const SG::ArenaAllocatorBase::Stats::Stat &stat, FmtContext &ctx) const
C++20 style formatters.