From 891e161e82b9603bbc71dc37aa8fde60b5cdda9d Mon Sep 17 00:00:00 2001 From: mutouyun Date: Sat, 16 Sep 2023 20:35:54 +0800 Subject: [PATCH] Prepare for `monotonic_buffer_resource` implementation. --- include/libimp/byte.h | 4 +- include/libpmr/allocator.h | 14 +++--- include/libpmr/memory_resource.h | 4 +- include/libpmr/monotonic_buffer_resource.h | 48 ++++++++++++++++++++ src/libpmr/monotonic_buffer_resource.cpp | 52 ++++++++++++++++++++++ 5 files changed, 112 insertions(+), 10 deletions(-) create mode 100644 include/libpmr/monotonic_buffer_resource.h create mode 100644 src/libpmr/monotonic_buffer_resource.cpp diff --git a/include/libimp/byte.h b/include/libimp/byte.h index 982d173..eb4400f 100644 --- a/include/libimp/byte.h +++ b/include/libimp/byte.h @@ -56,12 +56,12 @@ public: #endif // LIBIMP_CPP_LIB_BYTE_ template > - explicit constexpr operator T() const noexcept { + constexpr operator T() const noexcept { return static_cast(bits_); } #ifdef LIBIMP_CPP_LIB_BYTE_ - explicit constexpr operator std::byte() const noexcept { + constexpr operator std::byte() const noexcept { /// \brief C++17 relaxed enum class initialization rules. /// \see https://en.cppreference.com/w/cpp/language/enum#enum_relaxed_init_cpp17 return std::byte{bits_}; diff --git a/include/libpmr/allocator.h b/include/libpmr/allocator.h index ea880a8..c02c8d7 100644 --- a/include/libpmr/allocator.h +++ b/include/libpmr/allocator.h @@ -20,12 +20,12 @@ LIBPMR_NAMESPACE_BEG_ /** * \brief An allocator which exhibits different allocation behavior - * depending upon the memory resource from which it is constructed. + * depending upon the memory resource from which it is constructed. * * \note Unlike `std::pmr::polymorphic_allocator`, it does not - * rely on a specific inheritance relationship and only restricts - * the interface behavior of the incoming memory resource object to - * conform to `std::pmr::memory_resource`. + * rely on a specific inheritance relationship and only restricts + * the interface behavior of the incoming memory resource object to + * conform to `std::pmr::memory_resource`. * * \see https://en.cppreference.com/w/cpp/memory/memory_resource * https://en.cppreference.com/w/cpp/memory/polymorphic_allocator @@ -88,6 +88,8 @@ class LIBIMP_EXPORT allocator { holder_mr_base const &get_holder() const noexcept; public: + /// \brief Constructs an `allocator` using the return value of + /// `new_delete_resource::get()` as the underlying memory resource. allocator() noexcept; ~allocator() noexcept; @@ -97,8 +99,8 @@ public: allocator(allocator &&other) noexcept = default; allocator &operator=(allocator &&other) & noexcept = default; - /// \brief Constructs a allocator from a memory resource pointer - /// The lifetime of the pointer must be longer than that of allocator. + /// \brief Constructs a allocator from a memory resource pointer. + /// \note The lifetime of the pointer must be longer than that of allocator. template = true> allocator(T *p_mr) noexcept { if (p_mr == nullptr) { diff --git a/include/libpmr/memory_resource.h b/include/libpmr/memory_resource.h index 523f6c2..8ec6c9e 100644 --- a/include/libpmr/memory_resource.h +++ b/include/libpmr/memory_resource.h @@ -40,9 +40,9 @@ using verify_memory_resource = 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. - * + * 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 { diff --git a/include/libpmr/monotonic_buffer_resource.h b/include/libpmr/monotonic_buffer_resource.h new file mode 100644 index 0000000..0fa2df9 --- /dev/null +++ b/include/libpmr/monotonic_buffer_resource.h @@ -0,0 +1,48 @@ +/** + * \file libpmr/monotonic_buffer_resource.h + * \author mutouyun (orz@orzz.org) + * \brief A special-purpose the `allocator` that releases the allocated memory only when the resource is destroyed. + * \date 2023-09-16 + */ +#pragma once + +#include // std::size_t, std::max_align_t + +#include "libimp/export.h" +#include "libimp/span.h" +#include "libimp/byte.h" + +#include "libpmr/def.h" +#include "libpmr/allocator.h" + +LIBPMR_NAMESPACE_BEG_ + +/** + * \class LIBIMP_EXPORT monotonic_buffer_resource + * \brief A special-purpose memory resource class + * that releases the allocated memory only when the resource is destroyed. + * \see https://en.cppreference.com/w/cpp/memory/monotonic_buffer_resource + */ +class LIBIMP_EXPORT monotonic_buffer_resource { + +public: + monotonic_buffer_resource() noexcept; + explicit monotonic_buffer_resource(allocator upstream); + explicit monotonic_buffer_resource(std::size_t initial_size); + monotonic_buffer_resource(std::size_t initial_size, allocator upstream); + monotonic_buffer_resource(::LIBIMP::span<::LIBIMP::byte> buffer) noexcept; + monotonic_buffer_resource(::LIBIMP::span<::LIBIMP::byte> buffer, allocator upstream) noexcept; + + ~monotonic_buffer_resource(); + + monotonic_buffer_resource(monotonic_buffer_resource const &) = delete; + monotonic_buffer_resource &operator=(monotonic_buffer_resource const &) = delete; + + allocator upstream_resource() const noexcept; + void release(); + + void *allocate(std::size_t bytes, std::size_t alignment = alignof(std::max_align_t)) noexcept; + void deallocate(void *p, std::size_t bytes, std::size_t alignment = alignof(std::max_align_t)) noexcept; +}; + +LIBPMR_NAMESPACE_END_ diff --git a/src/libpmr/monotonic_buffer_resource.cpp b/src/libpmr/monotonic_buffer_resource.cpp new file mode 100644 index 0000000..16bbeba --- /dev/null +++ b/src/libpmr/monotonic_buffer_resource.cpp @@ -0,0 +1,52 @@ + +#include "libimp/log.h" + +#include "libpmr/monotonic_buffer_resource.h" + +LIBPMR_NAMESPACE_BEG_ + +monotonic_buffer_resource::monotonic_buffer_resource() noexcept { + +} + +monotonic_buffer_resource::monotonic_buffer_resource(allocator upstream) { + +} + +monotonic_buffer_resource::monotonic_buffer_resource(std::size_t initial_size) { + +} + +monotonic_buffer_resource::monotonic_buffer_resource(std::size_t initial_size, allocator upstream) { + +} + +monotonic_buffer_resource::monotonic_buffer_resource(::LIBIMP::span<::LIBIMP::byte> buffer) noexcept { + +} + +monotonic_buffer_resource::monotonic_buffer_resource(::LIBIMP::span<::LIBIMP::byte> buffer, allocator upstream) noexcept { + +} + +monotonic_buffer_resource::~monotonic_buffer_resource() { + +} + +allocator monotonic_buffer_resource::upstream_resource() const noexcept { + +} + +void monotonic_buffer_resource::release() { + +} + +void *monotonic_buffer_resource::allocate(std::size_t bytes, std::size_t alignment) noexcept { + +} + +void monotonic_buffer_resource::deallocate(void *p, std::size_t bytes, std::size_t alignment) noexcept { + +} + +LIBPMR_NAMESPACE_END_