Merge "liblp: Try to merge extents during partition resizes."

am: b0c9318493

Change-Id: Ib124843c47f710665e1d9134eae11edb7575863e
This commit is contained in:
David Anderson 2018-08-03 15:58:35 -07:00 committed by android-build-merger
commit 8660f5a85c
3 changed files with 19 additions and 0 deletions

View file

@ -84,6 +84,19 @@ Partition::Partition(const std::string& name, const std::string& guid, uint32_t
void Partition::AddExtent(std::unique_ptr<Extent>&& extent) {
size_ += extent->num_sectors() * LP_SECTOR_SIZE;
if (LinearExtent* new_extent = extent->AsLinearExtent()) {
if (!extents_.empty() && extents_.back()->AsLinearExtent() &&
extents_.back()->AsLinearExtent()->end_sector() == new_extent->physical_sector()) {
// If the previous extent can be merged into this new one, do so
// to avoid creating unnecessary extents.
LinearExtent* prev_extent = extents_.back()->AsLinearExtent();
extent = std::make_unique<LinearExtent>(
prev_extent->num_sectors() + new_extent->num_sectors(),
prev_extent->physical_sector());
extents_.pop_back();
}
}
extents_.push_back(std::move(extent));
}

View file

@ -69,6 +69,11 @@ TEST(liblp, ResizePartition) {
EXPECT_EQ(system->size(), 131072);
EXPECT_EQ(system->extents().size(), 1);
EXPECT_EQ(system->extents()[0]->num_sectors(), 131072 / LP_SECTOR_SIZE);
// Test resizing again, that the extents are merged together.
builder->ResizePartition(system, 1024 * 256);
EXPECT_EQ(system->size(), 1024 * 256);
EXPECT_EQ(system->extents().size(), 1);
EXPECT_EQ(system->extents()[0]->num_sectors(), (1024 * 256) / LP_SECTOR_SIZE);
// Test shrinking within the same extent.
builder->ResizePartition(system, 32768);

View file

@ -81,6 +81,7 @@ class LinearExtent final : public Extent {
LinearExtent* AsLinearExtent() override { return this; }
uint64_t physical_sector() const { return physical_sector_; }
uint64_t end_sector() const { return physical_sector_ + num_sectors_; }
private:
uint64_t physical_sector_;