diff --git a/source/common/common/mem_block_builder.h b/source/common/common/mem_block_builder.h index 9749583b4947..ce0c6827dc8b 100644 --- a/source/common/common/mem_block_builder.h +++ b/source/common/common/mem_block_builder.h @@ -30,13 +30,15 @@ template class MemBlockBuilder { MemBlockBuilder() {} /** - * Populates (or repopulates) the MemBlockBuilder to make it the specified - * capacity. This does not have resize semantics; when populate() is called any - * previous contents are erased. + * Allocates (or reallocates) memory for the MemBlockBuilder to make it the + * specified capacity. This does not have resize semantics; when setCapacity() + * is called any previous contents are erased. * * @param capacity The number of memory elements to allocate. */ - void populate(uint64_t capacity) { populateHelper(capacity, std::make_unique(capacity)); } + void setCapacity(uint64_t capacity) { + setCapacityHelper(capacity, std::make_unique(capacity)); + } /** * @return the capacity. @@ -88,7 +90,7 @@ template class MemBlockBuilder { /** * Empties the contents of this. */ - void reset() { populateHelper(0, std::unique_ptr(nullptr)); } + void reset() { setCapacityHelper(0, std::unique_ptr(nullptr)); } /** * Returns the underlying storage as a unique pointer, clearing this. @@ -96,7 +98,7 @@ template class MemBlockBuilder { * @return the transferred storage. */ std::unique_ptr release() { - write_span_.reset(); + write_span_ = absl::MakeSpan(static_cast(nullptr), 0); return std::move(data_); } @@ -111,7 +113,7 @@ template class MemBlockBuilder { uint64_t size() const { return write_span_.data() - data_.get(); } private: - void populateHelper(uint64_t capacity, std::unique_ptr data) { + void setCapacityHelper(uint64_t capacity, std::unique_ptr data) { data_ = std::move(data); write_span_ = absl::MakeSpan(data_.get(), capacity); } diff --git a/test/common/common/mem_block_builder_test.cc b/test/common/common/mem_block_builder_test.cc index 6435ebeb5558..051067b47661 100644 --- a/test/common/common/mem_block_builder_test.cc +++ b/test/common/common/mem_block_builder_test.cc @@ -17,7 +17,7 @@ TEST(MemBlockBuilderTest, AppendUint8) { MemBlockBuilder append; EXPECT_EQ(0, append.capacity()); - append.populate(7); + append.setCapacity(7); EXPECT_EQ(7, append.capacity()); append.appendOne(8); append.appendOne(9); @@ -28,7 +28,10 @@ TEST(MemBlockBuilderTest, AppendUint8) { append.appendBlock(mem_block); EXPECT_EQ(0, append.capacityRemaining()); - EXPECT_EQ((std::vector{8, 9, 5, 6, 7, 8, 9}), append.span()); + uint64_t size = append.size(); + std::unique_ptr data = append.release(); + EXPECT_EQ((std::vector{8, 9, 5, 6, 7, 8, 9}), + std::vector(data.get(), data.get() + size)); mem_block.reset(); EXPECT_EQ(0, mem_block.capacity()); @@ -45,7 +48,7 @@ TEST(MemBlockBuilderTest, AppendUint32) { MemBlockBuilder append; EXPECT_EQ(0, append.capacity()); - append.populate(7); + append.setCapacity(7); EXPECT_EQ(7, append.capacity()); append.appendOne(100008); append.appendOne(100009); @@ -56,8 +59,10 @@ TEST(MemBlockBuilderTest, AppendUint32) { append.appendBlock(mem_block); EXPECT_EQ(0, append.capacityRemaining()); + uint64_t size = append.size(); + std::unique_ptr data = append.release(); EXPECT_EQ((std::vector{100008, 100009, 100005, 100006, 100007, 100008, 100009}), - append.span()); + std::vector(data.get(), data.get() + size)); mem_block.reset(); EXPECT_EQ(0, mem_block.capacity());