Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
zhztheplayer committed Aug 7, 2024
1 parent 2d215fe commit fa1f313
Showing 1 changed file with 9 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,15 @@ public long borrow(long size) {
if (size == 0) {
return 0;
}
Preconditions.checkState(overTarget.usedBytes() == 0);
long granted = target.borrow(size);
long majorSize = target.usedBytes();
long expectedOverAcquired = (long) (ratio * majorSize);
long overAcquired = overTarget.usedBytes();
long diff = expectedOverAcquired - overAcquired;
if (diff >= 0) { // otherwise, there might be a spill happened during the last borrow() call
overTarget.borrow(diff); // we don't have to check the returned value
}
long overSize = (long) (ratio * majorSize);
long overAcquired = overTarget.borrow(overSize);
Preconditions.checkState(overAcquired == overTarget.usedBytes());
long releasedOverSize = overTarget.repay(overSize);
Preconditions.checkState(releasedOverSize == overSize);
Preconditions.checkState(overTarget.usedBytes() == 0);
return granted;
}

Expand All @@ -71,16 +72,8 @@ public long repay(long size) {
if (size == 0) {
return 0;
}
long freed = target.repay(size);
// clean up the over-acquired target
long overAcquired = overTarget.usedBytes();
long freedOverAcquired = overTarget.repay(overAcquired);
Preconditions.checkArgument(
freedOverAcquired == overAcquired,
"Freed over-acquired size is not equal to requested size");
Preconditions.checkArgument(
overTarget.usedBytes() == 0, "Over-acquired target was not cleaned up");
return freed;
Preconditions.checkState(overTarget.usedBytes() == 0);
return target.repay(size);
}

@Override
Expand Down

0 comments on commit fa1f313

Please sign in to comment.