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> |
| 16 | #include <cstddef> |
| 17 | #include <cstdlib> |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 18 | #include <climits> |
Marshall Clow | c3deeb5 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 19 | #include <cassert> |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 20 | |
Marshall Clow | cbf166a | 2015-06-03 19:56:43 +0000 | [diff] [blame] | 21 | #include "test_macros.h" |
| 22 | |
Eric Fiselier | 55b31b4e | 2016-11-23 01:18:56 +0000 | [diff] [blame] | 23 | template <class Alloc> |
Eric Fiselier | 341c9dd | 2016-11-23 09:16:12 +0000 | [diff] [blame] | 24 | inline typename std::allocator_traits<Alloc>::size_type |
| 25 | alloc_max_size(Alloc const &a) { |
Eric Fiselier | 55b31b4e | 2016-11-23 01:18:56 +0000 | [diff] [blame] | 26 | typedef std::allocator_traits<Alloc> AT; |
| 27 | return AT::max_size(a); |
| 28 | } |
| 29 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 30 | class test_alloc_base |
| 31 | { |
| 32 | protected: |
Marshall Clow | c3deeb5 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 33 | static int time_to_throw; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 34 | public: |
| 35 | static int throw_after; |
Marshall Clow | c3deeb5 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 36 | static int count; |
| 37 | static int alloc_count; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | int test_alloc_base::count = 0; |
Marshall Clow | c3deeb5 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 41 | int test_alloc_base::time_to_throw = 0; |
| 42 | int test_alloc_base::alloc_count = 0; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 43 | int test_alloc_base::throw_after = INT_MAX; |
| 44 | |
| 45 | template <class T> |
| 46 | class test_allocator |
| 47 | : public test_alloc_base |
| 48 | { |
| 49 | int data_; |
| 50 | |
| 51 | template <class U> friend class test_allocator; |
| 52 | public: |
| 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 Fiselier | 3ca4566 | 2016-12-11 02:47:36 +0000 | [diff] [blame^] | 64 | 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 Clow | c3deeb5 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 67 | : data_(a.data_) {++count;} |
Eric Fiselier | 3ca4566 | 2016-12-11 02:47:36 +0000 | [diff] [blame^] | 68 | template <class U> test_allocator(const test_allocator<U>& a) TEST_NOEXCEPT |
Marshall Clow | c3deeb5 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 69 | : data_(a.data_) {++count;} |
Eric Fiselier | 3ca4566 | 2016-12-11 02:47:36 +0000 | [diff] [blame^] | 70 | ~test_allocator() TEST_NOEXCEPT {assert(data_ >= 0); --count; data_ = -1;} |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 71 | 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 Clow | c3deeb5 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 75 | assert(data_ >= 0); |
| 76 | if (time_to_throw >= throw_after) { |
Eric Fiselier | 54613ab | 2016-09-25 03:34:28 +0000 | [diff] [blame] | 77 | #ifndef TEST_HAS_NO_EXCEPTIONS |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 78 | throw std::bad_alloc(); |
Howard Hinnant | 65a87cc | 2013-03-23 17:27:16 +0000 | [diff] [blame] | 79 | #else |
| 80 | std::terminate(); |
| 81 | #endif |
| 82 | } |
Marshall Clow | c3deeb5 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 83 | ++time_to_throw; |
| 84 | ++alloc_count; |
Eric Fiselier | b11df18 | 2015-06-14 23:30:09 +0000 | [diff] [blame] | 85 | return (pointer)::operator new(n * sizeof(T)); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 86 | } |
Eric Fiselier | 7626f77 | 2016-04-28 03:17:56 +0000 | [diff] [blame] | 87 | void deallocate(pointer p, size_type) |
Eric Fiselier | b11df18 | 2015-06-14 23:30:09 +0000 | [diff] [blame] | 88 | {assert(data_ >= 0); --alloc_count; ::operator delete((void*)p);} |
Eric Fiselier | 3ca4566 | 2016-12-11 02:47:36 +0000 | [diff] [blame^] | 89 | size_type max_size() const TEST_NOEXCEPT |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 90 | {return UINT_MAX / sizeof(T);} |
Eric Fiselier | 4fae502 | 2016-06-15 01:53:32 +0000 | [diff] [blame] | 91 | #if TEST_STD_VER < 11 |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 92 | void construct(pointer p, const T& val) |
Eric Fiselier | 4fae502 | 2016-06-15 01:53:32 +0000 | [diff] [blame] | 93 | {::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 Fiselier | 408d6eb | 2016-06-22 01:02:08 +0000 | [diff] [blame] | 98 | void destroy(pointer p) |
Eric Fiselier | 89c9191 | 2016-10-07 18:51:33 +0000 | [diff] [blame] | 99 | {p->~T();} |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 100 | 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 Clow | 5f7c2db | 2014-04-18 17:23:36 +0000 | [diff] [blame] | 106 | template <class T> |
| 107 | class non_default_test_allocator |
| 108 | : public test_alloc_base |
| 109 | { |
| 110 | int data_; |
| 111 | |
| 112 | template <class U> friend class non_default_test_allocator; |
| 113 | public: |
| 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 Fiselier | 3ca4566 | 2016-12-11 02:47:36 +0000 | [diff] [blame^] | 125 | // 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 Clow | 5f7c2db | 2014-04-18 17:23:36 +0000 | [diff] [blame] | 128 | : data_(a.data_) {++count;} |
Eric Fiselier | 3ca4566 | 2016-12-11 02:47:36 +0000 | [diff] [blame^] | 129 | 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] | 130 | : data_(a.data_) {++count;} |
Eric Fiselier | 3ca4566 | 2016-12-11 02:47:36 +0000 | [diff] [blame^] | 131 | ~non_default_test_allocator() TEST_NOEXCEPT {assert(data_ >= 0); --count; data_ = -1;} |
Marshall Clow | 5f7c2db | 2014-04-18 17:23:36 +0000 | [diff] [blame] | 132 | 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 Fiselier | 54613ab | 2016-09-25 03:34:28 +0000 | [diff] [blame] | 138 | #ifndef TEST_HAS_NO_EXCEPTIONS |
Marshall Clow | 5f7c2db | 2014-04-18 17:23:36 +0000 | [diff] [blame] | 139 | throw std::bad_alloc(); |
| 140 | #else |
| 141 | std::terminate(); |
| 142 | #endif |
| 143 | } |
| 144 | ++time_to_throw; |
| 145 | ++alloc_count; |
Eric Fiselier | b11df18 | 2015-06-14 23:30:09 +0000 | [diff] [blame] | 146 | return (pointer)::operator new (n * sizeof(T)); |
Marshall Clow | 5f7c2db | 2014-04-18 17:23:36 +0000 | [diff] [blame] | 147 | } |
Eric Fiselier | 7626f77 | 2016-04-28 03:17:56 +0000 | [diff] [blame] | 148 | void deallocate(pointer p, size_type) |
Eric Fiselier | b11df18 | 2015-06-14 23:30:09 +0000 | [diff] [blame] | 149 | {assert(data_ >= 0); --alloc_count; ::operator delete((void*)p); } |
Eric Fiselier | 3ca4566 | 2016-12-11 02:47:36 +0000 | [diff] [blame^] | 150 | size_type max_size() const TEST_NOEXCEPT |
Marshall Clow | 5f7c2db | 2014-04-18 17:23:36 +0000 | [diff] [blame] | 151 | {return UINT_MAX / sizeof(T);} |
Eric Fiselier | 4fae502 | 2016-06-15 01:53:32 +0000 | [diff] [blame] | 152 | #if TEST_STD_VER < 11 |
Marshall Clow | 5f7c2db | 2014-04-18 17:23:36 +0000 | [diff] [blame] | 153 | void construct(pointer p, const T& val) |
Eric Fiselier | 4fae502 | 2016-06-15 01:53:32 +0000 | [diff] [blame] | 154 | {::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 Clow | 5f7c2db | 2014-04-18 17:23:36 +0000 | [diff] [blame] | 159 | 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 Clow | c3deeb5 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 167 | template <> |
| 168 | class test_allocator<void> |
| 169 | : public test_alloc_base |
| 170 | { |
| 171 | int data_; |
| 172 | |
| 173 | template <class U> friend class test_allocator; |
| 174 | public: |
| 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 Fiselier | 3ca4566 | 2016-12-11 02:47:36 +0000 | [diff] [blame^] | 184 | 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 Clow | c3deeb5 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 187 | : data_(a.data_) {} |
Eric Fiselier | 3ca4566 | 2016-12-11 02:47:36 +0000 | [diff] [blame^] | 188 | template <class U> test_allocator(const test_allocator<U>& a) TEST_NOEXCEPT |
Marshall Clow | c3deeb5 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 189 | : data_(a.data_) {} |
Eric Fiselier | 3ca4566 | 2016-12-11 02:47:36 +0000 | [diff] [blame^] | 190 | ~test_allocator() TEST_NOEXCEPT {data_ = -1;} |
Marshall Clow | c3deeb5 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 191 | |
| 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 Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 198 | template <class T> |
| 199 | class other_allocator |
| 200 | { |
| 201 | int data_; |
| 202 | |
| 203 | template <class U> friend class other_allocator; |
| 204 | |
| 205 | public: |
| 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 Fiselier | b11df18 | 2015-06-14 23:30:09 +0000 | [diff] [blame] | 213 | {return (T*)::operator new(n * sizeof(T));} |
Eric Fiselier | 7626f77 | 2016-04-28 03:17:56 +0000 | [diff] [blame] | 214 | void deallocate(T* p, std::size_t) |
Eric Fiselier | b11df18 | 2015-06-14 23:30:09 +0000 | [diff] [blame] | 215 | {::operator delete((void*)p);} |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 216 | |
| 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 Fiselier | 54613ab | 2016-09-25 03:34:28 +0000 | [diff] [blame] | 229 | #if TEST_STD_VER < 11 |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 230 | std::size_t max_size() const |
| 231 | {return UINT_MAX / sizeof(T);} |
Eric Fiselier | 54613ab | 2016-09-25 03:34:28 +0000 | [diff] [blame] | 232 | #endif |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 233 | |
| 234 | }; |
| 235 | |
Marshall Clow | dc3eb83 | 2016-07-11 21:38:08 +0000 | [diff] [blame] | 236 | #if TEST_STD_VER >= 11 |
| 237 | |
| 238 | struct Ctor_Tag {}; |
| 239 | |
| 240 | template <typename T> class TaggingAllocator; |
| 241 | |
| 242 | struct 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 | |
| 267 | private: |
| 268 | // Not Destructible. |
| 269 | ~Tag_X() { } |
| 270 | |
| 271 | // Erasable from vector<X, TaggingAllocator<X>>. |
| 272 | friend class TaggingAllocator<Tag_X>; |
| 273 | }; |
| 274 | |
| 275 | |
| 276 | template<typename T> |
| 277 | class TaggingAllocator { |
| 278 | public: |
| 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 Fiselier | 89c9191 | 2016-10-07 18:51:33 +0000 | [diff] [blame] | 299 | { p->~U(); } |
Marshall Clow | dc3eb83 | 2016-07-11 21:38:08 +0000 | [diff] [blame] | 300 | }; |
| 301 | |
| 302 | template<typename T, typename U> |
| 303 | bool |
| 304 | operator==(const TaggingAllocator<T>&, const TaggingAllocator<U>&) |
| 305 | { return true; } |
| 306 | |
| 307 | template<typename T, typename U> |
| 308 | bool |
| 309 | operator!=(const TaggingAllocator<T>&, const TaggingAllocator<U>&) |
| 310 | { return false; } |
| 311 | #endif |
| 312 | |
Eric Fiselier | 69a4f66 | 2016-10-08 00:56:22 +0000 | [diff] [blame] | 313 | template <std::size_t MaxAllocs> |
| 314 | struct 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 | |
| 339 | template <class T, std::size_t N> |
| 340 | class limited_allocator |
| 341 | { |
Eric Fiselier | a9fcc1d | 2016-10-12 11:35:37 +0000 | [diff] [blame] | 342 | template <class U, std::size_t UN> friend class limited_allocator; |
Eric Fiselier | 69a4f66 | 2016-10-08 00:56:22 +0000 | [diff] [blame] | 343 | typedef limited_alloc_handle<N> BuffT; |
| 344 | std::shared_ptr<BuffT> handle_; |
| 345 | public: |
| 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 | |
| 364 | private: |
| 365 | limited_allocator& operator=(const limited_allocator&);// = delete; |
| 366 | |
| 367 | public: |
| 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 | |
| 375 | template <class T, class U, std::size_t N> |
| 376 | inline bool operator==(limited_allocator<T, N> const& LHS, |
| 377 | limited_allocator<U, N> const& RHS) { |
| 378 | return LHS.getHandle() == RHS.getHandle(); |
| 379 | } |
| 380 | |
| 381 | template <class T, class U, std::size_t N> |
| 382 | inline bool operator!=(limited_allocator<T, N> const& LHS, |
| 383 | limited_allocator<U, N> const& RHS) { |
| 384 | return !(LHS == RHS); |
| 385 | } |
| 386 | |
Marshall Clow | dc3eb83 | 2016-07-11 21:38:08 +0000 | [diff] [blame] | 387 | |
Howard Hinnant | 8f2f7e7 | 2010-08-22 00:15:28 +0000 | [diff] [blame] | 388 | #endif // TEST_ALLOCATOR_H |