Skip to content

Commit ac17cc1

Browse files
committed
Provide a From<AllocError> impl for KernelError
This makes the try_alloc functions trivial, so we inline them.
1 parent 614dd86 commit ac17cc1

File tree

5 files changed

+13
-20
lines changed

5 files changed

+13
-20
lines changed

drivers/char/rust_example/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// SPDX-License-Identifier: GPL-2.0
22

33
#![no_std]
4-
#![feature(global_asm)]
4+
#![feature(allocator_api, global_asm)]
55

66
extern crate alloc;
77

88
use alloc::boxed::Box;
99
use core::pin::Pin;
1010
use kernel::prelude::*;
11-
use kernel::{cstr, file_operations::FileOperations, miscdev, try_alloc};
11+
use kernel::{cstr, file_operations::FileOperations, miscdev};
1212

1313
module! {
1414
type: RustExample,
@@ -37,7 +37,7 @@ impl FileOperations for RustFile {
3737

3838
fn open() -> KernelResult<Self::Wrapper> {
3939
println!("rust file was opened!");
40-
try_alloc(Self)
40+
Ok(Box::try_new(Self)?)
4141
}
4242
}
4343

rust/kernel/src/error.rs

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
use core::num::TryFromIntError;
44

5+
use alloc::alloc::AllocError;
6+
57
use crate::bindings;
68
use crate::c_types;
79

@@ -30,3 +32,9 @@ impl From<TryFromIntError> for Error {
3032
}
3133

3234
pub type KernelResult<T> = Result<T, Error>;
35+
36+
impl From<AllocError> for Error {
37+
fn from(_: AllocError) -> Error {
38+
Error::ENOMEM
39+
}
40+
}

rust/kernel/src/lib.rs

-14
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ compile_error!("Missing kernel configuration for conditional compilation");
1212

1313
extern crate alloc;
1414

15-
use alloc::boxed::Box;
1615
use core::panic::PanicInfo;
17-
use core::pin::Pin;
1816

1917
mod allocator;
2018
pub mod bindings;
@@ -59,15 +57,3 @@ fn panic(_info: &PanicInfo) -> ! {
5957

6058
#[global_allocator]
6159
static ALLOCATOR: allocator::KernelAllocator = allocator::KernelAllocator;
62-
63-
/// Attempts to allocate memory for `value` using the global allocator. On success, `value` is
64-
/// moved into it and returned to the caller wrapped in a `Box`.
65-
pub fn try_alloc<T>(value: T) -> KernelResult<Box<T>> {
66-
Box::try_new(value).map_err(|_| Error::ENOMEM)
67-
}
68-
69-
/// Attempts to allocate memory for `value` using the global allocator. On success, `value` is
70-
/// moved into it and returned to the caller wrapped in a pinned `Box`.
71-
pub fn try_alloc_pinned<T>(value: T) -> KernelResult<Pin<Box<T>>> {
72-
Ok(Pin::from(try_alloc(value)?))
73-
}

rust/kernel/src/miscdev.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl Registration {
2828
name: CStr<'static>,
2929
minor: Option<i32>,
3030
) -> KernelResult<Pin<Box<Self>>> {
31-
let mut r = crate::try_alloc_pinned(Self::new())?;
31+
let mut r = Pin::from(Box::try_new(Self::new())?);
3232
r.as_mut().register::<T>(name, minor)?;
3333
Ok(r)
3434
}

rust/kernel/src/sysctl.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use core::sync::atomic;
99
use crate::bindings;
1010
use crate::c_types;
1111
use crate::error;
12-
use crate::try_alloc;
1312
use crate::types;
1413
use crate::user_ptr::{UserSlicePtr, UserSlicePtrWriter};
1514

@@ -130,7 +129,7 @@ impl<T: SysctlStorage> Sysctl<T> {
130129
return Err(error::Error::EINVAL);
131130
}
132131

133-
let storage = try_alloc(storage)?;
132+
let storage = Box::try_new(storage)?;
134133
let mut table = vec![
135134
bindings::ctl_table {
136135
procname: name.as_ptr() as *const i8,

0 commit comments

Comments
 (0)