2 // Testing allocator for the C++ library testsuite.
4 // Copyright (C) 2002, 2003 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 class allocation_tracker
46 typedef std::size_t size_type
;
49 allocate(size_type blocksize
)
51 allocationTotal_
+= blocksize
;
52 return ::operator new(blocksize
);
56 construct() { constructCount_
++; }
59 destroy() { destructCount_
++; }
62 deallocate(void* p
, size_type blocksize
)
65 deallocationTotal_
+= blocksize
;
69 allocationTotal() { return allocationTotal_
; }
72 deallocationTotal() { return deallocationTotal_
; }
75 constructCount() { return constructCount_
; }
78 destructCount() { return destructCount_
; }
84 deallocationTotal_
= 0;
90 static size_type allocationTotal_
;
91 static size_type deallocationTotal_
;
92 static int constructCount_
;
93 static int destructCount_
;
96 // A simple basic allocator that just forwards to the
97 // allocation_tracker to fulfill memory requests. This class is
98 // templated on the target object type, but tracker isn't.
103 typedef T value_type
;
105 typedef const T
* const_pointer
;
106 typedef T
& reference
;
107 typedef const T
& const_reference
;
108 typedef std::size_t size_type
;
109 typedef std::ptrdiff_t difference_type
;
111 template<class U
> struct rebind
{ typedef tracker_alloc
<U
> other
; };
114 address(reference value
) const
118 address(const_reference value
) const
121 tracker_alloc() throw()
124 tracker_alloc(const tracker_alloc
&) throw()
128 tracker_alloc(const tracker_alloc
<U
>&) throw()
131 ~tracker_alloc() throw()
135 max_size() const throw()
136 { return std::numeric_limits
<std::size_t>::max() / sizeof(T
); }
139 allocate(size_type n
, const void* = 0)
141 return static_cast<pointer
>(allocation_tracker::allocate(n
* sizeof(T
)));
145 construct(pointer p
, const T
& value
)
148 allocation_tracker::construct();
155 allocation_tracker::destroy();
159 deallocate(pointer p
, size_type num
)
160 { allocation_tracker::deallocate(p
, num
* sizeof(T
)); }
163 template<class T1
, class T2
>
165 operator==(const tracker_alloc
<T1
>&, const tracker_alloc
<T2
>&) throw()
168 template<class T1
, class T2
>
170 operator!=(const tracker_alloc
<T1
>&, const tracker_alloc
<T2
>&) throw()
172 }; // namespace __gnu_test
174 #endif // _GLIBCXX_TESTSUITE_ALLOCATOR_H