ATLAS Offline Software
Loading...
Searching...
No Matches
TestAlloc.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 * Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration.
4 */
11
12
13#ifndef TESTTOOLS_TESTALLOC_H
14#define TESTTOOLS_TESTALLOC_H
15
16
17namespace Athena_test {
18
19
27template <class T>
29{
30public:
32 typedef T* pointer;
33 typedef const T* const_pointer;
34 typedef T& reference;
35 typedef const T& const_reference;
36 typedef T value_type;
37 typedef size_t size_type;
38 typedef ptrdiff_t difference_type;
39
40 constexpr static size_t MAGIC = 0xdeadbeeffeedabba;
41
42 struct Head {
43 size_t i[2];
44 };
45 pointer allocate (size_type n, const void* = 0)
46 {
47 void* p = malloc (sizeof(Head) + n*sizeof(T));
48 if (!p) std::abort();
49 Head* h = (Head*)p;
50 h->i[0] = n;
51 h->i[1] = MAGIC;
52 return reinterpret_cast<pointer>(h+1);
53 }
54
56 {
57 Head* h = reinterpret_cast<Head*>(p);
58 --h;
59 if (h->i[0] != n ||
60 h->i[1] != MAGIC) {
61 std::abort();
62 }
63 free (h);
64 }
65
66 size_type max_size() const throw()
67 {
68 return 99999;
69 }
70
71 template <class... Args>
72 void construct (pointer p, Args&&... args)
73 {
74 // Suppress bogus warning seen with gcc 14.1.
75#if __GNUC__ >= 14
76# pragma GCC diagnostic push
77# pragma GCC diagnostic ignored "-Wstringop-overflow"
78#endif
79 new (p) T(std::forward<Args>(args)...);
80#if __GNUC__ >= 14
81# pragma GCC diagnostic pop
82#endif
83 }
84
86 {
87 p->~T();
88 }
89
91 {
92 return &x;
93 }
94
96 {
97 return &x;
98 }
99
100 // We have no state, so consider all instances equal.
101 bool operator== (const TestAlloc& /*other*/) const
102 { return true; }
103};
104
105
106} // namespace Athena_test
107
108
109#endif // not TESTTOOLS_TESTALLOC_H
#define x
Header file for AthHistogramAlgorithm.
To test handling of non-standard memory allocators.
Definition TestAlloc.h:29
const T & const_reference
Definition TestAlloc.h:35
const_pointer address(const_reference x) const
Definition TestAlloc.h:95
const T * const_pointer
Definition TestAlloc.h:33
pointer address(reference x) const
Definition TestAlloc.h:90
ptrdiff_t difference_type
Definition TestAlloc.h:38
size_type max_size() const
Definition TestAlloc.h:66
T * pointer
Standard STL allocator typedefs.
Definition TestAlloc.h:32
pointer allocate(size_type n, const void *=0)
Definition TestAlloc.h:45
void deallocate(pointer p, size_type n)
Definition TestAlloc.h:55
bool operator==(const TestAlloc &) const
Definition TestAlloc.h:101
void construct(pointer p, Args &&... args)
Definition TestAlloc.h:72
static constexpr size_t MAGIC
Definition TestAlloc.h:40
void destroy(pointer p)
Definition TestAlloc.h:85
functions & macros to test the difference between floats