2 // Copyright (C) 2007-2016 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/>.
26 struct counter_error
: public std::exception
{ };
33 counter() : _M_count(0), _M_throw(true) { }
35 ~counter() throw (counter_error
)
37 if (_M_throw
&& _M_count
!= 0)
38 throw counter_error();
42 increment() { get()._M_count
++; }
45 decrement() { get()._M_count
--; }
55 count() { return get()._M_count
; }
58 exceptions(bool __b
) { get()._M_throw
= __b
; }
61 template<typename Alloc
, bool uses_global_new
>
63 check_new(Alloc a
= Alloc())
65 __gnu_test::counter::exceptions(false);
67 const bool __b((__gnu_test::counter::count() > 0) == uses_global_new
);
69 throw std::logic_error("counter not incremented");
73 template<typename Alloc
, bool uses_global_delete
>
75 check_delete(Alloc a
= Alloc())
77 __gnu_test::counter::exceptions(false);
78 typename
Alloc::pointer p
= a
.allocate(10);
79 const std::size_t count1
= __gnu_test::counter::count();
81 const std::size_t count2
= __gnu_test::counter::count();
82 const bool __b((count2
< count1
) == uses_global_delete
);
84 throw std::logic_error("counter not decremented");
87 } // namespace __gnu_test
89 void* operator new(std::size_t size
) throw(std::bad_alloc
)
91 std::printf("operator new is called \n");
92 void* p
= std::malloc(size
);
94 throw std::bad_alloc();
95 __gnu_test::counter::increment();
99 void operator delete(void* p
) throw()
101 std::printf("operator delete is called \n");
105 __gnu_test::counter::decrement();
107 std::size_t count
= __gnu_test::counter::count();
109 std::printf("All memory released \n");
111 std::printf("%lu allocations to be released \n", (unsigned long)count
);