Skip to content

Commit 2821ff3

Browse files
bjorn3intel-lab-lkp
authored andcommitted
rust: alloc: Add realloc and alloc_zeroed to the GlobalAlloc impl
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: Rust-for-Linux#68 Signed-off-by: Björn Roy Baron <bjorn3_gh@protonmail.com>
1 parent d2e3115 commit 2821ff3

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

rust/kernel/allocator.rs

+20
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,26 @@ unsafe impl GlobalAlloc for KernelAllocator {
2121
bindings::kfree(ptr as *const core::ffi::c_void);
2222
}
2323
}
24+
25+
unsafe fn realloc(&self, ptr: *mut u8, _layout: Layout, new_size: usize) -> *mut u8 {
26+
unsafe {
27+
bindings::krealloc(
28+
ptr as *const core::ffi::c_void,
29+
new_size,
30+
bindings::GFP_KERNEL,
31+
) as *mut u8
32+
}
33+
}
34+
35+
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
36+
unsafe {
37+
bindings::krealloc(
38+
core::ptr::null(),
39+
layout.size(),
40+
bindings::GFP_KERNEL | bindings::__GFP_ZERO,
41+
) as *mut u8
42+
}
43+
}
2444
}
2545

2646
#[global_allocator]

0 commit comments

Comments
 (0)