Daily bump.
[gcc.git] / libstdc++-v3 / testsuite / util / replacement_memory_operators.h
blob2516cd24ae1ea852db6159516253c07d19aaa704
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.
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/>.
19 #include <exception>
20 #include <stdexcept>
21 #include <cstdlib>
22 #include <cstdio>
23 #include <testsuite_hooks.h>
25 namespace __gnu_test
27 struct counter_error : public std::exception { };
29 struct counter
31 std::size_t _M_count;
32 std::size_t _M_increments, _M_decrements;
33 bool _M_throw;
35 counter() : _M_count(0), _M_throw(true) { }
37 ~counter() THROW (counter_error)
39 if (_M_throw && _M_count != 0)
40 throw counter_error();
43 static void
44 increment()
46 counter& cntr = get();
47 cntr._M_count++;
48 cntr._M_increments++;
51 static void
52 decrement()
54 counter& cntr = get();
55 cntr._M_count--;
56 cntr._M_decrements++;
59 static counter&
60 get()
62 static counter g;
63 return g;
66 static std::size_t
67 count() { return get()._M_count; }
69 static void
70 exceptions(bool __b) { get()._M_throw = __b; }
72 static void
73 reset()
75 counter& cntr = get();
76 cntr._M_increments = cntr._M_decrements = 0;
79 struct scope
81 scope() : _M_count(counter::count())
82 { counter::get()._M_count = 0; }
83 ~scope()
84 { counter::get()._M_count = _M_count; }
86 private:
87 std::size_t _M_count;
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
99 template<typename Alloc, bool uses_global_new>
100 bool
101 check_new(Alloc a = Alloc())
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;
112 template<typename Alloc, bool uses_global_delete>
113 bool
114 check_delete(Alloc a = Alloc())
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;
130 } // namespace __gnu_test
132 void* operator new(std::size_t size) THROW(std::bad_alloc)
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;
142 void operator delete(void* p) throw()
144 std::printf("operator delete is called \n");
145 if (p)
147 std::free(p);
148 __gnu_test::counter::decrement();
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);
158 #if __cpp_sized_deallocation
159 void operator delete(void* p, std::size_t) throw() { ::operator delete(p); }
160 #endif