mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/chenhuacai/linux-loongson
synced 2025-09-04 02:13:09 +00:00
rust: alloc: implement ReallocFunc
`ReallocFunc` is an abstraction for the kernel's realloc derivates, such as `krealloc`, `vrealloc` and `kvrealloc`. All of the named functions share the same function signature and implement the same semantics. The `ReallocFunc` abstractions provides a generalized wrapper around those, to trivialize the implementation of `Kmalloc`, `Vmalloc` and `KVmalloc` in subsequent patches. Reviewed-by: Benno Lossin <benno.lossin@proton.me> Reviewed-by: Gary Guo <gary@garyguo.net> Signed-off-by: Danilo Krummrich <dakr@kernel.org> Link: https://lore.kernel.org/r/20241004154149.93856-5-dakr@kernel.org [ Added temporary `allow(dead_code)` for `dangling_from_layout` to clean warning in `rusttest` target as discussed in the list (but it is needed earlier, i.e. in this patch already). Added colon. - Miguel ] Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
This commit is contained in:
parent
941e655314
commit
8a799831fc
@ -187,3 +187,12 @@ pub unsafe trait Allocator {
|
|||||||
let _ = unsafe { Self::realloc(Some(ptr), Layout::new::<()>(), layout, Flags(0)) };
|
let _ = unsafe { Self::realloc(Some(ptr), Layout::new::<()>(), layout, Flags(0)) };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
/// Returns a properly aligned dangling pointer from the given `layout`.
|
||||||
|
pub(crate) fn dangling_from_layout(layout: Layout) -> NonNull<u8> {
|
||||||
|
let ptr = layout.align() as *mut u8;
|
||||||
|
|
||||||
|
// SAFETY: `layout.align()` (and hence `ptr`) is guaranteed to be non-zero.
|
||||||
|
unsafe { NonNull::new_unchecked(ptr) }
|
||||||
|
}
|
||||||
|
@ -1,10 +1,20 @@
|
|||||||
// SPDX-License-Identifier: GPL-2.0
|
// SPDX-License-Identifier: GPL-2.0
|
||||||
|
|
||||||
//! Allocator support.
|
//! Allocator support.
|
||||||
|
//!
|
||||||
|
//! Documentation for the kernel's memory allocators can found in the "Memory Allocation Guide"
|
||||||
|
//! linked below. For instance, this includes the concept of "get free page" (GFP) flags and the
|
||||||
|
//! typical application of the different kernel allocators.
|
||||||
|
//!
|
||||||
|
//! Reference: <https://docs.kernel.org/core-api/memory-allocation.html>
|
||||||
|
|
||||||
use super::{flags::*, Flags};
|
use super::{flags::*, Flags};
|
||||||
use core::alloc::{GlobalAlloc, Layout};
|
use core::alloc::{GlobalAlloc, Layout};
|
||||||
use core::ptr;
|
use core::ptr;
|
||||||
|
use core::ptr::NonNull;
|
||||||
|
|
||||||
|
use crate::alloc::AllocError;
|
||||||
|
use crate::bindings;
|
||||||
|
|
||||||
struct Kmalloc;
|
struct Kmalloc;
|
||||||
|
|
||||||
@ -36,6 +46,66 @@ pub(crate) unsafe fn krealloc_aligned(ptr: *mut u8, new_layout: Layout, flags: F
|
|||||||
unsafe { bindings::krealloc(ptr as *const core::ffi::c_void, size, flags.0) as *mut u8 }
|
unsafe { bindings::krealloc(ptr as *const core::ffi::c_void, size, flags.0) as *mut u8 }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// # Invariants
|
||||||
|
///
|
||||||
|
/// One of the following: `krealloc`, `vrealloc`, `kvrealloc`.
|
||||||
|
struct ReallocFunc(
|
||||||
|
unsafe extern "C" fn(*const core::ffi::c_void, usize, u32) -> *mut core::ffi::c_void,
|
||||||
|
);
|
||||||
|
|
||||||
|
#[expect(dead_code)]
|
||||||
|
impl ReallocFunc {
|
||||||
|
/// # Safety
|
||||||
|
///
|
||||||
|
/// This method has the same safety requirements as [`Allocator::realloc`].
|
||||||
|
///
|
||||||
|
/// # Guarantees
|
||||||
|
///
|
||||||
|
/// This method has the same guarantees as `Allocator::realloc`. Additionally
|
||||||
|
/// - it accepts any pointer to a valid memory allocation allocated by this function.
|
||||||
|
/// - memory allocated by this function remains valid until it is passed to this function.
|
||||||
|
unsafe fn call(
|
||||||
|
&self,
|
||||||
|
ptr: Option<NonNull<u8>>,
|
||||||
|
layout: Layout,
|
||||||
|
old_layout: Layout,
|
||||||
|
flags: Flags,
|
||||||
|
) -> Result<NonNull<[u8]>, AllocError> {
|
||||||
|
let size = aligned_size(layout);
|
||||||
|
let ptr = match ptr {
|
||||||
|
Some(ptr) => {
|
||||||
|
if old_layout.size() == 0 {
|
||||||
|
ptr::null()
|
||||||
|
} else {
|
||||||
|
ptr.as_ptr()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => ptr::null(),
|
||||||
|
};
|
||||||
|
|
||||||
|
// SAFETY:
|
||||||
|
// - `self.0` is one of `krealloc`, `vrealloc`, `kvrealloc` and thus only requires that
|
||||||
|
// `ptr` is NULL or valid.
|
||||||
|
// - `ptr` is either NULL or valid by the safety requirements of this function.
|
||||||
|
//
|
||||||
|
// GUARANTEE:
|
||||||
|
// - `self.0` is one of `krealloc`, `vrealloc`, `kvrealloc`.
|
||||||
|
// - Those functions provide the guarantees of this function.
|
||||||
|
let raw_ptr = unsafe {
|
||||||
|
// If `size == 0` and `ptr != NULL` the memory behind the pointer is freed.
|
||||||
|
self.0(ptr.cast(), size, flags.0).cast()
|
||||||
|
};
|
||||||
|
|
||||||
|
let ptr = if size == 0 {
|
||||||
|
crate::alloc::dangling_from_layout(layout)
|
||||||
|
} else {
|
||||||
|
NonNull::new(raw_ptr).ok_or(AllocError)?
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(NonNull::slice_from_raw_parts(ptr, size))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// SAFETY: TODO.
|
// SAFETY: TODO.
|
||||||
unsafe impl GlobalAlloc for Kmalloc {
|
unsafe impl GlobalAlloc for Kmalloc {
|
||||||
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
||||||
|
Loading…
Reference in New Issue
Block a user