Skip to content

Commit

Permalink
Enable new ScopedReservation to inherit another.
Browse files Browse the repository at this point in the history
Bug: b:233089187
Bug: b:237811834

Make it possible to create a new scoped reservation for the same
resource interface as another one - useful when the latter is not
available directly (this eliminates the need to propagate the interface
to where it is needed).
Also some clean-ups in the code/declarations.

Change-Id: I0e7f9f4bdbef593eab7c52fbec018715e629839b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3754714
Reviewed-by: Hong Xu <xuhong@google.com>
Commit-Queue: Hong Xu <xuhong@google.com>
Auto-Submit: Leonid Baraz <lbaraz@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1023479}
  • Loading branch information
Leonid Baraz authored and Chromium LUCI CQ committed Jul 12, 2022
1 parent ac02c05 commit b455392
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
11 changes: 11 additions & 0 deletions components/reporting/resources/resource_interface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ ScopedReservation::ScopedReservation(
size_ = size;
}

ScopedReservation::ScopedReservation(
uint64_t size,
const ScopedReservation& other_reservation) noexcept
: resource_interface_(other_reservation.resource_interface_) {
if (size == 0uL || !resource_interface_.get() ||
!resource_interface_->Reserve(size)) {
return;
}
size_ = size;
}

ScopedReservation::ScopedReservation(ScopedReservation&& other) noexcept
: resource_interface_(other.resource_interface_),
size_(std::exchange(other.size_, absl::nullopt)) {}
Expand Down
4 changes: 4 additions & 0 deletions components/reporting/resources/resource_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ class ScopedReservation {
ScopedReservation(
uint64_t size,
scoped_refptr<ResourceInterface> resource_interface) noexcept;
// New reservation on the same resource interface as |other_reservation|.
ScopedReservation(uint64_t size,
const ScopedReservation& other_reservation) noexcept;
// Move constructor.
ScopedReservation(ScopedReservation&& other) noexcept;
ScopedReservation(const ScopedReservation& other) = delete;
ScopedReservation& operator=(ScopedReservation&& other) = delete;
Expand Down
21 changes: 21 additions & 0 deletions components/reporting/resources/resource_interface_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,27 @@ TEST_P(ResourceInterfaceTest, ScopedReservationRepeatingHandOvers) {
Eq(resource_interface()->GetTotal() - 1));
}

TEST_P(ResourceInterfaceTest, ScopedReservationRepeatingCopyHandOvers) {
uint64_t size = resource_interface()->GetTotal() / 2;
ScopedReservation scoped_reservation(size, resource_interface());
EXPECT_TRUE(scoped_reservation.reserved());

for (; size >= 2; size /= 2) {
ScopedReservation another_reservation(size / 2, scoped_reservation);
EXPECT_TRUE(another_reservation.reserved());
scoped_reservation.HandOver(another_reservation);
}
EXPECT_THAT(resource_interface()->GetUsed(),
Eq(resource_interface()->GetTotal() - 1));
}

TEST_P(ResourceInterfaceTest, ScopedReservationFailureToCopyFromEmpty) {
ScopedReservation scoped_reservation;
uint64_t size = resource_interface()->GetTotal() / 2;
ScopedReservation another_reservation(size, scoped_reservation);
EXPECT_FALSE(scoped_reservation.reserved());
}

TEST_P(ResourceInterfaceTest, ScopedReservationRepeatingHandOversToEmpty) {
ScopedReservation scoped_reservation;
EXPECT_FALSE(scoped_reservation.reserved());
Expand Down

0 comments on commit b455392

Please sign in to comment.