diff --git a/CHANGELOG.md b/CHANGELOG.md index 31f2996..4d2f5c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [Unreleased] + +### Fixed + +- Fix panic in `tlsf::Heap::used`. + ## [v0.7.0] - 2026-01-03 ### Added diff --git a/examples/tlsf_integration_test.rs b/examples/tlsf_integration_test.rs index 5bf5601..7a911e7 100644 --- a/examples/tlsf_integration_test.rs +++ b/examples/tlsf_integration_test.rs @@ -33,6 +33,8 @@ pub type TestTable<'a> = &'a [(fn() -> (), &'static str)]; fn test_global_heap() { const ELEMS: usize = 250; + assert_eq!(HEAP_SIZE, HEAP.free() + HEAP.used()); + let initial_free = HEAP.free(); let mut allocated = LinkedList::new(); for _ in 0..ELEMS { @@ -51,6 +53,8 @@ fn test_global_heap() { for i in 0..ELEMS { assert_eq!(allocated.pop_front().unwrap(), i as i32); } + assert_eq!(HEAP_SIZE, HEAP.free() + HEAP.used()); + assert_eq!(initial_free, HEAP.free()); } fn test_allocator_api() { diff --git a/src/tlsf.rs b/src/tlsf.rs index 7a5017f..7155aaa 100644 --- a/src/tlsf.rs +++ b/src/tlsf.rs @@ -101,7 +101,8 @@ impl Heap { /// Get the amount of bytes used by the allocator. pub fn used(&self) -> usize { critical_section::with(|cs| { - self.heap.borrow_ref_mut(cs).raw_block_size - self.free_with_cs(cs) + let free = self.free_with_cs(cs); + self.heap.borrow_ref_mut(cs).raw_block_size - free }) }