* java/lang/ClassLoader.java (findLoadedClass): Now synchronized.
[platform/upstream/gcc.git] / libstdc++-v3 / testsuite / testsuite_allocator.h
1 // Testing allocator for the C++ library testsuite.
2 //
3 // Copyright (C) 2002 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
10 //
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.
15 //
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,
19 // USA.
20 //
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.
29
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
33
34 #ifndef _GLIBCPP_TESTSUITE_ALLOCATOR_H
35 #define _GLIBCPP_TESTSUITE_ALLOCATOR_H
36
37 #include <cstddef>
38 #include <limits>
39
40 class gnu_allocator_tracker
41 {
42  public:
43   typedef std::size_t    size_type; 
44   
45   static void*
46   allocate(size_type blocksize)
47   {
48     allocationTotal_ += blocksize;
49     return ::operator new(blocksize);
50   }
51
52   static void
53   construct()
54   { constructCount_++; }
55
56   static void
57   destroy()
58   { destructCount_++; }
59
60   static void
61   deallocate(void* p, size_type blocksize)
62   {
63     ::operator delete(p);
64     deallocationTotal_ += blocksize;
65   }
66
67   static size_type
68   allocationTotal() 
69   { return allocationTotal_; }
70
71   static size_type
72   deallocationTotal()
73   { return deallocationTotal_; }
74
75   static int
76   constructCount() 
77   { return constructCount_; }
78
79   static int
80   destructCount() 
81   { return destructCount_; }
82     
83   static void
84   resetCounts()
85   {
86     allocationTotal_ = 0;
87     deallocationTotal_ = 0;
88     constructCount_ = 0;
89     destructCount_ = 0;
90   }
91
92  private:
93   static size_type  allocationTotal_;
94   static size_type  deallocationTotal_;
95   static int        constructCount_;
96   static int        destructCount_;
97 };
98
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
102 // isn't.
103 template<class T>
104   class gnu_new_allocator
105   {
106   public:
107     typedef T              value_type;
108     typedef T*             pointer;
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; 
114     
115     template<class U> struct rebind { typedef gnu_new_allocator<U> other; };
116     
117     pointer
118     address(reference value) const
119     { return &value; }
120     
121     const_pointer
122     address(const_reference value) const
123     { return &value; }
124     
125     gnu_new_allocator() throw()
126     { }
127
128     gnu_new_allocator(const gnu_new_allocator&) throw()
129     { }
130
131     template<class U>
132       gnu_new_allocator(const gnu_new_allocator<U>&) throw()
133       { }
134
135     ~gnu_new_allocator() throw()
136     { }
137
138     size_type
139     max_size() const throw()
140     { return std::numeric_limits<std::size_t>::max() / sizeof(T); }
141
142     pointer
143     allocate(size_type num, const void* = 0)
144     { 
145       return static_cast<pointer>(gnu_allocator_tracker::allocate(num * 
146                                                                   sizeof(T))); 
147     }
148
149     void
150     construct(pointer p, const T& value)
151     {
152       new (p) T(value);
153       gnu_allocator_tracker::construct();
154     }
155
156     void
157     destroy(pointer p)
158     {
159       p->~T();
160       gnu_allocator_tracker::destroy();
161     }
162
163     void
164     deallocate(pointer p, size_type num)
165     { gnu_allocator_tracker::deallocate(p, num * sizeof(T)); }
166   };
167
168 template<class T1, class T2>
169   bool
170   operator==(const gnu_new_allocator<T1>&, 
171              const gnu_new_allocator<T2>&) throw()
172   { return true; }
173
174 template<class T1, class T2>
175   bool
176   operator!=(const gnu_new_allocator<T1>&, 
177              const gnu_new_allocator<T2>&) throw()
178   { return false; }
179
180 #endif // _GLIBCPP_TESTSUITE_ALLOCATOR_H
181