Skip to content

Commit 65df6d7

Browse files
committed
Properly fix calls to the rlsf library
1 parent a10552a commit 65df6d7

File tree

2 files changed

+10
-24
lines changed

2 files changed

+10
-24
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1010
### Fixed
1111

1212
- Fix panic in `tlsf::Heap::used`.
13-
- Work around https://github.com/yvt/rlsf/pull/21 by aligning block before passing it
14-
to `Tlsf::insert_free_block_ptr`.
13+
- Fix panic in `tlsf::Heap::free` in case the value returned from `insert_free_block_ptr`
14+
does not conver the full memory range passed in.
1515

1616
## [v0.7.0] - 2026-01-03
1717

src/tlsf.rs

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -76,37 +76,23 @@ impl Heap {
7676
critical_section::with(|cs| {
7777
let mut heap = self.heap.borrow_ref_mut(cs);
7878
assert!(!heap.initialized);
79-
// Work around https://github.com/yvt/rlsf/pull/21 by aligning block before passing
80-
// it to `Tlsf::insert_free_block_ptr`.
81-
if let Some((aligned_start_addr, usable_size)) = Self::align(start_addr, size) {
79+
let block: NonNull<[u8]> =
80+
NonNull::slice_from_raw_parts(NonNull::new_unchecked(start_addr as *mut u8), size);
81+
if let Some(actual_size) = heap.tlsf.insert_free_block_ptr(block) {
8282
let block: NonNull<[u8]> = NonNull::slice_from_raw_parts(
83-
NonNull::new_unchecked(aligned_start_addr as *mut u8),
84-
usable_size,
83+
NonNull::new_unchecked(start_addr as *mut u8),
84+
actual_size.get(),
8585
);
86-
if heap.tlsf.insert_free_block_ptr(block).is_some() {
87-
heap.initialized = true;
88-
heap.raw_block = Some(block);
89-
heap.raw_block_size = size;
90-
}
86+
heap.initialized = true;
87+
heap.raw_block = Some(block);
88+
heap.raw_block_size = size;
9189
}
9290
if !heap.initialized {
9391
panic!("Allocation too small for heap");
9492
}
9593
});
9694
}
9795

98-
/// Align `start_addr` to `rlsf::GRANULARITY` and make
99-
/// `size` a multiple of `2*rlsf::GRANULARITY`.
100-
fn align(start_addr: usize, size: usize) -> Option<(usize, usize)> {
101-
let align_offset: usize = (start_addr as *const u8).align_offset(rlsf::GRANULARITY);
102-
if align_offset >= size {
103-
return None;
104-
}
105-
let reduced_size: usize = size - align_offset;
106-
let usable_size: usize = reduced_size - (reduced_size % (rlsf::GRANULARITY * 2));
107-
Some((start_addr + align_offset, usable_size))
108-
}
109-
11096
fn alloc(&self, layout: Layout) -> Option<NonNull<u8>> {
11197
critical_section::with(|cs| self.heap.borrow_ref_mut(cs).tlsf.allocate(layout))
11298
}

0 commit comments

Comments
 (0)