Skip to content

Commit 5a888c2

Browse files
Danilo Krummrichojeda
authored andcommitted
rust: alloc: add module allocator_test
`Allocator`s, such as `Kmalloc`, will be used by e.g. `Box` and `Vec` in subsequent patches, and hence this dependency propagates throughout the whole kernel. Add the `allocator_test` module that provides an empty implementation for all `Allocator`s in the kernel, such that we don't break the `rusttest` make target in subsequent patches. Reviewed-by: Alice Ryhl <aliceryhl@google.com> 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-8-dakr@kernel.org [ Added missing `_old_layout` parameter as discussed. - Miguel ] Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent a34822d commit 5a888c2

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

rust/kernel/alloc.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22

33
//! Extensions to the [`alloc`] crate.
44
5-
#[cfg(not(test))]
6-
#[cfg(not(testlib))]
5+
#[cfg(not(any(test, testlib)))]
76
pub mod allocator;
87
pub mod box_ext;
98
pub mod vec_ext;
109

10+
#[cfg(any(test, testlib))]
11+
pub mod allocator_test;
12+
13+
#[cfg(any(test, testlib))]
14+
pub use self::allocator_test as allocator;
15+
1116
/// Indicates an allocation error.
1217
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
1318
pub struct AllocError;

rust/kernel/alloc/allocator_test.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#![allow(missing_docs)]
4+
5+
use super::{AllocError, Allocator, Flags};
6+
use core::alloc::Layout;
7+
use core::ptr::NonNull;
8+
9+
pub struct Kmalloc;
10+
11+
unsafe impl Allocator for Kmalloc {
12+
unsafe fn realloc(
13+
_ptr: Option<NonNull<u8>>,
14+
_layout: Layout,
15+
_old_layout: Layout,
16+
_flags: Flags,
17+
) -> Result<NonNull<[u8]>, AllocError> {
18+
panic!();
19+
}
20+
}

0 commit comments

Comments
 (0)