Marshall Clow | 354d39c | 2014-01-16 16:58:45 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 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 Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 10 | #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 Clow | c3deeb5 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 18 | #include <cassert> |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 19 | |
Marshall Clow | cbf166a | 2015-06-03 19:56:43 +0000 | [diff] [blame] | 20 | #include "test_macros.h" |
| 21 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 22 | class test_alloc_base |
| 23 | { |
| 24 | protected: |
Marshall Clow | c3deeb5 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 25 | static int time_to_throw; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 26 | public: |
| 27 | static int throw_after; |
Marshall Clow | c3deeb5 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 28 | static int count; |
| 29 | static int alloc_count; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | int test_alloc_base::count = 0; |
Marshall Clow | c3deeb5 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 33 | int test_alloc_base::time_to_throw = 0; |
| 34 | int test_alloc_base::alloc_count = 0; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 35 | int test_alloc_base::throw_after = INT_MAX; |
| 36 | |
| 37 | template <class T> |
| 38 | class test_allocator |
| 39 | : public test_alloc_base |
| 40 | { |
| 41 | int data_; |
| 42 | |
| 43 | template <class U> friend class test_allocator; |
| 44 | public: |
| 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 Clow | c3deeb5 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 56 | test_allocator() throw() : data_(0) {++count;} |
| 57 | explicit test_allocator(int i) throw() : data_(i) {++count;} |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 58 | test_allocator(const test_allocator& a) throw() |
Marshall Clow | c3deeb5 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 59 | : data_(a.data_) {++count;} |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 60 | template <class U> test_allocator(const test_allocator<U>& a) throw() |
Marshall Clow | c3deeb5 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 61 | : data_(a.data_) {++count;} |
| 62 | ~test_allocator() throw() {assert(data_ >= 0); --count; data_ = -1;} |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 63 | 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 Clow | c3deeb5 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 67 | assert(data_ >= 0); |
| 68 | if (time_to_throw >= throw_after) { |
Howard Hinnant | 65a87cc | 2013-03-23 17:27:16 +0000 | [diff] [blame] | 69 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 70 | throw std::bad_alloc(); |
Howard Hinnant | 65a87cc | 2013-03-23 17:27:16 +0000 | [diff] [blame] | 71 | #else |
| 72 | std::terminate(); |
| 73 | #endif |
| 74 | } |
Marshall Clow | c3deeb5 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 75 | ++time_to_throw; |
| 76 | ++alloc_count; |
Eric Fiselier | b11df18 | 2015-06-14 23:30:09 +0000 | [diff] [blame] | 77 | return (pointer)::operator new(n * sizeof(T)); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 78 | } |
Eric Fiselier | 7626f77 | 2016-04-28 03:17:56 +0000 | [diff] [blame] | 79 | void deallocate(pointer p, size_type) |
Eric Fiselier | b11df18 | 2015-06-14 23:30:09 +0000 | [diff] [blame] | 80 | {assert(data_ >= 0); --alloc_count; ::operator delete((void*)p);} |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 81 | size_type max_size() const throw() |
| 82 | {return UINT_MAX / sizeof(T);} |
Eric Fiselier | 4fae502 | 2016-06-15 01:53:32 +0000 | [diff] [blame] | 83 | #if TEST_STD_VER < 11 |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 84 | void construct(pointer p, const T& val) |
Eric Fiselier | 4fae502 | 2016-06-15 01:53:32 +0000 | [diff] [blame] | 85 | {::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 |
Eric Fiselier | 408d6eb | 2016-06-22 01:02:08 +0000 | [diff] [blame] | 90 | void destroy(pointer p) |
| 91 | { |
| 92 | p->~T(); |
| 93 | ((void)p); // Prevent MSVC's spurious unused warning |
| 94 | } |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 95 | friend bool operator==(const test_allocator& x, const test_allocator& y) |
| 96 | {return x.data_ == y.data_;} |
| 97 | friend bool operator!=(const test_allocator& x, const test_allocator& y) |
| 98 | {return !(x == y);} |
| 99 | }; |
| 100 | |
Marshall Clow | 5f7c2db | 2014-04-18 17:23:36 +0000 | [diff] [blame] | 101 | template <class T> |
| 102 | class non_default_test_allocator |
| 103 | : public test_alloc_base |
| 104 | { |
| 105 | int data_; |
| 106 | |
| 107 | template <class U> friend class non_default_test_allocator; |
| 108 | public: |
| 109 | |
| 110 | typedef unsigned size_type; |
| 111 | typedef int difference_type; |
| 112 | typedef T value_type; |
| 113 | typedef value_type* pointer; |
| 114 | typedef const value_type* const_pointer; |
| 115 | typedef typename std::add_lvalue_reference<value_type>::type reference; |
| 116 | typedef typename std::add_lvalue_reference<const value_type>::type const_reference; |
| 117 | |
| 118 | template <class U> struct rebind {typedef non_default_test_allocator<U> other;}; |
| 119 | |
| 120 | // non_default_test_allocator() throw() : data_(0) {++count;} |
| 121 | explicit non_default_test_allocator(int i) throw() : data_(i) {++count;} |
| 122 | non_default_test_allocator(const non_default_test_allocator& a) throw() |
| 123 | : data_(a.data_) {++count;} |
| 124 | template <class U> non_default_test_allocator(const non_default_test_allocator<U>& a) throw() |
| 125 | : data_(a.data_) {++count;} |
| 126 | ~non_default_test_allocator() throw() {assert(data_ >= 0); --count; data_ = -1;} |
| 127 | pointer address(reference x) const {return &x;} |
| 128 | const_pointer address(const_reference x) const {return &x;} |
| 129 | pointer allocate(size_type n, const void* = 0) |
| 130 | { |
| 131 | assert(data_ >= 0); |
| 132 | if (time_to_throw >= throw_after) { |
| 133 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 134 | throw std::bad_alloc(); |
| 135 | #else |
| 136 | std::terminate(); |
| 137 | #endif |
| 138 | } |
| 139 | ++time_to_throw; |
| 140 | ++alloc_count; |
Eric Fiselier | b11df18 | 2015-06-14 23:30:09 +0000 | [diff] [blame] | 141 | return (pointer)::operator new (n * sizeof(T)); |
Marshall Clow | 5f7c2db | 2014-04-18 17:23:36 +0000 | [diff] [blame] | 142 | } |
Eric Fiselier | 7626f77 | 2016-04-28 03:17:56 +0000 | [diff] [blame] | 143 | void deallocate(pointer p, size_type) |
Eric Fiselier | b11df18 | 2015-06-14 23:30:09 +0000 | [diff] [blame] | 144 | {assert(data_ >= 0); --alloc_count; ::operator delete((void*)p); } |
Marshall Clow | 5f7c2db | 2014-04-18 17:23:36 +0000 | [diff] [blame] | 145 | size_type max_size() const throw() |
| 146 | {return UINT_MAX / sizeof(T);} |
Eric Fiselier | 4fae502 | 2016-06-15 01:53:32 +0000 | [diff] [blame] | 147 | #if TEST_STD_VER < 11 |
Marshall Clow | 5f7c2db | 2014-04-18 17:23:36 +0000 | [diff] [blame] | 148 | void construct(pointer p, const T& val) |
Eric Fiselier | 4fae502 | 2016-06-15 01:53:32 +0000 | [diff] [blame] | 149 | {::new(static_cast<void*>(p)) T(val);} |
| 150 | #else |
| 151 | template <class U> void construct(pointer p, U&& val) |
| 152 | {::new(static_cast<void*>(p)) T(std::forward<U>(val));} |
| 153 | #endif |
Marshall Clow | 5f7c2db | 2014-04-18 17:23:36 +0000 | [diff] [blame] | 154 | void destroy(pointer p) {p->~T();} |
| 155 | |
| 156 | friend bool operator==(const non_default_test_allocator& x, const non_default_test_allocator& y) |
| 157 | {return x.data_ == y.data_;} |
| 158 | friend bool operator!=(const non_default_test_allocator& x, const non_default_test_allocator& y) |
| 159 | {return !(x == y);} |
| 160 | }; |
| 161 | |
Marshall Clow | c3deeb5 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 162 | template <> |
| 163 | class test_allocator<void> |
| 164 | : public test_alloc_base |
| 165 | { |
| 166 | int data_; |
| 167 | |
| 168 | template <class U> friend class test_allocator; |
| 169 | public: |
| 170 | |
| 171 | typedef unsigned size_type; |
| 172 | typedef int difference_type; |
| 173 | typedef void value_type; |
| 174 | typedef value_type* pointer; |
| 175 | typedef const value_type* const_pointer; |
| 176 | |
| 177 | template <class U> struct rebind {typedef test_allocator<U> other;}; |
| 178 | |
Eric Fiselier | fc8e8c0 | 2015-08-28 05:00:25 +0000 | [diff] [blame] | 179 | test_allocator() throw() : data_(0) {} |
Marshall Clow | c3deeb5 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 180 | explicit test_allocator(int i) throw() : data_(i) {} |
| 181 | test_allocator(const test_allocator& a) throw() |
| 182 | : data_(a.data_) {} |
| 183 | template <class U> test_allocator(const test_allocator<U>& a) throw() |
| 184 | : data_(a.data_) {} |
Eric Fiselier | fc8e8c0 | 2015-08-28 05:00:25 +0000 | [diff] [blame] | 185 | ~test_allocator() throw() {data_ = -1;} |
Marshall Clow | c3deeb5 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 186 | |
| 187 | friend bool operator==(const test_allocator& x, const test_allocator& y) |
| 188 | {return x.data_ == y.data_;} |
| 189 | friend bool operator!=(const test_allocator& x, const test_allocator& y) |
| 190 | {return !(x == y);} |
| 191 | }; |
| 192 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 193 | template <class T> |
| 194 | class other_allocator |
| 195 | { |
| 196 | int data_; |
| 197 | |
| 198 | template <class U> friend class other_allocator; |
| 199 | |
| 200 | public: |
| 201 | typedef T value_type; |
| 202 | |
| 203 | other_allocator() : data_(-1) {} |
| 204 | explicit other_allocator(int i) : data_(i) {} |
| 205 | template <class U> other_allocator(const other_allocator<U>& a) |
| 206 | : data_(a.data_) {} |
| 207 | T* allocate(std::size_t n) |
Eric Fiselier | b11df18 | 2015-06-14 23:30:09 +0000 | [diff] [blame] | 208 | {return (T*)::operator new(n * sizeof(T));} |
Eric Fiselier | 7626f77 | 2016-04-28 03:17:56 +0000 | [diff] [blame] | 209 | void deallocate(T* p, std::size_t) |
Eric Fiselier | b11df18 | 2015-06-14 23:30:09 +0000 | [diff] [blame] | 210 | {::operator delete((void*)p);} |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 211 | |
| 212 | other_allocator select_on_container_copy_construction() const |
| 213 | {return other_allocator(-2);} |
| 214 | |
| 215 | friend bool operator==(const other_allocator& x, const other_allocator& y) |
| 216 | {return x.data_ == y.data_;} |
| 217 | friend bool operator!=(const other_allocator& x, const other_allocator& y) |
| 218 | {return !(x == y);} |
| 219 | |
| 220 | typedef std::true_type propagate_on_container_copy_assignment; |
| 221 | typedef std::true_type propagate_on_container_move_assignment; |
| 222 | typedef std::true_type propagate_on_container_swap; |
| 223 | |
| 224 | #ifdef _LIBCPP_HAS_NO_ADVANCED_SFINAE |
| 225 | std::size_t max_size() const |
| 226 | {return UINT_MAX / sizeof(T);} |
Howard Hinnant | 8f2f7e7 | 2010-08-22 00:15:28 +0000 | [diff] [blame] | 227 | #endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 228 | |
| 229 | }; |
| 230 | |
Marshall Clow | dc3eb83 | 2016-07-11 21:38:08 +0000 | [diff] [blame] | 231 | #if TEST_STD_VER >= 11 |
| 232 | |
| 233 | struct Ctor_Tag {}; |
| 234 | |
| 235 | template <typename T> class TaggingAllocator; |
| 236 | |
| 237 | struct Tag_X { |
| 238 | // All constructors must be passed the Tag type. |
| 239 | |
| 240 | // DefaultInsertable into vector<X, TaggingAllocator<X>>, |
| 241 | Tag_X(Ctor_Tag) {} |
| 242 | // CopyInsertable into vector<X, TaggingAllocator<X>>, |
| 243 | Tag_X(Ctor_Tag, const Tag_X&) {} |
| 244 | // MoveInsertable into vector<X, TaggingAllocator<X>>, and |
| 245 | Tag_X(Ctor_Tag, Tag_X&&) {} |
| 246 | |
| 247 | // EmplaceConstructible into vector<X, TaggingAllocator<X>> from args. |
| 248 | template<typename... Args> |
| 249 | Tag_X(Ctor_Tag, Args&&...) { } |
| 250 | |
| 251 | // not DefaultConstructible, CopyConstructible or MoveConstructible. |
| 252 | Tag_X() = delete; |
| 253 | Tag_X(const Tag_X&) = delete; |
| 254 | Tag_X(Tag_X&&) = delete; |
| 255 | |
| 256 | // CopyAssignable. |
| 257 | Tag_X& operator=(const Tag_X&) { return *this; } |
| 258 | |
| 259 | // MoveAssignable. |
| 260 | Tag_X& operator=(Tag_X&&) { return *this; } |
| 261 | |
| 262 | private: |
| 263 | // Not Destructible. |
| 264 | ~Tag_X() { } |
| 265 | |
| 266 | // Erasable from vector<X, TaggingAllocator<X>>. |
| 267 | friend class TaggingAllocator<Tag_X>; |
| 268 | }; |
| 269 | |
| 270 | |
| 271 | template<typename T> |
| 272 | class TaggingAllocator { |
| 273 | public: |
| 274 | using value_type = T; |
| 275 | TaggingAllocator() = default; |
| 276 | |
| 277 | template<typename U> |
| 278 | TaggingAllocator(const TaggingAllocator<U>&) { } |
| 279 | |
| 280 | T* allocate(std::size_t n) { return std::allocator<T>{}.allocate(n); } |
| 281 | |
| 282 | void deallocate(T* p, std::size_t n) { std::allocator<T>{}.deallocate(p, n); } |
| 283 | |
| 284 | template<typename... Args> |
| 285 | void construct(Tag_X* p, Args&&... args) |
| 286 | { ::new((void*)p) Tag_X(Ctor_Tag{}, std::forward<Args>(args)...); } |
| 287 | |
| 288 | template<typename U, typename... Args> |
| 289 | void construct(U* p, Args&&... args) |
| 290 | { ::new((void*)p) U(std::forward<Args>(args)...); } |
| 291 | |
| 292 | template<typename U, typename... Args> |
| 293 | void destroy(U* p) |
Eric Fiselier | 7da93bb | 2016-08-03 05:46:36 +0000 | [diff] [blame] | 294 | { |
| 295 | p->~U(); |
| 296 | ((void)p); // Prevent MSVC's spurious unused warning |
| 297 | } |
Marshall Clow | dc3eb83 | 2016-07-11 21:38:08 +0000 | [diff] [blame] | 298 | }; |
| 299 | |
| 300 | template<typename T, typename U> |
| 301 | bool |
| 302 | operator==(const TaggingAllocator<T>&, const TaggingAllocator<U>&) |
| 303 | { return true; } |
| 304 | |
| 305 | template<typename T, typename U> |
| 306 | bool |
| 307 | operator!=(const TaggingAllocator<T>&, const TaggingAllocator<U>&) |
| 308 | { return false; } |
| 309 | #endif |
| 310 | |
| 311 | |
Howard Hinnant | 8f2f7e7 | 2010-08-22 00:15:28 +0000 | [diff] [blame] | 312 | #endif // TEST_ALLOCATOR_H |