diff --git a/src/utils/memory.cpp b/src/utils/memory.cpp index c5a0de12b..761a13f2f 100644 --- a/src/utils/memory.cpp +++ b/src/utils/memory.cpp @@ -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; } diff --git a/tests/unit/utils_memory.cpp b/tests/unit/utils_memory.cpp index 22e49ad37..89544fd1b 100644 --- a/tests/unit/utils_memory.cpp +++ b/tests/unit/utils_memory.cpp @@ -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: