1 // SPDX-License-Identifier: GPL-2.0
5 use core::alloc::{GlobalAlloc, Layout};
10 struct KernelAllocator;
12 unsafe impl GlobalAlloc for KernelAllocator {
13 unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
14 // `krealloc()` is used instead of `kmalloc()` because the latter is
15 // an inline function and cannot be bound to as a result.
16 unsafe { bindings::krealloc(ptr::null(), layout.size(), bindings::GFP_KERNEL) as *mut u8 }
19 unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
21 bindings::kfree(ptr as *const core::ffi::c_void);
27 static ALLOCATOR: KernelAllocator = KernelAllocator;
29 // `rustc` only generates these for some crate types. Even then, we would need
30 // to extract the object file that has them from the archive. For the moment,
31 // let's generate them ourselves instead.
33 // Note that `#[no_mangle]` implies exported too, nowadays.
35 fn __rust_alloc(size: usize, _align: usize) -> *mut u8 {
36 unsafe { bindings::krealloc(core::ptr::null(), size, bindings::GFP_KERNEL) as *mut u8 }
40 fn __rust_dealloc(ptr: *mut u8, _size: usize, _align: usize) {
41 unsafe { bindings::kfree(ptr as *const core::ffi::c_void) };
45 fn __rust_realloc(ptr: *mut u8, _old_size: usize, _align: usize, new_size: usize) -> *mut u8 {
48 ptr as *const core::ffi::c_void,
56 fn __rust_alloc_zeroed(size: usize, _align: usize) -> *mut u8 {
61 bindings::GFP_KERNEL | bindings::__GFP_ZERO,