]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/util/replacement_memory_operators.h
testsuite_allocator.h (check_new, [...]): Move to ...
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / util / replacement_memory_operators.h
1 //
2 // Copyright (C) 2007, 2009 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
21 namespace __gnu_test
22 {
23 struct counter_error : public std::exception { };
24
25 struct counter
26 {
27 size_t _M_count;
28 bool _M_throw;
29
30 counter() : _M_count(0), _M_throw(true) { }
31
32 ~counter()
33 {
34 if (_M_throw && _M_count != 0)
35 throw counter_error();
36 }
37
38 static void
39 increment() { get()._M_count++; }
40
41 static void
42 decrement() { get()._M_count--; }
43
44 static counter&
45 get()
46 {
47 static counter g;
48 return g;
49 }
50
51 static size_t
52 count() { return get()._M_count; }
53
54 static void
55 exceptions(bool __b) { get()._M_throw = __b; }
56 };
57
58 template<typename Alloc, bool uses_global_new>
59 bool
60 check_new(Alloc a = Alloc())
61 {
62 __gnu_test::counter::exceptions(false);
63 a.allocate(10);
64 const bool __b((__gnu_test::counter::count() > 0) == uses_global_new);
65 if (!__b)
66 throw std::logic_error("counter not incremented");
67 return __b;
68 }
69
70 template<typename Alloc, bool uses_global_delete>
71 bool
72 check_delete(Alloc a = Alloc())
73 {
74 __gnu_test::counter::exceptions(false);
75 typename Alloc::pointer p = a.allocate(10);
76 const std::size_t count1 = __gnu_test::counter::count();
77 a.deallocate(p, 10);
78 const std::size_t count2 = __gnu_test::counter::count();
79 const bool __b((count2 < count1) == uses_global_delete);
80 if (!__b)
81 throw std::logic_error("counter not decremented");
82 return __b;
83 }
84 } // namespace __gnu_test
85
86 void* operator new(std::size_t size) throw(std::bad_alloc)
87 {
88 printf("operator new is called \n");
89 void* p = std::malloc(size);
90 if (p == NULL)
91 throw std::bad_alloc();
92 __gnu_test::counter::increment();
93 return p;
94 }
95
96 void operator delete(void* p) throw()
97 {
98 printf("operator delete is called \n");
99 if (p != NULL)
100 {
101 std::free(p);
102 __gnu_test::counter::decrement();
103
104 std::size_t count = __gnu_test::counter::count();
105 if (count == 0)
106 printf("All memory released \n");
107 else
108 printf("%lu allocations to be released \n", count);
109 }
110 }