blob: 04f15abf7fb261e3b3065ae86d3b1b6e35e621e8 [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>
16#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;
Howard Hinnant3e519522010-05-11 19:42:16 +000038};
39
40int test_alloc_base::count = 0;
Marshall Clowc3deeb52013-12-03 00:18:10 +000041int test_alloc_base::time_to_throw = 0;
42int test_alloc_base::alloc_count = 0;
Howard Hinnant3e519522010-05-11 19:42:16 +000043int test_alloc_base::throw_after = INT_MAX;
44
45template <class T>
46class test_allocator
47 : public test_alloc_base
48{
49 int data_;
50
51 template <class U> friend class test_allocator;
52public:
53
54 typedef unsigned size_type;
55 typedef int difference_type;
56 typedef T value_type;
57 typedef value_type* pointer;
58 typedef const value_type* const_pointer;
59 typedef typename std::add_lvalue_reference<value_type>::type reference;
60 typedef typename std::add_lvalue_reference<const value_type>::type const_reference;
61
62 template <class U> struct rebind {typedef test_allocator<U> other;};
63
Eric Fiselier3ca45662016-12-11 02:47:36 +000064 test_allocator() TEST_NOEXCEPT : data_(0) {++count;}
65 explicit test_allocator(int i) TEST_NOEXCEPT : data_(i) {++count;}
66 test_allocator(const test_allocator& a) TEST_NOEXCEPT
Marshall Clowc3deeb52013-12-03 00:18:10 +000067 : data_(a.data_) {++count;}
Eric Fiselier3ca45662016-12-11 02:47:36 +000068 template <class U> test_allocator(const test_allocator<U>& a) TEST_NOEXCEPT
Marshall Clowc3deeb52013-12-03 00:18:10 +000069 : data_(a.data_) {++count;}
Eric Fiselier3ca45662016-12-11 02:47:36 +000070 ~test_allocator() TEST_NOEXCEPT {assert(data_ >= 0); --count; data_ = -1;}
Howard Hinnant3e519522010-05-11 19:42:16 +000071 pointer address(reference x) const {return &x;}
72 const_pointer address(const_reference x) const {return &x;}
73 pointer allocate(size_type n, const void* = 0)
74 {
Marshall Clowc3deeb52013-12-03 00:18:10 +000075 assert(data_ >= 0);
76 if (time_to_throw >= throw_after) {
Eric Fiselier54613ab2016-09-25 03:34:28 +000077#ifndef TEST_HAS_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +000078 throw std::bad_alloc();
Howard Hinnant65a87cc2013-03-23 17:27:16 +000079#else
80 std::terminate();
81#endif
82 }
Marshall Clowc3deeb52013-12-03 00:18:10 +000083 ++time_to_throw;
84 ++alloc_count;
Eric Fiselierb11df182015-06-14 23:30:09 +000085 return (pointer)::operator new(n * sizeof(T));
Howard Hinnant3e519522010-05-11 19:42:16 +000086 }
Eric Fiselier7626f772016-04-28 03:17:56 +000087 void deallocate(pointer p, size_type)
Eric Fiselierb11df182015-06-14 23:30:09 +000088 {assert(data_ >= 0); --alloc_count; ::operator delete((void*)p);}
Eric Fiselier3ca45662016-12-11 02:47:36 +000089 size_type max_size() const TEST_NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +000090 {return UINT_MAX / sizeof(T);}
Eric Fiselier4fae5022016-06-15 01:53:32 +000091#if TEST_STD_VER < 11
Howard Hinnant3e519522010-05-11 19:42:16 +000092 void construct(pointer p, const T& val)
Eric Fiselier4fae5022016-06-15 01:53:32 +000093 {::new(static_cast<void*>(p)) T(val);}
94#else
95 template <class U> void construct(pointer p, U&& val)
96 {::new(static_cast<void*>(p)) T(std::forward<U>(val));}
97#endif
Eric Fiselier408d6eb2016-06-22 01:02:08 +000098 void destroy(pointer p)
Eric Fiselier89c91912016-10-07 18:51:33 +000099 {p->~T();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000100 friend bool operator==(const test_allocator& x, const test_allocator& y)
101 {return x.data_ == y.data_;}
102 friend bool operator!=(const test_allocator& x, const test_allocator& y)
103 {return !(x == y);}
104};
105
Marshall Clow5f7c2db2014-04-18 17:23:36 +0000106template <class T>
107class non_default_test_allocator
108 : public test_alloc_base
109{
110 int data_;
111
112 template <class U> friend class non_default_test_allocator;
113public:
114
115 typedef unsigned size_type;
116 typedef int difference_type;
117 typedef T value_type;
118 typedef value_type* pointer;
119 typedef const value_type* const_pointer;
120 typedef typename std::add_lvalue_reference<value_type>::type reference;
121 typedef typename std::add_lvalue_reference<const value_type>::type const_reference;
122
123 template <class U> struct rebind {typedef non_default_test_allocator<U> other;};
124
Eric Fiselier3ca45662016-12-11 02:47:36 +0000125// non_default_test_allocator() TEST_NOEXCEPT : data_(0) {++count;}
126 explicit non_default_test_allocator(int i) TEST_NOEXCEPT : data_(i) {++count;}
127 non_default_test_allocator(const non_default_test_allocator& a) TEST_NOEXCEPT
Marshall Clow5f7c2db2014-04-18 17:23:36 +0000128 : data_(a.data_) {++count;}
Eric Fiselier3ca45662016-12-11 02:47:36 +0000129 template <class U> non_default_test_allocator(const non_default_test_allocator<U>& a) TEST_NOEXCEPT
Marshall Clow5f7c2db2014-04-18 17:23:36 +0000130 : data_(a.data_) {++count;}
Eric Fiselier3ca45662016-12-11 02:47:36 +0000131 ~non_default_test_allocator() TEST_NOEXCEPT {assert(data_ >= 0); --count; data_ = -1;}
Marshall Clow5f7c2db2014-04-18 17:23:36 +0000132 pointer address(reference x) const {return &x;}
133 const_pointer address(const_reference x) const {return &x;}
134 pointer allocate(size_type n, const void* = 0)
135 {
136 assert(data_ >= 0);
137 if (time_to_throw >= throw_after) {
Eric Fiselier54613ab2016-09-25 03:34:28 +0000138#ifndef TEST_HAS_NO_EXCEPTIONS
Marshall Clow5f7c2db2014-04-18 17:23:36 +0000139 throw std::bad_alloc();
140#else
141 std::terminate();
142#endif
143 }
144 ++time_to_throw;
145 ++alloc_count;
Eric Fiselierb11df182015-06-14 23:30:09 +0000146 return (pointer)::operator new (n * sizeof(T));
Marshall Clow5f7c2db2014-04-18 17:23:36 +0000147 }
Eric Fiselier7626f772016-04-28 03:17:56 +0000148 void deallocate(pointer p, size_type)
Eric Fiselierb11df182015-06-14 23:30:09 +0000149 {assert(data_ >= 0); --alloc_count; ::operator delete((void*)p); }
Eric Fiselier3ca45662016-12-11 02:47:36 +0000150 size_type max_size() const TEST_NOEXCEPT
Marshall Clow5f7c2db2014-04-18 17:23:36 +0000151 {return UINT_MAX / sizeof(T);}
Eric Fiselier4fae5022016-06-15 01:53:32 +0000152#if TEST_STD_VER < 11
Marshall Clow5f7c2db2014-04-18 17:23:36 +0000153 void construct(pointer p, const T& val)
Eric Fiselier4fae5022016-06-15 01:53:32 +0000154 {::new(static_cast<void*>(p)) T(val);}
155#else
156 template <class U> void construct(pointer p, U&& val)
157 {::new(static_cast<void*>(p)) T(std::forward<U>(val));}
158#endif
Marshall Clow5f7c2db2014-04-18 17:23:36 +0000159 void destroy(pointer p) {p->~T();}
160
161 friend bool operator==(const non_default_test_allocator& x, const non_default_test_allocator& y)
162 {return x.data_ == y.data_;}
163 friend bool operator!=(const non_default_test_allocator& x, const non_default_test_allocator& y)
164 {return !(x == y);}
165};
166
Marshall Clowc3deeb52013-12-03 00:18:10 +0000167template <>
168class test_allocator<void>
169 : public test_alloc_base
170{
171 int data_;
172
173 template <class U> friend class test_allocator;
174public:
175
176 typedef unsigned size_type;
177 typedef int difference_type;
178 typedef void value_type;
179 typedef value_type* pointer;
180 typedef const value_type* const_pointer;
181
182 template <class U> struct rebind {typedef test_allocator<U> other;};
183
Eric Fiselier3ca45662016-12-11 02:47:36 +0000184 test_allocator() TEST_NOEXCEPT : data_(0) {}
185 explicit test_allocator(int i) TEST_NOEXCEPT : data_(i) {}
186 test_allocator(const test_allocator& a) TEST_NOEXCEPT
Marshall Clowc3deeb52013-12-03 00:18:10 +0000187 : data_(a.data_) {}
Eric Fiselier3ca45662016-12-11 02:47:36 +0000188 template <class U> test_allocator(const test_allocator<U>& a) TEST_NOEXCEPT
Marshall Clowc3deeb52013-12-03 00:18:10 +0000189 : data_(a.data_) {}
Eric Fiselier3ca45662016-12-11 02:47:36 +0000190 ~test_allocator() TEST_NOEXCEPT {data_ = -1;}
Marshall Clowc3deeb52013-12-03 00:18:10 +0000191
192 friend bool operator==(const test_allocator& x, const test_allocator& y)
193 {return x.data_ == y.data_;}
194 friend bool operator!=(const test_allocator& x, const test_allocator& y)
195 {return !(x == y);}
196};
197
Howard Hinnant3e519522010-05-11 19:42:16 +0000198template <class T>
199class other_allocator
200{
201 int data_;
202
203 template <class U> friend class other_allocator;
204
205public:
206 typedef T value_type;
207
208 other_allocator() : data_(-1) {}
209 explicit other_allocator(int i) : data_(i) {}
210 template <class U> other_allocator(const other_allocator<U>& a)
211 : data_(a.data_) {}
212 T* allocate(std::size_t n)
Eric Fiselierb11df182015-06-14 23:30:09 +0000213 {return (T*)::operator new(n * sizeof(T));}
Eric Fiselier7626f772016-04-28 03:17:56 +0000214 void deallocate(T* p, std::size_t)
Eric Fiselierb11df182015-06-14 23:30:09 +0000215 {::operator delete((void*)p);}
Howard Hinnant3e519522010-05-11 19:42:16 +0000216
217 other_allocator select_on_container_copy_construction() const
218 {return other_allocator(-2);}
219
220 friend bool operator==(const other_allocator& x, const other_allocator& y)
221 {return x.data_ == y.data_;}
222 friend bool operator!=(const other_allocator& x, const other_allocator& y)
223 {return !(x == y);}
224
225 typedef std::true_type propagate_on_container_copy_assignment;
226 typedef std::true_type propagate_on_container_move_assignment;
227 typedef std::true_type propagate_on_container_swap;
228
Eric Fiselier54613ab2016-09-25 03:34:28 +0000229#if TEST_STD_VER < 11
Howard Hinnant3e519522010-05-11 19:42:16 +0000230 std::size_t max_size() const
231 {return UINT_MAX / sizeof(T);}
Eric Fiselier54613ab2016-09-25 03:34:28 +0000232#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000233
234};
235
Marshall Clowdc3eb832016-07-11 21:38:08 +0000236#if TEST_STD_VER >= 11
237
238struct Ctor_Tag {};
239
240template <typename T> class TaggingAllocator;
241
242struct Tag_X {
243 // All constructors must be passed the Tag type.
244
245 // DefaultInsertable into vector<X, TaggingAllocator<X>>,
246 Tag_X(Ctor_Tag) {}
247 // CopyInsertable into vector<X, TaggingAllocator<X>>,
248 Tag_X(Ctor_Tag, const Tag_X&) {}
249 // MoveInsertable into vector<X, TaggingAllocator<X>>, and
250 Tag_X(Ctor_Tag, Tag_X&&) {}
251
252 // EmplaceConstructible into vector<X, TaggingAllocator<X>> from args.
253 template<typename... Args>
254 Tag_X(Ctor_Tag, Args&&...) { }
255
256 // not DefaultConstructible, CopyConstructible or MoveConstructible.
257 Tag_X() = delete;
258 Tag_X(const Tag_X&) = delete;
259 Tag_X(Tag_X&&) = delete;
260
261 // CopyAssignable.
262 Tag_X& operator=(const Tag_X&) { return *this; }
263
264 // MoveAssignable.
265 Tag_X& operator=(Tag_X&&) { return *this; }
266
267private:
268 // Not Destructible.
269 ~Tag_X() { }
270
271 // Erasable from vector<X, TaggingAllocator<X>>.
272 friend class TaggingAllocator<Tag_X>;
273};
274
275
276template<typename T>
277class TaggingAllocator {
278public:
279 using value_type = T;
280 TaggingAllocator() = default;
281
282 template<typename U>
283 TaggingAllocator(const TaggingAllocator<U>&) { }
284
285 T* allocate(std::size_t n) { return std::allocator<T>{}.allocate(n); }
286
287 void deallocate(T* p, std::size_t n) { std::allocator<T>{}.deallocate(p, n); }
288
289 template<typename... Args>
290 void construct(Tag_X* p, Args&&... args)
291 { ::new((void*)p) Tag_X(Ctor_Tag{}, std::forward<Args>(args)...); }
292
293 template<typename U, typename... Args>
294 void construct(U* p, Args&&... args)
295 { ::new((void*)p) U(std::forward<Args>(args)...); }
296
297 template<typename U, typename... Args>
298 void destroy(U* p)
Eric Fiselier89c91912016-10-07 18:51:33 +0000299 { p->~U(); }
Marshall Clowdc3eb832016-07-11 21:38:08 +0000300};
301
302template<typename T, typename U>
303bool
304operator==(const TaggingAllocator<T>&, const TaggingAllocator<U>&)
305{ return true; }
306
307template<typename T, typename U>
308bool
309operator!=(const TaggingAllocator<T>&, const TaggingAllocator<U>&)
310{ return false; }
311#endif
312
Eric Fiselier69a4f662016-10-08 00:56:22 +0000313template <std::size_t MaxAllocs>
314struct limited_alloc_handle {
315 std::size_t outstanding_;
316 void* last_alloc_;
317
318 limited_alloc_handle() : outstanding_(0), last_alloc_(nullptr) {}
319
320 template <class T>
321 T *allocate(std::size_t N) {
322 if (N + outstanding_ > MaxAllocs)
323 TEST_THROW(std::bad_alloc());
324 last_alloc_ = ::operator new(N*sizeof(T));
325 outstanding_ += N;
326 return static_cast<T*>(last_alloc_);
327 }
328
329 void deallocate(void* ptr, std::size_t N) {
330 if (ptr == last_alloc_) {
331 last_alloc_ = nullptr;
332 assert(outstanding_ >= N);
333 outstanding_ -= N;
334 }
335 ::operator delete(ptr);
336 }
337};
338
339template <class T, std::size_t N>
340class limited_allocator
341{
Eric Fiseliera9fcc1d2016-10-12 11:35:37 +0000342 template <class U, std::size_t UN> friend class limited_allocator;
Eric Fiselier69a4f662016-10-08 00:56:22 +0000343 typedef limited_alloc_handle<N> BuffT;
344 std::shared_ptr<BuffT> handle_;
345public:
346 typedef T value_type;
347 typedef value_type* pointer;
348 typedef const value_type* const_pointer;
349 typedef value_type& reference;
350 typedef const value_type& const_reference;
351 typedef std::size_t size_type;
352 typedef std::ptrdiff_t difference_type;
353
354 template <class U> struct rebind { typedef limited_allocator<U, N> other; };
355
356 limited_allocator() : handle_(new BuffT) {}
357
358 limited_allocator(limited_allocator const& other) : handle_(other.handle_) {}
359
360 template <class U>
361 explicit limited_allocator(limited_allocator<U, N> const& other)
362 : handle_(other.handle_) {}
363
364private:
365 limited_allocator& operator=(const limited_allocator&);// = delete;
366
367public:
368 pointer allocate(size_type n) { return handle_->template allocate<T>(n); }
369 void deallocate(pointer p, size_type n) { handle_->deallocate(p, n); }
370 size_type max_size() const {return N;}
371
372 BuffT* getHandle() const { return handle_.get(); }
373};
374
375template <class T, class U, std::size_t N>
376inline bool operator==(limited_allocator<T, N> const& LHS,
377 limited_allocator<U, N> const& RHS) {
378 return LHS.getHandle() == RHS.getHandle();
379}
380
381template <class T, class U, std::size_t N>
382inline bool operator!=(limited_allocator<T, N> const& LHS,
383 limited_allocator<U, N> const& RHS) {
384 return !(LHS == RHS);
385}
386
Marshall Clowdc3eb832016-07-11 21:38:08 +0000387
Howard Hinnant8f2f7e72010-08-22 00:15:28 +0000388#endif // TEST_ALLOCATOR_H