Skip to content

Commit d6f40f4

Browse files
committed
Refactor noexcept specifier for monotonic_buffer_resource
1 parent 9206e67 commit d6f40f4

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

include/libpmr/monotonic_buffer_resource.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,18 @@ class LIBIMP_EXPORT monotonic_buffer_resource {
4242
public:
4343
monotonic_buffer_resource() noexcept;
4444
explicit monotonic_buffer_resource(allocator upstream) noexcept;
45-
explicit monotonic_buffer_resource(std::size_t initial_size);
46-
monotonic_buffer_resource(std::size_t initial_size, allocator upstream);
45+
explicit monotonic_buffer_resource(std::size_t initial_size) noexcept;
46+
monotonic_buffer_resource(std::size_t initial_size, allocator upstream) noexcept;
4747
monotonic_buffer_resource(::LIBIMP::span<::LIBIMP::byte> buffer) noexcept;
4848
monotonic_buffer_resource(::LIBIMP::span<::LIBIMP::byte> buffer, allocator upstream) noexcept;
4949

50-
~monotonic_buffer_resource();
50+
~monotonic_buffer_resource() noexcept;
5151

5252
monotonic_buffer_resource(monotonic_buffer_resource const &) = delete;
5353
monotonic_buffer_resource &operator=(monotonic_buffer_resource const &) = delete;
5454

5555
allocator upstream_resource() const noexcept;
56-
void release();
56+
void release() noexcept;
5757

5858
void *allocate(std::size_t bytes, std::size_t alignment = alignof(std::max_align_t)) noexcept;
5959
void deallocate(void *p, std::size_t bytes, std::size_t alignment = alignof(std::max_align_t)) noexcept;

src/libpmr/monotonic_buffer_resource.cpp

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,32 @@
55

66
#include "libimp/log.h"
77
#include "libimp/aligned.h"
8+
#include "libimp/detect_plat.h"
89

910
#include "libpmr/monotonic_buffer_resource.h"
1011

1112
LIBPMR_NAMESPACE_BEG_
1213
namespace {
1314

1415
template <typename Node>
15-
Node *make_node(allocator const &upstream, std::size_t initial_size, std::size_t alignment) {
16+
Node *make_node(allocator const &upstream, std::size_t initial_size, std::size_t alignment) noexcept {
1617
LIBIMP_LOG_();
1718
auto sz = ::LIBIMP::round_up(sizeof(Node), alignment) + initial_size;
18-
auto *node = static_cast<Node *>(upstream.allocate(sz));
19-
if (node == nullptr) {
19+
LIBIMP_TRY {
20+
auto *node = static_cast<Node *>(upstream.allocate(sz));
21+
if (node == nullptr) {
22+
log.error("failed: allocate memory for `monotonic_buffer_resource`'s node.",
23+
" bytes = ", initial_size, ", alignment = ", alignment);
24+
return nullptr;
25+
}
26+
node->next = nullptr;
27+
node->size = sz;
28+
return node;
29+
} LIBIMP_CATCH(...) {
2030
log.error("failed: allocate memory for `monotonic_buffer_resource`'s node.",
2131
" bytes = ", initial_size, ", alignment = ", alignment);
2232
return nullptr;
2333
}
24-
node->next = nullptr;
25-
node->size = sz;
26-
return node;
2734
}
2835

2936
std::size_t next_buffer_size(std::size_t size) noexcept {
@@ -38,10 +45,10 @@ monotonic_buffer_resource::monotonic_buffer_resource() noexcept
3845
monotonic_buffer_resource::monotonic_buffer_resource(allocator upstream) noexcept
3946
: monotonic_buffer_resource(0, std::move(upstream)) {}
4047

41-
monotonic_buffer_resource::monotonic_buffer_resource(std::size_t initial_size)
48+
monotonic_buffer_resource::monotonic_buffer_resource(std::size_t initial_size) noexcept
4249
: monotonic_buffer_resource(initial_size, allocator{}) {}
4350

44-
monotonic_buffer_resource::monotonic_buffer_resource(std::size_t initial_size, allocator upstream)
51+
monotonic_buffer_resource::monotonic_buffer_resource(std::size_t initial_size, allocator upstream) noexcept
4552
: upstream_ (std::move(upstream))
4653
, free_list_ (nullptr)
4754
, head_ (nullptr)
@@ -62,19 +69,24 @@ monotonic_buffer_resource::monotonic_buffer_resource(::LIBIMP::span<::LIBIMP::by
6269
, initial_buffer_(buffer.begin())
6370
, initial_size_ (buffer.size()) {}
6471

65-
monotonic_buffer_resource::~monotonic_buffer_resource() {
72+
monotonic_buffer_resource::~monotonic_buffer_resource() noexcept {
6673
release();
6774
}
6875

6976
allocator monotonic_buffer_resource::upstream_resource() const noexcept {
7077
return upstream_;
7178
}
7279

73-
void monotonic_buffer_resource::release() {
74-
while (free_list_ != nullptr) {
75-
auto *next = free_list_->next;
76-
upstream_.deallocate(free_list_, free_list_->size);
77-
free_list_ = next;
80+
void monotonic_buffer_resource::release() noexcept {
81+
LIBIMP_LOG_();
82+
LIBIMP_TRY {
83+
while (free_list_ != nullptr) {
84+
auto *next = free_list_->next;
85+
upstream_.deallocate(free_list_, free_list_->size);
86+
free_list_ = next;
87+
}
88+
} LIBIMP_CATCH(...) {
89+
log.error("failed: deallocate memory for `monotonic_buffer_resource`.");
7890
}
7991
// reset to initial state at contruction
8092
if ((head_ = initial_buffer_) != nullptr) {

0 commit comments

Comments
 (0)