rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] rust: alloc: Add realloc and alloc_zeroed to the GlobalAlloc impl
@ 2023-06-22 19:24 Björn Roy Baron via B4 Relay
  2023-06-23  1:07 ` Martin Rodriguez Reboredo
  2023-06-23 17:35 ` Boqun Feng
  0 siblings, 2 replies; 6+ messages in thread
From: Björn Roy Baron via B4 Relay @ 2023-06-22 19:24 UTC (permalink / raw)
  To: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Boqun Feng,
	Gary Guo, Benno Lossin
  Cc: rust-for-linux, linux-kernel, Björn Roy Baron

From: Björn Roy Baron <bjorn3_gh@protonmail.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@protonmail.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@protonmail.com>


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-06-25 23:14 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-06-22 19:24 [PATCH] rust: alloc: Add realloc and alloc_zeroed to the GlobalAlloc impl Björn Roy Baron via B4 Relay
2023-06-23  1:07 ` Martin Rodriguez Reboredo
2023-06-23 17:35 ` Boqun Feng
2023-06-24 14:40   ` Björn Roy Baron
2023-06-25 23:13     ` Boqun Feng
2023-06-25 20:39   ` Gary Guo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).