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:
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;

View File

@ -5,6 +5,7 @@
#include "libimp/log.h"
#include "libimp/aligned.h"
#include "libimp/detect_plat.h"
#include "libpmr/monotonic_buffer_resource.h"
@ -12,9 +13,10 @@ LIBPMR_NAMESPACE_BEG_
namespace {
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_();
auto sz = ::LIBIMP::round_up(sizeof(Node), alignment) + initial_size;
LIBIMP_TRY {
auto *node = static_cast<Node *>(upstream.allocate(sz));
if (node == nullptr) {
log.error("failed: allocate memory for `monotonic_buffer_resource`'s node.",
@ -24,6 +26,11 @@ Node *make_node(allocator const &upstream, std::size_t initial_size, std::size_t
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;
}
}
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,12 +77,17 @@ allocator monotonic_buffer_resource::upstream_resource() const noexcept {
return upstream_;
}
void monotonic_buffer_resource::release() {
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) {
tail_ = head_ + initial_size_;