[PATCH] rust: alloc: Add realloc and alloc_zeroed to the GlobalAlloc impl

From: Björn Roy Baron via B4 Relay
Date: Thu Jun 22 2023 - 15:25:28 EST


From: Björn Roy Baron <bjorn3_gh@xxxxxxxxxxxxxx>

While there are default impls for these methods, using the respective C
api's is faster. Currently neither the existing nor these new
GlobalAlloc method implementations are actually called. Instead the
__rust_* function defined below the GlobalAlloc impl are used. With
rustc 1.71 these functions will be gone and all allocation calls will go
through the GlobalAlloc implementation.

Link: https://github.com/Rust-for-Linux/linux/issues/68
Signed-off-by: Björn Roy Baron <bjorn3_gh@xxxxxxxxxxxxxx>
---
rust/kernel/allocator.rs | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)

diff --git a/rust/kernel/allocator.rs b/rust/kernel/allocator.rs
index 397a3dd57a9b..e0a27b1326b5 100644
--- a/rust/kernel/allocator.rs
+++ b/rust/kernel/allocator.rs
@@ -21,6 +21,26 @@ unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
bindings::kfree(ptr as *const core::ffi::c_void);
}
}
+
+ unsafe fn realloc(&self, ptr: *mut u8, _layout: Layout, new_size: usize) -> *mut u8 {
+ unsafe {
+ bindings::krealloc(
+ ptr as *const core::ffi::c_void,
+ new_size,
+ bindings::GFP_KERNEL,
+ ) as *mut u8
+ }
+ }
+
+ unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
+ unsafe {
+ bindings::krealloc(
+ core::ptr::null(),
+ layout.size(),
+ bindings::GFP_KERNEL | bindings::__GFP_ZERO,
+ ) as *mut u8
+ }
+ }
}

#[global_allocator]

---
base-commit: d2e3115d717197cb2bc020dd1f06b06538474ac3
change-id: 20230622-global_alloc_methods-abf5b5e38dba

Best regards,
--
Björn Roy Baron <bjorn3_gh@xxxxxxxxxxxxxx>