blob: b96e6c5cbf4ef73ff3ee190f8bf8ade771115327 [file] [log] [blame]
Marshall Clow354d39c2014-01-16 16:58:45 +00001//===----------------------------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Howard Hinnant3e519522010-05-11 19:42:16 +000010#ifndef TEST_ALLOCATOR_H
11#define TEST_ALLOCATOR_H
12
13#include <cstddef>
14#include <type_traits>
15#include <cstdlib>
16#include <new>
17#include <climits>
Marshall Clowc3deeb52013-12-03 00:18:10 +000018#include <cassert>
Howard Hinnant3e519522010-05-11 19:42:16 +000019
Marshall Clowcbf166a2015-06-03 19:56:43 +000020#include "test_macros.h"
21
Howard Hinnant3e519522010-05-11 19:42:16 +000022class test_alloc_base
23{
24protected:
Marshall Clowc3deeb52013-12-03 00:18:10 +000025 static int time_to_throw;
Howard Hinnant3e519522010-05-11 19:42:16 +000026public:
27 static int throw_after;
Marshall Clowc3deeb52013-12-03 00:18:10 +000028 static int count;
29 static int alloc_count;
Howard Hinnant3e519522010-05-11 19:42:16 +000030};
31
32int test_alloc_base::count = 0;
Marshall Clowc3deeb52013-12-03 00:18:10 +000033int test_alloc_base::time_to_throw = 0;
34int test_alloc_base::alloc_count = 0;
Howard Hinnant3e519522010-05-11 19:42:16 +000035int test_alloc_base::throw_after = INT_MAX;
36
37template <class T>
38class test_allocator
39 : public test_alloc_base
40{
41 int data_;
42
43 template <class U> friend class test_allocator;
44public:
45
46 typedef unsigned size_type;
47 typedef int difference_type;
48 typedef T value_type;
49 typedef value_type* pointer;
50 typedef const value_type* const_pointer;
51 typedef typename std::add_lvalue_reference<value_type>::type reference;
52 typedef typename std::add_lvalue_reference<const value_type>::type const_reference;
53
54 template <class U> struct rebind {typedef test_allocator<U> other;};
55
Marshall Clowc3deeb52013-12-03 00:18:10 +000056 test_allocator() throw() : data_(0) {++count;}
57 explicit test_allocator(int i) throw() : data_(i) {++count;}
Howard Hinnant3e519522010-05-11 19:42:16 +000058 test_allocator(const test_allocator& a) throw()
Marshall Clowc3deeb52013-12-03 00:18:10 +000059 : data_(a.data_) {++count;}
Howard Hinnant3e519522010-05-11 19:42:16 +000060 template <class U> test_allocator(const test_allocator<U>& a) throw()
Marshall Clowc3deeb52013-12-03 00:18:10 +000061 : data_(a.data_) {++count;}
62 ~test_allocator() throw() {assert(data_ >= 0); --count; data_ = -1;}
Howard Hinnant3e519522010-05-11 19:42:16 +000063 pointer address(reference x) const {return &x;}
64 const_pointer address(const_reference x) const {return &x;}
65 pointer allocate(size_type n, const void* = 0)
66 {
Marshall Clowc3deeb52013-12-03 00:18:10 +000067 assert(data_ >= 0);
68 if (time_to_throw >= throw_after) {
Howard Hinnant65a87cc2013-03-23 17:27:16 +000069#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +000070 throw std::bad_alloc();
Howard Hinnant65a87cc2013-03-23 17:27:16 +000071#else
72 std::terminate();
73#endif
74 }
Marshall Clowc3deeb52013-12-03 00:18:10 +000075 ++time_to_throw;
76 ++alloc_count;
Eric Fiselierb11df182015-06-14 23:30:09 +000077 return (pointer)::operator new(n * sizeof(T));
Howard Hinnant3e519522010-05-11 19:42:16 +000078 }
Eric Fiselier7626f772016-04-28 03:17:56 +000079 void deallocate(pointer p, size_type)
Eric Fiselierb11df182015-06-14 23:30:09 +000080 {assert(data_ >= 0); --alloc_count; ::operator delete((void*)p);}
Howard Hinnant3e519522010-05-11 19:42:16 +000081 size_type max_size() const throw()
82 {return UINT_MAX / sizeof(T);}
Eric Fiselier4fae5022016-06-15 01:53:32 +000083#if TEST_STD_VER < 11
Howard Hinnant3e519522010-05-11 19:42:16 +000084 void construct(pointer p, const T& val)
Eric Fiselier4fae5022016-06-15 01:53:32 +000085 {::new(static_cast<void*>(p)) T(val);}
86#else
87 template <class U> void construct(pointer p, U&& val)
88 {::new(static_cast<void*>(p)) T(std::forward<U>(val));}
89#endif
Howard Hinnant3e519522010-05-11 19:42:16 +000090 void destroy(pointer p) {p->~T();}
Howard Hinnant3e519522010-05-11 19:42:16 +000091 friend bool operator==(const test_allocator& x, const test_allocator& y)
92 {return x.data_ == y.data_;}
93 friend bool operator!=(const test_allocator& x, const test_allocator& y)
94 {return !(x == y);}
95};
96
Marshall Clow5f7c2db2014-04-18 17:23:36 +000097template <class T>
98class non_default_test_allocator
99 : public test_alloc_base
100{
101 int data_;
102
103 template <class U> friend class non_default_test_allocator;
104public:
105
106 typedef unsigned size_type;
107 typedef int difference_type;
108 typedef T value_type;
109 typedef value_type* pointer;
110 typedef const value_type* const_pointer;
111 typedef typename std::add_lvalue_reference<value_type>::type reference;
112 typedef typename std::add_lvalue_reference<const value_type>::type const_reference;
113
114 template <class U> struct rebind {typedef non_default_test_allocator<U> other;};
115
116// non_default_test_allocator() throw() : data_(0) {++count;}
117 explicit non_default_test_allocator(int i) throw() : data_(i) {++count;}
118 non_default_test_allocator(const non_default_test_allocator& a) throw()
119 : data_(a.data_) {++count;}
120 template <class U> non_default_test_allocator(const non_default_test_allocator<U>& a) throw()
121 : data_(a.data_) {++count;}
122 ~non_default_test_allocator() throw() {assert(data_ >= 0); --count; data_ = -1;}
123 pointer address(reference x) const {return &x;}
124 const_pointer address(const_reference x) const {return &x;}
125 pointer allocate(size_type n, const void* = 0)
126 {
127 assert(data_ >= 0);
128 if (time_to_throw >= throw_after) {
129#ifndef _LIBCPP_NO_EXCEPTIONS
130 throw std::bad_alloc();
131#else
132 std::terminate();
133#endif
134 }
135 ++time_to_throw;
136 ++alloc_count;
Eric Fiselierb11df182015-06-14 23:30:09 +0000137 return (pointer)::operator new (n * sizeof(T));
Marshall Clow5f7c2db2014-04-18 17:23:36 +0000138 }
Eric Fiselier7626f772016-04-28 03:17:56 +0000139 void deallocate(pointer p, size_type)
Eric Fiselierb11df182015-06-14 23:30:09 +0000140 {assert(data_ >= 0); --alloc_count; ::operator delete((void*)p); }
Marshall Clow5f7c2db2014-04-18 17:23:36 +0000141 size_type max_size() const throw()
142 {return UINT_MAX / sizeof(T);}
Eric Fiselier4fae5022016-06-15 01:53:32 +0000143#if TEST_STD_VER < 11
Marshall Clow5f7c2db2014-04-18 17:23:36 +0000144 void construct(pointer p, const T& val)
Eric Fiselier4fae5022016-06-15 01:53:32 +0000145 {::new(static_cast<void*>(p)) T(val);}
146#else
147 template <class U> void construct(pointer p, U&& val)
148 {::new(static_cast<void*>(p)) T(std::forward<U>(val));}
149#endif
Marshall Clow5f7c2db2014-04-18 17:23:36 +0000150 void destroy(pointer p) {p->~T();}
151
152 friend bool operator==(const non_default_test_allocator& x, const non_default_test_allocator& y)
153 {return x.data_ == y.data_;}
154 friend bool operator!=(const non_default_test_allocator& x, const non_default_test_allocator& y)
155 {return !(x == y);}
156};
157
Marshall Clowc3deeb52013-12-03 00:18:10 +0000158template <>
159class test_allocator<void>
160 : public test_alloc_base
161{
162 int data_;
163
164 template <class U> friend class test_allocator;
165public:
166
167 typedef unsigned size_type;
168 typedef int difference_type;
169 typedef void value_type;
170 typedef value_type* pointer;
171 typedef const value_type* const_pointer;
172
173 template <class U> struct rebind {typedef test_allocator<U> other;};
174
Eric Fiselierfc8e8c02015-08-28 05:00:25 +0000175 test_allocator() throw() : data_(0) {}
Marshall Clowc3deeb52013-12-03 00:18:10 +0000176 explicit test_allocator(int i) throw() : data_(i) {}
177 test_allocator(const test_allocator& a) throw()
178 : data_(a.data_) {}
179 template <class U> test_allocator(const test_allocator<U>& a) throw()
180 : data_(a.data_) {}
Eric Fiselierfc8e8c02015-08-28 05:00:25 +0000181 ~test_allocator() throw() {data_ = -1;}
Marshall Clowc3deeb52013-12-03 00:18:10 +0000182
183 friend bool operator==(const test_allocator& x, const test_allocator& y)
184 {return x.data_ == y.data_;}
185 friend bool operator!=(const test_allocator& x, const test_allocator& y)
186 {return !(x == y);}
187};
188
Howard Hinnant3e519522010-05-11 19:42:16 +0000189template <class T>
190class other_allocator
191{
192 int data_;
193
194 template <class U> friend class other_allocator;
195
196public:
197 typedef T value_type;
198
199 other_allocator() : data_(-1) {}
200 explicit other_allocator(int i) : data_(i) {}
201 template <class U> other_allocator(const other_allocator<U>& a)
202 : data_(a.data_) {}
203 T* allocate(std::size_t n)
Eric Fiselierb11df182015-06-14 23:30:09 +0000204 {return (T*)::operator new(n * sizeof(T));}
Eric Fiselier7626f772016-04-28 03:17:56 +0000205 void deallocate(T* p, std::size_t)
Eric Fiselierb11df182015-06-14 23:30:09 +0000206 {::operator delete((void*)p);}
Howard Hinnant3e519522010-05-11 19:42:16 +0000207
208 other_allocator select_on_container_copy_construction() const
209 {return other_allocator(-2);}
210
211 friend bool operator==(const other_allocator& x, const other_allocator& y)
212 {return x.data_ == y.data_;}
213 friend bool operator!=(const other_allocator& x, const other_allocator& y)
214 {return !(x == y);}
215
216 typedef std::true_type propagate_on_container_copy_assignment;
217 typedef std::true_type propagate_on_container_move_assignment;
218 typedef std::true_type propagate_on_container_swap;
219
220#ifdef _LIBCPP_HAS_NO_ADVANCED_SFINAE
221 std::size_t max_size() const
222 {return UINT_MAX / sizeof(T);}
Howard Hinnant8f2f7e72010-08-22 00:15:28 +0000223#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnant3e519522010-05-11 19:42:16 +0000224
225};
226
Howard Hinnant8f2f7e72010-08-22 00:15:28 +0000227#endif // TEST_ALLOCATOR_H