| 1 | // Licensed to the .NET Foundation under one or more agreements. |
|---|---|
| 2 | // The .NET Foundation licenses this file to you under the MIT license. |
| 3 | // See the LICENSE file in the project root for more information. |
| 4 | |
| 5 | #pragma once |
| 6 | |
| 7 | #include "new.h" |
| 8 | |
| 9 | namespace jitstd |
| 10 | { |
| 11 | |
| 12 | template <typename T> |
| 13 | class allocator; |
| 14 | |
| 15 | template <> |
| 16 | class allocator<void> |
| 17 | { |
| 18 | public: |
| 19 | typedef size_t size_type; |
| 20 | typedef ptrdiff_t difference_type; |
| 21 | typedef void* pointer; |
| 22 | typedef const void* const_pointer; |
| 23 | typedef void value_type; |
| 24 | |
| 25 | template <typename U> |
| 26 | struct rebind |
| 27 | { |
| 28 | typedef allocator<U> allocator; |
| 29 | }; |
| 30 | |
| 31 | private: |
| 32 | allocator(); |
| 33 | |
| 34 | public: |
| 35 | inline allocator(CompAllocator alloc); |
| 36 | |
| 37 | template <typename U> |
| 38 | inline allocator(const allocator<U>& alloc); |
| 39 | |
| 40 | inline allocator(const allocator& alloc); |
| 41 | |
| 42 | template <typename U> |
| 43 | inline allocator& operator=(const allocator<U>& alloc); |
| 44 | |
| 45 | private: |
| 46 | CompAllocator m_alloc; |
| 47 | template <typename U> |
| 48 | friend class allocator; |
| 49 | }; |
| 50 | |
| 51 | allocator<void>::allocator(CompAllocator alloc) |
| 52 | : m_alloc(alloc) |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | allocator<void>::allocator(const allocator& alloc) |
| 57 | : m_alloc(alloc.m_alloc) |
| 58 | { |
| 59 | } |
| 60 | |
| 61 | template <typename U> |
| 62 | allocator<void>::allocator(const allocator<U>& alloc) |
| 63 | : m_alloc(alloc.m_alloc) |
| 64 | { |
| 65 | } |
| 66 | |
| 67 | template <typename U> |
| 68 | allocator<void>& allocator<void>::operator=(const allocator<U>& alloc) |
| 69 | { |
| 70 | m_alloc = alloc.m_alloc; |
| 71 | return *this; |
| 72 | } |
| 73 | |
| 74 | template <typename T> |
| 75 | class allocator |
| 76 | { |
| 77 | public: |
| 78 | typedef size_t size_type; |
| 79 | typedef ptrdiff_t difference_type; |
| 80 | typedef T* pointer; |
| 81 | typedef T& reference; |
| 82 | typedef const T* const_pointer; |
| 83 | typedef const T& const_reference; |
| 84 | typedef T value_type; |
| 85 | |
| 86 | private: |
| 87 | allocator(); |
| 88 | public: |
| 89 | allocator(CompAllocator alloc); |
| 90 | |
| 91 | template <typename U> |
| 92 | allocator(const allocator<U>& alloc); |
| 93 | |
| 94 | allocator(const allocator& alloc); |
| 95 | |
| 96 | template <typename U> |
| 97 | allocator& operator=(const allocator<U>& alloc); |
| 98 | |
| 99 | pointer address(reference val); |
| 100 | const_pointer address(const_reference val) const; |
| 101 | pointer allocate(size_type count, allocator<void>::const_pointer hint = nullptr); |
| 102 | void construct(pointer ptr, const_reference val); |
| 103 | void deallocate(pointer ptr, size_type size); |
| 104 | void destroy(pointer ptr); |
| 105 | size_type max_size() const; |
| 106 | template <typename U> |
| 107 | struct rebind |
| 108 | { |
| 109 | typedef allocator<U> allocator; |
| 110 | }; |
| 111 | |
| 112 | private: |
| 113 | CompAllocator m_alloc; |
| 114 | template <typename U> |
| 115 | friend class allocator; |
| 116 | }; |
| 117 | |
| 118 | } // end of namespace jitstd |
| 119 | |
| 120 | |
| 121 | namespace jitstd |
| 122 | { |
| 123 | |
| 124 | template <typename T> |
| 125 | allocator<T>::allocator(CompAllocator alloc) |
| 126 | : m_alloc(alloc) |
| 127 | { |
| 128 | } |
| 129 | |
| 130 | template <typename T> |
| 131 | template <typename U> |
| 132 | allocator<T>::allocator(const allocator<U>& alloc) |
| 133 | : m_alloc(alloc.m_alloc) |
| 134 | { |
| 135 | } |
| 136 | |
| 137 | template <typename T> |
| 138 | allocator<T>::allocator(const allocator<T>& alloc) |
| 139 | : m_alloc(alloc.m_alloc) |
| 140 | { |
| 141 | } |
| 142 | |
| 143 | template <typename T> |
| 144 | template <typename U> |
| 145 | allocator<T>& allocator<T>::operator=(const allocator<U>& alloc) |
| 146 | { |
| 147 | m_alloc = alloc.m_alloc; |
| 148 | return *this; |
| 149 | } |
| 150 | |
| 151 | template <typename T> |
| 152 | typename allocator<T>::pointer allocator<T>::address(reference val) |
| 153 | { |
| 154 | return &val; |
| 155 | } |
| 156 | |
| 157 | template <typename T> |
| 158 | typename allocator<T>::const_pointer allocator<T>::address(const_reference val) const |
| 159 | { |
| 160 | return &val; |
| 161 | } |
| 162 | |
| 163 | template <typename T> |
| 164 | T* allocator<T>::allocate(size_type count, allocator<void>::const_pointer hint) |
| 165 | { |
| 166 | return m_alloc.allocate<value_type>(count); |
| 167 | } |
| 168 | |
| 169 | template <typename T> |
| 170 | void allocator<T>::construct(pointer ptr, const_reference val) |
| 171 | { |
| 172 | new (ptr, placement_t()) value_type(val); |
| 173 | } |
| 174 | |
| 175 | template <typename T> |
| 176 | void allocator<T>::deallocate(pointer ptr, size_type size) |
| 177 | { |
| 178 | m_alloc.deallocate(ptr); |
| 179 | } |
| 180 | |
| 181 | template <typename T> |
| 182 | void allocator<T>::destroy(pointer ptr) |
| 183 | { |
| 184 | ptr->~T(); |
| 185 | } |
| 186 | |
| 187 | template <typename T> |
| 188 | typename allocator<T>::size_type allocator<T>::max_size() const |
| 189 | { |
| 190 | return (size_type) -1; |
| 191 | } |
| 192 | |
| 193 | } // end of namespace jitstd |
| 194 |