summaryrefslogtreecommitdiffstats
path: root/rust/kernel/alloc.rs
diff options
context:
space:
mode:
authorDanilo Krummrich <dakr@kernel.org>2024-10-04 17:41:08 +0200
committerMiguel Ojeda <ojeda@kernel.org>2024-10-15 22:56:27 +0200
commit8a799831fc63c988eec90d334fdd68ff5f2c7eb5 (patch)
tree17d05292dd8d54f2b73a45c94bec464f0e118bf8 /rust/kernel/alloc.rs
parent941e65531446c1eb5d573c5d30172117ebe96112 (diff)
downloadlinux-8a799831fc63c988eec90d334fdd68ff5f2c7eb5.tar.gz
linux-8a799831fc63c988eec90d334fdd68ff5f2c7eb5.tar.bz2
linux-8a799831fc63c988eec90d334fdd68ff5f2c7eb5.zip
rust: alloc: implement `ReallocFunc`
`ReallocFunc` is an abstraction for the kernel's realloc derivates, such as `krealloc`, `vrealloc` and `kvrealloc`. All of the named functions share the same function signature and implement the same semantics. The `ReallocFunc` abstractions provides a generalized wrapper around those, to trivialize the implementation of `Kmalloc`, `Vmalloc` and `KVmalloc` in subsequent patches. Reviewed-by: Benno Lossin <benno.lossin@proton.me> Reviewed-by: Gary Guo <gary@garyguo.net> Signed-off-by: Danilo Krummrich <dakr@kernel.org> Link: https://lore.kernel.org/r/20241004154149.93856-5-dakr@kernel.org [ Added temporary `allow(dead_code)` for `dangling_from_layout` to clean warning in `rusttest` target as discussed in the list (but it is needed earlier, i.e. in this patch already). Added colon. - Miguel ] Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
Diffstat (limited to 'rust/kernel/alloc.rs')
-rw-r--r--rust/kernel/alloc.rs9
1 files changed, 9 insertions, 0 deletions
diff --git a/rust/kernel/alloc.rs b/rust/kernel/alloc.rs
index 998779cc6976..95d402feb6ff 100644
--- a/rust/kernel/alloc.rs
+++ b/rust/kernel/alloc.rs
@@ -187,3 +187,12 @@ pub unsafe trait Allocator {
let _ = unsafe { Self::realloc(Some(ptr), Layout::new::<()>(), layout, Flags(0)) };
}
}
+
+#[allow(dead_code)]
+/// Returns a properly aligned dangling pointer from the given `layout`.
+pub(crate) fn dangling_from_layout(layout: Layout) -> NonNull<u8> {
+ let ptr = layout.align() as *mut u8;
+
+ // SAFETY: `layout.align()` (and hence `ptr`) is guaranteed to be non-zero.
+ unsafe { NonNull::new_unchecked(ptr) }
+}