2 // Testing allocator for the C++ library testsuite.
4 // Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
31 // This file provides an test instrumentation allocator that can be
32 // used to verify allocation functionality of standard library
33 // containers. 2002.11.25 smw
35 #ifndef _GLIBCXX_TESTSUITE_ALLOCATOR_H
36 #define _GLIBCXX_TESTSUITE_ALLOCATOR_H
43 bool new_called = false;
44 bool delete_called = false;
49 class allocation_tracker
52 typedef std::size_t size_type;
55 allocate(size_type blocksize)
57 allocationTotal_ += blocksize;
58 return ::operator new(blocksize);
62 construct() { constructCount_++; }
65 destroy() { destructCount_++; }
68 deallocate(void* p, size_type blocksize)
71 deallocationTotal_ += blocksize;
75 allocationTotal() { return allocationTotal_; }
78 deallocationTotal() { return deallocationTotal_; }
81 constructCount() { return constructCount_; }
84 destructCount() { return destructCount_; }
90 deallocationTotal_ = 0;
96 static size_type allocationTotal_;
97 static size_type deallocationTotal_;
98 static int constructCount_;
99 static int destructCount_;
102 // A simple basic allocator that just forwards to the
103 // allocation_tracker to fulfill memory requests. This class is
104 // templated on the target object type, but tracker isn't.
109 typedef T value_type;
111 typedef const T* const_pointer;
112 typedef T& reference;
113 typedef const T& const_reference;
114 typedef std::size_t size_type;
115 typedef std::ptrdiff_t difference_type;
117 template<class U> struct rebind { typedef tracker_alloc<U> other; };
120 address(reference value) const
124 address(const_reference value) const
127 tracker_alloc() throw()
130 tracker_alloc(const tracker_alloc&) throw()
134 tracker_alloc(const tracker_alloc<U>&) throw()
137 ~tracker_alloc() throw()
141 max_size() const throw()
142 { return std::numeric_limits<std::size_t>::max() / sizeof(T); }
145 allocate(size_type n, const void* = 0)
147 return static_cast<pointer>(allocation_tracker::allocate(n * sizeof(T)));
151 construct(pointer p, const T& value)
154 allocation_tracker::construct();
161 allocation_tracker::destroy();
165 deallocate(pointer p, size_type num)
166 { allocation_tracker::deallocate(p, num * sizeof(T)); }
169 template<class T1, class T2>
171 operator==(const tracker_alloc<T1>&, const tracker_alloc<T2>&) throw()
174 template<class T1, class T2>
176 operator!=(const tracker_alloc<T1>&, const tracker_alloc<T2>&) throw()
180 check_construct_destroy(const char* tag, int expected_c, int expected_d);
182 template<typename Alloc, bool uses_global_new>
184 check_new(Alloc a = Alloc())
186 bool test __attribute__((unused)) = true;
187 typename Alloc::pointer p = a.allocate(10);
188 test &= ( new_called == uses_global_new );
192 template<typename Alloc, bool uses_global_delete>
194 check_delete(Alloc a = Alloc())
196 bool test __attribute__((unused)) = true;
197 typename Alloc::pointer p = a.allocate(10);
199 test &= ( delete_called == uses_global_delete );
203 template<typename Alloc>
205 check_deallocate_null()
207 // Let's not core here...
209 a.deallocate(NULL, 1);
210 a.deallocate(NULL, 10);
213 template<typename Alloc>
215 check_allocate_max_size()
220 a.allocate(a.max_size() + 1);
222 catch(std::bad_alloc&)
233 }; // namespace __gnu_test
235 #endif // _GLIBCXX_TESTSUITE_ALLOCATOR_H