[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-Id: <20230622-global_alloc_methods-v1-1-3d3561593e23@protonmail.com>
Date: Thu, 22 Jun 2023 21:24:40 +0200
From: Björn Roy Baron via B4 Relay
<devnull+bjorn3_gh.protonmail.com@...nel.org>
To: Miguel Ojeda <ojeda@...nel.org>,
Alex Gaynor <alex.gaynor@...il.com>,
Wedson Almeida Filho <wedsonaf@...il.com>,
Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>,
Benno Lossin <benno.lossin@...ton.me>
Cc: rust-for-linux@...r.kernel.org, linux-kernel@...r.kernel.org,
Björn Roy Baron <bjorn3_gh@...tonmail.com>
Subject: [PATCH] rust: alloc: Add realloc and alloc_zeroed to the
GlobalAlloc impl
From: Björn Roy Baron <bjorn3_gh@...tonmail.com>
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@...tonmail.com>
---
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@...tonmail.com>
Powered by blists - more mailing lists