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 <[email protected]> Co-developed-by: Alex Gaynor <[email protected]> Signed-off-by: Alex Gaynor <[email protected]> Co-developed-by: Wedson Almeida Filho <[email protected]> Signed-off-by: Wedson Almeida Filho <[email protected]> Co-developed-by: Gary Guo <[email protected]> Signed-off-by: Gary Guo <[email protected]> Co-developed-by: Matthew Bakhtiari <[email protected]> Signed-off-by: Matthew Bakhtiari <[email protected]> Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 753dece commit 057b8d2

14 files changed

+100
-1
lines changed

rust/alloc/README.md

+33
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

+2
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

+3-1
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

+4
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

+2
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

+8
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

+9
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

+2
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

+2
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

+2
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};

rust/alloc/vec/into_iter.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// SPDX-License-Identifier: Apache-2.0 OR MIT
2+
13
#[cfg(not(no_global_oom_handling))]
24
use super::AsVecIntoIter;
35
use crate::alloc::{Allocator, Global};
@@ -9,6 +11,7 @@ use core::iter::{
911
};
1012
use core::marker::PhantomData;
1113
use core::mem::{self, ManuallyDrop};
14+
#[cfg(not(no_global_oom_handling))]
1215
use core::ops::Deref;
1316
use core::ptr::{self, NonNull};
1417
use core::slice::{self};
@@ -123,6 +126,7 @@ impl<T, A: Allocator> IntoIter<T, A> {
123126
}
124127

125128
/// Forgets to Drop the remaining elements while still allowing the backing allocation to be freed.
129+
#[allow(dead_code)]
126130
pub(crate) fn forget_remaining_elements(&mut self) {
127131
self.ptr = self.end;
128132
}

rust/alloc/vec/is_zero.rs

+2
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::boxed::Box;
24

35
#[rustc_specialization_trait]

rust/alloc/vec/mod.rs

+25
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// SPDX-License-Identifier: Apache-2.0 OR MIT
2+
13
//! A contiguous growable array type with heap-allocated contents, written
24
//! `Vec<T>`.
35
//!
@@ -1739,6 +1741,29 @@ impl<T, A: Allocator> Vec<T, A> {
17391741
}
17401742
}
17411743

1744+
/// Tries to append an element to the back of a collection.
1745+
///
1746+
/// # Examples
1747+
///
1748+
/// ```
1749+
/// let mut vec = vec![1, 2];
1750+
/// vec.try_push(3).unwrap();
1751+
/// assert_eq!(vec, [1, 2, 3]);
1752+
/// ```
1753+
#[inline]
1754+
#[stable(feature = "kernel", since = "1.0.0")]
1755+
pub fn try_push(&mut self, value: T) -> Result<(), TryReserveError> {
1756+
if self.len == self.buf.capacity() {
1757+
self.buf.try_reserve_for_push(self.len)?;
1758+
}
1759+
unsafe {
1760+
let end = self.as_mut_ptr().add(self.len);
1761+
ptr::write(end, value);
1762+
self.len += 1;
1763+
}
1764+
Ok(())
1765+
}
1766+
17421767
/// Removes the last element from a vector and returns it, or [`None`] if it
17431768
/// is empty.
17441769
///

rust/alloc/vec/partial_eq.rs

+2
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;
24
#[cfg(not(no_global_oom_handling))]
35
use crate::borrow::Cow;

0 commit comments

Comments
 (0)