47#ifndef ALEPH_TESTS_ALLOCATION_FAILURE_H
48#define ALEPH_TESTS_ALLOCATION_FAILURE_H
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
61#ifndef ALEPH_ALLOCATION_FAILURE_UNDER_TSAN
62# define ALEPH_ALLOCATION_FAILURE_UNDER_TSAN 0
67 bool fail_allocations =
false;
68 size_t refused_allocations = 0;
69 size_t allocations = 0;
77 refused_allocations = 0;
78 fail_allocations =
true;
81 ~No_Memory() { fail_allocations =
false; }
88 explicit Fail_At(
const size_t n)
noexcept
90 refused_allocations = 0;
91 fail_at = allocations + n;
94 ~Fail_At() { fail_at = 0; }
97#if !ALEPH_ALLOCATION_FAILURE_UNDER_TSAN
98 void * allocate(
const std::size_t size)
101 if (fail_allocations or allocations == fail_at)
103 ++refused_allocations;
104 throw std::bad_alloc();
106 if (
void * p = std::malloc(size == 0 ? 1 : size))
108 throw std::bad_alloc();
113#if !ALEPH_ALLOCATION_FAILURE_UNDER_TSAN
115void *
operator new(
const std::size_t size)
117 return allocate(size);
120void *
operator new[](
const std::size_t size)
122 return allocate(size);
127void *
operator new(
const std::size_t size,
const std::nothrow_t &)
noexcept
131 return allocate(size);
139void *
operator new[](
const std::size_t size,
const std::nothrow_t &)
noexcept
143 return allocate(size);
152#if defined(__GNUC__) && !defined(__clang__)
153# pragma GCC diagnostic push
154# pragma GCC diagnostic ignored "-Wmismatched-new-delete"
157void operator delete(
void * p)
noexcept
162void operator delete[](
void * p)
noexcept
167void operator delete(
void * p, std::size_t)
noexcept
172void operator delete[](
void * p, std::size_t)
noexcept
177void operator delete(
void * p,
const std::nothrow_t &)
noexcept
182void operator delete[](
void * p,
const std::nothrow_t &)
noexcept
187#if defined(__GNUC__) && !defined(__clang__)
188# pragma GCC diagnostic pop