#pragma once #include // std::numeric_limits #include // std::forward #include #include "libipc/pool_alloc.h" namespace ipc { namespace mem { //////////////////////////////////////////////////////////////// /// The allocator wrapper class for STL //////////////////////////////////////////////////////////////// namespace detail { template struct rebind { template using alloc_t = AllocP; }; template class AllocT> struct rebind> { template using alloc_t = AllocT; }; } // namespace detail template class allocator_wrapper { template friend class allocator_wrapper; public: // type definitions typedef T value_type; typedef value_type* pointer; typedef const value_type* const_pointer; typedef value_type& reference; typedef const value_type& const_reference; typedef std::size_t size_type; typedef std::ptrdiff_t difference_type; typedef AllocP alloc_policy; private: alloc_policy alloc_; public: allocator_wrapper() noexcept {} // construct by copying (do nothing) allocator_wrapper (const allocator_wrapper&) noexcept {} allocator_wrapper& operator=(const allocator_wrapper&) noexcept { return *this; } // construct from a related allocator (do nothing) template allocator_wrapper (const allocator_wrapper&) noexcept {} template allocator_wrapper& operator=(const allocator_wrapper&) noexcept { return *this; } allocator_wrapper (allocator_wrapper && rhs) noexcept : alloc_ ( std::move(rhs.alloc_) ) {} allocator_wrapper& operator=(allocator_wrapper && rhs) noexcept { alloc_ = std::move(rhs.alloc_); return *this; } public: // the other type of std_allocator template struct rebind { using other = allocator_wrapper< U, typename detail::rebind::template alloc_t >; }; constexpr size_type max_size(void) const noexcept { return (std::numeric_limits::max)() / sizeof(value_type); } public: pointer allocate(size_type count) noexcept { if (count == 0) return nullptr; if (count > this->max_size()) return nullptr; return static_cast(alloc_.alloc(count * sizeof(value_type))); } void deallocate(pointer p, size_type count) noexcept { alloc_.free(p, count * sizeof(value_type)); } template static void construct(pointer p, P && ... params) { ipc::mem::construct(p, std::forward

(params)...); } static void destroy(pointer p) { ipc::mem::destruct(p); } }; template class allocator_wrapper { public: // type definitions typedef void value_type; typedef value_type* pointer; typedef const value_type* const_pointer; typedef std::size_t size_type; typedef std::ptrdiff_t difference_type; typedef AllocP alloc_policy; }; template constexpr bool operator==(const allocator_wrapper&, const allocator_wrapper&) noexcept { return true; } template constexpr bool operator!=(const allocator_wrapper&, const allocator_wrapper&) noexcept { return false; } } // namespace mem } // namespace ipc