Skip to content

Commit 614dd86

Browse files
authored
Merge pull request #63 from Rust-for-Linux/nightly-try-alloc
Use new nightly function for fallible allocation
2 parents 00c1ce0 + ac14a94 commit 614dd86

File tree

3 files changed

+3
-24
lines changed

3 files changed

+3
-24
lines changed

.github/workflows/ci.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
- run: sudo add-apt-repository 'deb http://apt.llvm.org/focal/ llvm-toolchain-focal-11 main'
2222
- run: sudo apt-get update -y
2323
- run: sudo apt-get install -y clang-11 libelf-dev qemu-system-x86 busybox-static
24-
- run: rustup default nightly-2020-08-27
24+
- run: rustup default nightly-2021-01-02
2525
- run: rustup component add rustfmt
2626
- run: rustup component add rust-src
2727

Documentation/rust/quick-start.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ rustc and cargo
1313
***************
1414

1515
A recent nightly Rust toolchain (with, at least, ``rustc`` and ``cargo``)
16-
is required, e.g. ``nightly-2020-08-27``. In the future, this restriction
16+
is required, e.g. ``nightly-2021-01-02``. In the future, this restriction
1717
will be lifted.
1818

1919
If you are using ``rustup``, run::

rust/kernel/src/lib.rs

+1-22
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@ compile_error!("Missing kernel configuration for conditional compilation");
1313
extern crate alloc;
1414

1515
use alloc::boxed::Box;
16-
use core::alloc::Layout;
17-
use core::mem::MaybeUninit;
1816
use core::panic::PanicInfo;
1917
use core::pin::Pin;
20-
use core::ptr::NonNull;
2118

2219
mod allocator;
2320
pub mod bindings;
@@ -66,25 +63,7 @@ static ALLOCATOR: allocator::KernelAllocator = allocator::KernelAllocator;
6663
/// Attempts to allocate memory for `value` using the global allocator. On success, `value` is
6764
/// moved into it and returned to the caller wrapped in a `Box`.
6865
pub fn try_alloc<T>(value: T) -> KernelResult<Box<T>> {
69-
let layout = Layout::new::<MaybeUninit<T>>();
70-
let ptr: NonNull<MaybeUninit<T>> = if layout.size() == 0 {
71-
NonNull::dangling()
72-
// SAFETY: We checked that the layout size is nonzero.
73-
} else if let Some(nn) = NonNull::new(unsafe { alloc::alloc::alloc(layout) }) {
74-
nn.cast()
75-
} else {
76-
return Err(Error::ENOMEM);
77-
};
78-
79-
unsafe {
80-
// SAFETY: `ptr` was just allocated and isn't used afterwards.
81-
let mut b = Box::from_raw(ptr.as_ptr());
82-
// SAFETY: The pointer is valid for write and is properly aligned. The dangling pointer
83-
// case is only when the size of the value is zero; writing zero bytes to it is allowed.
84-
b.as_mut_ptr().write(value);
85-
// SAFETY: The value was initialised in the call above.
86-
Ok(Box::from_raw(Box::into_raw(b) as *mut T))
87-
}
66+
Box::try_new(value).map_err(|_| Error::ENOMEM)
8867
}
8968

9069
/// Attempts to allocate memory for `value` using the global allocator. On success, `value` is

0 commit comments

Comments
 (0)