| 1 |
// |
| 2 |
// Copyright (C) 2007-2025 Free Software Foundation, Inc. |
| 3 |
// |
| 4 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 5 |
// software; you can redistribute it and/or modify it under the |
| 6 |
// terms of the GNU General Public License as published by the |
| 7 |
// Free Software Foundation; either version 3, or (at your option) |
| 8 |
// any later version. |
| 9 |
// |
| 10 |
// This library is distributed in the hope that it will be useful, |
| 11 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
// GNU General Public License for more details. |
| 14 |
// |
| 15 |
// You should have received a copy of the GNU General Public License along |
| 16 |
// with this library; see the file COPYING3. If not see |
| 17 |
// <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
#include <exception> |
| 20 |
#include <stdexcept> |
| 21 |
#include <cstdlib> |
| 22 |
#include <cstdio> |
| 23 |
#include <testsuite_hooks.h> |
| 24 |
|
| 25 |
namespace __gnu_test |
| 26 |
{ |
| 27 |
struct counter_error : public std::exception { }; |
| 28 |
|
| 29 |
struct counter |
| 30 |
{ |
| 31 |
std::size_t _M_count; |
| 32 |
std::size_t _M_increments, _M_decrements; |
| 33 |
bool _M_throw; |
| 34 |
|
| 35 |
counter() : _M_count(0), _M_throw(true) { } |
| 36 |
|
| 37 |
~counter() THROW (counter_error) |
| 38 |
{ |
| 39 |
if (_M_throw && _M_count != 0) |
| 40 |
throw counter_error(); |
| 41 |
} |
| 42 |
|
| 43 |
static void |
| 44 |
increment() |
| 45 |
{ |
| 46 |
counter& cntr = get(); |
| 47 |
cntr._M_count++; |
| 48 |
cntr._M_increments++; |
| 49 |
} |
| 50 |
|
| 51 |
static void |
| 52 |
decrement() |
| 53 |
{ |
| 54 |
counter& cntr = get(); |
| 55 |
cntr._M_count--; |
| 56 |
cntr._M_decrements++; |
| 57 |
} |
| 58 |
|
| 59 |
static counter& |
| 60 |
get() |
| 61 |
{ |
| 62 |
static counter g; |
| 63 |
return g; |
| 64 |
} |
| 65 |
|
| 66 |
static std::size_t |
| 67 |
count() { return get()._M_count; } |
| 68 |
|
| 69 |
static void |
| 70 |
exceptions(bool __b) { get()._M_throw = __b; } |
| 71 |
|
| 72 |
static void |
| 73 |
reset() |
| 74 |
{ |
| 75 |
counter& cntr = get(); |
| 76 |
cntr._M_increments = cntr._M_decrements = 0; |
| 77 |
} |
| 78 |
|
| 79 |
struct scope |
| 80 |
{ |
| 81 |
scope() : _M_count(counter::count()) |
| 82 |
{ counter::get()._M_count = 0; } |
| 83 |
~scope() |
| 84 |
{ counter::get()._M_count = _M_count; } |
| 85 |
|
| 86 |
private: |
| 87 |
std::size_t _M_count; |
| 88 |
|
| 89 |
#if __cplusplus >= 201103L |
| 90 |
scope(const scope&) = delete; |
| 91 |
scope& operator=(const scope&) = delete; |
| 92 |
#else |
| 93 |
scope(const scope&); |
| 94 |
scope& operator=(const scope&); |
| 95 |
#endif |
| 96 |
}; |
| 97 |
}; |
| 98 |
|
| 99 |
template<typename Alloc, bool uses_global_new> |
| 100 |
bool |
| 101 |
check_new(Alloc a = Alloc()) |
| 102 |
{ |
| 103 |
__gnu_test::counter::scope s; |
| 104 |
__gnu_test::counter::exceptions(false); |
| 105 |
(void) a.allocate(10); |
| 106 |
const bool __b((__gnu_test::counter::count() > 0) == uses_global_new); |
| 107 |
if (!__b) |
| 108 |
throw std::logic_error("counter not incremented"); |
| 109 |
return __b; |
| 110 |
} |
| 111 |
|
| 112 |
template<typename Alloc, bool uses_global_delete> |
| 113 |
bool |
| 114 |
check_delete(Alloc a = Alloc()) |
| 115 |
{ |
| 116 |
__gnu_test::counter::exceptions(false); |
| 117 |
#if __cplusplus >= 201103L |
| 118 |
auto p = a.allocate(10); |
| 119 |
#else |
| 120 |
typename Alloc::pointer p = a.allocate(10); |
| 121 |
#endif |
| 122 |
const std::size_t count1 = __gnu_test::counter::count(); |
| 123 |
a.deallocate(p, 10); |
| 124 |
const std::size_t count2 = __gnu_test::counter::count(); |
| 125 |
const bool __b((count2 < count1) == uses_global_delete); |
| 126 |
if (!__b) |
| 127 |
throw std::logic_error("counter not decremented"); |
| 128 |
return __b; |
| 129 |
} |
| 130 |
} // namespace __gnu_test |
| 131 |
|
| 132 |
void* operator new(std::size_t size) THROW(std::bad_alloc) |
| 133 |
{ |
| 134 |
std::printf("operator new is called \n"); |
| 135 |
void* p = std::malloc(size); |
| 136 |
if (!p) |
| 137 |
throw std::bad_alloc(); |
| 138 |
__gnu_test::counter::increment(); |
| 139 |
return p; |
| 140 |
} |
| 141 |
|
| 142 |
void operator delete(void* p) throw() |
| 143 |
{ |
| 144 |
std::printf("operator delete is called \n"); |
| 145 |
if (p) |
| 146 |
{ |
| 147 |
std::free(p); |
| 148 |
__gnu_test::counter::decrement(); |
| 149 |
|
| 150 |
std::size_t count = __gnu_test::counter::count(); |
| 151 |
if (count == 0) |
| 152 |
std::printf("All memory released \n"); |
| 153 |
else |
| 154 |
std::printf("%lu allocations to be released \n", (unsigned long)count); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
#if __cpp_sized_deallocation |
| 159 |
void operator delete(void* p, std::size_t) throw() { ::operator delete(p); } |
| 160 |
#endif |