Skip to content

Commit 057b8d2

Browse files
committed
rust: adapt alloc crate to the kernel
This customizes the subset of the Rust standard library `alloc` that was just imported as-is, mainly by: - Adding SPDX license identifiers. - Skipping modules (e.g. `rc` and `sync`) via new `cfg`s. - Adding fallible (`try_*`) versions of existing infallible methods (i.e. returning a `Result` instead of panicking). Since the standard library requires stable/unstable attributes, these additions are annotated with: #[stable(feature = "kernel", since = "1.0.0")] Using "kernel" as the feature allows to have the additions clearly marked. The "1.0.0" version is just a placeholder. (At the moment, only one is needed, but in the future more fallible methods will be added). Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Co-developed-by: Alex Gaynor <alex.gaynor@gmail.com> Signed-off-by: Alex Gaynor <alex.gaynor@gmail.com> Co-developed-by: Wedson Almeida Filho <wedsonaf@google.com> Signed-off-by: Wedson Almeida Filho <wedsonaf@google.com> Co-developed-by: Gary Guo <gary@garyguo.net> Signed-off-by: Gary Guo <gary@garyguo.net> Co-developed-by: Matthew Bakhtiari <dev@mtbk.me> Signed-off-by: Matthew Bakhtiari <dev@mtbk.me> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent 753dece commit 057b8d2

File tree

14 files changed

+100
-1
lines changed

14 files changed

+100
-1
lines changed

rust/alloc/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# `alloc`
2+
3+
These source files come from the Rust standard library, hosted in
4+
the <https://github.com/rust-lang/rust> repository, licensed under
5+
"Apache-2.0 OR MIT" and adapted for kernel use. For copyright details,
6+
see <https://github.com/rust-lang/rust/blob/master/COPYRIGHT>.
7+
8+
Please note that these files should be kept as close as possible to
9+
upstream. In general, only additions should be performed (e.g. new
10+
methods). Eventually, changes should make it into upstream so that,
11+
at some point, this fork can be dropped from the kernel tree.
12+
13+
14+
## Rationale
15+
16+
On one hand, kernel folks wanted to keep `alloc` in-tree to have more
17+
freedom in both workflow and actual features if actually needed
18+
(e.g. receiver types if we ended up using them), which is reasonable.
19+
20+
On the other hand, Rust folks wanted to keep `alloc` as close as
21+
upstream as possible and avoid as much divergence as possible, which
22+
is also reasonable.
23+
24+
We agreed on a middle-ground: we would keep a subset of `alloc`
25+
in-tree that would be as small and as close as possible to upstream.
26+
Then, upstream can start adding the functions that we add to `alloc`
27+
etc., until we reach a point where the kernel already knows exactly
28+
what it needs in `alloc` and all the new methods are merged into
29+
upstream, so that we can drop `alloc` from the kernel tree and go back
30+
to using the upstream one.
31+
32+
By doing this, the kernel can go a bit faster now, and Rust can
33+
slowly incorporate and discuss the changes as needed.

rust/alloc/alloc.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// SPDX-License-Identifier: Apache-2.0 OR MIT
2+
13
//! Memory allocation APIs
24
35
#![stable(feature = "alloc_module", since = "1.28.0")]

rust/alloc/borrow.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// SPDX-License-Identifier: Apache-2.0 OR MIT
2+
13
//! A module for working with borrowed data.
24
35
#![stable(feature = "rust1", since = "1.0.0")]
@@ -11,7 +13,7 @@ use core::ops::{Add, AddAssign};
1113
#[stable(feature = "rust1", since = "1.0.0")]
1214
pub use core::borrow::{Borrow, BorrowMut};
1315

14-
use crate::fmt;
16+
use core::fmt;
1517
#[cfg(not(no_global_oom_handling))]
1618
use crate::string::String;
1719

rust/alloc/boxed.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// SPDX-License-Identifier: Apache-2.0 OR MIT
2+
13
//! A pointer type for heap allocation.
24
//!
35
//! [`Box<T>`], casually referred to as a 'box', provides the simplest form of
@@ -163,9 +165,11 @@ use crate::str::from_boxed_utf8_unchecked;
163165
#[cfg(not(no_global_oom_handling))]
164166
use crate::vec::Vec;
165167

168+
#[cfg(not(no_thin))]
166169
#[unstable(feature = "thin_box", issue = "92791")]
167170
pub use thin::ThinBox;
168171

172+
#[cfg(not(no_thin))]
169173
mod thin;
170174

171175
/// A pointer type for heap allocation.

rust/alloc/collections/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// SPDX-License-Identifier: Apache-2.0 OR MIT
2+
13
//! Collection types.
24
35
#![stable(feature = "rust1", since = "1.0.0")]

rust/alloc/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// SPDX-License-Identifier: Apache-2.0 OR MIT
2+
13
//! # The Rust core allocation and collections library
24
//!
35
//! This library provides smart pointers and collections for managing
@@ -192,6 +194,7 @@ extern crate std;
192194
extern crate test;
193195

194196
// Module with internal macros used by other modules (needs to be included before other modules).
197+
#[cfg(not(no_macros))]
195198
#[macro_use]
196199
mod macros;
197200

@@ -216,11 +219,16 @@ pub mod borrow;
216219
pub mod collections;
217220
#[cfg(not(no_global_oom_handling))]
218221
pub mod ffi;
222+
#[cfg(not(no_fmt))]
219223
pub mod fmt;
224+
#[cfg(not(no_rc))]
220225
pub mod rc;
221226
pub mod slice;
227+
#[cfg(not(no_str))]
222228
pub mod str;
229+
#[cfg(not(no_string))]
223230
pub mod string;
231+
#[cfg(not(no_sync))]
224232
#[cfg(target_has_atomic = "ptr")]
225233
pub mod sync;
226234
#[cfg(all(not(no_global_oom_handling), target_has_atomic = "ptr"))]

rust/alloc/raw_vec.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// SPDX-License-Identifier: Apache-2.0 OR MIT
2+
13
#![unstable(feature = "raw_vec_internals", reason = "unstable const warnings", issue = "none")]
24

35
use core::alloc::LayoutError;
@@ -307,6 +309,12 @@ impl<T, A: Allocator> RawVec<T, A> {
307309
}
308310
}
309311

312+
/// The same as `reserve_for_push`, but returns on errors instead of panicking or aborting.
313+
#[inline(never)]
314+
pub fn try_reserve_for_push(&mut self, len: usize) -> Result<(), TryReserveError> {
315+
self.grow_amortized(len, 1)
316+
}
317+
310318
/// Ensures that the buffer contains at least enough space to hold `len +
311319
/// additional` elements. If it doesn't already, will reallocate the
312320
/// minimum possible amount of memory necessary. Generally this will be
@@ -421,6 +429,7 @@ impl<T, A: Allocator> RawVec<T, A> {
421429
Ok(())
422430
}
423431

432+
#[allow(dead_code)]
424433
fn shrink(&mut self, cap: usize) -> Result<(), TryReserveError> {
425434
assert!(cap <= self.capacity(), "Tried to shrink to a larger capacity");
426435

rust/alloc/slice.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// SPDX-License-Identifier: Apache-2.0 OR MIT
2+
13
//! A dynamically-sized view into a contiguous sequence, `[T]`.
24
//!
35
//! *[See also the slice primitive type](slice).*

rust/alloc/vec/drain.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// SPDX-License-Identifier: Apache-2.0 OR MIT
2+
13
use crate::alloc::{Allocator, Global};
24
use core::fmt;
35
use core::iter::{FusedIterator, TrustedLen};

rust/alloc/vec/drain_filter.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// SPDX-License-Identifier: Apache-2.0 OR MIT
2+
13
use crate::alloc::{Allocator, Global};
24
use core::ptr::{self};
35
use core::slice::{self};

0 commit comments

Comments
 (0)