]>
Commit | Line | Data |
---|---|---|
83ffe9cd | 1 | // Copyright (C) 2004-2023 Free Software Foundation, Inc. |
03f9ea44 DM |
2 | // |
3 | // This file is part of the GNU ISO C++ Library. This library is free | |
4 | // software; you can redistribute it and/or modify it under the | |
5 | // terms of the GNU General Public License as published by the | |
748086b7 | 6 | // Free Software Foundation; either version 3, or (at your option) |
03f9ea44 | 7 | // any later version. |
0d04fe49 | 8 | |
03f9ea44 DM |
9 | // This library is distributed in the hope that it will be useful, |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | // GNU General Public License for more details. | |
0d04fe49 | 13 | |
03f9ea44 | 14 | // You should have received a copy of the GNU General Public License along |
748086b7 JJ |
15 | // with this library; see the file COPYING3. If not see |
16 | // <http://www.gnu.org/licenses/>. | |
03f9ea44 DM |
17 | |
18 | #include <deque> | |
19 | #include <ext/new_allocator.h> | |
20 | ||
21 | using namespace std; | |
22 | using __gnu_cxx::new_allocator; | |
23 | ||
24 | template<typename T> | |
0d04fe49 | 25 | class clear_alloc : public new_allocator<T> |
03f9ea44 DM |
26 | { |
27 | public: | |
28 | ||
29 | template <typename T1> | |
0d04fe49 | 30 | struct rebind |
03f9ea44 DM |
31 | { typedef clear_alloc<T1> other; }; |
32 | ||
f4a1faa5 | 33 | virtual void clear() throw() |
03f9ea44 DM |
34 | { } |
35 | ||
f4a1faa5 | 36 | clear_alloc() throw() |
03f9ea44 | 37 | { } |
0d04fe49 JW |
38 | |
39 | clear_alloc(clear_alloc const&) throw() : new_allocator<T>() | |
03f9ea44 | 40 | { } |
0d04fe49 | 41 | |
03f9ea44 | 42 | template<typename T1> |
8b5bc374 | 43 | clear_alloc(clear_alloc<T1> const&) throw() |
03f9ea44 DM |
44 | { } |
45 | ||
f4a1faa5 | 46 | virtual ~clear_alloc() throw() |
03f9ea44 DM |
47 | { this->clear(); } |
48 | ||
49 | T* allocate(typename new_allocator<T>::size_type n, const void *hint = 0) | |
50 | { | |
51 | this->clear(); | |
52 | return new_allocator<T>::allocate(n, hint); | |
53 | } | |
0d04fe49 | 54 | |
03f9ea44 DM |
55 | void deallocate(T *ptr, typename new_allocator<T>::size_type n) |
56 | { | |
57 | this->clear(); | |
58 | new_allocator<T>::deallocate(ptr, n); | |
59 | } | |
60 | }; | |
61 | ||
62 | template<typename Container> | |
63 | void Check_Container() | |
64 | { | |
65 | Container* pic = new Container; | |
66 | int x = 230; | |
0d04fe49 | 67 | |
03f9ea44 DM |
68 | while (x--) |
69 | { | |
70 | pic->push_back(x); | |
71 | } | |
0d04fe49 JW |
72 | |
73 | (void) pic->get_allocator(); | |
74 | ||
03f9ea44 DM |
75 | // The following has led to infinite recursions or cores. |
76 | pic->clear(); | |
77 | ||
78 | delete pic; | |
79 | } | |
80 | ||
81 | ||
82 | int main() | |
83 | { | |
84 | Check_Container<std::deque<int, clear_alloc<int> > >(); | |
85 | return 0; | |
86 | } | |
87 |