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; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | int test_alloc_base::count = 0; |
Marshall Clow | c3deeb5 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 42 | int test_alloc_base::time_to_throw = 0; |
| 43 | int test_alloc_base::alloc_count = 0; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 44 | int test_alloc_base::throw_after = INT_MAX; |
| 45 | |
| 46 | template <class T> |
| 47 | class test_allocator |
| 48 | : public test_alloc_base |
| 49 | { |
Eric Fiselier | 1286bc5 | 2016-12-11 03:41:12 +0000 | [diff] [blame] | 50 | int data_; // participates in equality |
| 51 | int id_; // unique identifier, doesn't participate in equality |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 52 | template <class U> friend class test_allocator; |
| 53 | public: |
| 54 | |
| 55 | typedef unsigned size_type; |
| 56 | typedef int difference_type; |
| 57 | typedef T value_type; |
| 58 | typedef value_type* pointer; |
| 59 | typedef const value_type* const_pointer; |
| 60 | typedef typename std::add_lvalue_reference<value_type>::type reference; |
| 61 | typedef typename std::add_lvalue_reference<const value_type>::type const_reference; |
| 62 | |
| 63 | template <class U> struct rebind {typedef test_allocator<U> other;}; |
| 64 | |
Eric Fiselier | 1286bc5 | 2016-12-11 03:41:12 +0000 | [diff] [blame] | 65 | test_allocator() TEST_NOEXCEPT : data_(0), id_(0) {++count;} |
| 66 | explicit test_allocator(int i, int id = 0) TEST_NOEXCEPT : data_(i), id_(id) |
| 67 | {++count;} |
Eric Fiselier | 3ca4566 | 2016-12-11 02:47:36 +0000 | [diff] [blame] | 68 | test_allocator(const test_allocator& a) TEST_NOEXCEPT |
Eric Fiselier | 1286bc5 | 2016-12-11 03:41:12 +0000 | [diff] [blame] | 69 | : data_(a.data_), id_(a.id_) {++count;} |
Eric Fiselier | 3ca4566 | 2016-12-11 02:47:36 +0000 | [diff] [blame] | 70 | template <class U> test_allocator(const test_allocator<U>& a) TEST_NOEXCEPT |
Eric Fiselier | 1286bc5 | 2016-12-11 03:41:12 +0000 | [diff] [blame] | 71 | : data_(a.data_), id_(a.id_) {++count;} |
| 72 | ~test_allocator() TEST_NOEXCEPT { |
| 73 | assert(data_ >= 0); assert(id_ >= 0); |
| 74 | --count; data_ = -1; id_ = -1; |
| 75 | } |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 76 | pointer address(reference x) const {return &x;} |
| 77 | const_pointer address(const_reference x) const {return &x;} |
| 78 | pointer allocate(size_type n, const void* = 0) |
| 79 | { |
Marshall Clow | c3deeb5 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 80 | assert(data_ >= 0); |
| 81 | if (time_to_throw >= throw_after) { |
Eric Fiselier | 54613ab | 2016-09-25 03:34:28 +0000 | [diff] [blame] | 82 | #ifndef TEST_HAS_NO_EXCEPTIONS |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 83 | throw std::bad_alloc(); |
Howard Hinnant | 65a87cc | 2013-03-23 17:27:16 +0000 | [diff] [blame] | 84 | #else |
| 85 | std::terminate(); |
| 86 | #endif |
| 87 | } |
Marshall Clow | c3deeb5 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 88 | ++time_to_throw; |
| 89 | ++alloc_count; |
Eric Fiselier | b11df18 | 2015-06-14 23:30:09 +0000 | [diff] [blame] | 90 | return (pointer)::operator new(n * sizeof(T)); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 91 | } |
Eric Fiselier | 7626f77 | 2016-04-28 03:17:56 +0000 | [diff] [blame] | 92 | void deallocate(pointer p, size_type) |
Eric Fiselier | b11df18 | 2015-06-14 23:30:09 +0000 | [diff] [blame] | 93 | {assert(data_ >= 0); --alloc_count; ::operator delete((void*)p);} |
Eric Fiselier | 3ca4566 | 2016-12-11 02:47:36 +0000 | [diff] [blame] | 94 | size_type max_size() const TEST_NOEXCEPT |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 95 | {return UINT_MAX / sizeof(T);} |
Eric Fiselier | 4fae502 | 2016-06-15 01:53:32 +0000 | [diff] [blame] | 96 | #if TEST_STD_VER < 11 |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 97 | void construct(pointer p, const T& val) |
Eric Fiselier | 4fae502 | 2016-06-15 01:53:32 +0000 | [diff] [blame] | 98 | {::new(static_cast<void*>(p)) T(val);} |
| 99 | #else |
| 100 | template <class U> void construct(pointer p, U&& val) |
| 101 | {::new(static_cast<void*>(p)) T(std::forward<U>(val));} |
| 102 | #endif |
Eric Fiselier | 408d6eb | 2016-06-22 01:02:08 +0000 | [diff] [blame] | 103 | void destroy(pointer p) |
Eric Fiselier | 89c9191 | 2016-10-07 18:51:33 +0000 | [diff] [blame] | 104 | {p->~T();} |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 105 | friend bool operator==(const test_allocator& x, const test_allocator& y) |
| 106 | {return x.data_ == y.data_;} |
| 107 | friend bool operator!=(const test_allocator& x, const test_allocator& y) |
| 108 | {return !(x == y);} |
Eric Fiselier | 1286bc5 | 2016-12-11 03:41:12 +0000 | [diff] [blame] | 109 | |
| 110 | int get_data() const { return data_; } |
| 111 | int get_id() const { return id_; } |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 112 | }; |
| 113 | |
Marshall Clow | 5f7c2db | 2014-04-18 17:23:36 +0000 | [diff] [blame] | 114 | template <class T> |
| 115 | class non_default_test_allocator |
| 116 | : public test_alloc_base |
| 117 | { |
| 118 | int data_; |
| 119 | |
| 120 | template <class U> friend class non_default_test_allocator; |
| 121 | public: |
| 122 | |
| 123 | typedef unsigned size_type; |
| 124 | typedef int difference_type; |
| 125 | typedef T value_type; |
| 126 | typedef value_type* pointer; |
| 127 | typedef const value_type* const_pointer; |
| 128 | typedef typename std::add_lvalue_reference<value_type>::type reference; |
| 129 | typedef typename std::add_lvalue_reference<const value_type>::type const_reference; |
| 130 | |
| 131 | template <class U> struct rebind {typedef non_default_test_allocator<U> other;}; |
| 132 | |
Eric Fiselier | 3ca4566 | 2016-12-11 02:47:36 +0000 | [diff] [blame] | 133 | // non_default_test_allocator() TEST_NOEXCEPT : data_(0) {++count;} |
| 134 | explicit non_default_test_allocator(int i) TEST_NOEXCEPT : data_(i) {++count;} |
| 135 | non_default_test_allocator(const non_default_test_allocator& a) TEST_NOEXCEPT |
Marshall Clow | 5f7c2db | 2014-04-18 17:23:36 +0000 | [diff] [blame] | 136 | : data_(a.data_) {++count;} |
Eric Fiselier | 3ca4566 | 2016-12-11 02:47:36 +0000 | [diff] [blame] | 137 | 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] | 138 | : data_(a.data_) {++count;} |
Eric Fiselier | 3ca4566 | 2016-12-11 02:47:36 +0000 | [diff] [blame] | 139 | ~non_default_test_allocator() TEST_NOEXCEPT {assert(data_ >= 0); --count; data_ = -1;} |
Marshall Clow | 5f7c2db | 2014-04-18 17:23:36 +0000 | [diff] [blame] | 140 | pointer address(reference x) const {return &x;} |
| 141 | const_pointer address(const_reference x) const {return &x;} |
| 142 | pointer allocate(size_type n, const void* = 0) |
| 143 | { |
| 144 | assert(data_ >= 0); |
| 145 | if (time_to_throw >= throw_after) { |
Eric Fiselier | 54613ab | 2016-09-25 03:34:28 +0000 | [diff] [blame] | 146 | #ifndef TEST_HAS_NO_EXCEPTIONS |
Marshall Clow | 5f7c2db | 2014-04-18 17:23:36 +0000 | [diff] [blame] | 147 | throw std::bad_alloc(); |
| 148 | #else |
| 149 | std::terminate(); |
| 150 | #endif |
| 151 | } |
| 152 | ++time_to_throw; |
| 153 | ++alloc_count; |
Eric Fiselier | b11df18 | 2015-06-14 23:30:09 +0000 | [diff] [blame] | 154 | return (pointer)::operator new (n * sizeof(T)); |
Marshall Clow | 5f7c2db | 2014-04-18 17:23:36 +0000 | [diff] [blame] | 155 | } |
Eric Fiselier | 7626f77 | 2016-04-28 03:17:56 +0000 | [diff] [blame] | 156 | void deallocate(pointer p, size_type) |
Eric Fiselier | b11df18 | 2015-06-14 23:30:09 +0000 | [diff] [blame] | 157 | {assert(data_ >= 0); --alloc_count; ::operator delete((void*)p); } |
Eric Fiselier | 3ca4566 | 2016-12-11 02:47:36 +0000 | [diff] [blame] | 158 | size_type max_size() const TEST_NOEXCEPT |
Marshall Clow | 5f7c2db | 2014-04-18 17:23:36 +0000 | [diff] [blame] | 159 | {return UINT_MAX / sizeof(T);} |
Eric Fiselier | 4fae502 | 2016-06-15 01:53:32 +0000 | [diff] [blame] | 160 | #if TEST_STD_VER < 11 |
Marshall Clow | 5f7c2db | 2014-04-18 17:23:36 +0000 | [diff] [blame] | 161 | void construct(pointer p, const T& val) |
Eric Fiselier | 4fae502 | 2016-06-15 01:53:32 +0000 | [diff] [blame] | 162 | {::new(static_cast<void*>(p)) T(val);} |
| 163 | #else |
| 164 | template <class U> void construct(pointer p, U&& val) |
| 165 | {::new(static_cast<void*>(p)) T(std::forward<U>(val));} |
| 166 | #endif |
Marshall Clow | 5f7c2db | 2014-04-18 17:23:36 +0000 | [diff] [blame] | 167 | void destroy(pointer p) {p->~T();} |
| 168 | |
| 169 | friend bool operator==(const non_default_test_allocator& x, const non_default_test_allocator& y) |
| 170 | {return x.data_ == y.data_;} |
| 171 | friend bool operator!=(const non_default_test_allocator& x, const non_default_test_allocator& y) |
| 172 | {return !(x == y);} |
| 173 | }; |
| 174 | |
Marshall Clow | c3deeb5 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 175 | template <> |
| 176 | class test_allocator<void> |
| 177 | : public test_alloc_base |
| 178 | { |
| 179 | int data_; |
Eric Fiselier | 1286bc5 | 2016-12-11 03:41:12 +0000 | [diff] [blame] | 180 | int id_; |
Marshall Clow | c3deeb5 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 181 | |
| 182 | template <class U> friend class test_allocator; |
| 183 | public: |
| 184 | |
| 185 | typedef unsigned size_type; |
| 186 | typedef int difference_type; |
| 187 | typedef void value_type; |
| 188 | typedef value_type* pointer; |
| 189 | typedef const value_type* const_pointer; |
| 190 | |
| 191 | template <class U> struct rebind {typedef test_allocator<U> other;}; |
| 192 | |
Eric Fiselier | 1286bc5 | 2016-12-11 03:41:12 +0000 | [diff] [blame] | 193 | test_allocator() TEST_NOEXCEPT : data_(0), id_(0) {} |
| 194 | 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] | 195 | test_allocator(const test_allocator& a) TEST_NOEXCEPT |
Eric Fiselier | 1286bc5 | 2016-12-11 03:41:12 +0000 | [diff] [blame] | 196 | : data_(a.data_), id_(a.id_) {} |
Eric Fiselier | 3ca4566 | 2016-12-11 02:47:36 +0000 | [diff] [blame] | 197 | template <class U> test_allocator(const test_allocator<U>& a) TEST_NOEXCEPT |
Eric Fiselier | 1286bc5 | 2016-12-11 03:41:12 +0000 | [diff] [blame] | 198 | : data_(a.data_), id_(a.id_) {} |
| 199 | ~test_allocator() TEST_NOEXCEPT {data_ = -1; id_ = -1; } |
| 200 | |
| 201 | int get_id() const { return id_; } |
| 202 | int get_data() const { return data_; } |
Marshall Clow | c3deeb5 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 203 | |
| 204 | friend bool operator==(const test_allocator& x, const test_allocator& y) |
| 205 | {return x.data_ == y.data_;} |
| 206 | friend bool operator!=(const test_allocator& x, const test_allocator& y) |
| 207 | {return !(x == y);} |
| 208 | }; |
| 209 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 210 | template <class T> |
| 211 | class other_allocator |
| 212 | { |
| 213 | int data_; |
| 214 | |
| 215 | template <class U> friend class other_allocator; |
| 216 | |
| 217 | public: |
| 218 | typedef T value_type; |
| 219 | |
| 220 | other_allocator() : data_(-1) {} |
| 221 | explicit other_allocator(int i) : data_(i) {} |
| 222 | template <class U> other_allocator(const other_allocator<U>& a) |
| 223 | : data_(a.data_) {} |
| 224 | T* allocate(std::size_t n) |
Eric Fiselier | b11df18 | 2015-06-14 23:30:09 +0000 | [diff] [blame] | 225 | {return (T*)::operator new(n * sizeof(T));} |
Eric Fiselier | 7626f77 | 2016-04-28 03:17:56 +0000 | [diff] [blame] | 226 | void deallocate(T* p, std::size_t) |
Eric Fiselier | b11df18 | 2015-06-14 23:30:09 +0000 | [diff] [blame] | 227 | {::operator delete((void*)p);} |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 228 | |
| 229 | other_allocator select_on_container_copy_construction() const |
| 230 | {return other_allocator(-2);} |
| 231 | |
| 232 | friend bool operator==(const other_allocator& x, const other_allocator& y) |
| 233 | {return x.data_ == y.data_;} |
| 234 | friend bool operator!=(const other_allocator& x, const other_allocator& y) |
| 235 | {return !(x == y);} |
| 236 | |
| 237 | typedef std::true_type propagate_on_container_copy_assignment; |
| 238 | typedef std::true_type propagate_on_container_move_assignment; |
| 239 | typedef std::true_type propagate_on_container_swap; |
| 240 | |
Eric Fiselier | 54613ab | 2016-09-25 03:34:28 +0000 | [diff] [blame] | 241 | #if TEST_STD_VER < 11 |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 242 | std::size_t max_size() const |
| 243 | {return UINT_MAX / sizeof(T);} |
Eric Fiselier | 54613ab | 2016-09-25 03:34:28 +0000 | [diff] [blame] | 244 | #endif |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 245 | |
| 246 | }; |
| 247 | |
Marshall Clow | dc3eb83 | 2016-07-11 21:38:08 +0000 | [diff] [blame] | 248 | #if TEST_STD_VER >= 11 |
| 249 | |
| 250 | struct Ctor_Tag {}; |
| 251 | |
| 252 | template <typename T> class TaggingAllocator; |
| 253 | |
| 254 | struct Tag_X { |
| 255 | // All constructors must be passed the Tag type. |
| 256 | |
| 257 | // DefaultInsertable into vector<X, TaggingAllocator<X>>, |
| 258 | Tag_X(Ctor_Tag) {} |
| 259 | // CopyInsertable into vector<X, TaggingAllocator<X>>, |
| 260 | Tag_X(Ctor_Tag, const Tag_X&) {} |
| 261 | // MoveInsertable into vector<X, TaggingAllocator<X>>, and |
| 262 | Tag_X(Ctor_Tag, Tag_X&&) {} |
| 263 | |
| 264 | // EmplaceConstructible into vector<X, TaggingAllocator<X>> from args. |
| 265 | template<typename... Args> |
| 266 | Tag_X(Ctor_Tag, Args&&...) { } |
| 267 | |
| 268 | // not DefaultConstructible, CopyConstructible or MoveConstructible. |
| 269 | Tag_X() = delete; |
| 270 | Tag_X(const Tag_X&) = delete; |
| 271 | Tag_X(Tag_X&&) = delete; |
| 272 | |
| 273 | // CopyAssignable. |
| 274 | Tag_X& operator=(const Tag_X&) { return *this; } |
| 275 | |
| 276 | // MoveAssignable. |
| 277 | Tag_X& operator=(Tag_X&&) { return *this; } |
| 278 | |
| 279 | private: |
| 280 | // Not Destructible. |
| 281 | ~Tag_X() { } |
| 282 | |
| 283 | // Erasable from vector<X, TaggingAllocator<X>>. |
| 284 | friend class TaggingAllocator<Tag_X>; |
| 285 | }; |
| 286 | |
| 287 | |
| 288 | template<typename T> |
| 289 | class TaggingAllocator { |
| 290 | public: |
| 291 | using value_type = T; |
| 292 | TaggingAllocator() = default; |
| 293 | |
| 294 | template<typename U> |
| 295 | TaggingAllocator(const TaggingAllocator<U>&) { } |
| 296 | |
| 297 | T* allocate(std::size_t n) { return std::allocator<T>{}.allocate(n); } |
| 298 | |
| 299 | void deallocate(T* p, std::size_t n) { std::allocator<T>{}.deallocate(p, n); } |
| 300 | |
| 301 | template<typename... Args> |
| 302 | void construct(Tag_X* p, Args&&... args) |
| 303 | { ::new((void*)p) Tag_X(Ctor_Tag{}, std::forward<Args>(args)...); } |
| 304 | |
| 305 | template<typename U, typename... Args> |
| 306 | void construct(U* p, Args&&... args) |
| 307 | { ::new((void*)p) U(std::forward<Args>(args)...); } |
| 308 | |
| 309 | template<typename U, typename... Args> |
| 310 | void destroy(U* p) |
Eric Fiselier | 89c9191 | 2016-10-07 18:51:33 +0000 | [diff] [blame] | 311 | { p->~U(); } |
Marshall Clow | dc3eb83 | 2016-07-11 21:38:08 +0000 | [diff] [blame] | 312 | }; |
| 313 | |
| 314 | template<typename T, typename U> |
| 315 | bool |
| 316 | operator==(const TaggingAllocator<T>&, const TaggingAllocator<U>&) |
| 317 | { return true; } |
| 318 | |
| 319 | template<typename T, typename U> |
| 320 | bool |
| 321 | operator!=(const TaggingAllocator<T>&, const TaggingAllocator<U>&) |
| 322 | { return false; } |
| 323 | #endif |
| 324 | |
Eric Fiselier | 69a4f66 | 2016-10-08 00:56:22 +0000 | [diff] [blame] | 325 | template <std::size_t MaxAllocs> |
| 326 | struct limited_alloc_handle { |
| 327 | std::size_t outstanding_; |
| 328 | void* last_alloc_; |
| 329 | |
| 330 | limited_alloc_handle() : outstanding_(0), last_alloc_(nullptr) {} |
| 331 | |
| 332 | template <class T> |
| 333 | T *allocate(std::size_t N) { |
| 334 | if (N + outstanding_ > MaxAllocs) |
| 335 | TEST_THROW(std::bad_alloc()); |
| 336 | last_alloc_ = ::operator new(N*sizeof(T)); |
| 337 | outstanding_ += N; |
| 338 | return static_cast<T*>(last_alloc_); |
| 339 | } |
| 340 | |
| 341 | void deallocate(void* ptr, std::size_t N) { |
| 342 | if (ptr == last_alloc_) { |
| 343 | last_alloc_ = nullptr; |
| 344 | assert(outstanding_ >= N); |
| 345 | outstanding_ -= N; |
| 346 | } |
| 347 | ::operator delete(ptr); |
| 348 | } |
| 349 | }; |
| 350 | |
| 351 | template <class T, std::size_t N> |
| 352 | class limited_allocator |
| 353 | { |
Eric Fiselier | a9fcc1d | 2016-10-12 11:35:37 +0000 | [diff] [blame] | 354 | template <class U, std::size_t UN> friend class limited_allocator; |
Eric Fiselier | 69a4f66 | 2016-10-08 00:56:22 +0000 | [diff] [blame] | 355 | typedef limited_alloc_handle<N> BuffT; |
| 356 | std::shared_ptr<BuffT> handle_; |
| 357 | public: |
| 358 | typedef T value_type; |
| 359 | typedef value_type* pointer; |
| 360 | typedef const value_type* const_pointer; |
| 361 | typedef value_type& reference; |
| 362 | typedef const value_type& const_reference; |
| 363 | typedef std::size_t size_type; |
| 364 | typedef std::ptrdiff_t difference_type; |
| 365 | |
| 366 | template <class U> struct rebind { typedef limited_allocator<U, N> other; }; |
| 367 | |
| 368 | limited_allocator() : handle_(new BuffT) {} |
| 369 | |
| 370 | limited_allocator(limited_allocator const& other) : handle_(other.handle_) {} |
| 371 | |
| 372 | template <class U> |
| 373 | explicit limited_allocator(limited_allocator<U, N> const& other) |
| 374 | : handle_(other.handle_) {} |
| 375 | |
| 376 | private: |
| 377 | limited_allocator& operator=(const limited_allocator&);// = delete; |
| 378 | |
| 379 | public: |
| 380 | pointer allocate(size_type n) { return handle_->template allocate<T>(n); } |
| 381 | void deallocate(pointer p, size_type n) { handle_->deallocate(p, n); } |
| 382 | size_type max_size() const {return N;} |
| 383 | |
| 384 | BuffT* getHandle() const { return handle_.get(); } |
| 385 | }; |
| 386 | |
| 387 | template <class T, class U, std::size_t N> |
| 388 | inline bool operator==(limited_allocator<T, N> const& LHS, |
| 389 | limited_allocator<U, N> const& RHS) { |
| 390 | return LHS.getHandle() == RHS.getHandle(); |
| 391 | } |
| 392 | |
| 393 | template <class T, class U, std::size_t N> |
| 394 | inline bool operator!=(limited_allocator<T, N> const& LHS, |
| 395 | limited_allocator<U, N> const& RHS) { |
| 396 | return !(LHS == RHS); |
| 397 | } |
| 398 | |
Marshall Clow | dc3eb83 | 2016-07-11 21:38:08 +0000 | [diff] [blame] | 399 | |
Howard Hinnant | 8f2f7e7 | 2010-08-22 00:15:28 +0000 | [diff] [blame] | 400 | #endif // TEST_ALLOCATOR_H |