From 2822eb62998381d4822a73576794421cb7630314 Mon Sep 17 00:00:00 2001 From: ylzh10 <46687083+ylzh10@users.noreply.github.com> Date: Tue, 18 Feb 2025 11:02:25 -0800 Subject: [PATCH 01/16] add `available()` to AddressAllocator Signed-off-by: ylzh10 <46687083+ylzh10@users.noreply.github.com> --- src/address_allocator.rs | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/address_allocator.rs b/src/address_allocator.rs index 5580154..c8e8008 100644 --- a/src/address_allocator.rs +++ b/src/address_allocator.rs @@ -29,6 +29,8 @@ pub struct AddressAllocator { // tree will represent a memory location and can have two states either // `NodeState::Free` or `NodeState::Allocated`. interval_tree: IntervalTree, + // Available memory space + available: usize, } impl AddressAllocator { @@ -43,6 +45,7 @@ impl AddressAllocator { Ok(AddressAllocator { address_space: aux_range, interval_tree: IntervalTree::new(aux_range), + available: aux_range.len() as usize, }) } @@ -63,13 +66,22 @@ impl AddressAllocator { policy: AllocPolicy, ) -> Result { let constraint = Constraint::new(size, alignment, policy)?; - self.interval_tree.allocate(constraint) + let allocated = self.interval_tree.allocate(constraint); + self.available -= allocated.len() as usize; + Ok(allocated) } /// Deletes the specified memory slot or returns `ResourceNotAvailable` if /// the node was not allocated before. pub fn free(&mut self, key: &RangeInclusive) -> Result<()> { - self.interval_tree.free(key) + self.interval_tree.free(key); + self.available += key.len() as usize; + Ok() + } + + /// Returns the available memory size in this allocator. + pub fn available(&self) -> usize { + self.available } } @@ -158,20 +170,27 @@ mod tests { #[test] fn test_allocate_with_alignment_first_ok() { let mut pool = AddressAllocator::new(0x1000, 0x1000).unwrap(); + assert_eq!(pool.available(), 0x1000); + // Allocate aligned 0x110 assert_eq!( pool.allocate(0x110, 0x100, AllocPolicy::FirstMatch) .unwrap(), RangeInclusive::new(0x1000, 0x110F).unwrap() ); + assert_eq!(pool.available(), 0x1000 - 0x110); + // Allocate aligned 0x100 assert_eq!( pool.allocate(0x100, 0x100, AllocPolicy::FirstMatch) .unwrap(), RangeInclusive::new(0x1200, 0x12FF).unwrap() ); + assert_eq!(pool.available(), 0x1000 - 0x110 - 0x100); + // Allocate unaligned 0x10 assert_eq!( pool.allocate(0x10, 0x100, AllocPolicy::FirstMatch).unwrap(), RangeInclusive::new(0x1300, 0x130F).unwrap() ); + assert_eq!(pool.available(), 0x1000 - 0x110 - 0x100 - 0x100); } #[test] @@ -230,18 +249,24 @@ mod tests { #[test] fn test_tree_allocate_address_free_and_realloc() { let mut pool = AddressAllocator::new(0x1000, 0x1000).unwrap(); + assert_eq!(pool.available(), 0x1000); + assert_eq!( pool.allocate(0x800, 0x100, AllocPolicy::FirstMatch) .unwrap(), RangeInclusive::new(0x1000, 0x17FF).unwrap() ); + assert_eq!(pool.available(), 0x1000 - 0x800); let _ = pool.free(&RangeInclusive::new(0x1000, 0x17FF).unwrap()); + assert_eq!(pool.available(), 0x1000); + assert_eq!( pool.allocate(0x800, 0x100, AllocPolicy::FirstMatch) .unwrap(), RangeInclusive::new(0x1000, 0x17FF).unwrap() ); + assert_eq!(pool.available(), 0x1000 - 0x800); } #[test] From bd35e909d727944d4400cb4f8b83789fdc95b0dc Mon Sep 17 00:00:00 2001 From: ylzh10 <46687083+ylzh10@users.noreply.github.com> Date: Tue, 18 Feb 2025 13:17:41 -0800 Subject: [PATCH 02/16] Create rust.yml Signed-off-by: ylzh10 <46687083+ylzh10@users.noreply.github.com> --- .github/workflows/rust.yml | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 .github/workflows/rust.yml diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml new file mode 100644 index 0000000..9fd45e0 --- /dev/null +++ b/.github/workflows/rust.yml @@ -0,0 +1,22 @@ +name: Rust + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +env: + CARGO_TERM_COLOR: always + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Build + run: cargo build --verbose + - name: Run tests + run: cargo test --verbose From 24d446e727f5d5d8834b9b9529e1a668afe6094c Mon Sep 17 00:00:00 2001 From: ylzh10 <46687083+ylzh10@users.noreply.github.com> Date: Tue, 18 Feb 2025 13:22:05 -0800 Subject: [PATCH 03/16] Update CHANGELOG.md Signed-off-by: ylzh10 <46687083+ylzh10@users.noreply.github.com> --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c87434..5283954 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ ## Upcoming version ### Added + +- Added `available()` API in `AddressAllocator` to allow get thee + available memories after `allocate/free()` + ### Changed ### Fixed ### Removed From e0e65842fc0b4a911581668f6e83b0bb78aff4e5 Mon Sep 17 00:00:00 2001 From: ylzh10 <46687083+ylzh10@users.noreply.github.com> Date: Tue, 18 Feb 2025 13:22:44 -0800 Subject: [PATCH 04/16] Update address_allocator.rs Signed-off-by: ylzh10 <46687083+ylzh10@users.noreply.github.com> --- src/address_allocator.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/address_allocator.rs b/src/address_allocator.rs index c8e8008..f9bc766 100644 --- a/src/address_allocator.rs +++ b/src/address_allocator.rs @@ -68,7 +68,7 @@ impl AddressAllocator { let constraint = Constraint::new(size, alignment, policy)?; let allocated = self.interval_tree.allocate(constraint); self.available -= allocated.len() as usize; - Ok(allocated) + allocated } /// Deletes the specified memory slot or returns `ResourceNotAvailable` if @@ -76,7 +76,7 @@ impl AddressAllocator { pub fn free(&mut self, key: &RangeInclusive) -> Result<()> { self.interval_tree.free(key); self.available += key.len() as usize; - Ok() + Ok(()) } /// Returns the available memory size in this allocator. From 9a474cbb014d4da6a052f4cf82ac9cb72706de62 Mon Sep 17 00:00:00 2001 From: ylzh10 <46687083+ylzh10@users.noreply.github.com> Date: Tue, 18 Feb 2025 13:24:42 -0800 Subject: [PATCH 05/16] Update address_allocator.rs Signed-off-by: ylzh10 <46687083+ylzh10@users.noreply.github.com> --- src/address_allocator.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/address_allocator.rs b/src/address_allocator.rs index f9bc766..a2f1e11 100644 --- a/src/address_allocator.rs +++ b/src/address_allocator.rs @@ -66,9 +66,9 @@ impl AddressAllocator { policy: AllocPolicy, ) -> Result { let constraint = Constraint::new(size, alignment, policy)?; - let allocated = self.interval_tree.allocate(constraint); + let allocated = self.interval_tree.allocate(constraint)?; self.available -= allocated.len() as usize; - allocated + Ok(allocated) } /// Deletes the specified memory slot or returns `ResourceNotAvailable` if From 2a1009934b73e89b4c3d0fcadd8d90b05278033f Mon Sep 17 00:00:00 2001 From: ylzh10 <46687083+ylzh10@users.noreply.github.com> Date: Tue, 18 Feb 2025 13:30:02 -0800 Subject: [PATCH 06/16] Update address_allocator.rs Signed-off-by: ylzh10 <46687083+ylzh10@users.noreply.github.com> --- src/address_allocator.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/address_allocator.rs b/src/address_allocator.rs index a2f1e11..f960b27 100644 --- a/src/address_allocator.rs +++ b/src/address_allocator.rs @@ -171,26 +171,32 @@ mod tests { fn test_allocate_with_alignment_first_ok() { let mut pool = AddressAllocator::new(0x1000, 0x1000).unwrap(); assert_eq!(pool.available(), 0x1000); - // Allocate aligned 0x110 + // Allocate 0x110 assert_eq!( pool.allocate(0x110, 0x100, AllocPolicy::FirstMatch) .unwrap(), RangeInclusive::new(0x1000, 0x110F).unwrap() ); assert_eq!(pool.available(), 0x1000 - 0x110); - // Allocate aligned 0x100 + // Allocate 0x100 assert_eq!( pool.allocate(0x100, 0x100, AllocPolicy::FirstMatch) .unwrap(), RangeInclusive::new(0x1200, 0x12FF).unwrap() ); assert_eq!(pool.available(), 0x1000 - 0x110 - 0x100); - // Allocate unaligned 0x10 + // Allocate 0x10 assert_eq!( pool.allocate(0x10, 0x100, AllocPolicy::FirstMatch).unwrap(), RangeInclusive::new(0x1300, 0x130F).unwrap() ); - assert_eq!(pool.available(), 0x1000 - 0x110 - 0x100 - 0x100); + assert_eq!(pool.available(), 0x1000 - 0x110 - 0x100 - 0x10); + // Allocate unaligned size 101 + assert_eq!( + pool.allocate(101, 0x100, AllocPolicy::FirstMatch).unwrap(), + RangeInclusive::new(0x1400, 0x14FF).unwrap() + ); + assert_eq!(pool.available(), 0x1000 - 0x110 - 0x100 - 0x10 - 0x100); } #[test] From 88f3bc944305901fad30fbe20fcea719eb4cf452 Mon Sep 17 00:00:00 2001 From: ylzh10 <46687083+ylzh10@users.noreply.github.com> Date: Tue, 18 Feb 2025 13:31:51 -0800 Subject: [PATCH 07/16] Update address_allocator.rs Signed-off-by: ylzh10 <46687083+ylzh10@users.noreply.github.com> --- src/address_allocator.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/address_allocator.rs b/src/address_allocator.rs index f960b27..ebd2ddb 100644 --- a/src/address_allocator.rs +++ b/src/address_allocator.rs @@ -74,7 +74,7 @@ impl AddressAllocator { /// Deletes the specified memory slot or returns `ResourceNotAvailable` if /// the node was not allocated before. pub fn free(&mut self, key: &RangeInclusive) -> Result<()> { - self.interval_tree.free(key); + self.interval_tree.free(key)?; self.available += key.len() as usize; Ok(()) } From 76487ba25e94283b4f4ef6212dc71fa668f5a1a1 Mon Sep 17 00:00:00 2001 From: ylzh10 <46687083+ylzh10@users.noreply.github.com> Date: Tue, 18 Feb 2025 13:33:43 -0800 Subject: [PATCH 08/16] Update address_allocator.rs Signed-off-by: ylzh10 <46687083+ylzh10@users.noreply.github.com> --- src/address_allocator.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/address_allocator.rs b/src/address_allocator.rs index ebd2ddb..501dd24 100644 --- a/src/address_allocator.rs +++ b/src/address_allocator.rs @@ -194,7 +194,7 @@ mod tests { // Allocate unaligned size 101 assert_eq!( pool.allocate(101, 0x100, AllocPolicy::FirstMatch).unwrap(), - RangeInclusive::new(0x1400, 0x14FF).unwrap() + RangeInclusive::new(0x1400, 0x1464).unwrap() ); assert_eq!(pool.available(), 0x1000 - 0x110 - 0x100 - 0x10 - 0x100); } From bcf2152fb50ed4db8f30377787910cdfbaeec796 Mon Sep 17 00:00:00 2001 From: ylzh10 <46687083+ylzh10@users.noreply.github.com> Date: Tue, 18 Feb 2025 13:35:54 -0800 Subject: [PATCH 09/16] Update address_allocator.rs Signed-off-by: ylzh10 <46687083+ylzh10@users.noreply.github.com> --- src/address_allocator.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/address_allocator.rs b/src/address_allocator.rs index 501dd24..c4bfdbb 100644 --- a/src/address_allocator.rs +++ b/src/address_allocator.rs @@ -196,7 +196,7 @@ mod tests { pool.allocate(101, 0x100, AllocPolicy::FirstMatch).unwrap(), RangeInclusive::new(0x1400, 0x1464).unwrap() ); - assert_eq!(pool.available(), 0x1000 - 0x110 - 0x100 - 0x10 - 0x100); + assert_eq!(pool.available(), 0x1000 - 0x110 - 0x100 - 0x10 - 0x64); } #[test] From a39368bd144b4db37bbd1336664f6a09990ef9b5 Mon Sep 17 00:00:00 2001 From: ylzh10 <46687083+ylzh10@users.noreply.github.com> Date: Tue, 18 Feb 2025 13:37:42 -0800 Subject: [PATCH 10/16] Update address_allocator.rs Signed-off-by: ylzh10 <46687083+ylzh10@users.noreply.github.com> --- src/address_allocator.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/address_allocator.rs b/src/address_allocator.rs index c4bfdbb..cb6ae1f 100644 --- a/src/address_allocator.rs +++ b/src/address_allocator.rs @@ -193,10 +193,10 @@ mod tests { assert_eq!(pool.available(), 0x1000 - 0x110 - 0x100 - 0x10); // Allocate unaligned size 101 assert_eq!( - pool.allocate(101, 0x100, AllocPolicy::FirstMatch).unwrap(), - RangeInclusive::new(0x1400, 0x1464).unwrap() + pool.allocate(0x3, 0x100, AllocPolicy::FirstMatch).unwrap(), + RangeInclusive::new(0x1400, 0x1402).unwrap() ); - assert_eq!(pool.available(), 0x1000 - 0x110 - 0x100 - 0x10 - 0x64); + assert_eq!(pool.available(), 0x1000 - 0x110 - 0x100 - 0x10 - 0x3); } #[test] From 473ecf5065cb407b298e8640b515d20e449d8ce3 Mon Sep 17 00:00:00 2001 From: ylzh10 <46687083+ylzh10@users.noreply.github.com> Date: Tue, 18 Feb 2025 13:48:29 -0800 Subject: [PATCH 11/16] Update address_allocator.rs Signed-off-by: ylzh10 <46687083+ylzh10@users.noreply.github.com> --- src/address_allocator.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/address_allocator.rs b/src/address_allocator.rs index cb6ae1f..c734b63 100644 --- a/src/address_allocator.rs +++ b/src/address_allocator.rs @@ -191,12 +191,6 @@ mod tests { RangeInclusive::new(0x1300, 0x130F).unwrap() ); assert_eq!(pool.available(), 0x1000 - 0x110 - 0x100 - 0x10); - // Allocate unaligned size 101 - assert_eq!( - pool.allocate(0x3, 0x100, AllocPolicy::FirstMatch).unwrap(), - RangeInclusive::new(0x1400, 0x1402).unwrap() - ); - assert_eq!(pool.available(), 0x1000 - 0x110 - 0x100 - 0x10 - 0x3); } #[test] From 8a5555dd0614318d9d1302ccb3c0071737136d51 Mon Sep 17 00:00:00 2001 From: ylzh10 <46687083+ylzh10@users.noreply.github.com> Date: Tue, 18 Feb 2025 13:58:54 -0800 Subject: [PATCH 12/16] Delete .github/workflows directory Signed-off-by: ylzh10 <46687083+ylzh10@users.noreply.github.com> --- .github/workflows/rust.yml | 22 ---------------------- 1 file changed, 22 deletions(-) delete mode 100644 .github/workflows/rust.yml diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml deleted file mode 100644 index 9fd45e0..0000000 --- a/.github/workflows/rust.yml +++ /dev/null @@ -1,22 +0,0 @@ -name: Rust - -on: - push: - branches: [ "main" ] - pull_request: - branches: [ "main" ] - -env: - CARGO_TERM_COLOR: always - -jobs: - build: - - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v4 - - name: Build - run: cargo build --verbose - - name: Run tests - run: cargo test --verbose From bc2324753504ef9ed85a4e968809966fa1393708 Mon Sep 17 00:00:00 2001 From: ylzh10 <46687083+ylzh10@users.noreply.github.com> Date: Tue, 18 Feb 2025 14:03:13 -0800 Subject: [PATCH 13/16] Update CHANGELOG.md Signed-off-by: ylzh10 <46687083+ylzh10@users.noreply.github.com> --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5283954..2ee7b2a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,8 +4,8 @@ ### Added -- Added `available()` API in `AddressAllocator` to allow get thee - available memories after `allocate/free()` +- Added `available()` API in `AddressAllocator` to allow get the + available memories after `allocate/free()`s ### Changed ### Fixed From a7f91c7b918c80ac1bb027e2112b270e45f2ef50 Mon Sep 17 00:00:00 2001 From: ylzh10 <46687083+ylzh10@users.noreply.github.com> Date: Tue, 18 Feb 2025 22:12:04 +0000 Subject: [PATCH 14/16] update changelog.md Signed-off-by: ylzh10 <46687083+ylzh10@users.noreply.github.com> --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ee7b2a..cd23c2e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ ### Added -- Added `available()` API in `AddressAllocator` to allow get the +- Added `available()` API in `AddressAllocator` to allow getting the available memories after `allocate/free()`s ### Changed From 5d5d67c147983ce1daf9770be6a203fae6608212 Mon Sep 17 00:00:00 2001 From: ylzh10 <46687083+ylzh10@users.noreply.github.com> Date: Tue, 18 Feb 2025 22:17:43 +0000 Subject: [PATCH 15/16] update format Signed-off-by: ylzh10 <46687083+ylzh10@users.noreply.github.com> --- CHANGELOG.md | 4 ++-- src/address_allocator.rs | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cd23c2e..81e3287 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,8 +4,8 @@ ### Added -- Added `available()` API in `AddressAllocator` to allow getting the - available memories after `allocate/free()`s +- Added `available()` API in `AddressAllocator` to allow + getting the available memories after `allocate/free()`s ### Changed ### Fixed diff --git a/src/address_allocator.rs b/src/address_allocator.rs index c734b63..7b79d14 100644 --- a/src/address_allocator.rs +++ b/src/address_allocator.rs @@ -250,17 +250,17 @@ mod tests { fn test_tree_allocate_address_free_and_realloc() { let mut pool = AddressAllocator::new(0x1000, 0x1000).unwrap(); assert_eq!(pool.available(), 0x1000); - + // Allocate 0x800 assert_eq!( pool.allocate(0x800, 0x100, AllocPolicy::FirstMatch) .unwrap(), RangeInclusive::new(0x1000, 0x17FF).unwrap() ); assert_eq!(pool.available(), 0x1000 - 0x800); - + // Free 0x800 let _ = pool.free(&RangeInclusive::new(0x1000, 0x17FF).unwrap()); assert_eq!(pool.available(), 0x1000); - + // Allocate 0x800 again assert_eq!( pool.allocate(0x800, 0x100, AllocPolicy::FirstMatch) .unwrap(), From f73905f1f889637a795c3ab35b10f86a088d4c3a Mon Sep 17 00:00:00 2001 From: ylzh10 <46687083+ylzh10@users.noreply.github.com> Date: Tue, 18 Feb 2025 11:02:25 -0800 Subject: [PATCH 16/16] add `available()` to AddressAllocator Signed-off-by: ylzh10 <46687083+ylzh10@users.noreply.github.com> Create rust.yml Signed-off-by: ylzh10 <46687083+ylzh10@users.noreply.github.com> Update CHANGELOG.md Signed-off-by: ylzh10 <46687083+ylzh10@users.noreply.github.com> Update address_allocator.rs Signed-off-by: ylzh10 <46687083+ylzh10@users.noreply.github.com> Update address_allocator.rs Signed-off-by: ylzh10 <46687083+ylzh10@users.noreply.github.com> Update address_allocator.rs Signed-off-by: ylzh10 <46687083+ylzh10@users.noreply.github.com> Update address_allocator.rs Signed-off-by: ylzh10 <46687083+ylzh10@users.noreply.github.com> Update address_allocator.rs Signed-off-by: ylzh10 <46687083+ylzh10@users.noreply.github.com> Update address_allocator.rs Signed-off-by: ylzh10 <46687083+ylzh10@users.noreply.github.com> Update address_allocator.rs Signed-off-by: ylzh10 <46687083+ylzh10@users.noreply.github.com> Update address_allocator.rs Signed-off-by: ylzh10 <46687083+ylzh10@users.noreply.github.com> Delete .github/workflows directory Signed-off-by: ylzh10 <46687083+ylzh10@users.noreply.github.com> Update CHANGELOG.md Signed-off-by: ylzh10 <46687083+ylzh10@users.noreply.github.com> update changelog.md Signed-off-by: ylzh10 <46687083+ylzh10@users.noreply.github.com> update format Signed-off-by: ylzh10 <46687083+ylzh10@users.noreply.github.com> --- CHANGELOG.md | 4 ++++ src/address_allocator.rs | 31 ++++++++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c87434..81e3287 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ ## Upcoming version ### Added + +- Added `available()` API in `AddressAllocator` to allow + getting the available memories after `allocate/free()`s + ### Changed ### Fixed ### Removed diff --git a/src/address_allocator.rs b/src/address_allocator.rs index 5580154..7b79d14 100644 --- a/src/address_allocator.rs +++ b/src/address_allocator.rs @@ -29,6 +29,8 @@ pub struct AddressAllocator { // tree will represent a memory location and can have two states either // `NodeState::Free` or `NodeState::Allocated`. interval_tree: IntervalTree, + // Available memory space + available: usize, } impl AddressAllocator { @@ -43,6 +45,7 @@ impl AddressAllocator { Ok(AddressAllocator { address_space: aux_range, interval_tree: IntervalTree::new(aux_range), + available: aux_range.len() as usize, }) } @@ -63,13 +66,22 @@ impl AddressAllocator { policy: AllocPolicy, ) -> Result { let constraint = Constraint::new(size, alignment, policy)?; - self.interval_tree.allocate(constraint) + let allocated = self.interval_tree.allocate(constraint)?; + self.available -= allocated.len() as usize; + Ok(allocated) } /// Deletes the specified memory slot or returns `ResourceNotAvailable` if /// the node was not allocated before. pub fn free(&mut self, key: &RangeInclusive) -> Result<()> { - self.interval_tree.free(key) + self.interval_tree.free(key)?; + self.available += key.len() as usize; + Ok(()) + } + + /// Returns the available memory size in this allocator. + pub fn available(&self) -> usize { + self.available } } @@ -158,20 +170,27 @@ mod tests { #[test] fn test_allocate_with_alignment_first_ok() { let mut pool = AddressAllocator::new(0x1000, 0x1000).unwrap(); + assert_eq!(pool.available(), 0x1000); + // Allocate 0x110 assert_eq!( pool.allocate(0x110, 0x100, AllocPolicy::FirstMatch) .unwrap(), RangeInclusive::new(0x1000, 0x110F).unwrap() ); + assert_eq!(pool.available(), 0x1000 - 0x110); + // Allocate 0x100 assert_eq!( pool.allocate(0x100, 0x100, AllocPolicy::FirstMatch) .unwrap(), RangeInclusive::new(0x1200, 0x12FF).unwrap() ); + assert_eq!(pool.available(), 0x1000 - 0x110 - 0x100); + // Allocate 0x10 assert_eq!( pool.allocate(0x10, 0x100, AllocPolicy::FirstMatch).unwrap(), RangeInclusive::new(0x1300, 0x130F).unwrap() ); + assert_eq!(pool.available(), 0x1000 - 0x110 - 0x100 - 0x10); } #[test] @@ -230,18 +249,24 @@ mod tests { #[test] fn test_tree_allocate_address_free_and_realloc() { let mut pool = AddressAllocator::new(0x1000, 0x1000).unwrap(); + assert_eq!(pool.available(), 0x1000); + // Allocate 0x800 assert_eq!( pool.allocate(0x800, 0x100, AllocPolicy::FirstMatch) .unwrap(), RangeInclusive::new(0x1000, 0x17FF).unwrap() ); - + assert_eq!(pool.available(), 0x1000 - 0x800); + // Free 0x800 let _ = pool.free(&RangeInclusive::new(0x1000, 0x17FF).unwrap()); + assert_eq!(pool.available(), 0x1000); + // Allocate 0x800 again assert_eq!( pool.allocate(0x800, 0x100, AllocPolicy::FirstMatch) .unwrap(), RangeInclusive::new(0x1000, 0x17FF).unwrap() ); + assert_eq!(pool.available(), 0x1000 - 0x800); } #[test]