/** * \file libpmr/memory_resource.h * \author mutouyun (orz@orzz.org) * \brief Implement memory allocation strategies that can be used by pmr::allocator. * \date 2022-11-13 */ #pragma once #include #include // std::size_t, std::max_align_t #include "libimp/export.h" #include "libpmr/def.h" LIBPMR_NAMESPACE_BEG_ /// \brief Helper trait for memory resource. template struct has_allocate : std::false_type {}; template struct has_allocate().allocate(std::declval(), std::declval())), void * >::value>::type> : std::true_type {}; template struct has_deallocate : std::false_type {}; template struct has_deallocate().deallocate(std::declval(), std::declval(), std::declval())) > : std::true_type {}; template using verify_memory_resource = std::enable_if_t::value && has_deallocate::value, bool>; /** * \class LIBIMP_EXPORT new_delete_resource * \brief A memory resource that uses the * standard memory allocation and deallocation interface to allocate memory. * \see https://en.cppreference.com/w/cpp/memory/new_delete_resource */ class LIBIMP_EXPORT new_delete_resource { public: /// \brief Returns a pointer to a `new_delete_resource`. static new_delete_resource *get() noexcept; /// \brief Allocates storage with a size of at least bytes bytes, aligned to the specified alignment. /// \remark Returns nullptr if storage of the requested size and alignment cannot be obtained. /// \see https://en.cppreference.com/w/cpp/memory/memory_resource/do_allocate void *allocate(std::size_t bytes, std::size_t alignment = alignof(std::max_align_t)) noexcept; /// \brief Deallocates the storage pointed to by p. /// \see https://en.cppreference.com/w/cpp/memory/memory_resource/deallocate void deallocate(void *p, std::size_t bytes, std::size_t alignment = alignof(std::max_align_t)) noexcept; }; LIBPMR_NAMESPACE_END_