blob: 7644bc7c9686a8add498744755dd9a26754b6874 [file] [log] [blame]
Howard Hinnantf39daa82010-08-28 21:01:06 +00001#ifndef TEST_ALLOCATOR_H
2#define TEST_ALLOCATOR_H
3
4#include <cstddef>
5#include <type_traits>
6#include <cstdlib>
7#include <new>
8#include <climits>
9
10class test_alloc_base
11{
12public:
13 static int count;
14public:
15 static int throw_after;
16};
17
18int test_alloc_base::count = 0;
19int test_alloc_base::throw_after = INT_MAX;
20
21template <class T>
22class test_allocator
23 : public test_alloc_base
24{
25 int data_;
26
27 template <class U> friend class test_allocator;
28public:
29
30 typedef unsigned size_type;
31 typedef int difference_type;
32 typedef T value_type;
33 typedef value_type* pointer;
34 typedef const value_type* const_pointer;
35 typedef typename std::add_lvalue_reference<value_type>::type reference;
36 typedef typename std::add_lvalue_reference<const value_type>::type const_reference;
37
38 template <class U> struct rebind {typedef test_allocator<U> other;};
39
40 test_allocator() throw() : data_(-1) {}
41 explicit test_allocator(int i) throw() : data_(i) {}
42 test_allocator(const test_allocator& a) throw()
43 : data_(a.data_) {}
44 template <class U> test_allocator(const test_allocator<U>& a) throw()
45 : data_(a.data_) {}
46 ~test_allocator() throw() {data_ = 0;}
47 pointer address(reference x) const {return &x;}
48 const_pointer address(const_reference x) const {return &x;}
49 pointer allocate(size_type n, const void* = 0)
50 {
51 if (count >= throw_after)
52 throw std::bad_alloc();
53 ++count;
54 return (pointer)std::malloc(n * sizeof(T));
55 }
56 void deallocate(pointer p, size_type n)
57 {--count; std::free(p);}
58 size_type max_size() const throw()
59 {return UINT_MAX / sizeof(T);}
60 void construct(pointer p, const T& val)
61 {::new(p) T(val);}
Howard Hinnant73d21a42010-09-04 23:28:19 +000062#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantf39daa82010-08-28 21:01:06 +000063 void construct(pointer p, T&& val)
64 {::new(p) T(std::move(val));}
Howard Hinnant73d21a42010-09-04 23:28:19 +000065#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantf39daa82010-08-28 21:01:06 +000066 void destroy(pointer p) {p->~T();}
67
68 friend bool operator==(const test_allocator& x, const test_allocator& y)
69 {return x.data_ == y.data_;}
70 friend bool operator!=(const test_allocator& x, const test_allocator& y)
71 {return !(x == y);}
72};
73
74template <>
75class test_allocator<void>
76 : public test_alloc_base
77{
78 int data_;
79
80 template <class U> friend class test_allocator;
81public:
82
83 typedef unsigned size_type;
84 typedef int difference_type;
85 typedef void value_type;
86 typedef value_type* pointer;
87 typedef const value_type* const_pointer;
88
89 template <class U> struct rebind {typedef test_allocator<U> other;};
90
91 test_allocator() throw() : data_(-1) {}
92 explicit test_allocator(int i) throw() : data_(i) {}
93 test_allocator(const test_allocator& a) throw()
94 : data_(a.data_) {}
95 template <class U> test_allocator(const test_allocator<U>& a) throw()
96 : data_(a.data_) {}
97 ~test_allocator() throw() {data_ = 0;}
98
99 friend bool operator==(const test_allocator& x, const test_allocator& y)
100 {return x.data_ == y.data_;}
101 friend bool operator!=(const test_allocator& x, const test_allocator& y)
102 {return !(x == y);}
103};
104
105template <class T>
106class other_allocator
107{
108 int data_;
109
110 template <class U> friend class other_allocator;
111
112public:
113 typedef T value_type;
114
115 other_allocator() : data_(-1) {}
116 explicit other_allocator(int i) : data_(i) {}
117 template <class U> other_allocator(const other_allocator<U>& a)
118 : data_(a.data_) {}
119 T* allocate(std::size_t n)
120 {return (T*)std::malloc(n * sizeof(T));}
121 void deallocate(T* p, std::size_t n)
122 {std::free(p);}
123
124 other_allocator select_on_container_copy_construction() const
125 {return other_allocator(-2);}
126
127 friend bool operator==(const other_allocator& x, const other_allocator& y)
128 {return x.data_ == y.data_;}
129 friend bool operator!=(const other_allocator& x, const other_allocator& y)
130 {return !(x == y);}
131
132 typedef std::true_type propagate_on_container_copy_assignment;
133 typedef std::true_type propagate_on_container_move_assignment;
134 typedef std::true_type propagate_on_container_swap;
135
136#ifdef _LIBCPP_HAS_NO_ADVANCED_SFINAE
137 std::size_t max_size() const
138 {return UINT_MAX / sizeof(T);}
139#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
140
141};
142
143#endif // TEST_ALLOCATOR_H