Fix MonotonicBufferResource buffer growth

Reviewers: teon.banek

Reviewed By: teon.banek

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D2483
This commit is contained in:
Matej Ferencevic 2019-10-15 14:30:51 +02:00
parent 7afa4fd7b3
commit e5a4a84de2
2 changed files with 37 additions and 4 deletions
src/utils
tests/unit

View File

@ -37,10 +37,7 @@ MonotonicBufferResource::MonotonicBufferResource(void *buffer,
MemoryResource *memory)
: memory_(memory),
initial_buffer_(buffer),
initial_size_(buffer_size),
next_buffer_size_(GrowMonotonicBuffer(
initial_size_, std::numeric_limits<size_t>::max() - sizeof(Buffer))) {
}
initial_size_(buffer_size) {}
MonotonicBufferResource::MonotonicBufferResource(
MonotonicBufferResource &&other) noexcept
@ -48,6 +45,7 @@ MonotonicBufferResource::MonotonicBufferResource(
current_buffer_(other.current_buffer_),
initial_buffer_(other.initial_buffer_),
initial_size_(other.initial_size_),
next_buffer_size_(other.next_buffer_size_),
allocated_(other.allocated_) {
other.current_buffer_ = nullptr;
}
@ -60,6 +58,7 @@ MonotonicBufferResource &MonotonicBufferResource::operator=(
current_buffer_ = other.current_buffer_;
initial_buffer_ = other.initial_buffer_;
initial_size_ = other.initial_size_;
next_buffer_size_ = other.next_buffer_size_;
allocated_ = other.allocated_;
other.current_buffer_ = nullptr;
other.allocated_ = 0U;
@ -76,6 +75,7 @@ void MonotonicBufferResource::Release() {
b = next;
}
current_buffer_ = nullptr;
next_buffer_size_ = initial_size_;
allocated_ = 0U;
}

View File

@ -329,6 +329,39 @@ TEST(PoolResource, BlockDeallocation) {
EXPECT_EQ(test_mem.new_count_, 0U);
}
class AllocationTrackingMemory final : public utils::MemoryResource {
public:
std::vector<size_t> allocated_sizes_;
private:
void *DoAllocate(size_t bytes, size_t alignment) override {
allocated_sizes_.push_back(bytes);
return utils::NewDeleteResource()->Allocate(bytes, alignment);
}
void DoDeallocate(void *ptr, size_t bytes, size_t alignment) override {
return utils::NewDeleteResource()->Deallocate(ptr, bytes, alignment);
}
bool DoIsEqual(const utils::MemoryResource &other) const noexcept override {
return this == &other;
}
};
// NOLINTNEXTLINE(hicpp-special-member-functions)
TEST(MonotonicBufferResource, ResetGrowthFactor) {
AllocationTrackingMemory test_mem;
constexpr size_t stack_data_size = 1024;
char stack_data[stack_data_size];
utils::MonotonicBufferResource mem(&stack_data[0], stack_data_size,
&test_mem);
mem.Allocate(stack_data_size + 1);
mem.Release();
mem.Allocate(stack_data_size + 1);
ASSERT_EQ(test_mem.allocated_sizes_.size(), 2);
ASSERT_EQ(test_mem.allocated_sizes_.front(), test_mem.allocated_sizes_.back());
}
// NOLINTNEXTLINE(hicpp-special-member-functions)
class ContainerWithAllocatorLast final {
public: