2 // Copyright (C) 2007-2025 Free Software Foundation, Inc.
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)
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/>.
23 #include <testsuite_hooks.h>
27 struct counter_error
: public std::exception
{ };
32 std::size_t _M_increments
, _M_decrements
;
35 counter() : _M_count(0), _M_throw(true) { }
37 ~counter() THROW (counter_error
)
39 if (_M_throw
&& _M_count
!= 0)
40 throw counter_error();
46 counter
& cntr
= get();
54 counter
& cntr
= get();
67 count() { return get()._M_count
; }
70 exceptions(bool __b
) { get()._M_throw
= __b
; }
75 counter
& cntr
= get();
76 cntr
._M_increments
= cntr
._M_decrements
= 0;
81 scope() : _M_count(counter::count())
82 { counter::get()._M_count
= 0; }
84 { counter::get()._M_count
= _M_count
; }
89 #if __cplusplus >= 201103L
90 scope(const scope
&) = delete;
91 scope
& operator=(const scope
&) = delete;
94 scope
& operator=(const scope
&);
99 template<typename Alloc
, bool uses_global_new
>
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
);
108 throw std::logic_error("counter not incremented");
112 template<typename Alloc
, bool uses_global_delete
>
114 check_delete(Alloc a
= Alloc())
116 __gnu_test::counter::exceptions(false);
117 #if __cplusplus >= 201103L
118 auto p
= a
.allocate(10);
120 typename
Alloc::pointer p
= a
.allocate(10);
122 const std::size_t count1
= __gnu_test::counter::count();
124 const std::size_t count2
= __gnu_test::counter::count();
125 const bool __b((count2
< count1
) == uses_global_delete
);
127 throw std::logic_error("counter not decremented");
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
);
137 throw std::bad_alloc();
138 __gnu_test::counter::increment();
142 void operator delete(void* p
) throw()
144 std::printf("operator delete is called \n");
148 __gnu_test::counter::decrement();
150 std::size_t count
= __gnu_test::counter::count();
152 std::printf("All memory released \n");
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
); }