From 9e896c0ac2c823d7f20fc0bcb516f9ddeb0e969b Mon Sep 17 00:00:00 2001 From: Victorien Elvinger Date: Wed, 25 Feb 2026 10:40:40 +0100 Subject: [PATCH] fix(tlsf): forward realloc to tlsf --- src/tlsf.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/tlsf.rs b/src/tlsf.rs index d77a675..ba2093a 100644 --- a/src/tlsf.rs +++ b/src/tlsf.rs @@ -104,6 +104,15 @@ impl Heap { }) } + unsafe fn realloc(&self, ptr: *mut u8, new_layout: Layout) -> Option> { + critical_section::with(|cs| { + self.heap + .borrow_ref_mut(cs) + .tlsf + .reallocate(NonNull::new_unchecked(ptr), new_layout) + }) + } + /// Get the amount of bytes used by the allocator. pub fn used(&self) -> usize { critical_section::with(|cs| { @@ -144,6 +153,15 @@ unsafe impl GlobalAlloc for Heap { unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { self.dealloc(ptr, layout) } + + unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 { + // SAFETY: `layout.align()` is a power of two, and the size preconidtion + // is upheld by the caller + let new_layout = + unsafe { core::alloc::Layout::from_size_align_unchecked(new_size, layout.align()) }; + self.realloc(ptr, new_layout) + .map_or(ptr::null_mut(), |allocation| allocation.as_ptr()) + } } #[cfg(feature = "allocator_api")]