Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
allocation_failure.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
47#ifndef ALEPH_TESTS_ALLOCATION_FAILURE_H
48#define ALEPH_TESTS_ALLOCATION_FAILURE_H
49
50#include <cstddef>
51#include <cstdlib>
52#include <new>
53
54#if defined(__SANITIZE_THREAD__)
55# define ALEPH_ALLOCATION_FAILURE_UNDER_TSAN 1
56#elif defined(__has_feature)
57# if __has_feature(thread_sanitizer)
58# define ALEPH_ALLOCATION_FAILURE_UNDER_TSAN 1
59# endif
60#endif
61#ifndef ALEPH_ALLOCATION_FAILURE_UNDER_TSAN
62# define ALEPH_ALLOCATION_FAILURE_UNDER_TSAN 0
63#endif
64
65namespace
66{
67 bool fail_allocations = false; // while true, operator new throws
68 size_t refused_allocations = 0;
69 size_t allocations = 0;
70 size_t fail_at = 0; // the number of the allocation that throws; 0 for none
71
72 // While it lives, every allocation through operator new fails
73 struct No_Memory
74 {
75 No_Memory() noexcept
76 {
77 refused_allocations = 0;
78 fail_allocations = true;
79 }
80
81 ~No_Memory() { fail_allocations = false; }
82 };
83
84 // While it lives, the allocation number n from now on, counted from 1,
85 // fails
86 struct Fail_At
87 {
88 explicit Fail_At(const size_t n) noexcept
89 {
90 refused_allocations = 0;
91 fail_at = allocations + n;
92 }
93
94 ~Fail_At() { fail_at = 0; }
95 };
96
97#if !ALEPH_ALLOCATION_FAILURE_UNDER_TSAN
98 void * allocate(const std::size_t size)
99 {
100 ++allocations;
101 if (fail_allocations or allocations == fail_at)
102 {
103 ++refused_allocations;
104 throw std::bad_alloc();
105 }
106 if (void * p = std::malloc(size == 0 ? 1 : size))
107 return p;
108 throw std::bad_alloc();
109 }
110#endif
111}
112
113#if !ALEPH_ALLOCATION_FAILURE_UNDER_TSAN
114
115void * operator new(const std::size_t size)
116{
117 return allocate(size);
118}
119
120void * operator new[](const std::size_t size)
121{
122 return allocate(size);
123}
124
125// GoogleTest allocates with these too; under AddressSanitizer, its own
126// versions would give the operator delete below memory it did not take
127void * operator new(const std::size_t size, const std::nothrow_t &) noexcept
128{
129 try
130 {
131 return allocate(size);
132 }
133 catch (...)
134 {
135 return nullptr;
136 }
137}
138
139void * operator new[](const std::size_t size, const std::nothrow_t &) noexcept
140{
141 try
142 {
143 return allocate(size);
144 }
145 catch (...)
146 {
147 return nullptr;
148 }
149}
150
151// They free what operator new took from malloc; GCC does not see that
152#if defined(__GNUC__) && !defined(__clang__)
153# pragma GCC diagnostic push
154# pragma GCC diagnostic ignored "-Wmismatched-new-delete"
155#endif
156
157void operator delete(void * p) noexcept
158{
159 std::free(p);
160}
161
162void operator delete[](void * p) noexcept
163{
164 std::free(p);
165}
166
167void operator delete(void * p, std::size_t) noexcept
168{
169 std::free(p);
170}
171
172void operator delete[](void * p, std::size_t) noexcept
173{
174 std::free(p);
175}
176
177void operator delete(void * p, const std::nothrow_t &) noexcept
178{
179 std::free(p);
180}
181
182void operator delete[](void * p, const std::nothrow_t &) noexcept
183{
184 std::free(p);
185}
186
187#if defined(__GNUC__) && !defined(__clang__)
188# pragma GCC diagnostic pop
189#endif
190
191#endif // !ALEPH_ALLOCATION_FAILURE_UNDER_TSAN
192
193#endif // ALEPH_TESTS_ALLOCATION_FAILURE_H