Skip to content

Commit 7aac554

Browse files
committed
update
Signed-off-by: ylzh10 <[email protected]>
1 parent 5436d5f commit 7aac554

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

src/address_allocator.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,21 @@ impl AddressAllocator {
6767
) -> Result<RangeInclusive> {
6868
let constraint = Constraint::new(size, alignment, policy)?;
6969
let allocated = self.interval_tree.allocate(constraint)?;
70-
self.used += allocated.len() as usize;
70+
self.used.checked_add(allocated.len() as usize).expect("Failed to calculate used memory");
7171
Ok(allocated)
7272
}
7373

7474
/// Deletes the specified memory slot or returns `ResourceNotAvailable` if
7575
/// the node was not allocated before.
7676
pub fn free(&mut self, key: &RangeInclusive) -> Result<()> {
7777
self.interval_tree.free(key)?;
78-
self.used -= key.len() as usize;
78+
self.used.checked_sub(key.len() as usize).expect("Failed to calculate used memory");
7979
Ok(())
8080
}
8181

8282
/// Returns the used memory size in this allocator.
83-
/// NOTE that due to fragmentations, it's not guaranteed that the next
84-
/// allocate() call after querying the used memory can succeed with
85-
/// allocating all unused memories and it may still return OOM.
83+
/// NOTE that due to fragmentations, not all unused memory may be available
84+
/// for next `allocate()` call!
8685
pub fn used(&self) -> usize {
8786
self.used
8887
}

0 commit comments

Comments
 (0)