blob: 60f9a21b244dedd06782b98d3f6a7d442c68387a [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;
Eric Fiselier8cef7fd2018-06-05 22:32:52 +000039 static int copied;
40 static int moved;
41 static int converted;
42
43 const static int destructed_value = -1;
44 const static int default_value = 0;
45 const static int moved_value = INT_MAX;
46
47 static void clear() {
48 assert(count == 0 && "clearing leaking allocator data?");
49 count = 0;
50 time_to_throw = 0;
51 alloc_count = 0;
52 throw_after = INT_MAX;
53 clear_ctor_counters();
54 }
55
56 static void clear_ctor_counters() {
57 copied = 0;
58 moved = 0;
59 converted = 0;
60 }
Howard Hinnant3e519522010-05-11 19:42:16 +000061};
62
63int test_alloc_base::count = 0;
Marshall Clowc3deeb52013-12-03 00:18:10 +000064int test_alloc_base::time_to_throw = 0;
65int test_alloc_base::alloc_count = 0;
Howard Hinnant3e519522010-05-11 19:42:16 +000066int test_alloc_base::throw_after = INT_MAX;
Eric Fiselier8cef7fd2018-06-05 22:32:52 +000067int test_alloc_base::copied = 0;
68int test_alloc_base::moved = 0;
69int test_alloc_base::converted = 0;
Howard Hinnant3e519522010-05-11 19:42:16 +000070
71template <class T>
72class test_allocator
73 : public test_alloc_base
74{
Eric Fiselier1286bc52016-12-11 03:41:12 +000075 int data_; // participates in equality
76 int id_; // unique identifier, doesn't participate in equality
Howard Hinnant3e519522010-05-11 19:42:16 +000077 template <class U> friend class test_allocator;
78public:
79
80 typedef unsigned size_type;
81 typedef int difference_type;
82 typedef T value_type;
83 typedef value_type* pointer;
84 typedef const value_type* const_pointer;
85 typedef typename std::add_lvalue_reference<value_type>::type reference;
86 typedef typename std::add_lvalue_reference<const value_type>::type const_reference;
87
88 template <class U> struct rebind {typedef test_allocator<U> other;};
89
Eric Fiselier1286bc52016-12-11 03:41:12 +000090 test_allocator() TEST_NOEXCEPT : data_(0), id_(0) {++count;}
91 explicit test_allocator(int i, int id = 0) TEST_NOEXCEPT : data_(i), id_(id)
92 {++count;}
Eric Fiselier8cef7fd2018-06-05 22:32:52 +000093 test_allocator(const test_allocator& a) TEST_NOEXCEPT : data_(a.data_),
94 id_(a.id_) {
95 ++count;
96 ++copied;
97 assert(a.data_ != destructed_value && a.id_ != destructed_value &&
98 "copying from destroyed allocator");
99 }
100#if TEST_STD_VER >= 11
101 test_allocator(test_allocator&& a) TEST_NOEXCEPT : data_(a.data_),
102 id_(a.id_) {
103 ++count;
104 ++moved;
105 assert(a.data_ != destructed_value && a.id_ != destructed_value &&
106 "moving from destroyed allocator");
107 a.data_ = moved_value;
108 a.id_ = moved_value;
109 }
110#endif
111 template <class U>
112 test_allocator(const test_allocator<U>& a) TEST_NOEXCEPT : data_(a.data_),
113 id_(a.id_) {
114 ++count;
115 ++converted;
116 }
Eric Fiselier1286bc52016-12-11 03:41:12 +0000117 ~test_allocator() TEST_NOEXCEPT {
118 assert(data_ >= 0); assert(id_ >= 0);
Eric Fiselier8cef7fd2018-06-05 22:32:52 +0000119 --count;
120 data_ = destructed_value;
121 id_ = destructed_value;
Eric Fiselier1286bc52016-12-11 03:41:12 +0000122 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000123 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 {
Marshall Clowc3deeb52013-12-03 00:18:10 +0000127 assert(data_ >= 0);
128 if (time_to_throw >= throw_after) {
Eric Fiselier54613ab2016-09-25 03:34:28 +0000129#ifndef TEST_HAS_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +0000130 throw std::bad_alloc();
Howard Hinnant65a87cc2013-03-23 17:27:16 +0000131#else
132 std::terminate();
133#endif
134 }
Marshall Clowc3deeb52013-12-03 00:18:10 +0000135 ++time_to_throw;
136 ++alloc_count;
Eric Fiselierb11df182015-06-14 23:30:09 +0000137 return (pointer)::operator new(n * sizeof(T));
Howard Hinnant3e519522010-05-11 19:42:16 +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);}
Eric Fiselier3ca45662016-12-11 02:47:36 +0000141 size_type max_size() const TEST_NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000142 {return UINT_MAX / sizeof(T);}
Eric Fiselier4fae5022016-06-15 01:53:32 +0000143#if TEST_STD_VER < 11
Howard Hinnant3e519522010-05-11 19:42:16 +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
Eric Fiselier408d6eb2016-06-22 01:02:08 +0000150 void destroy(pointer p)
Eric Fiselier89c91912016-10-07 18:51:33 +0000151 {p->~T();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000152 friend bool operator==(const test_allocator& x, const test_allocator& y)
153 {return x.data_ == y.data_;}
154 friend bool operator!=(const test_allocator& x, const test_allocator& y)
155 {return !(x == y);}
Eric Fiselier1286bc52016-12-11 03:41:12 +0000156
157 int get_data() const { return data_; }
158 int get_id() const { return id_; }
Howard Hinnant3e519522010-05-11 19:42:16 +0000159};
160
Marshall Clow5f7c2db2014-04-18 17:23:36 +0000161template <class T>
162class non_default_test_allocator
163 : public test_alloc_base
164{
165 int data_;
166
167 template <class U> friend class non_default_test_allocator;
168public:
169
170 typedef unsigned size_type;
171 typedef int difference_type;
172 typedef T value_type;
173 typedef value_type* pointer;
174 typedef const value_type* const_pointer;
175 typedef typename std::add_lvalue_reference<value_type>::type reference;
176 typedef typename std::add_lvalue_reference<const value_type>::type const_reference;
177
178 template <class U> struct rebind {typedef non_default_test_allocator<U> other;};
179
Eric Fiselier3ca45662016-12-11 02:47:36 +0000180// non_default_test_allocator() TEST_NOEXCEPT : data_(0) {++count;}
181 explicit non_default_test_allocator(int i) TEST_NOEXCEPT : data_(i) {++count;}
182 non_default_test_allocator(const non_default_test_allocator& a) TEST_NOEXCEPT
Marshall Clow5f7c2db2014-04-18 17:23:36 +0000183 : data_(a.data_) {++count;}
Eric Fiselier3ca45662016-12-11 02:47:36 +0000184 template <class U> non_default_test_allocator(const non_default_test_allocator<U>& a) TEST_NOEXCEPT
Marshall Clow5f7c2db2014-04-18 17:23:36 +0000185 : data_(a.data_) {++count;}
Eric Fiselier3ca45662016-12-11 02:47:36 +0000186 ~non_default_test_allocator() TEST_NOEXCEPT {assert(data_ >= 0); --count; data_ = -1;}
Marshall Clow5f7c2db2014-04-18 17:23:36 +0000187 pointer address(reference x) const {return &x;}
188 const_pointer address(const_reference x) const {return &x;}
189 pointer allocate(size_type n, const void* = 0)
190 {
191 assert(data_ >= 0);
192 if (time_to_throw >= throw_after) {
Eric Fiselier54613ab2016-09-25 03:34:28 +0000193#ifndef TEST_HAS_NO_EXCEPTIONS
Marshall Clow5f7c2db2014-04-18 17:23:36 +0000194 throw std::bad_alloc();
195#else
196 std::terminate();
197#endif
198 }
199 ++time_to_throw;
200 ++alloc_count;
Eric Fiselierb11df182015-06-14 23:30:09 +0000201 return (pointer)::operator new (n * sizeof(T));
Marshall Clow5f7c2db2014-04-18 17:23:36 +0000202 }
Eric Fiselier7626f772016-04-28 03:17:56 +0000203 void deallocate(pointer p, size_type)
Eric Fiselierb11df182015-06-14 23:30:09 +0000204 {assert(data_ >= 0); --alloc_count; ::operator delete((void*)p); }
Eric Fiselier3ca45662016-12-11 02:47:36 +0000205 size_type max_size() const TEST_NOEXCEPT
Marshall Clow5f7c2db2014-04-18 17:23:36 +0000206 {return UINT_MAX / sizeof(T);}
Eric Fiselier4fae5022016-06-15 01:53:32 +0000207#if TEST_STD_VER < 11
Marshall Clow5f7c2db2014-04-18 17:23:36 +0000208 void construct(pointer p, const T& val)
Eric Fiselier4fae5022016-06-15 01:53:32 +0000209 {::new(static_cast<void*>(p)) T(val);}
210#else
211 template <class U> void construct(pointer p, U&& val)
212 {::new(static_cast<void*>(p)) T(std::forward<U>(val));}
213#endif
Marshall Clow5f7c2db2014-04-18 17:23:36 +0000214 void destroy(pointer p) {p->~T();}
215
216 friend bool operator==(const non_default_test_allocator& x, const non_default_test_allocator& y)
217 {return x.data_ == y.data_;}
218 friend bool operator!=(const non_default_test_allocator& x, const non_default_test_allocator& y)
219 {return !(x == y);}
220};
221
Marshall Clowc3deeb52013-12-03 00:18:10 +0000222template <>
223class test_allocator<void>
224 : public test_alloc_base
225{
226 int data_;
Eric Fiselier1286bc52016-12-11 03:41:12 +0000227 int id_;
Marshall Clowc3deeb52013-12-03 00:18:10 +0000228
229 template <class U> friend class test_allocator;
230public:
231
232 typedef unsigned size_type;
233 typedef int difference_type;
234 typedef void value_type;
235 typedef value_type* pointer;
236 typedef const value_type* const_pointer;
237
238 template <class U> struct rebind {typedef test_allocator<U> other;};
239
Eric Fiselier1286bc52016-12-11 03:41:12 +0000240 test_allocator() TEST_NOEXCEPT : data_(0), id_(0) {}
241 explicit test_allocator(int i, int id = 0) TEST_NOEXCEPT : data_(i), id_(id) {}
Eric Fiselier3ca45662016-12-11 02:47:36 +0000242 test_allocator(const test_allocator& a) TEST_NOEXCEPT
Eric Fiselier1286bc52016-12-11 03:41:12 +0000243 : data_(a.data_), id_(a.id_) {}
Eric Fiselier3ca45662016-12-11 02:47:36 +0000244 template <class U> test_allocator(const test_allocator<U>& a) TEST_NOEXCEPT
Eric Fiselier1286bc52016-12-11 03:41:12 +0000245 : data_(a.data_), id_(a.id_) {}
246 ~test_allocator() TEST_NOEXCEPT {data_ = -1; id_ = -1; }
247
248 int get_id() const { return id_; }
249 int get_data() const { return data_; }
Marshall Clowc3deeb52013-12-03 00:18:10 +0000250
251 friend bool operator==(const test_allocator& x, const test_allocator& y)
252 {return x.data_ == y.data_;}
253 friend bool operator!=(const test_allocator& x, const test_allocator& y)
254 {return !(x == y);}
255};
256
Howard Hinnant3e519522010-05-11 19:42:16 +0000257template <class T>
258class other_allocator
259{
260 int data_;
261
262 template <class U> friend class other_allocator;
263
264public:
265 typedef T value_type;
266
267 other_allocator() : data_(-1) {}
268 explicit other_allocator(int i) : data_(i) {}
269 template <class U> other_allocator(const other_allocator<U>& a)
270 : data_(a.data_) {}
271 T* allocate(std::size_t n)
Eric Fiselierb11df182015-06-14 23:30:09 +0000272 {return (T*)::operator new(n * sizeof(T));}
Eric Fiselier7626f772016-04-28 03:17:56 +0000273 void deallocate(T* p, std::size_t)
Eric Fiselierb11df182015-06-14 23:30:09 +0000274 {::operator delete((void*)p);}
Howard Hinnant3e519522010-05-11 19:42:16 +0000275
276 other_allocator select_on_container_copy_construction() const
277 {return other_allocator(-2);}
278
279 friend bool operator==(const other_allocator& x, const other_allocator& y)
280 {return x.data_ == y.data_;}
281 friend bool operator!=(const other_allocator& x, const other_allocator& y)
282 {return !(x == y);}
283
284 typedef std::true_type propagate_on_container_copy_assignment;
285 typedef std::true_type propagate_on_container_move_assignment;
286 typedef std::true_type propagate_on_container_swap;
287
Eric Fiselier54613ab2016-09-25 03:34:28 +0000288#if TEST_STD_VER < 11
Howard Hinnant3e519522010-05-11 19:42:16 +0000289 std::size_t max_size() const
290 {return UINT_MAX / sizeof(T);}
Eric Fiselier54613ab2016-09-25 03:34:28 +0000291#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000292
293};
294
Marshall Clowdc3eb832016-07-11 21:38:08 +0000295#if TEST_STD_VER >= 11
296
297struct Ctor_Tag {};
298
299template <typename T> class TaggingAllocator;
300
301struct Tag_X {
302 // All constructors must be passed the Tag type.
303
304 // DefaultInsertable into vector<X, TaggingAllocator<X>>,
305 Tag_X(Ctor_Tag) {}
306 // CopyInsertable into vector<X, TaggingAllocator<X>>,
307 Tag_X(Ctor_Tag, const Tag_X&) {}
308 // MoveInsertable into vector<X, TaggingAllocator<X>>, and
309 Tag_X(Ctor_Tag, Tag_X&&) {}
310
311 // EmplaceConstructible into vector<X, TaggingAllocator<X>> from args.
312 template<typename... Args>
313 Tag_X(Ctor_Tag, Args&&...) { }
314
315 // not DefaultConstructible, CopyConstructible or MoveConstructible.
316 Tag_X() = delete;
317 Tag_X(const Tag_X&) = delete;
318 Tag_X(Tag_X&&) = delete;
319
320 // CopyAssignable.
321 Tag_X& operator=(const Tag_X&) { return *this; }
322
323 // MoveAssignable.
324 Tag_X& operator=(Tag_X&&) { return *this; }
325
326private:
327 // Not Destructible.
328 ~Tag_X() { }
329
330 // Erasable from vector<X, TaggingAllocator<X>>.
331 friend class TaggingAllocator<Tag_X>;
332};
333
334
335template<typename T>
336class TaggingAllocator {
337public:
338 using value_type = T;
339 TaggingAllocator() = default;
340
341 template<typename U>
342 TaggingAllocator(const TaggingAllocator<U>&) { }
343
344 T* allocate(std::size_t n) { return std::allocator<T>{}.allocate(n); }
345
346 void deallocate(T* p, std::size_t n) { std::allocator<T>{}.deallocate(p, n); }
347
348 template<typename... Args>
349 void construct(Tag_X* p, Args&&... args)
350 { ::new((void*)p) Tag_X(Ctor_Tag{}, std::forward<Args>(args)...); }
351
352 template<typename U, typename... Args>
353 void construct(U* p, Args&&... args)
354 { ::new((void*)p) U(std::forward<Args>(args)...); }
355
356 template<typename U, typename... Args>
357 void destroy(U* p)
Eric Fiselier89c91912016-10-07 18:51:33 +0000358 { p->~U(); }
Marshall Clowdc3eb832016-07-11 21:38:08 +0000359};
360
361template<typename T, typename U>
362bool
363operator==(const TaggingAllocator<T>&, const TaggingAllocator<U>&)
364{ return true; }
365
366template<typename T, typename U>
367bool
368operator!=(const TaggingAllocator<T>&, const TaggingAllocator<U>&)
369{ return false; }
370#endif
371
Eric Fiselier69a4f662016-10-08 00:56:22 +0000372template <std::size_t MaxAllocs>
373struct limited_alloc_handle {
374 std::size_t outstanding_;
375 void* last_alloc_;
376
377 limited_alloc_handle() : outstanding_(0), last_alloc_(nullptr) {}
378
379 template <class T>
380 T *allocate(std::size_t N) {
381 if (N + outstanding_ > MaxAllocs)
382 TEST_THROW(std::bad_alloc());
383 last_alloc_ = ::operator new(N*sizeof(T));
384 outstanding_ += N;
385 return static_cast<T*>(last_alloc_);
386 }
387
388 void deallocate(void* ptr, std::size_t N) {
389 if (ptr == last_alloc_) {
390 last_alloc_ = nullptr;
391 assert(outstanding_ >= N);
392 outstanding_ -= N;
393 }
394 ::operator delete(ptr);
395 }
396};
397
398template <class T, std::size_t N>
399class limited_allocator
400{
Eric Fiseliera9fcc1d2016-10-12 11:35:37 +0000401 template <class U, std::size_t UN> friend class limited_allocator;
Eric Fiselier69a4f662016-10-08 00:56:22 +0000402 typedef limited_alloc_handle<N> BuffT;
403 std::shared_ptr<BuffT> handle_;
404public:
405 typedef T value_type;
406 typedef value_type* pointer;
407 typedef const value_type* const_pointer;
408 typedef value_type& reference;
409 typedef const value_type& const_reference;
410 typedef std::size_t size_type;
411 typedef std::ptrdiff_t difference_type;
412
413 template <class U> struct rebind { typedef limited_allocator<U, N> other; };
414
415 limited_allocator() : handle_(new BuffT) {}
416
417 limited_allocator(limited_allocator const& other) : handle_(other.handle_) {}
418
419 template <class U>
420 explicit limited_allocator(limited_allocator<U, N> const& other)
421 : handle_(other.handle_) {}
422
423private:
424 limited_allocator& operator=(const limited_allocator&);// = delete;
425
426public:
427 pointer allocate(size_type n) { return handle_->template allocate<T>(n); }
428 void deallocate(pointer p, size_type n) { handle_->deallocate(p, n); }
429 size_type max_size() const {return N;}
430
431 BuffT* getHandle() const { return handle_.get(); }
432};
433
434template <class T, class U, std::size_t N>
435inline bool operator==(limited_allocator<T, N> const& LHS,
436 limited_allocator<U, N> const& RHS) {
437 return LHS.getHandle() == RHS.getHandle();
438}
439
440template <class T, class U, std::size_t N>
441inline bool operator!=(limited_allocator<T, N> const& LHS,
442 limited_allocator<U, N> const& RHS) {
443 return !(LHS == RHS);
444}
445
Marshall Clowdc3eb832016-07-11 21:38:08 +0000446
Howard Hinnant8f2f7e72010-08-22 00:15:28 +0000447#endif // TEST_ALLOCATOR_H