public inbox for rust-for-linux@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] rust: alloc: add test for realloc with zero size acting as free
@ 2026-04-24  5:29 Hsiu Che Yu
  0 siblings, 0 replies; only message in thread
From: Hsiu Che Yu @ 2026-04-24  5:29 UTC (permalink / raw)
  To: Danilo Krummrich
  Cc: Hsiu Che Yu, Lorenzo Stoakes, Vlastimil Babka, Liam R. Howlett,
	Uladzislau Rezki, Miguel Ojeda, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, rust-for-linux, linux-kernel

The `Allocator` trait documents that reallocating with a target size of
zero must be equivalent to freeing the memory:

    "If the requested size is zero, `realloc` behaves equivalent
    to `free`."

Add unit test to verify that `KMalloc`, `Vmalloc`, and `KVmalloc` all
conform to this contract.

Signed-off-by: Hsiu Che Yu <yu.whisper.personal@gmail.com>
---
 rust/kernel/alloc/allocator.rs | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs
index 63bfb91b3671..6120587db205 100644
--- a/rust/kernel/alloc/allocator.rs
+++ b/rust/kernel/alloc/allocator.rs
@@ -305,4 +305,23 @@ fn is_aligned_to(&self, align: usize) -> bool {
 
         Ok(())
     }
+
+    #[test]
+    fn test_realloc_zero_size_is_free() -> Result {
+        const INIT_SIZE: usize = 64;
+
+        let old = Layout::from_size_align(INIT_SIZE, 1).unwrap();
+        let new = Layout::from_size_align(0, 1).unwrap();
+
+        let ptr = unsafe { Kmalloc::realloc(None, new, old, GFP_KERNEL, NumaNode::NO_NODE)? };
+        assert_eq!(ptr.len(), 0);
+
+        let ptr = unsafe { Vmalloc::realloc(None, new, old, GFP_KERNEL, NumaNode::NO_NODE)? };
+        assert_eq!(ptr.len(), 0);
+
+        let ptr = unsafe { KVmalloc::realloc(None, new, old, GFP_KERNEL, NumaNode::NO_NODE)? };
+        assert_eq!(ptr.len(), 0);
+
+        Ok(())
+    }
 }
-- 
2.43.0


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-04-24  5:30 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-24  5:29 [PATCH] rust: alloc: add test for realloc with zero size acting as free Hsiu Che Yu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox