|
1 | 1 | /** |
2 | | - * \file libipc/polymorphic_allocator |
| 2 | + * \file libipc/bytes_allocator.h |
3 | 3 | * \author mutouyun ([email protected]) |
4 | 4 | * \brief A generic polymorphic memory allocator. |
5 | 5 | */ |
@@ -50,13 +50,13 @@ using is_memory_resource = |
50 | 50 | * \brief An allocator which exhibits different allocation behavior |
51 | 51 | * depending upon the memory resource from which it is constructed. |
52 | 52 | * |
53 | | - * \note Unlike `std::pmr::polymorphic_allocator`, it does not |
| 53 | + * \note Unlike `std::pmr::container_allocator`, it does not |
54 | 54 | * rely on a specific inheritance relationship and only restricts |
55 | 55 | * the interface behavior of the incoming memory resource object to |
56 | 56 | * conform to `std::pmr::memory_resource`. |
57 | 57 | * |
58 | 58 | * \see https://en.cppreference.com/w/cpp/memory/memory_resource |
59 | | - * https://en.cppreference.com/w/cpp/memory/polymorphic_allocator |
| 59 | + * https://en.cppreference.com/w/cpp/memory/container_allocator |
60 | 60 | */ |
61 | 61 | class LIBIPC_EXPORT bytes_allocator { |
62 | 62 |
|
@@ -159,88 +159,5 @@ class LIBIPC_EXPORT bytes_allocator { |
159 | 159 | } |
160 | 160 | }; |
161 | 161 |
|
162 | | -/** |
163 | | - * \brief An allocator that can be used by all standard library containers, |
164 | | - * based on ipc::bytes_allocator. |
165 | | - * |
166 | | - * \see https://en.cppreference.com/w/cpp/memory/allocator |
167 | | - * https://en.cppreference.com/w/cpp/memory/polymorphic_allocator |
168 | | - */ |
169 | | -template <typename T> |
170 | | -class polymorphic_allocator { |
171 | | - |
172 | | - template <typename U> |
173 | | - friend class polymorphic_allocator; |
174 | | - |
175 | | -public: |
176 | | - // type definitions |
177 | | - typedef T value_type; |
178 | | - typedef value_type * pointer; |
179 | | - typedef const value_type *const_pointer; |
180 | | - typedef value_type & reference; |
181 | | - typedef const value_type &const_reference; |
182 | | - typedef std::size_t size_type; |
183 | | - typedef std::ptrdiff_t difference_type; |
184 | | - |
185 | | -private: |
186 | | - bytes_allocator alloc_; |
187 | | - |
188 | | -public: |
189 | | - // the other type of std_allocator |
190 | | - template <typename U> |
191 | | - struct rebind { |
192 | | - using other = polymorphic_allocator<U>; |
193 | | - }; |
194 | | - |
195 | | - polymorphic_allocator() noexcept {} |
196 | | - |
197 | | - template <typename P, is_memory_resource<P> = true> |
198 | | - polymorphic_allocator(P *p_mr) noexcept : alloc_(p_mr) {} |
199 | | - |
200 | | - // construct by copying (do nothing) |
201 | | - polymorphic_allocator (polymorphic_allocator<T> const &) noexcept {} |
202 | | - polymorphic_allocator& operator=(polymorphic_allocator<T> const &) noexcept { return *this; } |
203 | | - |
204 | | - // construct from a related allocator (do nothing) |
205 | | - template <typename U> polymorphic_allocator (polymorphic_allocator<U> const &) noexcept {} |
206 | | - template <typename U> polymorphic_allocator &operator=(polymorphic_allocator<U> const &) noexcept { return *this; } |
207 | | - |
208 | | - polymorphic_allocator (polymorphic_allocator &&) noexcept = default; |
209 | | - polymorphic_allocator& operator=(polymorphic_allocator &&) noexcept = default; |
210 | | - |
211 | | - constexpr size_type max_size(void) const noexcept { |
212 | | - return (std::numeric_limits<size_type>::max)() / sizeof(value_type); |
213 | | - } |
214 | | - |
215 | | - pointer allocate(size_type count) noexcept { |
216 | | - if (count == 0) return nullptr; |
217 | | - if (count > this->max_size()) return nullptr; |
218 | | - return static_cast<pointer>(alloc_.allocate(count * sizeof(value_type), alignof(T))); |
219 | | - } |
220 | | - |
221 | | - void deallocate(pointer p, size_type count) noexcept { |
222 | | - alloc_.deallocate(p, count * sizeof(value_type), alignof(T)); |
223 | | - } |
224 | | - |
225 | | - template <typename... P> |
226 | | - static void construct(pointer p, P && ... params) { |
227 | | - std::ignore = ipc::construct<T>(p, std::forward<P>(params)...); |
228 | | - } |
229 | | - |
230 | | - static void destroy(pointer p) { |
231 | | - std::ignore = ipc::destroy(p); |
232 | | - } |
233 | | -}; |
234 | | - |
235 | | -template <typename T, typename U> |
236 | | -constexpr bool operator==(polymorphic_allocator<T> const &, polymorphic_allocator<U> const &) noexcept { |
237 | | - return true; |
238 | | -} |
239 | | - |
240 | | -template <typename T, typename U> |
241 | | -constexpr bool operator!=(polymorphic_allocator<T> const &, polymorphic_allocator<U> const &) noexcept { |
242 | | - return false; |
243 | | -} |
244 | | - |
245 | 162 | } // namespace mem |
246 | 163 | } // namespace ipc |
0 commit comments