2017-03-14 Richard Biener <rguenther@suse.de>
[official-gcc.git] / libstdc++-v3 / testsuite / util / replacement_memory_operators.h
blob8cf3b39a6a2021f45589a0ee126ee2780aedea26
1 //
2 // Copyright (C) 2007-2017 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 bool _M_throw;
34 counter() : _M_count(0), _M_throw(true) { }
36 ~counter() THROW (counter_error)
38 if (_M_throw && _M_count != 0)
39 throw counter_error();
42 static void
43 increment() { get()._M_count++; }
45 static void
46 decrement() { get()._M_count--; }
48 static counter&
49 get()
51 static counter g;
52 return g;
55 static std::size_t
56 count() { return get()._M_count; }
58 static void
59 exceptions(bool __b) { get()._M_throw = __b; }
62 template<typename Alloc, bool uses_global_new>
63 bool
64 check_new(Alloc a = Alloc())
66 __gnu_test::counter::exceptions(false);
67 a.allocate(10);
68 const bool __b((__gnu_test::counter::count() > 0) == uses_global_new);
69 if (!__b)
70 throw std::logic_error("counter not incremented");
71 return __b;
74 template<typename Alloc, bool uses_global_delete>
75 bool
76 check_delete(Alloc a = Alloc())
78 __gnu_test::counter::exceptions(false);
79 typename Alloc::pointer p = a.allocate(10);
80 const std::size_t count1 = __gnu_test::counter::count();
81 a.deallocate(p, 10);
82 const std::size_t count2 = __gnu_test::counter::count();
83 const bool __b((count2 < count1) == uses_global_delete);
84 if (!__b)
85 throw std::logic_error("counter not decremented");
86 return __b;
88 } // namespace __gnu_test
90 void* operator new(std::size_t size) THROW(std::bad_alloc)
92 std::printf("operator new is called \n");
93 void* p = std::malloc(size);
94 if (!p)
95 throw std::bad_alloc();
96 __gnu_test::counter::increment();
97 return p;
100 void operator delete(void* p) throw()
102 std::printf("operator delete is called \n");
103 if (p)
105 std::free(p);
106 __gnu_test::counter::decrement();
108 std::size_t count = __gnu_test::counter::count();
109 if (count == 0)
110 std::printf("All memory released \n");
111 else
112 std::printf("%lu allocations to be released \n", (unsigned long)count);