/** * \file libipc/container_allocator.h * \author mutouyun (orz@orzz.org) * \brief An allocator that can be used by all standard library containers. */ #pragma once #include #include #include "libipc/imp/uninitialized.h" #include "libipc/mem/new.h" namespace ipc { namespace mem { /** * \brief An allocator that can be used by all standard library containers. * * \see https://en.cppreference.com/w/cpp/memory/allocator * https://en.cppreference.com/w/cpp/memory/polymorphic_allocator */ template class container_allocator { template friend class container_allocator; 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; // the other type of std_allocator template struct rebind { using other = container_allocator; }; container_allocator() noexcept {} // construct by copying (do nothing) container_allocator (container_allocator const &) noexcept {} container_allocator& operator=(container_allocator const &) noexcept { return *this; } // construct from a related allocator (do nothing) template container_allocator (container_allocator const &) noexcept {} template container_allocator &operator=(container_allocator const &) noexcept { return *this; } container_allocator (container_allocator &&) noexcept = default; container_allocator& operator=(container_allocator &&) noexcept = default; constexpr size_type max_size() const noexcept { return 1; } pointer allocate(size_type count) noexcept { if (count == 0) return nullptr; if (count > this->max_size()) return nullptr; return mem::$new(); } void deallocate(pointer p, size_type count) noexcept { if (count == 0) return; if (count > this->max_size()) return; mem::$delete(p); } template static void construct(pointer p, P && ... params) { std::ignore = ipc::construct(p, std::forward

(params)...); } static void destroy(pointer p) { std::ignore = ipc::destroy(p); } }; template constexpr bool operator==(container_allocator const &, container_allocator const &) noexcept { return true; } template constexpr bool operator!=(container_allocator const &, container_allocator const &) noexcept { return false; } } // namespace mem } // namespace ipc