From d6f40f44d36c9b279a664cc583be8dd24009387e Mon Sep 17 00:00:00 2001 From: mutouyun Date: Sun, 10 Mar 2024 00:26:38 +0800 Subject: [PATCH] Refactor noexcept specifier for `monotonic_buffer_resource` --- include/libpmr/monotonic_buffer_resource.h | 8 ++--- src/libpmr/monotonic_buffer_resource.cpp | 40 ++++++++++++++-------- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/include/libpmr/monotonic_buffer_resource.h b/include/libpmr/monotonic_buffer_resource.h index 6b0d4df..d2ffb5b 100644 --- a/include/libpmr/monotonic_buffer_resource.h +++ b/include/libpmr/monotonic_buffer_resource.h @@ -42,18 +42,18 @@ class LIBIMP_EXPORT monotonic_buffer_resource { public: monotonic_buffer_resource() noexcept; explicit monotonic_buffer_resource(allocator upstream) noexcept; - explicit monotonic_buffer_resource(std::size_t initial_size); - monotonic_buffer_resource(std::size_t initial_size, allocator upstream); + explicit monotonic_buffer_resource(std::size_t initial_size) noexcept; + 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, allocator upstream) noexcept; - ~monotonic_buffer_resource(); + ~monotonic_buffer_resource() noexcept; 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 release() 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; diff --git a/src/libpmr/monotonic_buffer_resource.cpp b/src/libpmr/monotonic_buffer_resource.cpp index f43d723..23810b6 100644 --- a/src/libpmr/monotonic_buffer_resource.cpp +++ b/src/libpmr/monotonic_buffer_resource.cpp @@ -5,6 +5,7 @@ #include "libimp/log.h" #include "libimp/aligned.h" +#include "libimp/detect_plat.h" #include "libpmr/monotonic_buffer_resource.h" @@ -12,18 +13,24 @@ LIBPMR_NAMESPACE_BEG_ namespace { template -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_(); auto sz = ::LIBIMP::round_up(sizeof(Node), alignment) + initial_size; - auto *node = static_cast(upstream.allocate(sz)); - if (node == nullptr) { + LIBIMP_TRY { + auto *node = static_cast(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.", " bytes = ", initial_size, ", alignment = ", alignment); return nullptr; } - node->next = nullptr; - node->size = sz; - return node; } 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(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::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)) , free_list_ (nullptr) , head_ (nullptr) @@ -62,7 +69,7 @@ monotonic_buffer_resource::monotonic_buffer_resource(::LIBIMP::span<::LIBIMP::by , initial_buffer_(buffer.begin()) , initial_size_ (buffer.size()) {} -monotonic_buffer_resource::~monotonic_buffer_resource() { +monotonic_buffer_resource::~monotonic_buffer_resource() noexcept { release(); } @@ -70,11 +77,16 @@ allocator monotonic_buffer_resource::upstream_resource() const noexcept { return upstream_; } -void monotonic_buffer_resource::release() { - while (free_list_ != nullptr) { - auto *next = free_list_->next; - upstream_.deallocate(free_list_, free_list_->size); - free_list_ = next; +void monotonic_buffer_resource::release() noexcept { + LIBIMP_LOG_(); + LIBIMP_TRY { + while (free_list_ != nullptr) { + 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 if ((head_ = initial_buffer_) != nullptr) {