Skip to content

Commit

Permalink
Merge pull request #383
Browse files Browse the repository at this point in the history
  • Loading branch information
Neverlord committed Dec 4, 2023
2 parents 69d9eae + c3f7ab3 commit 68411ce
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
8 changes: 8 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
2.8.0-dev.9 | 2023-12-04 16:02:54 +0100

* Fix MBR allocations for >= 1024 bytes (Dominik Charousset, Corelight)

When trying to allocate a single object with size >= 1024, the current
implementation crashes with a stack overflow. This patch allows blocks
to exceed the hard-coded size limit to accommodate larger objects.

2.8.0-dev.7 | 2023-11-16 16:54:09 +0100

* Fix error::context (Dominik Charousset, Corelight)
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.8.0-dev.7
2.8.0-dev.9
4 changes: 2 additions & 2 deletions include/broker/detail/monotonic_buffer_resource.hh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace broker::detail {
class monotonic_buffer_resource {
public:
monotonic_buffer_resource() {
allocate_block(nullptr);
allocate_block(nullptr, 0);
}

monotonic_buffer_resource(const monotonic_buffer_resource&) = delete;
Expand All @@ -38,7 +38,7 @@ private:
void* bytes;
};

void allocate_block(block* prev_block);
void allocate_block(block* prev_block, size_t min_size);

void destroy() noexcept;

Expand Down
27 changes: 16 additions & 11 deletions src/detail/monotonic_buffer_resource.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "broker/detail/monotonic_buffer_resource.hh"

#include <algorithm>
#include <cstdlib>
#include <memory>

Expand All @@ -15,23 +16,27 @@ constexpr size_t block_size = 1024;
} // namespace

void* monotonic_buffer_resource::allocate(size_t num_bytes, size_t alignment) {
if (auto res = std::align(alignment, num_bytes, current_->bytes,
remaining_)) {
current_->bytes = static_cast<std::byte*>(res) + num_bytes;
remaining_ -= num_bytes;
return res;
} else {
allocate_block(current_);
return allocate(num_bytes, alignment);
auto res = std::align(alignment, num_bytes, current_->bytes, remaining_);
if (res == nullptr) {
allocate_block(current_, num_bytes);
res = std::align(alignment, num_bytes, current_->bytes, remaining_);
if (res == nullptr)
throw std::bad_alloc();
}
current_->bytes = static_cast<std::byte*>(res) + num_bytes;
remaining_ -= num_bytes;
return res;
}

void monotonic_buffer_resource::allocate_block(block* prev_block) {
if (auto vptr = malloc(block_size)) {
void monotonic_buffer_resource::allocate_block(block* prev_block,
size_t min_size) {
auto size = std::max(block_size,
min_size + sizeof(block) + sizeof(max_align_t));
if (auto vptr = malloc(size)) {
current_ = static_cast<block*>(vptr);
current_->next = prev_block;
current_->bytes = static_cast<std::byte*>(vptr) + sizeof(block);
remaining_ = block_size - sizeof(block);
remaining_ = size - sizeof(block);
} else {
throw std::bad_alloc();
}
Expand Down

0 comments on commit 68411ce

Please sign in to comment.