1 // Testing allocator for the C++ library testsuite.
3 // Copyright (C) 2002 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 // This file provides an test instrumentation allocator that can be
31 // used to verify allocation functionality of standard library
32 // containers. 2002.11.25 smw
34 #ifndef _GLIBCPP_TESTSUITE_ALLOCATOR_H
35 #define _GLIBCPP_TESTSUITE_ALLOCATOR_H
40 class gnu_allocator_tracker
43 typedef std::size_t size_type;
46 allocate(size_type blocksize)
48 allocationTotal_ += blocksize;
49 return ::operator new(blocksize);
54 { constructCount_++; }
61 deallocate(void* p, size_type blocksize)
64 deallocationTotal_ += blocksize;
69 { return allocationTotal_; }
73 { return deallocationTotal_; }
77 { return constructCount_; }
81 { return destructCount_; }
87 deallocationTotal_ = 0;
93 static size_type allocationTotal_;
94 static size_type deallocationTotal_;
95 static int constructCount_;
96 static int destructCount_;
99 // A simple basic allocator that just forwards to the
100 // gnu_allocator_tracker to fulfill memory requests. This class is
101 // templated on the target object type, but gnu_allocator_tracker
104 class gnu_new_allocator
107 typedef T value_type;
109 typedef const T* const_pointer;
110 typedef T& reference;
111 typedef const T& const_reference;
112 typedef std::size_t size_type;
113 typedef std::ptrdiff_t difference_type;
115 template<class U> struct rebind { typedef gnu_new_allocator<U> other; };
118 address(reference value) const
122 address(const_reference value) const
125 gnu_new_allocator() throw()
128 gnu_new_allocator(const gnu_new_allocator&) throw()
132 gnu_new_allocator(const gnu_new_allocator<U>&) throw()
135 ~gnu_new_allocator() throw()
139 max_size() const throw()
140 { return std::numeric_limits<std::size_t>::max() / sizeof(T); }
143 allocate(size_type num, const void* = 0)
145 return static_cast<pointer>(gnu_allocator_tracker::allocate(num *
150 construct(pointer p, const T& value)
153 gnu_allocator_tracker::construct();
160 gnu_allocator_tracker::destroy();
164 deallocate(pointer p, size_type num)
165 { gnu_allocator_tracker::deallocate(p, num * sizeof(T)); }
168 template<class T1, class T2>
170 operator==(const gnu_new_allocator<T1>&,
171 const gnu_new_allocator<T2>&) throw()
174 template<class T1, class T2>
176 operator!=(const gnu_new_allocator<T1>&,
177 const gnu_new_allocator<T2>&) throw()
180 #endif // _GLIBCPP_TESTSUITE_ALLOCATOR_H