mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-07 01:06:45 +08:00
Refactor noexcept specifier for monotonic_buffer_resource
This commit is contained in:
parent
9206e674c1
commit
d6f40f44d3
@ -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;
|
||||||
|
|||||||
@ -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,9 +13,10 @@ 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;
|
||||||
|
LIBIMP_TRY {
|
||||||
auto *node = static_cast<Node *>(upstream.allocate(sz));
|
auto *node = static_cast<Node *>(upstream.allocate(sz));
|
||||||
if (node == nullptr) {
|
if (node == nullptr) {
|
||||||
log.error("failed: allocate memory for `monotonic_buffer_resource`'s node.",
|
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->next = nullptr;
|
||||||
node->size = sz;
|
node->size = sz;
|
||||||
return node;
|
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 {
|
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,12 +77,17 @@ allocator monotonic_buffer_resource::upstream_resource() const noexcept {
|
|||||||
return upstream_;
|
return upstream_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void monotonic_buffer_resource::release() {
|
void monotonic_buffer_resource::release() noexcept {
|
||||||
|
LIBIMP_LOG_();
|
||||||
|
LIBIMP_TRY {
|
||||||
while (free_list_ != nullptr) {
|
while (free_list_ != nullptr) {
|
||||||
auto *next = free_list_->next;
|
auto *next = free_list_->next;
|
||||||
upstream_.deallocate(free_list_, free_list_->size);
|
upstream_.deallocate(free_list_, free_list_->size);
|
||||||
free_list_ = next;
|
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) {
|
||||||
tail_ = head_ + initial_size_;
|
tail_ = head_ + initial_size_;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user