5
5
6
6
#include " libimp/log.h"
7
7
#include " libimp/aligned.h"
8
+ #include " libimp/detect_plat.h"
8
9
9
10
#include " libpmr/monotonic_buffer_resource.h"
10
11
11
12
LIBPMR_NAMESPACE_BEG_
12
13
namespace {
13
14
14
15
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 {
16
17
LIBIMP_LOG_ ();
17
18
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 (...) {
20
30
log.error (" failed: allocate memory for `monotonic_buffer_resource`'s node." ,
21
31
" bytes = " , initial_size, " , alignment = " , alignment);
22
32
return nullptr ;
23
33
}
24
- node->next = nullptr ;
25
- node->size = sz;
26
- return node;
27
34
}
28
35
29
36
std::size_t next_buffer_size (std::size_t size) noexcept {
@@ -38,10 +45,10 @@ monotonic_buffer_resource::monotonic_buffer_resource() noexcept
38
45
monotonic_buffer_resource::monotonic_buffer_resource (allocator upstream) noexcept
39
46
: monotonic_buffer_resource(0 , std::move(upstream)) {}
40
47
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
42
49
: monotonic_buffer_resource(initial_size, allocator{}) {}
43
50
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
45
52
: upstream_ (std::move(upstream))
46
53
, free_list_ (nullptr )
47
54
, head_ (nullptr )
@@ -62,19 +69,24 @@ monotonic_buffer_resource::monotonic_buffer_resource(::LIBIMP::span<::LIBIMP::by
62
69
, initial_buffer_(buffer.begin())
63
70
, initial_size_ (buffer.size()) {}
64
71
65
- monotonic_buffer_resource::~monotonic_buffer_resource () {
72
+ monotonic_buffer_resource::~monotonic_buffer_resource () noexcept {
66
73
release ();
67
74
}
68
75
69
76
allocator monotonic_buffer_resource::upstream_resource () const noexcept {
70
77
return upstream_;
71
78
}
72
79
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`." );
78
90
}
79
91
// reset to initial state at contruction
80
92
if ((head_ = initial_buffer_) != nullptr ) {
0 commit comments