mirror of
https://github.com/torvalds/linux.git
synced 2026-10-07 05:31:08 -04:00
The `kernel` crate currently includes all the abstractions that wrap kernel features written in C. These abstractions call the C side of the kernel via the generated bindings with the `bindgen` tool. Modules developed in Rust should never call the bindings themselves. In the future, as the abstractions grow in number, we may need to split this crate into several, possibly following a similar subdivision in subsystems as the kernel itself and/or moving the code to the actual subsystems. Reviewed-by: Greg Kroah-Hartman <[email protected]> Co-developed-by: Alex Gaynor <[email protected]> Signed-off-by: Alex Gaynor <[email protected]> Co-developed-by: Geoffrey Thomas <[email protected]> Signed-off-by: Geoffrey Thomas <[email protected]> Co-developed-by: Finn Behrens <[email protected]> Signed-off-by: Finn Behrens <[email protected]> Co-developed-by: Adam Bratschi-Kaye <[email protected]> Signed-off-by: Adam Bratschi-Kaye <[email protected]> Co-developed-by: Sven Van Asbroeck <[email protected]> Signed-off-by: Sven Van Asbroeck <[email protected]> Co-developed-by: Gary Guo <[email protected]> Signed-off-by: Gary Guo <[email protected]> Co-developed-by: Boris-Chengbiao Zhou <[email protected]> Signed-off-by: Boris-Chengbiao Zhou <[email protected]> Co-developed-by: Boqun Feng <[email protected]> Signed-off-by: Boqun Feng <[email protected]> Co-developed-by: Fox Chen <[email protected]> Signed-off-by: Fox Chen <[email protected]> Co-developed-by: Viktor Garske <[email protected]> Signed-off-by: Viktor Garske <[email protected]> Co-developed-by: Dariusz Sosnowski <[email protected]> Signed-off-by: Dariusz Sosnowski <[email protected]> Co-developed-by: Léo Lanteri Thauvin <[email protected]> Signed-off-by: Léo Lanteri Thauvin <[email protected]> Co-developed-by: Niklas Mohrin <[email protected]> Signed-off-by: Niklas Mohrin <[email protected]> Co-developed-by: Milan Landaverde <[email protected]> Signed-off-by: Milan Landaverde <[email protected]> Co-developed-by: Morgan Bartlett <[email protected]> Signed-off-by: Morgan Bartlett <[email protected]> Co-developed-by: Maciej Falkowski <[email protected]> Signed-off-by: Maciej Falkowski <[email protected]> Co-developed-by: Nándor István Krácser <[email protected]> Signed-off-by: Nándor István Krácser <[email protected]> Co-developed-by: David Gow <[email protected]> Signed-off-by: David Gow <[email protected]> Co-developed-by: John Baublitz <[email protected]> Signed-off-by: John Baublitz <[email protected]> Co-developed-by: Björn Roy Baron <[email protected]> Signed-off-by: Björn Roy Baron <[email protected]> Signed-off-by: Wedson Almeida Filho <[email protected]> Co-developed-by: Miguel Ojeda <[email protected]> Signed-off-by: Miguel Ojeda <[email protected]>
65 lines
1.8 KiB
Rust
65 lines
1.8 KiB
Rust
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
//! Allocator support.
|
|
|
|
use core::alloc::{GlobalAlloc, Layout};
|
|
use core::ptr;
|
|
|
|
use crate::bindings;
|
|
|
|
struct KernelAllocator;
|
|
|
|
unsafe impl GlobalAlloc for KernelAllocator {
|
|
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
|
// `krealloc()` is used instead of `kmalloc()` because the latter is
|
|
// an inline function and cannot be bound to as a result.
|
|
unsafe { bindings::krealloc(ptr::null(), layout.size(), bindings::GFP_KERNEL) as *mut u8 }
|
|
}
|
|
|
|
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
|
|
unsafe {
|
|
bindings::kfree(ptr as *const core::ffi::c_void);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[global_allocator]
|
|
static ALLOCATOR: KernelAllocator = KernelAllocator;
|
|
|
|
// `rustc` only generates these for some crate types. Even then, we would need
|
|
// to extract the object file that has them from the archive. For the moment,
|
|
// let's generate them ourselves instead.
|
|
//
|
|
// Note that `#[no_mangle]` implies exported too, nowadays.
|
|
#[no_mangle]
|
|
fn __rust_alloc(size: usize, _align: usize) -> *mut u8 {
|
|
unsafe { bindings::krealloc(core::ptr::null(), size, bindings::GFP_KERNEL) as *mut u8 }
|
|
}
|
|
|
|
#[no_mangle]
|
|
fn __rust_dealloc(ptr: *mut u8, _size: usize, _align: usize) {
|
|
unsafe { bindings::kfree(ptr as *const core::ffi::c_void) };
|
|
}
|
|
|
|
#[no_mangle]
|
|
fn __rust_realloc(ptr: *mut u8, _old_size: usize, _align: usize, new_size: usize) -> *mut u8 {
|
|
unsafe {
|
|
bindings::krealloc(
|
|
ptr as *const core::ffi::c_void,
|
|
new_size,
|
|
bindings::GFP_KERNEL,
|
|
) as *mut u8
|
|
}
|
|
}
|
|
|
|
#[no_mangle]
|
|
fn __rust_alloc_zeroed(size: usize, _align: usize) -> *mut u8 {
|
|
unsafe {
|
|
bindings::krealloc(
|
|
core::ptr::null(),
|
|
size,
|
|
bindings::GFP_KERNEL | bindings::__GFP_ZERO,
|
|
) as *mut u8
|
|
}
|
|
}
|