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 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 13 | #include <type_traits> |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 14 | #include <new> |
Eric Fiselier | 653179b | 2016-10-08 00:57:56 +0000 | [diff] [blame] | 15 | #include <memory> |
Eric Fiselier | 1286bc5 | 2016-12-11 03:41:12 +0000 | [diff] [blame] | 16 | #include <utility> |
Eric Fiselier | 653179b | 2016-10-08 00:57:56 +0000 | [diff] [blame] | 17 | #include <cstddef> |
| 18 | #include <cstdlib> |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 19 | #include <climits> |
Marshall Clow | c3deeb5 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 20 | #include <cassert> |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 21 | |
Marshall Clow | cbf166a | 2015-06-03 19:56:43 +0000 | [diff] [blame] | 22 | #include "test_macros.h" |
| 23 | |
Eric Fiselier | 55b31b4e | 2016-11-23 01:18:56 +0000 | [diff] [blame] | 24 | template <class Alloc> |
Eric Fiselier | 341c9dd | 2016-11-23 09:16:12 +0000 | [diff] [blame] | 25 | inline typename std::allocator_traits<Alloc>::size_type |
| 26 | alloc_max_size(Alloc const &a) { |
Eric Fiselier | 55b31b4e | 2016-11-23 01:18:56 +0000 | [diff] [blame] | 27 | typedef std::allocator_traits<Alloc> AT; |
| 28 | return AT::max_size(a); |
| 29 | } |
| 30 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 31 | class test_alloc_base |
| 32 | { |
| 33 | protected: |
Marshall Clow | c3deeb5 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 34 | static int time_to_throw; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 35 | public: |
| 36 | static int throw_after; |
Marshall Clow | c3deeb5 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 37 | static int count; |
| 38 | static int alloc_count; |
Eric Fiselier | 8cef7fd | 2018-06-05 22:32:52 +0000 | [diff] [blame] | 39 | 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 Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | int test_alloc_base::count = 0; |
Marshall Clow | c3deeb5 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 64 | int test_alloc_base::time_to_throw = 0; |
| 65 | int test_alloc_base::alloc_count = 0; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 66 | int test_alloc_base::throw_after = INT_MAX; |
Eric Fiselier | 8cef7fd | 2018-06-05 22:32:52 +0000 | [diff] [blame] | 67 | int test_alloc_base::copied = 0; |
| 68 | int test_alloc_base::moved = 0; |
| 69 | int test_alloc_base::converted = 0; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 70 | |
| 71 | template <class T> |
| 72 | class test_allocator |
| 73 | : public test_alloc_base |
| 74 | { |
Eric Fiselier | 1286bc5 | 2016-12-11 03:41:12 +0000 | [diff] [blame] | 75 | int data_; // participates in equality |
| 76 | int id_; // unique identifier, doesn't participate in equality |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 77 | template <class U> friend class test_allocator; |
| 78 | public: |
| 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 Fiselier | 1286bc5 | 2016-12-11 03:41:12 +0000 | [diff] [blame] | 90 | 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 Fiselier | 8cef7fd | 2018-06-05 22:32:52 +0000 | [diff] [blame] | 93 | 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 Fiselier | 1286bc5 | 2016-12-11 03:41:12 +0000 | [diff] [blame] | 117 | ~test_allocator() TEST_NOEXCEPT { |
| 118 | assert(data_ >= 0); assert(id_ >= 0); |
Eric Fiselier | 8cef7fd | 2018-06-05 22:32:52 +0000 | [diff] [blame] | 119 | --count; |
| 120 | data_ = destructed_value; |
| 121 | id_ = destructed_value; |
Eric Fiselier | 1286bc5 | 2016-12-11 03:41:12 +0000 | [diff] [blame] | 122 | } |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 123 | 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 Clow | c3deeb5 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 127 | assert(data_ >= 0); |
| 128 | if (time_to_throw >= throw_after) { |
Eric Fiselier | 54613ab | 2016-09-25 03:34:28 +0000 | [diff] [blame] | 129 | #ifndef TEST_HAS_NO_EXCEPTIONS |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 130 | throw std::bad_alloc(); |
Howard Hinnant | 65a87cc | 2013-03-23 17:27:16 +0000 | [diff] [blame] | 131 | #else |
| 132 | std::terminate(); |
| 133 | #endif |
| 134 | } |
Marshall Clow | c3deeb5 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 135 | ++time_to_throw; |
| 136 | ++alloc_count; |
Eric Fiselier | b11df18 | 2015-06-14 23:30:09 +0000 | [diff] [blame] | 137 | return (pointer)::operator new(n * sizeof(T)); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 138 | } |
Eric Fiselier | 7626f77 | 2016-04-28 03:17:56 +0000 | [diff] [blame] | 139 | void deallocate(pointer p, size_type) |
Eric Fiselier | b11df18 | 2015-06-14 23:30:09 +0000 | [diff] [blame] | 140 | {assert(data_ >= 0); --alloc_count; ::operator delete((void*)p);} |
Eric Fiselier | 3ca4566 | 2016-12-11 02:47:36 +0000 | [diff] [blame] | 141 | size_type max_size() const TEST_NOEXCEPT |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 142 | {return UINT_MAX / sizeof(T);} |
Eric Fiselier | 4fae502 | 2016-06-15 01:53:32 +0000 | [diff] [blame] | 143 | #if TEST_STD_VER < 11 |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 144 | void construct(pointer p, const T& val) |
Eric Fiselier | 4fae502 | 2016-06-15 01:53:32 +0000 | [diff] [blame] | 145 | {::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 Fiselier | 408d6eb | 2016-06-22 01:02:08 +0000 | [diff] [blame] | 150 | void destroy(pointer p) |
Eric Fiselier | 89c9191 | 2016-10-07 18:51:33 +0000 | [diff] [blame] | 151 | {p->~T();} |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 152 | 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 Fiselier | 1286bc5 | 2016-12-11 03:41:12 +0000 | [diff] [blame] | 156 | |
| 157 | int get_data() const { return data_; } |
| 158 | int get_id() const { return id_; } |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 159 | }; |
| 160 | |
Marshall Clow | 5f7c2db | 2014-04-18 17:23:36 +0000 | [diff] [blame] | 161 | template <class T> |
| 162 | class non_default_test_allocator |
| 163 | : public test_alloc_base |
| 164 | { |
| 165 | int data_; |
| 166 | |
| 167 | template <class U> friend class non_default_test_allocator; |
| 168 | public: |
| 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 Fiselier | 3ca4566 | 2016-12-11 02:47:36 +0000 | [diff] [blame] | 180 | // 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 Clow | 5f7c2db | 2014-04-18 17:23:36 +0000 | [diff] [blame] | 183 | : data_(a.data_) {++count;} |
Eric Fiselier | 3ca4566 | 2016-12-11 02:47:36 +0000 | [diff] [blame] | 184 | template <class U> non_default_test_allocator(const non_default_test_allocator<U>& a) TEST_NOEXCEPT |
Marshall Clow | 5f7c2db | 2014-04-18 17:23:36 +0000 | [diff] [blame] | 185 | : data_(a.data_) {++count;} |
Eric Fiselier | 3ca4566 | 2016-12-11 02:47:36 +0000 | [diff] [blame] | 186 | ~non_default_test_allocator() TEST_NOEXCEPT {assert(data_ >= 0); --count; data_ = -1;} |
Marshall Clow | 5f7c2db | 2014-04-18 17:23:36 +0000 | [diff] [blame] | 187 | 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 Fiselier | 54613ab | 2016-09-25 03:34:28 +0000 | [diff] [blame] | 193 | #ifndef TEST_HAS_NO_EXCEPTIONS |
Marshall Clow | 5f7c2db | 2014-04-18 17:23:36 +0000 | [diff] [blame] | 194 | throw std::bad_alloc(); |
| 195 | #else |
| 196 | std::terminate(); |
| 197 | #endif |
| 198 | } |
| 199 | ++time_to_throw; |
| 200 | ++alloc_count; |
Eric Fiselier | b11df18 | 2015-06-14 23:30:09 +0000 | [diff] [blame] | 201 | return (pointer)::operator new (n * sizeof(T)); |
Marshall Clow | 5f7c2db | 2014-04-18 17:23:36 +0000 | [diff] [blame] | 202 | } |
Eric Fiselier | 7626f77 | 2016-04-28 03:17:56 +0000 | [diff] [blame] | 203 | void deallocate(pointer p, size_type) |
Eric Fiselier | b11df18 | 2015-06-14 23:30:09 +0000 | [diff] [blame] | 204 | {assert(data_ >= 0); --alloc_count; ::operator delete((void*)p); } |
Eric Fiselier | 3ca4566 | 2016-12-11 02:47:36 +0000 | [diff] [blame] | 205 | size_type max_size() const TEST_NOEXCEPT |
Marshall Clow | 5f7c2db | 2014-04-18 17:23:36 +0000 | [diff] [blame] | 206 | {return UINT_MAX / sizeof(T);} |
Eric Fiselier | 4fae502 | 2016-06-15 01:53:32 +0000 | [diff] [blame] | 207 | #if TEST_STD_VER < 11 |
Marshall Clow | 5f7c2db | 2014-04-18 17:23:36 +0000 | [diff] [blame] | 208 | void construct(pointer p, const T& val) |
Eric Fiselier | 4fae502 | 2016-06-15 01:53:32 +0000 | [diff] [blame] | 209 | {::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 Clow | 5f7c2db | 2014-04-18 17:23:36 +0000 | [diff] [blame] | 214 | 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 Clow | c3deeb5 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 222 | template <> |
| 223 | class test_allocator<void> |
| 224 | : public test_alloc_base |
| 225 | { |
| 226 | int data_; |
Eric Fiselier | 1286bc5 | 2016-12-11 03:41:12 +0000 | [diff] [blame] | 227 | int id_; |
Marshall Clow | c3deeb5 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 228 | |
| 229 | template <class U> friend class test_allocator; |
| 230 | public: |
| 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 Fiselier | 1286bc5 | 2016-12-11 03:41:12 +0000 | [diff] [blame] | 240 | test_allocator() TEST_NOEXCEPT : data_(0), id_(0) {} |
| 241 | explicit test_allocator(int i, int id = 0) TEST_NOEXCEPT : data_(i), id_(id) {} |
Eric Fiselier | 3ca4566 | 2016-12-11 02:47:36 +0000 | [diff] [blame] | 242 | test_allocator(const test_allocator& a) TEST_NOEXCEPT |
Eric Fiselier | 1286bc5 | 2016-12-11 03:41:12 +0000 | [diff] [blame] | 243 | : data_(a.data_), id_(a.id_) {} |
Eric Fiselier | 3ca4566 | 2016-12-11 02:47:36 +0000 | [diff] [blame] | 244 | template <class U> test_allocator(const test_allocator<U>& a) TEST_NOEXCEPT |
Eric Fiselier | 1286bc5 | 2016-12-11 03:41:12 +0000 | [diff] [blame] | 245 | : 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 Clow | c3deeb5 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 250 | |
| 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 Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 257 | template <class T> |
| 258 | class other_allocator |
| 259 | { |
| 260 | int data_; |
| 261 | |
| 262 | template <class U> friend class other_allocator; |
| 263 | |
| 264 | public: |
| 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 Fiselier | b11df18 | 2015-06-14 23:30:09 +0000 | [diff] [blame] | 272 | {return (T*)::operator new(n * sizeof(T));} |
Eric Fiselier | 7626f77 | 2016-04-28 03:17:56 +0000 | [diff] [blame] | 273 | void deallocate(T* p, std::size_t) |
Eric Fiselier | b11df18 | 2015-06-14 23:30:09 +0000 | [diff] [blame] | 274 | {::operator delete((void*)p);} |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 275 | |
| 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 Fiselier | 54613ab | 2016-09-25 03:34:28 +0000 | [diff] [blame] | 288 | #if TEST_STD_VER < 11 |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 289 | std::size_t max_size() const |
| 290 | {return UINT_MAX / sizeof(T);} |
Eric Fiselier | 54613ab | 2016-09-25 03:34:28 +0000 | [diff] [blame] | 291 | #endif |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 292 | |
| 293 | }; |
| 294 | |
Marshall Clow | dc3eb83 | 2016-07-11 21:38:08 +0000 | [diff] [blame] | 295 | #if TEST_STD_VER >= 11 |
| 296 | |
| 297 | struct Ctor_Tag {}; |
| 298 | |
| 299 | template <typename T> class TaggingAllocator; |
| 300 | |
| 301 | struct 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 | |
| 326 | private: |
| 327 | // Not Destructible. |
| 328 | ~Tag_X() { } |
| 329 | |
| 330 | // Erasable from vector<X, TaggingAllocator<X>>. |
| 331 | friend class TaggingAllocator<Tag_X>; |
| 332 | }; |
| 333 | |
| 334 | |
| 335 | template<typename T> |
| 336 | class TaggingAllocator { |
| 337 | public: |
| 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 Fiselier | 89c9191 | 2016-10-07 18:51:33 +0000 | [diff] [blame] | 358 | { p->~U(); } |
Marshall Clow | dc3eb83 | 2016-07-11 21:38:08 +0000 | [diff] [blame] | 359 | }; |
| 360 | |
| 361 | template<typename T, typename U> |
| 362 | bool |
| 363 | operator==(const TaggingAllocator<T>&, const TaggingAllocator<U>&) |
| 364 | { return true; } |
| 365 | |
| 366 | template<typename T, typename U> |
| 367 | bool |
| 368 | operator!=(const TaggingAllocator<T>&, const TaggingAllocator<U>&) |
| 369 | { return false; } |
| 370 | #endif |
| 371 | |
Eric Fiselier | 69a4f66 | 2016-10-08 00:56:22 +0000 | [diff] [blame] | 372 | template <std::size_t MaxAllocs> |
| 373 | struct 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 | |
| 398 | template <class T, std::size_t N> |
| 399 | class limited_allocator |
| 400 | { |
Eric Fiselier | a9fcc1d | 2016-10-12 11:35:37 +0000 | [diff] [blame] | 401 | template <class U, std::size_t UN> friend class limited_allocator; |
Eric Fiselier | 69a4f66 | 2016-10-08 00:56:22 +0000 | [diff] [blame] | 402 | typedef limited_alloc_handle<N> BuffT; |
| 403 | std::shared_ptr<BuffT> handle_; |
| 404 | public: |
| 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 | |
| 423 | private: |
| 424 | limited_allocator& operator=(const limited_allocator&);// = delete; |
| 425 | |
| 426 | public: |
| 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 | |
| 434 | template <class T, class U, std::size_t N> |
| 435 | inline bool operator==(limited_allocator<T, N> const& LHS, |
| 436 | limited_allocator<U, N> const& RHS) { |
| 437 | return LHS.getHandle() == RHS.getHandle(); |
| 438 | } |
| 439 | |
| 440 | template <class T, class U, std::size_t N> |
| 441 | inline bool operator!=(limited_allocator<T, N> const& LHS, |
| 442 | limited_allocator<U, N> const& RHS) { |
| 443 | return !(LHS == RHS); |
| 444 | } |
| 445 | |
Marshall Clow | dc3eb83 | 2016-07-11 21:38:08 +0000 | [diff] [blame] | 446 | |
Howard Hinnant | 8f2f7e7 | 2010-08-22 00:15:28 +0000 | [diff] [blame] | 447 | #endif // TEST_ALLOCATOR_H |