Refactor noexcept specifier for monotonic_buffer_resource

This commit is contained in:
mutouyun 2024-03-10 00:26:38 +08:00
parent 9206e674c1
commit d6f40f44d3
2 changed files with 30 additions and 18 deletions

View File

@ -42,18 +42,18 @@ class LIBIMP_EXPORT monotonic_buffer_resource {
public: public:
monotonic_buffer_resource() noexcept; monotonic_buffer_resource() noexcept;
explicit monotonic_buffer_resource(allocator upstream) noexcept; explicit monotonic_buffer_resource(allocator upstream) noexcept;
explicit monotonic_buffer_resource(std::size_t initial_size); explicit monotonic_buffer_resource(std::size_t initial_size) noexcept;
monotonic_buffer_resource(std::size_t initial_size, allocator upstream); monotonic_buffer_resource(std::size_t initial_size, allocator upstream) noexcept;
monotonic_buffer_resource(::LIBIMP::span<::LIBIMP::byte> buffer) noexcept; monotonic_buffer_resource(::LIBIMP::span<::LIBIMP::byte> buffer) noexcept;
monotonic_buffer_resource(::LIBIMP::span<::LIBIMP::byte> buffer, allocator upstream) noexcept; monotonic_buffer_resource(::LIBIMP::span<::LIBIMP::byte> buffer, allocator upstream) noexcept;
~monotonic_buffer_resource(); ~monotonic_buffer_resource() noexcept;
monotonic_buffer_resource(monotonic_buffer_resource const &) = delete; monotonic_buffer_resource(monotonic_buffer_resource const &) = delete;
monotonic_buffer_resource &operator=(monotonic_buffer_resource const &) = delete; monotonic_buffer_resource &operator=(monotonic_buffer_resource const &) = delete;
allocator upstream_resource() const noexcept; allocator upstream_resource() const noexcept;
void release(); void release() noexcept;
void *allocate(std::size_t bytes, std::size_t alignment = alignof(std::max_align_t)) noexcept; 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; void deallocate(void *p, std::size_t bytes, std::size_t alignment = alignof(std::max_align_t)) noexcept;

View File

@ -5,6 +5,7 @@
#include "libimp/log.h" #include "libimp/log.h"
#include "libimp/aligned.h" #include "libimp/aligned.h"
#include "libimp/detect_plat.h"
#include "libpmr/monotonic_buffer_resource.h" #include "libpmr/monotonic_buffer_resource.h"
@ -12,18 +13,24 @@ LIBPMR_NAMESPACE_BEG_
namespace { namespace {
template <typename Node> template <typename Node>
Node *make_node(allocator const &upstream, std::size_t initial_size, std::size_t alignment) { Node *make_node(allocator const &upstream, std::size_t initial_size, std::size_t alignment) noexcept {
LIBIMP_LOG_(); LIBIMP_LOG_();
auto sz = ::LIBIMP::round_up(sizeof(Node), alignment) + initial_size; auto sz = ::LIBIMP::round_up(sizeof(Node), alignment) + initial_size;
auto *node = static_cast<Node *>(upstream.allocate(sz)); LIBIMP_TRY {
if (node == nullptr) { auto *node = static_cast<Node *>(upstream.allocate(sz));
if (node == nullptr) {
log.error("failed: allocate memory for `monotonic_buffer_resource`'s node.",
" bytes = ", initial_size, ", alignment = ", alignment);
return nullptr;
}
node->next = nullptr;
node->size = sz;
return node;
} LIBIMP_CATCH(...) {
log.error("failed: allocate memory for `monotonic_buffer_resource`'s node.", log.error("failed: allocate memory for `monotonic_buffer_resource`'s node.",
" bytes = ", initial_size, ", alignment = ", alignment); " bytes = ", initial_size, ", alignment = ", alignment);
return nullptr; return nullptr;
} }
node->next = nullptr;
node->size = sz;
return node;
} }
std::size_t next_buffer_size(std::size_t size) noexcept { std::size_t next_buffer_size(std::size_t size) noexcept {
@ -38,10 +45,10 @@ monotonic_buffer_resource::monotonic_buffer_resource() noexcept
monotonic_buffer_resource::monotonic_buffer_resource(allocator upstream) noexcept monotonic_buffer_resource::monotonic_buffer_resource(allocator upstream) noexcept
: monotonic_buffer_resource(0, std::move(upstream)) {} : monotonic_buffer_resource(0, std::move(upstream)) {}
monotonic_buffer_resource::monotonic_buffer_resource(std::size_t initial_size) monotonic_buffer_resource::monotonic_buffer_resource(std::size_t initial_size) noexcept
: monotonic_buffer_resource(initial_size, allocator{}) {} : monotonic_buffer_resource(initial_size, allocator{}) {}
monotonic_buffer_resource::monotonic_buffer_resource(std::size_t initial_size, allocator upstream) monotonic_buffer_resource::monotonic_buffer_resource(std::size_t initial_size, allocator upstream) noexcept
: upstream_ (std::move(upstream)) : upstream_ (std::move(upstream))
, free_list_ (nullptr) , free_list_ (nullptr)
, head_ (nullptr) , head_ (nullptr)
@ -62,7 +69,7 @@ monotonic_buffer_resource::monotonic_buffer_resource(::LIBIMP::span<::LIBIMP::by
, initial_buffer_(buffer.begin()) , initial_buffer_(buffer.begin())
, initial_size_ (buffer.size()) {} , initial_size_ (buffer.size()) {}
monotonic_buffer_resource::~monotonic_buffer_resource() { monotonic_buffer_resource::~monotonic_buffer_resource() noexcept {
release(); release();
} }
@ -70,11 +77,16 @@ allocator monotonic_buffer_resource::upstream_resource() const noexcept {
return upstream_; return upstream_;
} }
void monotonic_buffer_resource::release() { void monotonic_buffer_resource::release() noexcept {
while (free_list_ != nullptr) { LIBIMP_LOG_();
auto *next = free_list_->next; LIBIMP_TRY {
upstream_.deallocate(free_list_, free_list_->size); while (free_list_ != nullptr) {
free_list_ = next; auto *next = free_list_->next;
upstream_.deallocate(free_list_, free_list_->size);
free_list_ = next;
}
} LIBIMP_CATCH(...) {
log.error("failed: deallocate memory for `monotonic_buffer_resource`.");
} }
// reset to initial state at contruction // reset to initial state at contruction
if ((head_ = initial_buffer_) != nullptr) { if ((head_ = initial_buffer_) != nullptr) {