From 9e896c0ac2c823d7f20fc0bcb516f9ddeb0e969b Mon Sep 17 00:00:00 2001 From: Victorien Elvinger Date: Wed, 25 Feb 2026 10:40:40 +0100 Subject: [PATCH 1/4] 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")] From 3e62af2fcd1fd66a17b0d9cfebf69a35d1a71a97 Mon Sep 17 00:00:00 2001 From: Victorien Elvinger Date: Tue, 3 Mar 2026 13:20:45 +0100 Subject: [PATCH 2/4] typo fix --- src/tlsf.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/tlsf.rs b/src/tlsf.rs index ba2093a..1702144 100644 --- a/src/tlsf.rs +++ b/src/tlsf.rs @@ -155,8 +155,8 @@ unsafe impl GlobalAlloc for Heap { } 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 + // SAFETY: `layout.align()` is a power of two, + // and the size precondition 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) From bf217b376f358a1bb31280da8820ede6dd8aad66 Mon Sep 17 00:00:00 2001 From: Victorien Elvinger Date: Tue, 3 Mar 2026 16:30:55 +0100 Subject: [PATCH 3/4] Inline comment --- src/tlsf.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/tlsf.rs b/src/tlsf.rs index 1702144..35b3698 100644 --- a/src/tlsf.rs +++ b/src/tlsf.rs @@ -155,8 +155,7 @@ unsafe impl GlobalAlloc for Heap { } 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 precondition is upheld by the caller. + // SAFETY: `layout.align()` is a power of two, and the size precondition 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) From f072c3cbf7b6caf6fa6888e1ce788b4e94a8d1f7 Mon Sep 17 00:00:00 2001 From: Victorien Elvinger Date: Tue, 3 Mar 2026 18:42:03 +0100 Subject: [PATCH 4/4] Revert to multilien comments --- src/tlsf.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/tlsf.rs b/src/tlsf.rs index 35b3698..1ce12dc 100644 --- a/src/tlsf.rs +++ b/src/tlsf.rs @@ -155,7 +155,8 @@ unsafe impl GlobalAlloc for Heap { } 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 precondition is upheld by the caller. + // SAFETY: `layout.align()` is a power of two, and the size precondition + // 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)