Skip to content

Commit 5436d5f

Browse files
committed
replace available() w/ used() API
Signed-off-by: ylzh10 <[email protected]>
1 parent 72e663c commit 5436d5f

File tree

2 files changed

+20
-23
lines changed

2 files changed

+20
-23
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
### Added
66

7-
- Added `available()` API in `AddressAllocator` to allow
8-
getting the available memories after `allocate/free()`s
7+
- Added `used()` API in `AddressAllocator` to allow
8+
getting the used memories after `allocate/free()`s
99

1010
### Changed
1111
### Fixed

src/address_allocator.rs

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,8 @@ pub struct AddressAllocator {
2929
// tree will represent a memory location and can have two states either
3030
// `NodeState::Free` or `NodeState::Allocated`.
3131
interval_tree: IntervalTree,
32-
// Available free memory space in the address space.
33-
// NOTE that due to fragmentations, |available| may not give the actual
34-
// available (contiguous) memory block that can be allocated in next
35-
// allocate() call.
36-
available: usize,
32+
// Used memory space in the address space.
33+
used: usize,
3734
}
3835

3936
impl AddressAllocator {
@@ -48,7 +45,7 @@ impl AddressAllocator {
4845
Ok(AddressAllocator {
4946
address_space: aux_range,
5047
interval_tree: IntervalTree::new(aux_range),
51-
available: aux_range.len() as usize,
48+
used: 0,
5249
})
5350
}
5451

@@ -70,24 +67,24 @@ impl AddressAllocator {
7067
) -> Result<RangeInclusive> {
7168
let constraint = Constraint::new(size, alignment, policy)?;
7269
let allocated = self.interval_tree.allocate(constraint)?;
73-
self.available -= allocated.len() as usize;
70+
self.used += allocated.len() as usize;
7471
Ok(allocated)
7572
}
7673

7774
/// Deletes the specified memory slot or returns `ResourceNotAvailable` if
7875
/// the node was not allocated before.
7976
pub fn free(&mut self, key: &RangeInclusive) -> Result<()> {
8077
self.interval_tree.free(key)?;
81-
self.available += key.len() as usize;
78+
self.used -= key.len() as usize;
8279
Ok(())
8380
}
8481

85-
/// Returns the available memory size in this allocator.
82+
/// Returns the used memory size in this allocator.
8683
/// NOTE that due to fragmentations, it's not guaranteed that the next
87-
/// allocate() call after querying the available memory can succeed with
88-
/// allocating those available memories and it may still return OOM.
89-
pub fn available(&self) -> usize {
90-
self.available
84+
/// allocate() call after querying the used memory can succeed with
85+
/// allocating all unused memories and it may still return OOM.
86+
pub fn used(&self) -> usize {
87+
self.used
9188
}
9289
}
9390

@@ -176,27 +173,27 @@ mod tests {
176173
#[test]
177174
fn test_allocate_with_alignment_first_ok() {
178175
let mut pool = AddressAllocator::new(0x1000, 0x1000).unwrap();
179-
assert_eq!(pool.available(), 0x1000);
176+
assert_eq!(pool.used(), 0);
180177
// Allocate 0x110
181178
assert_eq!(
182179
pool.allocate(0x110, 0x100, AllocPolicy::FirstMatch)
183180
.unwrap(),
184181
RangeInclusive::new(0x1000, 0x110F).unwrap()
185182
);
186-
assert_eq!(pool.available(), 0x1000 - 0x110);
183+
assert_eq!(pool.used(), 0x110);
187184
// Allocate 0x100
188185
assert_eq!(
189186
pool.allocate(0x100, 0x100, AllocPolicy::FirstMatch)
190187
.unwrap(),
191188
RangeInclusive::new(0x1200, 0x12FF).unwrap()
192189
);
193-
assert_eq!(pool.available(), 0x1000 - 0x110 - 0x100);
190+
assert_eq!(pool.used(), 0x110 + 0x100);
194191
// Allocate 0x10
195192
assert_eq!(
196193
pool.allocate(0x10, 0x100, AllocPolicy::FirstMatch).unwrap(),
197194
RangeInclusive::new(0x1300, 0x130F).unwrap()
198195
);
199-
assert_eq!(pool.available(), 0x1000 - 0x110 - 0x100 - 0x10);
196+
assert_eq!(pool.used(), 0x110 + 0x100 + 0x10);
200197
}
201198

202199
#[test]
@@ -255,24 +252,24 @@ mod tests {
255252
#[test]
256253
fn test_tree_allocate_address_free_and_realloc() {
257254
let mut pool = AddressAllocator::new(0x1000, 0x1000).unwrap();
258-
assert_eq!(pool.available(), 0x1000);
255+
assert_eq!(pool.used(), 0);
259256
// Allocate 0x800
260257
assert_eq!(
261258
pool.allocate(0x800, 0x100, AllocPolicy::FirstMatch)
262259
.unwrap(),
263260
RangeInclusive::new(0x1000, 0x17FF).unwrap()
264261
);
265-
assert_eq!(pool.available(), 0x1000 - 0x800);
262+
assert_eq!(pool.used(), 0x800);
266263
// Free 0x800
267264
let _ = pool.free(&RangeInclusive::new(0x1000, 0x17FF).unwrap());
268-
assert_eq!(pool.available(), 0x1000);
265+
assert_eq!(pool.used(), 0);
269266
// Allocate 0x800 again
270267
assert_eq!(
271268
pool.allocate(0x800, 0x100, AllocPolicy::FirstMatch)
272269
.unwrap(),
273270
RangeInclusive::new(0x1000, 0x17FF).unwrap()
274271
);
275-
assert_eq!(pool.available(), 0x1000 - 0x800);
272+
assert_eq!(pool.used(), 0x800);
276273
}
277274

278275
#[test]

0 commit comments

Comments
 (0)