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