OSDN Git Service

2011-10-07 Jonathan Wakely <jwakely.gcc@gmail.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / util / testsuite_allocator.h
1 // -*- C++ -*-
2 // Testing allocator for the C++ library testsuite.
3 //
4 // Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
5 // Free Software Foundation, Inc.
6 //
7 // This file is part of the GNU ISO C++ Library.  This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
11 // any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License along
19 // with this library; see the file COPYING3.  If not see
20 // <http://www.gnu.org/licenses/>.
21 //
22
23 // This file provides an test instrumentation allocator that can be
24 // used to verify allocation functionality of standard library
25 // containers.  2002.11.25 smw
26
27 #ifndef _GLIBCXX_TESTSUITE_ALLOCATOR_H
28 #define _GLIBCXX_TESTSUITE_ALLOCATOR_H
29
30 #include <tr1/unordered_map>
31 #include <bits/move.h>
32 #include <testsuite_hooks.h>
33
34 namespace __gnu_test
35 {
36   class tracker_allocator_counter
37   {
38   public:
39     typedef std::size_t    size_type; 
40
41     static void*
42     allocate(size_type blocksize)
43     {
44       void* p = ::operator new(blocksize);
45       allocationCount_ += blocksize;
46       return p;
47     }
48
49     static void
50     construct() { constructCount_++; }
51
52     static void
53     destroy() { destructCount_++; }
54
55     static void
56     deallocate(void* p, size_type blocksize)
57     {
58       ::operator delete(p);
59       deallocationCount_ += blocksize;
60     }
61
62     static size_type
63     get_allocation_count() { return allocationCount_; }
64
65     static size_type
66     get_deallocation_count() { return deallocationCount_; }
67
68     static int
69     get_construct_count() { return constructCount_; }
70
71     static int
72     get_destruct_count() { return destructCount_; }
73
74     static void
75     reset()
76     {
77       allocationCount_ = 0;
78       deallocationCount_ = 0;
79       constructCount_ = 0;
80       destructCount_ = 0;
81     }
82
83  private:
84     static size_type  allocationCount_;
85     static size_type  deallocationCount_;
86     static int        constructCount_;
87     static int        destructCount_;
88   };
89
90   // A simple basic allocator that just forwards to the
91   // tracker_allocator_counter to fulfill memory requests.  This class
92   // is templated on the target object type, but tracker isn't.
93   template<class T>
94   class tracker_allocator
95   {
96   private:
97     typedef tracker_allocator_counter counter_type;
98
99   public:
100     typedef T              value_type;
101     typedef T*             pointer;
102     typedef const T*       const_pointer;
103     typedef T&             reference;
104     typedef const T&       const_reference;
105     typedef std::size_t    size_type; 
106     typedef std::ptrdiff_t difference_type; 
107     
108     template<class U> struct rebind { typedef tracker_allocator<U> other; };
109     
110     pointer
111     address(reference value) const _GLIBCXX_NOEXCEPT
112     { return std::__addressof(value); }
113
114     const_pointer
115     address(const_reference value) const _GLIBCXX_NOEXCEPT
116     { return std::__addressof(value); }
117
118     tracker_allocator() _GLIBCXX_USE_NOEXCEPT
119     { }
120
121     tracker_allocator(const tracker_allocator&) _GLIBCXX_USE_NOEXCEPT
122     { }
123
124     template<class U>
125       tracker_allocator(const tracker_allocator<U>&) _GLIBCXX_USE_NOEXCEPT
126       { }
127
128     ~tracker_allocator() _GLIBCXX_USE_NOEXCEPT
129     { }
130
131     size_type
132     max_size() const _GLIBCXX_USE_NOEXCEPT
133     { return size_type(-1) / sizeof(T); }
134
135     pointer
136     allocate(size_type n, const void* = 0)
137     { return static_cast<pointer>(counter_type::allocate(n * sizeof(T))); }
138
139 #ifdef __GXX_EXPERIMENTAL_CXX0X__
140     template<typename U, typename... Args>
141       void
142       construct(U* p, Args&&... args) 
143       {
144         ::new((void *)p) U(std::forward<Args>(args)...);
145         counter_type::construct();
146       }
147
148     template<typename U>
149       void
150       destroy(U* p)
151       {
152         p->~U();
153         counter_type::destroy();
154       }
155 #else
156     void
157     construct(pointer p, const T& value)
158     {
159       ::new ((void *)p) T(value);
160       counter_type::construct();
161     }
162
163     void
164     destroy(pointer p)
165     {
166       p->~T();
167       counter_type::destroy();
168     }
169 #endif
170
171     void
172     deallocate(pointer p, size_type num)
173     { counter_type::deallocate(p, num * sizeof(T)); }
174   };
175
176   template<class T1, class T2>
177     bool
178     operator==(const tracker_allocator<T1>&, 
179                const tracker_allocator<T2>&) throw()
180     { return true; }
181
182   template<class T1, class T2>
183     bool
184     operator!=(const tracker_allocator<T1>&, 
185                const tracker_allocator<T2>&) throw()
186     { return false; }
187
188   bool
189   check_construct_destroy(const char* tag, int expected_c, int expected_d);
190
191   template<typename Alloc>
192     bool
193     check_deallocate_null()
194     {
195       // Let's not core here...
196       Alloc  a;
197       a.deallocate(0, 1);
198       a.deallocate(0, 10);
199       return true;
200     }
201
202   template<typename Alloc>
203     bool 
204     check_allocate_max_size()
205     {
206       Alloc a;
207       try
208         {
209           a.allocate(a.max_size() + 1);
210         }
211       catch(std::bad_alloc&)
212         {
213           return true;
214         }
215       catch(...)
216         {
217           throw;
218         }
219       throw;
220     }
221
222
223   // A simple allocator which can be constructed endowed of a given
224   // "personality" (an integer), queried in operator== to simulate the
225   // behavior of realworld "unequal" allocators (i.e., not exploiting
226   // the provision in 20.1.5/4, first bullet).  A global unordered_map,
227   // filled at allocation time with (pointer, personality) pairs, is
228   // then consulted to enforce the requirements in Table 32 about
229   // deallocation vs allocator equality.  Note that this allocator is
230   // swappable, not assignable, consistently with Option 3 of DR 431
231   // (see N1599).
232   struct uneq_allocator_base
233   {
234     typedef std::tr1::unordered_map<void*, int>   map_type;
235
236     // Avoid static initialization troubles and/or bad interactions
237     // with tests linking testsuite_allocator.o and playing globally
238     // with operator new/delete.
239     static map_type&
240     get_map()
241     {
242       static map_type alloc_map;
243       return alloc_map;
244     }
245   };
246
247   template<typename Tp>
248     class uneq_allocator
249     : private uneq_allocator_base
250     {
251     public:
252       typedef std::size_t                         size_type;
253       typedef std::ptrdiff_t                      difference_type;
254       typedef Tp*                                 pointer;
255       typedef const Tp*                           const_pointer;
256       typedef Tp&                                 reference;
257       typedef const Tp&                           const_reference;
258       typedef Tp                                  value_type;
259
260 #ifdef __GXX_EXPERIMENTAL_CXX0X__
261       typedef std::true_type                      propagate_on_container_swap;
262 #endif
263
264       template<typename Tp1>
265         struct rebind
266         { typedef uneq_allocator<Tp1> other; };
267
268       uneq_allocator() _GLIBCXX_USE_NOEXCEPT
269       : personality(0) { }
270
271       uneq_allocator(int person) _GLIBCXX_USE_NOEXCEPT
272       : personality(person) { }
273       
274       template<typename Tp1>
275         uneq_allocator(const uneq_allocator<Tp1>& b) _GLIBCXX_USE_NOEXCEPT
276         : personality(b.get_personality()) { }
277
278       ~uneq_allocator() _GLIBCXX_USE_NOEXCEPT
279       { }
280
281       int get_personality() const { return personality; }
282       
283       pointer
284       address(reference x) const _GLIBCXX_NOEXCEPT
285       { return std::__addressof(x); }
286     
287       const_pointer
288       address(const_reference x) const _GLIBCXX_NOEXCEPT
289       { return std::__addressof(x); }
290
291       pointer
292       allocate(size_type n, const void* = 0)
293       { 
294         if (__builtin_expect(n > this->max_size(), false))
295           std::__throw_bad_alloc();
296         
297         pointer p = static_cast<Tp*>(::operator new(n * sizeof(Tp)));
298         try
299           {
300             get_map().insert(map_type::value_type(reinterpret_cast<void*>(p),
301                                                   personality));
302           }
303         catch(...)
304           {
305             ::operator delete(p);
306             __throw_exception_again;
307           }
308         return p;
309       }
310
311       void
312       deallocate(pointer p, size_type)
313       {
314         bool test __attribute__((unused)) = true;
315
316         VERIFY( p );
317
318         map_type::iterator it = get_map().find(reinterpret_cast<void*>(p));
319         VERIFY( it != get_map().end() );
320
321         // Enforce requirements in Table 32 about deallocation vs
322         // allocator equality.
323         VERIFY( it->second == personality );
324
325         get_map().erase(it);
326         ::operator delete(p);
327       }
328
329       size_type
330       max_size() const _GLIBCXX_USE_NOEXCEPT 
331       { return size_type(-1) / sizeof(Tp); }
332
333 #ifdef __GXX_EXPERIMENTAL_CXX0X__
334       template<typename U, typename... Args>
335         void
336         construct(U* p, Args&&... args) 
337         { ::new((void *)p) U(std::forward<Args>(args)...); }
338
339       template<typename U>
340         void 
341         destroy(U* p) { p->~U(); }
342
343       // Not copy assignable...
344       uneq_allocator&
345       operator=(const uneq_allocator&) = delete;
346 #else
347       void 
348       construct(pointer p, const Tp& val) 
349       { ::new((void *)p) Tp(val); }
350
351       void 
352       destroy(pointer p) { p->~Tp(); }
353
354     private:
355       // Not assignable...
356       uneq_allocator&
357       operator=(const uneq_allocator&);
358 #endif
359
360     private:
361
362       // ... yet swappable!
363       friend inline void
364       swap(uneq_allocator& a, uneq_allocator& b)
365       { std::swap(a.personality, b.personality); } 
366       
367       template<typename Tp1>
368         friend inline bool
369         operator==(const uneq_allocator& a, const uneq_allocator<Tp1>& b)
370         { return a.personality == b.personality; }
371
372       template<typename Tp1>
373         friend inline bool
374         operator!=(const uneq_allocator& a, const uneq_allocator<Tp1>& b)
375         { return !(a == b); }
376       
377       int personality;
378     };
379
380 #ifdef __GXX_EXPERIMENTAL_CXX0X__
381   // An uneq_allocator which can be used to test allocator propagation.
382   template<typename Tp, bool Propagate>
383     class propagating_allocator : public uneq_allocator<Tp>
384     {
385       typedef uneq_allocator<Tp> base_alloc;
386       base_alloc& base() { return *this; }
387       const base_alloc& base() const  { return *this; }
388       void swap_base(base_alloc& b) { swap(b, this->base()); }
389
390       typedef std::integral_constant<bool, Propagate> trait_type;
391
392     public:
393       // default allocator_traits::rebind_alloc would select
394       // uneq_allocator::rebind so we must define rebind here
395       template<typename Up>
396         struct rebind { typedef propagating_allocator<Up, Propagate> other; };
397
398       propagating_allocator(int i) noexcept
399       : base_alloc(i)
400       { }
401
402       template<typename Up>
403         propagating_allocator(const propagating_allocator<Up, Propagate>& a)
404         noexcept
405         : base_alloc(a)
406         { }
407
408       propagating_allocator() noexcept = default;
409
410       propagating_allocator(const propagating_allocator&) noexcept = default;
411
412       template<bool P2>
413         propagating_allocator&
414         operator=(const propagating_allocator<Tp, P2>& a) noexcept
415         {
416           static_assert(P2, "assigning propagating_allocator<T, true>");
417           propagating_allocator(a).swap_base(*this);
418           return *this;
419         }
420
421       // postcondition: a.get_personality() == 0
422       propagating_allocator(propagating_allocator&& a) noexcept
423       : base_alloc()
424       { swap_base(a); }
425
426       // postcondition: a.get_personality() == 0
427       propagating_allocator&
428       operator=(propagating_allocator&& a) noexcept
429       {
430         propagating_allocator(std::move(a)).swap_base(*this);
431         return *this;
432       }
433
434       typedef trait_type propagate_on_container_copy_assignment;
435       typedef trait_type propagate_on_container_move_assignment;
436       typedef trait_type propagate_on_container_swap;
437
438       propagating_allocator select_on_container_copy_construction() const
439       { return Propagate ? *this : propagating_allocator(); }
440     };
441
442   // Class template supporting the minimal interface that satisfies the
443   // Allocator requirements, from example in [allocator.requirements]
444   template <class Tp>
445     struct SimpleAllocator
446     {
447       typedef Tp value_type;
448
449       SimpleAllocator() { }
450
451       template <class T>
452         SimpleAllocator(const SimpleAllocator<T>& other) { }
453
454       Tp *allocate(std::size_t n)
455       { return std::allocator<Tp>().allocate(n); }
456
457       void deallocate(Tp *p, std::size_t n)
458       { std::allocator<Tp>().deallocate(p, n); }
459     };
460
461   template <class T, class U>
462     bool operator==(const SimpleAllocator<T>&, const SimpleAllocator<U>&)
463     { return true; }
464   template <class T, class U>
465     bool operator!=(const SimpleAllocator<T>&, const SimpleAllocator<U>&)
466     { return false; }
467
468 #endif
469
470   template<typename Tp>
471     struct ExplicitConsAlloc : std::allocator<Tp>
472     {
473       ExplicitConsAlloc() { }
474
475       template<typename Up>
476         explicit
477         ExplicitConsAlloc(const ExplicitConsAlloc<Up>&) { }
478
479       template<typename Up>
480         struct rebind
481         { typedef ExplicitConsAlloc<Up> other; };
482     };
483
484 } // namespace __gnu_test
485
486 #endif // _GLIBCXX_TESTSUITE_ALLOCATOR_H