blob: 1d6acf5767eea44950e5e1fd10add3e71cd08370 [file] [log] [blame]
Marshall Clow354d39c2014-01-16 16:58:45 +00001//===----------------------------------------------------------------------===//
2//
Chandler Carruth57b08b02019-01-19 10:56:40 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Marshall Clow354d39c2014-01-16 16:58:45 +00006//
7//===----------------------------------------------------------------------===//
8
Howard Hinnant3e519522010-05-11 19:42:16 +00009#ifndef TEST_ALLOCATOR_H
10#define TEST_ALLOCATOR_H
11
Howard Hinnant3e519522010-05-11 19:42:16 +000012#include <type_traits>
Howard Hinnant3e519522010-05-11 19:42:16 +000013#include <new>
Eric Fiselier653179b2016-10-08 00:57:56 +000014#include <memory>
Eric Fiselier1286bc52016-12-11 03:41:12 +000015#include <utility>
Eric Fiselier653179b2016-10-08 00:57:56 +000016#include <cstddef>
17#include <cstdlib>
Howard Hinnant3e519522010-05-11 19:42:16 +000018#include <climits>
Marshall Clowc3deeb52013-12-03 00:18:10 +000019#include <cassert>
Howard Hinnant3e519522010-05-11 19:42:16 +000020
Marshall Clowcbf166a2015-06-03 19:56:43 +000021#include "test_macros.h"
22
Eric Fiselier55b31b4e2016-11-23 01:18:56 +000023template <class Alloc>
Eric Fiselier341c9dd2016-11-23 09:16:12 +000024inline typename std::allocator_traits<Alloc>::size_type
25alloc_max_size(Alloc const &a) {
Eric Fiselier55b31b4e2016-11-23 01:18:56 +000026 typedef std::allocator_traits<Alloc> AT;
27 return AT::max_size(a);
28}
29
Howard Hinnant3e519522010-05-11 19:42:16 +000030class test_alloc_base
31{
32protected:
Marshall Clowc3deeb52013-12-03 00:18:10 +000033 static int time_to_throw;
Howard Hinnant3e519522010-05-11 19:42:16 +000034public:
35 static int throw_after;
Marshall Clowc3deeb52013-12-03 00:18:10 +000036 static int count;
37 static int alloc_count;
Eric Fiselier8cef7fd2018-06-05 22:32:52 +000038 static int copied;
39 static int moved;
40 static int converted;
41
42 const static int destructed_value = -1;
43 const static int default_value = 0;
44 const static int moved_value = INT_MAX;
45
46 static void clear() {
47 assert(count == 0 && "clearing leaking allocator data?");
48 count = 0;
49 time_to_throw = 0;
50 alloc_count = 0;
51 throw_after = INT_MAX;
52 clear_ctor_counters();
53 }
54
55 static void clear_ctor_counters() {
56 copied = 0;
57 moved = 0;
58 converted = 0;
59 }
Howard Hinnant3e519522010-05-11 19:42:16 +000060};
61
62int test_alloc_base::count = 0;
Marshall Clowc3deeb52013-12-03 00:18:10 +000063int test_alloc_base::time_to_throw = 0;
64int test_alloc_base::alloc_count = 0;
Howard Hinnant3e519522010-05-11 19:42:16 +000065int test_alloc_base::throw_after = INT_MAX;
Eric Fiselier8cef7fd2018-06-05 22:32:52 +000066int test_alloc_base::copied = 0;
67int test_alloc_base::moved = 0;
68int test_alloc_base::converted = 0;
Howard Hinnant3e519522010-05-11 19:42:16 +000069
70template <class T>
71class test_allocator
72 : public test_alloc_base
73{
Eric Fiselier1286bc52016-12-11 03:41:12 +000074 int data_; // participates in equality
75 int id_; // unique identifier, doesn't participate in equality
Howard Hinnant3e519522010-05-11 19:42:16 +000076 template <class U> friend class test_allocator;
77public:
78
79 typedef unsigned size_type;
80 typedef int difference_type;
81 typedef T value_type;
82 typedef value_type* pointer;
83 typedef const value_type* const_pointer;
84 typedef typename std::add_lvalue_reference<value_type>::type reference;
85 typedef typename std::add_lvalue_reference<const value_type>::type const_reference;
86
87 template <class U> struct rebind {typedef test_allocator<U> other;};
88
Eric Fiselier1286bc52016-12-11 03:41:12 +000089 test_allocator() TEST_NOEXCEPT : data_(0), id_(0) {++count;}
90 explicit test_allocator(int i, int id = 0) TEST_NOEXCEPT : data_(i), id_(id)
91 {++count;}
Eric Fiselier8cef7fd2018-06-05 22:32:52 +000092 test_allocator(const test_allocator& a) TEST_NOEXCEPT : data_(a.data_),
93 id_(a.id_) {
94 ++count;
95 ++copied;
96 assert(a.data_ != destructed_value && a.id_ != destructed_value &&
97 "copying from destroyed allocator");
98 }
99#if TEST_STD_VER >= 11
100 test_allocator(test_allocator&& a) TEST_NOEXCEPT : data_(a.data_),
101 id_(a.id_) {
102 ++count;
103 ++moved;
104 assert(a.data_ != destructed_value && a.id_ != destructed_value &&
105 "moving from destroyed allocator");
106 a.data_ = moved_value;
107 a.id_ = moved_value;
108 }
109#endif
110 template <class U>
111 test_allocator(const test_allocator<U>& a) TEST_NOEXCEPT : data_(a.data_),
112 id_(a.id_) {
113 ++count;
114 ++converted;
115 }
Eric Fiselier1286bc52016-12-11 03:41:12 +0000116 ~test_allocator() TEST_NOEXCEPT {
117 assert(data_ >= 0); assert(id_ >= 0);
Eric Fiselier8cef7fd2018-06-05 22:32:52 +0000118 --count;
119 data_ = destructed_value;
120 id_ = destructed_value;
Eric Fiselier1286bc52016-12-11 03:41:12 +0000121 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000122 pointer address(reference x) const {return &x;}
123 const_pointer address(const_reference x) const {return &x;}
124 pointer allocate(size_type n, const void* = 0)
125 {
Marshall Clowc3deeb52013-12-03 00:18:10 +0000126 assert(data_ >= 0);
127 if (time_to_throw >= throw_after) {
Eric Fiselier54613ab2016-09-25 03:34:28 +0000128#ifndef TEST_HAS_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +0000129 throw std::bad_alloc();
Howard Hinnant65a87cc2013-03-23 17:27:16 +0000130#else
131 std::terminate();
132#endif
133 }
Marshall Clowc3deeb52013-12-03 00:18:10 +0000134 ++time_to_throw;
135 ++alloc_count;
Eric Fiselierb11df182015-06-14 23:30:09 +0000136 return (pointer)::operator new(n * sizeof(T));
Howard Hinnant3e519522010-05-11 19:42:16 +0000137 }
Eric Fiselier7626f772016-04-28 03:17:56 +0000138 void deallocate(pointer p, size_type)
Eric Fiselierb11df182015-06-14 23:30:09 +0000139 {assert(data_ >= 0); --alloc_count; ::operator delete((void*)p);}
Eric Fiselier3ca45662016-12-11 02:47:36 +0000140 size_type max_size() const TEST_NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000141 {return UINT_MAX / sizeof(T);}
Eric Fiselier4fae5022016-06-15 01:53:32 +0000142#if TEST_STD_VER < 11
Howard Hinnant3e519522010-05-11 19:42:16 +0000143 void construct(pointer p, const T& val)
Eric Fiselier4fae5022016-06-15 01:53:32 +0000144 {::new(static_cast<void*>(p)) T(val);}
145#else
146 template <class U> void construct(pointer p, U&& val)
147 {::new(static_cast<void*>(p)) T(std::forward<U>(val));}
148#endif
Eric Fiselier408d6eb2016-06-22 01:02:08 +0000149 void destroy(pointer p)
Eric Fiselier89c91912016-10-07 18:51:33 +0000150 {p->~T();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000151 friend bool operator==(const test_allocator& x, const test_allocator& y)
152 {return x.data_ == y.data_;}
153 friend bool operator!=(const test_allocator& x, const test_allocator& y)
154 {return !(x == y);}
Eric Fiselier1286bc52016-12-11 03:41:12 +0000155
156 int get_data() const { return data_; }
157 int get_id() const { return id_; }
Howard Hinnant3e519522010-05-11 19:42:16 +0000158};
159
Marshall Clow5f7c2db2014-04-18 17:23:36 +0000160template <class T>
161class non_default_test_allocator
162 : public test_alloc_base
163{
164 int data_;
165
166 template <class U> friend class non_default_test_allocator;
167public:
168
169 typedef unsigned size_type;
170 typedef int difference_type;
171 typedef T value_type;
172 typedef value_type* pointer;
173 typedef const value_type* const_pointer;
174 typedef typename std::add_lvalue_reference<value_type>::type reference;
175 typedef typename std::add_lvalue_reference<const value_type>::type const_reference;
176
177 template <class U> struct rebind {typedef non_default_test_allocator<U> other;};
178
Eric Fiselier3ca45662016-12-11 02:47:36 +0000179// non_default_test_allocator() TEST_NOEXCEPT : data_(0) {++count;}
180 explicit non_default_test_allocator(int i) TEST_NOEXCEPT : data_(i) {++count;}
181 non_default_test_allocator(const non_default_test_allocator& a) TEST_NOEXCEPT
Marshall Clow5f7c2db2014-04-18 17:23:36 +0000182 : data_(a.data_) {++count;}
Eric Fiselier3ca45662016-12-11 02:47:36 +0000183 template <class U> non_default_test_allocator(const non_default_test_allocator<U>& a) TEST_NOEXCEPT
Marshall Clow5f7c2db2014-04-18 17:23:36 +0000184 : data_(a.data_) {++count;}
Eric Fiselier3ca45662016-12-11 02:47:36 +0000185 ~non_default_test_allocator() TEST_NOEXCEPT {assert(data_ >= 0); --count; data_ = -1;}
Marshall Clow5f7c2db2014-04-18 17:23:36 +0000186 pointer address(reference x) const {return &x;}
187 const_pointer address(const_reference x) const {return &x;}
188 pointer allocate(size_type n, const void* = 0)
189 {
190 assert(data_ >= 0);
191 if (time_to_throw >= throw_after) {
Eric Fiselier54613ab2016-09-25 03:34:28 +0000192#ifndef TEST_HAS_NO_EXCEPTIONS
Marshall Clow5f7c2db2014-04-18 17:23:36 +0000193 throw std::bad_alloc();
194#else
195 std::terminate();
196#endif
197 }
198 ++time_to_throw;
199 ++alloc_count;
Eric Fiselierb11df182015-06-14 23:30:09 +0000200 return (pointer)::operator new (n * sizeof(T));
Marshall Clow5f7c2db2014-04-18 17:23:36 +0000201 }
Eric Fiselier7626f772016-04-28 03:17:56 +0000202 void deallocate(pointer p, size_type)
Eric Fiselierb11df182015-06-14 23:30:09 +0000203 {assert(data_ >= 0); --alloc_count; ::operator delete((void*)p); }
Eric Fiselier3ca45662016-12-11 02:47:36 +0000204 size_type max_size() const TEST_NOEXCEPT
Marshall Clow5f7c2db2014-04-18 17:23:36 +0000205 {return UINT_MAX / sizeof(T);}
Eric Fiselier4fae5022016-06-15 01:53:32 +0000206#if TEST_STD_VER < 11
Marshall Clow5f7c2db2014-04-18 17:23:36 +0000207 void construct(pointer p, const T& val)
Eric Fiselier4fae5022016-06-15 01:53:32 +0000208 {::new(static_cast<void*>(p)) T(val);}
209#else
210 template <class U> void construct(pointer p, U&& val)
211 {::new(static_cast<void*>(p)) T(std::forward<U>(val));}
212#endif
Marshall Clow5f7c2db2014-04-18 17:23:36 +0000213 void destroy(pointer p) {p->~T();}
214
215 friend bool operator==(const non_default_test_allocator& x, const non_default_test_allocator& y)
216 {return x.data_ == y.data_;}
217 friend bool operator!=(const non_default_test_allocator& x, const non_default_test_allocator& y)
218 {return !(x == y);}
219};
220
Marshall Clowc3deeb52013-12-03 00:18:10 +0000221template <>
222class test_allocator<void>
223 : public test_alloc_base
224{
225 int data_;
Eric Fiselier1286bc52016-12-11 03:41:12 +0000226 int id_;
Marshall Clowc3deeb52013-12-03 00:18:10 +0000227
228 template <class U> friend class test_allocator;
229public:
230
231 typedef unsigned size_type;
232 typedef int difference_type;
233 typedef void value_type;
234 typedef value_type* pointer;
235 typedef const value_type* const_pointer;
236
237 template <class U> struct rebind {typedef test_allocator<U> other;};
238
Eric Fiselier1286bc52016-12-11 03:41:12 +0000239 test_allocator() TEST_NOEXCEPT : data_(0), id_(0) {}
240 explicit test_allocator(int i, int id = 0) TEST_NOEXCEPT : data_(i), id_(id) {}
Eric Fiselier3ca45662016-12-11 02:47:36 +0000241 test_allocator(const test_allocator& a) TEST_NOEXCEPT
Eric Fiselier1286bc52016-12-11 03:41:12 +0000242 : data_(a.data_), id_(a.id_) {}
Eric Fiselier3ca45662016-12-11 02:47:36 +0000243 template <class U> test_allocator(const test_allocator<U>& a) TEST_NOEXCEPT
Eric Fiselier1286bc52016-12-11 03:41:12 +0000244 : data_(a.data_), id_(a.id_) {}
245 ~test_allocator() TEST_NOEXCEPT {data_ = -1; id_ = -1; }
246
247 int get_id() const { return id_; }
248 int get_data() const { return data_; }
Marshall Clowc3deeb52013-12-03 00:18:10 +0000249
250 friend bool operator==(const test_allocator& x, const test_allocator& y)
251 {return x.data_ == y.data_;}
252 friend bool operator!=(const test_allocator& x, const test_allocator& y)
253 {return !(x == y);}
254};
255
Howard Hinnant3e519522010-05-11 19:42:16 +0000256template <class T>
257class other_allocator
258{
259 int data_;
260
261 template <class U> friend class other_allocator;
262
263public:
264 typedef T value_type;
265
266 other_allocator() : data_(-1) {}
267 explicit other_allocator(int i) : data_(i) {}
268 template <class U> other_allocator(const other_allocator<U>& a)
269 : data_(a.data_) {}
270 T* allocate(std::size_t n)
Eric Fiselierb11df182015-06-14 23:30:09 +0000271 {return (T*)::operator new(n * sizeof(T));}
Eric Fiselier7626f772016-04-28 03:17:56 +0000272 void deallocate(T* p, std::size_t)
Eric Fiselierb11df182015-06-14 23:30:09 +0000273 {::operator delete((void*)p);}
Howard Hinnant3e519522010-05-11 19:42:16 +0000274
275 other_allocator select_on_container_copy_construction() const
276 {return other_allocator(-2);}
277
278 friend bool operator==(const other_allocator& x, const other_allocator& y)
279 {return x.data_ == y.data_;}
280 friend bool operator!=(const other_allocator& x, const other_allocator& y)
281 {return !(x == y);}
282
283 typedef std::true_type propagate_on_container_copy_assignment;
284 typedef std::true_type propagate_on_container_move_assignment;
285 typedef std::true_type propagate_on_container_swap;
286
Eric Fiselier54613ab2016-09-25 03:34:28 +0000287#if TEST_STD_VER < 11
Howard Hinnant3e519522010-05-11 19:42:16 +0000288 std::size_t max_size() const
289 {return UINT_MAX / sizeof(T);}
Eric Fiselier54613ab2016-09-25 03:34:28 +0000290#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000291
292};
293
Marshall Clowdc3eb832016-07-11 21:38:08 +0000294#if TEST_STD_VER >= 11
295
296struct Ctor_Tag {};
297
298template <typename T> class TaggingAllocator;
299
300struct Tag_X {
301 // All constructors must be passed the Tag type.
302
303 // DefaultInsertable into vector<X, TaggingAllocator<X>>,
304 Tag_X(Ctor_Tag) {}
305 // CopyInsertable into vector<X, TaggingAllocator<X>>,
306 Tag_X(Ctor_Tag, const Tag_X&) {}
307 // MoveInsertable into vector<X, TaggingAllocator<X>>, and
308 Tag_X(Ctor_Tag, Tag_X&&) {}
309
310 // EmplaceConstructible into vector<X, TaggingAllocator<X>> from args.
311 template<typename... Args>
312 Tag_X(Ctor_Tag, Args&&...) { }
313
314 // not DefaultConstructible, CopyConstructible or MoveConstructible.
315 Tag_X() = delete;
316 Tag_X(const Tag_X&) = delete;
317 Tag_X(Tag_X&&) = delete;
318
319 // CopyAssignable.
320 Tag_X& operator=(const Tag_X&) { return *this; }
321
322 // MoveAssignable.
323 Tag_X& operator=(Tag_X&&) { return *this; }
324
325private:
326 // Not Destructible.
327 ~Tag_X() { }
328
329 // Erasable from vector<X, TaggingAllocator<X>>.
330 friend class TaggingAllocator<Tag_X>;
331};
332
333
334template<typename T>
335class TaggingAllocator {
336public:
337 using value_type = T;
338 TaggingAllocator() = default;
339
340 template<typename U>
341 TaggingAllocator(const TaggingAllocator<U>&) { }
342
343 T* allocate(std::size_t n) { return std::allocator<T>{}.allocate(n); }
344
345 void deallocate(T* p, std::size_t n) { std::allocator<T>{}.deallocate(p, n); }
346
347 template<typename... Args>
348 void construct(Tag_X* p, Args&&... args)
349 { ::new((void*)p) Tag_X(Ctor_Tag{}, std::forward<Args>(args)...); }
350
351 template<typename U, typename... Args>
352 void construct(U* p, Args&&... args)
353 { ::new((void*)p) U(std::forward<Args>(args)...); }
354
355 template<typename U, typename... Args>
356 void destroy(U* p)
Eric Fiselier89c91912016-10-07 18:51:33 +0000357 { p->~U(); }
Marshall Clowdc3eb832016-07-11 21:38:08 +0000358};
359
360template<typename T, typename U>
361bool
362operator==(const TaggingAllocator<T>&, const TaggingAllocator<U>&)
363{ return true; }
364
365template<typename T, typename U>
366bool
367operator!=(const TaggingAllocator<T>&, const TaggingAllocator<U>&)
368{ return false; }
369#endif
370
Eric Fiselier69a4f662016-10-08 00:56:22 +0000371template <std::size_t MaxAllocs>
372struct limited_alloc_handle {
373 std::size_t outstanding_;
374 void* last_alloc_;
375
376 limited_alloc_handle() : outstanding_(0), last_alloc_(nullptr) {}
377
378 template <class T>
379 T *allocate(std::size_t N) {
380 if (N + outstanding_ > MaxAllocs)
381 TEST_THROW(std::bad_alloc());
382 last_alloc_ = ::operator new(N*sizeof(T));
383 outstanding_ += N;
384 return static_cast<T*>(last_alloc_);
385 }
386
387 void deallocate(void* ptr, std::size_t N) {
388 if (ptr == last_alloc_) {
389 last_alloc_ = nullptr;
390 assert(outstanding_ >= N);
391 outstanding_ -= N;
392 }
393 ::operator delete(ptr);
394 }
395};
396
397template <class T, std::size_t N>
398class limited_allocator
399{
Eric Fiseliera9fcc1d2016-10-12 11:35:37 +0000400 template <class U, std::size_t UN> friend class limited_allocator;
Eric Fiselier69a4f662016-10-08 00:56:22 +0000401 typedef limited_alloc_handle<N> BuffT;
402 std::shared_ptr<BuffT> handle_;
403public:
404 typedef T value_type;
405 typedef value_type* pointer;
406 typedef const value_type* const_pointer;
407 typedef value_type& reference;
408 typedef const value_type& const_reference;
409 typedef std::size_t size_type;
410 typedef std::ptrdiff_t difference_type;
411
412 template <class U> struct rebind { typedef limited_allocator<U, N> other; };
413
414 limited_allocator() : handle_(new BuffT) {}
415
416 limited_allocator(limited_allocator const& other) : handle_(other.handle_) {}
417
418 template <class U>
419 explicit limited_allocator(limited_allocator<U, N> const& other)
420 : handle_(other.handle_) {}
421
422private:
423 limited_allocator& operator=(const limited_allocator&);// = delete;
424
425public:
426 pointer allocate(size_type n) { return handle_->template allocate<T>(n); }
427 void deallocate(pointer p, size_type n) { handle_->deallocate(p, n); }
428 size_type max_size() const {return N;}
429
430 BuffT* getHandle() const { return handle_.get(); }
431};
432
433template <class T, class U, std::size_t N>
434inline bool operator==(limited_allocator<T, N> const& LHS,
435 limited_allocator<U, N> const& RHS) {
436 return LHS.getHandle() == RHS.getHandle();
437}
438
439template <class T, class U, std::size_t N>
440inline bool operator!=(limited_allocator<T, N> const& LHS,
441 limited_allocator<U, N> const& RHS) {
442 return !(LHS == RHS);
443}
444
Marshall Clowdc3eb832016-07-11 21:38:08 +0000445
Howard Hinnant8f2f7e72010-08-22 00:15:28 +0000446#endif // TEST_ALLOCATOR_H