Prepare for monotonic_buffer_resource implementation.

This commit is contained in:
mutouyun 2023-09-16 20:35:54 +08:00
parent 1c76f92687
commit 891e161e82
5 changed files with 112 additions and 10 deletions

View File

@ -56,12 +56,12 @@ public:
#endif // LIBIMP_CPP_LIB_BYTE_
template <typename T, typename = detail::is_integral<T>>
explicit constexpr operator T() const noexcept {
constexpr operator T() const noexcept {
return static_cast<T>(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_};

View File

@ -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 <typename T, verify_memory_resource<T> = true>
allocator(T *p_mr) noexcept {
if (p_mr == nullptr) {

View File

@ -40,9 +40,9 @@ using verify_memory_resource =
has_deallocate<T>::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 {

View File

@ -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 <cstddef> // 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_

View File

@ -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_