blob: e77796b676e748b86f8870e2281b15f5f10e8767 [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
Howard Hinnant3e519522010-05-11 19:42:16 +000013#include <type_traits>
Howard Hinnant3e519522010-05-11 19:42:16 +000014#include <new>
Eric Fiselier653179b2016-10-08 00:57:56 +000015#include <memory>
Eric Fiselier1286bc52016-12-11 03:41:12 +000016#include <utility>
Eric Fiselier653179b2016-10-08 00:57:56 +000017#include <cstddef>
18#include <cstdlib>
Howard Hinnant3e519522010-05-11 19:42:16 +000019#include <climits>
Marshall Clowc3deeb52013-12-03 00:18:10 +000020#include <cassert>
Howard Hinnant3e519522010-05-11 19:42:16 +000021
Marshall Clowcbf166a2015-06-03 19:56:43 +000022#include "test_macros.h"
23
Eric Fiselier55b31b4e2016-11-23 01:18:56 +000024template <class Alloc>
Eric Fiselier341c9dd2016-11-23 09:16:12 +000025inline typename std::allocator_traits<Alloc>::size_type
26alloc_max_size(Alloc const &a) {
Eric Fiselier55b31b4e2016-11-23 01:18:56 +000027 typedef std::allocator_traits<Alloc> AT;
28 return AT::max_size(a);
29}
30
Howard Hinnant3e519522010-05-11 19:42:16 +000031class test_alloc_base
32{
33protected:
Marshall Clowc3deeb52013-12-03 00:18:10 +000034 static int time_to_throw;
Howard Hinnant3e519522010-05-11 19:42:16 +000035public:
36 static int throw_after;
Marshall Clowc3deeb52013-12-03 00:18:10 +000037 static int count;
38 static int alloc_count;
Howard Hinnant3e519522010-05-11 19:42:16 +000039};
40
41int test_alloc_base::count = 0;
Marshall Clowc3deeb52013-12-03 00:18:10 +000042int test_alloc_base::time_to_throw = 0;
43int test_alloc_base::alloc_count = 0;
Howard Hinnant3e519522010-05-11 19:42:16 +000044int test_alloc_base::throw_after = INT_MAX;
45
46template <class T>
47class test_allocator
48 : public test_alloc_base
49{
Eric Fiselier1286bc52016-12-11 03:41:12 +000050 int data_; // participates in equality
51 int id_; // unique identifier, doesn't participate in equality
Howard Hinnant3e519522010-05-11 19:42:16 +000052 template <class U> friend class test_allocator;
53public:
54
55 typedef unsigned size_type;
56 typedef int difference_type;
57 typedef T value_type;
58 typedef value_type* pointer;
59 typedef const value_type* const_pointer;
60 typedef typename std::add_lvalue_reference<value_type>::type reference;
61 typedef typename std::add_lvalue_reference<const value_type>::type const_reference;
62
63 template <class U> struct rebind {typedef test_allocator<U> other;};
64
Eric Fiselier1286bc52016-12-11 03:41:12 +000065 test_allocator() TEST_NOEXCEPT : data_(0), id_(0) {++count;}
66 explicit test_allocator(int i, int id = 0) TEST_NOEXCEPT : data_(i), id_(id)
67 {++count;}
Eric Fiselier3ca45662016-12-11 02:47:36 +000068 test_allocator(const test_allocator& a) TEST_NOEXCEPT
Eric Fiselier1286bc52016-12-11 03:41:12 +000069 : data_(a.data_), id_(a.id_) {++count;}
Eric Fiselier3ca45662016-12-11 02:47:36 +000070 template <class U> test_allocator(const test_allocator<U>& a) TEST_NOEXCEPT
Eric Fiselier1286bc52016-12-11 03:41:12 +000071 : data_(a.data_), id_(a.id_) {++count;}
72 ~test_allocator() TEST_NOEXCEPT {
73 assert(data_ >= 0); assert(id_ >= 0);
74 --count; data_ = -1; id_ = -1;
75 }
Howard Hinnant3e519522010-05-11 19:42:16 +000076 pointer address(reference x) const {return &x;}
77 const_pointer address(const_reference x) const {return &x;}
78 pointer allocate(size_type n, const void* = 0)
79 {
Marshall Clowc3deeb52013-12-03 00:18:10 +000080 assert(data_ >= 0);
81 if (time_to_throw >= throw_after) {
Eric Fiselier54613ab2016-09-25 03:34:28 +000082#ifndef TEST_HAS_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +000083 throw std::bad_alloc();
Howard Hinnant65a87cc2013-03-23 17:27:16 +000084#else
85 std::terminate();
86#endif
87 }
Marshall Clowc3deeb52013-12-03 00:18:10 +000088 ++time_to_throw;
89 ++alloc_count;
Eric Fiselierb11df182015-06-14 23:30:09 +000090 return (pointer)::operator new(n * sizeof(T));
Howard Hinnant3e519522010-05-11 19:42:16 +000091 }
Eric Fiselier7626f772016-04-28 03:17:56 +000092 void deallocate(pointer p, size_type)
Eric Fiselierb11df182015-06-14 23:30:09 +000093 {assert(data_ >= 0); --alloc_count; ::operator delete((void*)p);}
Eric Fiselier3ca45662016-12-11 02:47:36 +000094 size_type max_size() const TEST_NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +000095 {return UINT_MAX / sizeof(T);}
Eric Fiselier4fae5022016-06-15 01:53:32 +000096#if TEST_STD_VER < 11
Howard Hinnant3e519522010-05-11 19:42:16 +000097 void construct(pointer p, const T& val)
Eric Fiselier4fae5022016-06-15 01:53:32 +000098 {::new(static_cast<void*>(p)) T(val);}
99#else
100 template <class U> void construct(pointer p, U&& val)
101 {::new(static_cast<void*>(p)) T(std::forward<U>(val));}
102#endif
Eric Fiselier408d6eb2016-06-22 01:02:08 +0000103 void destroy(pointer p)
Eric Fiselier89c91912016-10-07 18:51:33 +0000104 {p->~T();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000105 friend bool operator==(const test_allocator& x, const test_allocator& y)
106 {return x.data_ == y.data_;}
107 friend bool operator!=(const test_allocator& x, const test_allocator& y)
108 {return !(x == y);}
Eric Fiselier1286bc52016-12-11 03:41:12 +0000109
110 int get_data() const { return data_; }
111 int get_id() const { return id_; }
Howard Hinnant3e519522010-05-11 19:42:16 +0000112};
113
Marshall Clow5f7c2db2014-04-18 17:23:36 +0000114template <class T>
115class non_default_test_allocator
116 : public test_alloc_base
117{
118 int data_;
119
120 template <class U> friend class non_default_test_allocator;
121public:
122
123 typedef unsigned size_type;
124 typedef int difference_type;
125 typedef T value_type;
126 typedef value_type* pointer;
127 typedef const value_type* const_pointer;
128 typedef typename std::add_lvalue_reference<value_type>::type reference;
129 typedef typename std::add_lvalue_reference<const value_type>::type const_reference;
130
131 template <class U> struct rebind {typedef non_default_test_allocator<U> other;};
132
Eric Fiselier3ca45662016-12-11 02:47:36 +0000133// non_default_test_allocator() TEST_NOEXCEPT : data_(0) {++count;}
134 explicit non_default_test_allocator(int i) TEST_NOEXCEPT : data_(i) {++count;}
135 non_default_test_allocator(const non_default_test_allocator& a) TEST_NOEXCEPT
Marshall Clow5f7c2db2014-04-18 17:23:36 +0000136 : data_(a.data_) {++count;}
Eric Fiselier3ca45662016-12-11 02:47:36 +0000137 template <class U> non_default_test_allocator(const non_default_test_allocator<U>& a) TEST_NOEXCEPT
Marshall Clow5f7c2db2014-04-18 17:23:36 +0000138 : data_(a.data_) {++count;}
Eric Fiselier3ca45662016-12-11 02:47:36 +0000139 ~non_default_test_allocator() TEST_NOEXCEPT {assert(data_ >= 0); --count; data_ = -1;}
Marshall Clow5f7c2db2014-04-18 17:23:36 +0000140 pointer address(reference x) const {return &x;}
141 const_pointer address(const_reference x) const {return &x;}
142 pointer allocate(size_type n, const void* = 0)
143 {
144 assert(data_ >= 0);
145 if (time_to_throw >= throw_after) {
Eric Fiselier54613ab2016-09-25 03:34:28 +0000146#ifndef TEST_HAS_NO_EXCEPTIONS
Marshall Clow5f7c2db2014-04-18 17:23:36 +0000147 throw std::bad_alloc();
148#else
149 std::terminate();
150#endif
151 }
152 ++time_to_throw;
153 ++alloc_count;
Eric Fiselierb11df182015-06-14 23:30:09 +0000154 return (pointer)::operator new (n * sizeof(T));
Marshall Clow5f7c2db2014-04-18 17:23:36 +0000155 }
Eric Fiselier7626f772016-04-28 03:17:56 +0000156 void deallocate(pointer p, size_type)
Eric Fiselierb11df182015-06-14 23:30:09 +0000157 {assert(data_ >= 0); --alloc_count; ::operator delete((void*)p); }
Eric Fiselier3ca45662016-12-11 02:47:36 +0000158 size_type max_size() const TEST_NOEXCEPT
Marshall Clow5f7c2db2014-04-18 17:23:36 +0000159 {return UINT_MAX / sizeof(T);}
Eric Fiselier4fae5022016-06-15 01:53:32 +0000160#if TEST_STD_VER < 11
Marshall Clow5f7c2db2014-04-18 17:23:36 +0000161 void construct(pointer p, const T& val)
Eric Fiselier4fae5022016-06-15 01:53:32 +0000162 {::new(static_cast<void*>(p)) T(val);}
163#else
164 template <class U> void construct(pointer p, U&& val)
165 {::new(static_cast<void*>(p)) T(std::forward<U>(val));}
166#endif
Marshall Clow5f7c2db2014-04-18 17:23:36 +0000167 void destroy(pointer p) {p->~T();}
168
169 friend bool operator==(const non_default_test_allocator& x, const non_default_test_allocator& y)
170 {return x.data_ == y.data_;}
171 friend bool operator!=(const non_default_test_allocator& x, const non_default_test_allocator& y)
172 {return !(x == y);}
173};
174
Marshall Clowc3deeb52013-12-03 00:18:10 +0000175template <>
176class test_allocator<void>
177 : public test_alloc_base
178{
179 int data_;
Eric Fiselier1286bc52016-12-11 03:41:12 +0000180 int id_;
Marshall Clowc3deeb52013-12-03 00:18:10 +0000181
182 template <class U> friend class test_allocator;
183public:
184
185 typedef unsigned size_type;
186 typedef int difference_type;
187 typedef void value_type;
188 typedef value_type* pointer;
189 typedef const value_type* const_pointer;
190
191 template <class U> struct rebind {typedef test_allocator<U> other;};
192
Eric Fiselier1286bc52016-12-11 03:41:12 +0000193 test_allocator() TEST_NOEXCEPT : data_(0), id_(0) {}
194 explicit test_allocator(int i, int id = 0) TEST_NOEXCEPT : data_(i), id_(id) {}
Eric Fiselier3ca45662016-12-11 02:47:36 +0000195 test_allocator(const test_allocator& a) TEST_NOEXCEPT
Eric Fiselier1286bc52016-12-11 03:41:12 +0000196 : data_(a.data_), id_(a.id_) {}
Eric Fiselier3ca45662016-12-11 02:47:36 +0000197 template <class U> test_allocator(const test_allocator<U>& a) TEST_NOEXCEPT
Eric Fiselier1286bc52016-12-11 03:41:12 +0000198 : data_(a.data_), id_(a.id_) {}
199 ~test_allocator() TEST_NOEXCEPT {data_ = -1; id_ = -1; }
200
201 int get_id() const { return id_; }
202 int get_data() const { return data_; }
Marshall Clowc3deeb52013-12-03 00:18:10 +0000203
204 friend bool operator==(const test_allocator& x, const test_allocator& y)
205 {return x.data_ == y.data_;}
206 friend bool operator!=(const test_allocator& x, const test_allocator& y)
207 {return !(x == y);}
208};
209
Howard Hinnant3e519522010-05-11 19:42:16 +0000210template <class T>
211class other_allocator
212{
213 int data_;
214
215 template <class U> friend class other_allocator;
216
217public:
218 typedef T value_type;
219
220 other_allocator() : data_(-1) {}
221 explicit other_allocator(int i) : data_(i) {}
222 template <class U> other_allocator(const other_allocator<U>& a)
223 : data_(a.data_) {}
224 T* allocate(std::size_t n)
Eric Fiselierb11df182015-06-14 23:30:09 +0000225 {return (T*)::operator new(n * sizeof(T));}
Eric Fiselier7626f772016-04-28 03:17:56 +0000226 void deallocate(T* p, std::size_t)
Eric Fiselierb11df182015-06-14 23:30:09 +0000227 {::operator delete((void*)p);}
Howard Hinnant3e519522010-05-11 19:42:16 +0000228
229 other_allocator select_on_container_copy_construction() const
230 {return other_allocator(-2);}
231
232 friend bool operator==(const other_allocator& x, const other_allocator& y)
233 {return x.data_ == y.data_;}
234 friend bool operator!=(const other_allocator& x, const other_allocator& y)
235 {return !(x == y);}
236
237 typedef std::true_type propagate_on_container_copy_assignment;
238 typedef std::true_type propagate_on_container_move_assignment;
239 typedef std::true_type propagate_on_container_swap;
240
Eric Fiselier54613ab2016-09-25 03:34:28 +0000241#if TEST_STD_VER < 11
Howard Hinnant3e519522010-05-11 19:42:16 +0000242 std::size_t max_size() const
243 {return UINT_MAX / sizeof(T);}
Eric Fiselier54613ab2016-09-25 03:34:28 +0000244#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000245
246};
247
Marshall Clowdc3eb832016-07-11 21:38:08 +0000248#if TEST_STD_VER >= 11
249
250struct Ctor_Tag {};
251
252template <typename T> class TaggingAllocator;
253
254struct Tag_X {
255 // All constructors must be passed the Tag type.
256
257 // DefaultInsertable into vector<X, TaggingAllocator<X>>,
258 Tag_X(Ctor_Tag) {}
259 // CopyInsertable into vector<X, TaggingAllocator<X>>,
260 Tag_X(Ctor_Tag, const Tag_X&) {}
261 // MoveInsertable into vector<X, TaggingAllocator<X>>, and
262 Tag_X(Ctor_Tag, Tag_X&&) {}
263
264 // EmplaceConstructible into vector<X, TaggingAllocator<X>> from args.
265 template<typename... Args>
266 Tag_X(Ctor_Tag, Args&&...) { }
267
268 // not DefaultConstructible, CopyConstructible or MoveConstructible.
269 Tag_X() = delete;
270 Tag_X(const Tag_X&) = delete;
271 Tag_X(Tag_X&&) = delete;
272
273 // CopyAssignable.
274 Tag_X& operator=(const Tag_X&) { return *this; }
275
276 // MoveAssignable.
277 Tag_X& operator=(Tag_X&&) { return *this; }
278
279private:
280 // Not Destructible.
281 ~Tag_X() { }
282
283 // Erasable from vector<X, TaggingAllocator<X>>.
284 friend class TaggingAllocator<Tag_X>;
285};
286
287
288template<typename T>
289class TaggingAllocator {
290public:
291 using value_type = T;
292 TaggingAllocator() = default;
293
294 template<typename U>
295 TaggingAllocator(const TaggingAllocator<U>&) { }
296
297 T* allocate(std::size_t n) { return std::allocator<T>{}.allocate(n); }
298
299 void deallocate(T* p, std::size_t n) { std::allocator<T>{}.deallocate(p, n); }
300
301 template<typename... Args>
302 void construct(Tag_X* p, Args&&... args)
303 { ::new((void*)p) Tag_X(Ctor_Tag{}, std::forward<Args>(args)...); }
304
305 template<typename U, typename... Args>
306 void construct(U* p, Args&&... args)
307 { ::new((void*)p) U(std::forward<Args>(args)...); }
308
309 template<typename U, typename... Args>
310 void destroy(U* p)
Eric Fiselier89c91912016-10-07 18:51:33 +0000311 { p->~U(); }
Marshall Clowdc3eb832016-07-11 21:38:08 +0000312};
313
314template<typename T, typename U>
315bool
316operator==(const TaggingAllocator<T>&, const TaggingAllocator<U>&)
317{ return true; }
318
319template<typename T, typename U>
320bool
321operator!=(const TaggingAllocator<T>&, const TaggingAllocator<U>&)
322{ return false; }
323#endif
324
Eric Fiselier69a4f662016-10-08 00:56:22 +0000325template <std::size_t MaxAllocs>
326struct limited_alloc_handle {
327 std::size_t outstanding_;
328 void* last_alloc_;
329
330 limited_alloc_handle() : outstanding_(0), last_alloc_(nullptr) {}
331
332 template <class T>
333 T *allocate(std::size_t N) {
334 if (N + outstanding_ > MaxAllocs)
335 TEST_THROW(std::bad_alloc());
336 last_alloc_ = ::operator new(N*sizeof(T));
337 outstanding_ += N;
338 return static_cast<T*>(last_alloc_);
339 }
340
341 void deallocate(void* ptr, std::size_t N) {
342 if (ptr == last_alloc_) {
343 last_alloc_ = nullptr;
344 assert(outstanding_ >= N);
345 outstanding_ -= N;
346 }
347 ::operator delete(ptr);
348 }
349};
350
351template <class T, std::size_t N>
352class limited_allocator
353{
Eric Fiseliera9fcc1d2016-10-12 11:35:37 +0000354 template <class U, std::size_t UN> friend class limited_allocator;
Eric Fiselier69a4f662016-10-08 00:56:22 +0000355 typedef limited_alloc_handle<N> BuffT;
356 std::shared_ptr<BuffT> handle_;
357public:
358 typedef T value_type;
359 typedef value_type* pointer;
360 typedef const value_type* const_pointer;
361 typedef value_type& reference;
362 typedef const value_type& const_reference;
363 typedef std::size_t size_type;
364 typedef std::ptrdiff_t difference_type;
365
366 template <class U> struct rebind { typedef limited_allocator<U, N> other; };
367
368 limited_allocator() : handle_(new BuffT) {}
369
370 limited_allocator(limited_allocator const& other) : handle_(other.handle_) {}
371
372 template <class U>
373 explicit limited_allocator(limited_allocator<U, N> const& other)
374 : handle_(other.handle_) {}
375
376private:
377 limited_allocator& operator=(const limited_allocator&);// = delete;
378
379public:
380 pointer allocate(size_type n) { return handle_->template allocate<T>(n); }
381 void deallocate(pointer p, size_type n) { handle_->deallocate(p, n); }
382 size_type max_size() const {return N;}
383
384 BuffT* getHandle() const { return handle_.get(); }
385};
386
387template <class T, class U, std::size_t N>
388inline bool operator==(limited_allocator<T, N> const& LHS,
389 limited_allocator<U, N> const& RHS) {
390 return LHS.getHandle() == RHS.getHandle();
391}
392
393template <class T, class U, std::size_t N>
394inline bool operator!=(limited_allocator<T, N> const& LHS,
395 limited_allocator<U, N> const& RHS) {
396 return !(LHS == RHS);
397}
398
Marshall Clowdc3eb832016-07-11 21:38:08 +0000399
Howard Hinnant8f2f7e72010-08-22 00:15:28 +0000400#endif // TEST_ALLOCATOR_H