-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_heap.hpp
65 lines (48 loc) · 1.82 KB
/
test_heap.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2023 Laurynas Biveinis
#ifndef UNODB_DETAIL_TEST_HEAP_HPP
#define UNODB_DETAIL_TEST_HEAP_HPP
#include "global.hpp" // IWYU pragma: keep
#ifndef NDEBUG
#include <atomic>
#include <cstdint>
#include <new>
namespace unodb::test {
class allocation_failure_injector final {
public:
UNODB_DETAIL_DISABLE_MSVC_WARNING(4514)
static void reset() noexcept {
fail_on_nth_allocation_.store(0, std::memory_order_relaxed);
allocation_counter.store(0, std::memory_order_release);
}
static void fail_on_nth_allocation(
std::uint64_t n UNODB_DETAIL_USED_IN_DEBUG) noexcept {
fail_on_nth_allocation_.store(n, std::memory_order_release);
}
UNODB_DETAIL_DISABLE_GCC_WARNING("-Wanalyzer-malloc-leak")
static void maybe_fail() {
const auto fail_counter =
fail_on_nth_allocation_.load(std::memory_order_acquire);
if (UNODB_DETAIL_UNLIKELY(fail_counter != 0) &&
(allocation_counter.fetch_add(1, std::memory_order_relaxed) >=
fail_counter - 1)) {
throw std::bad_alloc{};
}
}
UNODB_DETAIL_RESTORE_GCC_WARNINGS()
UNODB_DETAIL_RESTORE_MSVC_WARNINGS()
private:
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
inline static std::atomic<std::uint64_t> allocation_counter{0};
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
inline static std::atomic<std::uint64_t> fail_on_nth_allocation_{0};
};
} // namespace unodb::test
#define UNODB_DETAIL_RESET_ALLOCATION_FAILURE_INJECTOR() \
unodb::test::allocation_failure_injector::reset()
#define UNODB_DETAIL_FAIL_ON_NTH_ALLOCATION(n) \
unodb::test::allocation_failure_injector::fail_on_nth_allocation(n)
#else // !NDEBUG
#define UNODB_DETAIL_RESET_ALLOCATION_FAILURE_INJECTOR()
#define UNODB_DETAIL_FAIL_ON_NTH_ALLOCATION(n)
#endif // !NDEBUG
#endif // UNODB_DETAIL_TEST_HEAP_HPP