Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [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 | |
| 10 | #ifndef TEST_ALLOCATOR_H |
| 11 | #define TEST_ALLOCATOR_H |
| 12 | |
| 13 | #include <cstddef> |
| 14 | #include <type_traits> |
| 15 | #include <utility> |
| 16 | #include <cstdlib> |
| 17 | #include <new> |
| 18 | #include <climits> |
| 19 | |
| 20 | class test_alloc_base |
| 21 | { |
| 22 | public: |
| 23 | static int count; |
| 24 | public: |
| 25 | static int throw_after; |
| 26 | }; |
| 27 | |
| 28 | int test_alloc_base::count = 0; |
| 29 | int test_alloc_base::throw_after = INT_MAX; |
| 30 | |
| 31 | template <class T> |
| 32 | class test_allocator |
| 33 | : public test_alloc_base |
| 34 | { |
| 35 | int data_; |
| 36 | |
| 37 | template <class U> friend class test_allocator; |
| 38 | public: |
| 39 | |
| 40 | typedef unsigned size_type; |
| 41 | typedef int difference_type; |
| 42 | typedef T value_type; |
| 43 | typedef value_type* pointer; |
| 44 | typedef const value_type* const_pointer; |
| 45 | typedef typename std::add_lvalue_reference<value_type>::type reference; |
| 46 | typedef typename std::add_lvalue_reference<const value_type>::type const_reference; |
| 47 | |
| 48 | template <class U> struct rebind {typedef test_allocator<U> other;}; |
| 49 | |
| 50 | test_allocator() throw() : data_(-1) {} |
| 51 | explicit test_allocator(int i) throw() : data_(i) {} |
| 52 | test_allocator(const test_allocator& a) throw() |
| 53 | : data_(a.data_) {} |
| 54 | template <class U> test_allocator(const test_allocator<U>& a) throw() |
| 55 | : data_(a.data_) {} |
| 56 | ~test_allocator() throw() {data_ = 0;} |
| 57 | pointer address(reference x) const {return &x;} |
| 58 | const_pointer address(const_reference x) const {return &x;} |
| 59 | pointer allocate(size_type n, const void* = 0) |
| 60 | { |
| 61 | if (count >= throw_after) { |
| 62 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 63 | throw std::bad_alloc(); |
| 64 | #else |
| 65 | std::terminate(); |
| 66 | #endif |
| 67 | } |
| 68 | ++count; |
| 69 | return (pointer)std::malloc(n * sizeof(T)); |
| 70 | } |
| 71 | void deallocate(pointer p, size_type n) |
| 72 | {--count; std::free(p);} |
| 73 | size_type max_size() const throw() |
| 74 | {return UINT_MAX / sizeof(T);} |
| 75 | void construct(pointer p, const T& val) |
| 76 | {::new(p) T(val);} |
| 77 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
| 78 | void construct(pointer p, T&& val) |
| 79 | {::new(p) T(std::move(val));} |
| 80 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
| 81 | void destroy(pointer p) {p->~T();} |
| 82 | |
| 83 | friend bool operator==(const test_allocator& x, const test_allocator& y) |
| 84 | {return x.data_ == y.data_;} |
| 85 | friend bool operator!=(const test_allocator& x, const test_allocator& y) |
| 86 | {return !(x == y);} |
| 87 | }; |
| 88 | |
| 89 | template <> |
| 90 | class test_allocator<void> |
| 91 | : public test_alloc_base |
| 92 | { |
| 93 | int data_; |
| 94 | |
| 95 | template <class U> friend class test_allocator; |
| 96 | public: |
| 97 | |
| 98 | typedef unsigned size_type; |
| 99 | typedef int difference_type; |
| 100 | typedef void value_type; |
| 101 | typedef value_type* pointer; |
| 102 | typedef const value_type* const_pointer; |
| 103 | |
| 104 | template <class U> struct rebind {typedef test_allocator<U> other;}; |
| 105 | |
| 106 | test_allocator() throw() : data_(-1) {} |
| 107 | explicit test_allocator(int i) throw() : data_(i) {} |
| 108 | test_allocator(const test_allocator& a) throw() |
| 109 | : data_(a.data_) {} |
| 110 | template <class U> test_allocator(const test_allocator<U>& a) throw() |
| 111 | : data_(a.data_) {} |
| 112 | ~test_allocator() throw() {data_ = 0;} |
| 113 | |
| 114 | friend bool operator==(const test_allocator& x, const test_allocator& y) |
| 115 | {return x.data_ == y.data_;} |
| 116 | friend bool operator!=(const test_allocator& x, const test_allocator& y) |
| 117 | {return !(x == y);} |
| 118 | }; |
| 119 | |
| 120 | template <class T> |
| 121 | class other_allocator |
| 122 | { |
| 123 | int data_; |
| 124 | |
| 125 | template <class U> friend class other_allocator; |
| 126 | |
| 127 | public: |
| 128 | typedef T value_type; |
| 129 | |
| 130 | other_allocator() : data_(-1) {} |
| 131 | explicit other_allocator(int i) : data_(i) {} |
| 132 | template <class U> other_allocator(const other_allocator<U>& a) |
| 133 | : data_(a.data_) {} |
| 134 | T* allocate(std::size_t n) |
| 135 | {return (T*)std::malloc(n * sizeof(T));} |
| 136 | void deallocate(T* p, std::size_t n) |
| 137 | {std::free(p);} |
| 138 | |
| 139 | other_allocator select_on_container_copy_construction() const |
| 140 | {return other_allocator(-2);} |
| 141 | |
| 142 | friend bool operator==(const other_allocator& x, const other_allocator& y) |
| 143 | {return x.data_ == y.data_;} |
| 144 | friend bool operator!=(const other_allocator& x, const other_allocator& y) |
| 145 | {return !(x == y);} |
| 146 | |
| 147 | typedef std::true_type propagate_on_container_copy_assignment; |
| 148 | typedef std::true_type propagate_on_container_move_assignment; |
| 149 | typedef std::true_type propagate_on_container_swap; |
| 150 | |
| 151 | #ifdef _LIBCPP_HAS_NO_ADVANCED_SFINAE |
| 152 | std::size_t max_size() const |
| 153 | {return UINT_MAX / sizeof(T);} |
| 154 | #endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE |
| 155 | |
| 156 | }; |
| 157 | |
| 158 | #endif // TEST_ALLOCATOR_H |