Skip to content

Commit 3cd202c

Browse files
committed
Merge tag 'alloc-7.2-rc1' of https://github.com/Rust-for-Linux/linux into rust-next
Pull alloc updates from Danilo Krummrich: - Fix the 'Vec::reserve()' doctest to properly account for the existing vector length in the capacity assertion. - Fix an incorrect operator in the 'Vec::extend_with()' SAFETY comment; add a doc test demonstrating basic usage and the zero-length case. - Cleanup all imports in the alloc module and its doctests to use the "kernel vertical" import style. * tag 'alloc-7.2-rc1' of https://github.com/Rust-for-Linux/linux: rust: alloc: cleanup doctest imports to "kernel vertical" style rust: alloc: cleanup imports and use "kernel vertical" style rust: alloc: fix `Vec::extend_with` SAFETY comment rust: alloc: add doc test for `Vec::extend_with` rust: alloc: fix assert in `Vec::reserve` doc test
2 parents 72d33b8 + ec51531 commit 3cd202c

7 files changed

Lines changed: 163 additions & 60 deletions

File tree

rust/kernel/alloc.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@ pub use self::kvec::Vec;
2222
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
2323
pub struct AllocError;
2424

25-
use crate::error::{code::EINVAL, Result};
26-
use core::{alloc::Layout, ptr::NonNull};
25+
use crate::prelude::*;
26+
27+
use core::{
28+
alloc::Layout,
29+
ptr::NonNull, //
30+
};
2731

2832
/// Flags to be used when allocating memory.
2933
///

rust/kernel/alloc/allocator.rs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,25 @@
88
//!
99
//! Reference: <https://docs.kernel.org/core-api/memory-allocation.html>
1010
11-
use super::Flags;
12-
use core::alloc::Layout;
13-
use core::ptr;
14-
use core::ptr::NonNull;
15-
16-
use crate::alloc::{AllocError, Allocator, NumaNode};
17-
use crate::bindings;
18-
use crate::page;
11+
use super::{
12+
AllocError,
13+
Allocator,
14+
Flags,
15+
NumaNode, //
16+
};
17+
18+
use crate::{
19+
bindings,
20+
page, //
21+
};
22+
23+
use core::{
24+
alloc::Layout,
25+
ptr::{
26+
self,
27+
NonNull, //
28+
}, //
29+
};
1930

2031
const ARCH_KMALLOC_MINALIGN: usize = bindings::ARCH_KMALLOC_MINALIGN;
2132

@@ -163,8 +174,11 @@ impl Vmalloc {
163174
/// # Examples
164175
///
165176
/// ```
166-
/// # use core::ptr::{NonNull, from_mut};
167-
/// # use kernel::{page, prelude::*};
177+
/// # use core::ptr::{
178+
/// # from_mut,
179+
/// # NonNull, //
180+
/// # };
181+
/// # use kernel::page;
168182
/// use kernel::alloc::allocator::Vmalloc;
169183
///
170184
/// let mut vbox = VBox::<[u8; page::PAGE_SIZE]>::new_uninit(GFP_KERNEL)?;

rust/kernel/alloc/allocator/iter.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
// SPDX-License-Identifier: GPL-2.0
22

33
use super::Vmalloc;
4+
45
use crate::page;
5-
use core::marker::PhantomData;
6-
use core::ptr::NonNull;
6+
7+
use core::{
8+
marker::PhantomData,
9+
ptr::NonNull, //
10+
};
711

812
/// An [`Iterator`] of [`page::BorrowedPage`] items owned by a [`Vmalloc`] allocation.
913
///

rust/kernel/alloc/kbox.rs

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,47 @@
33
//! Implementation of [`Box`].
44
55
#[allow(unused_imports)] // Used in doc comments.
6-
use super::allocator::{KVmalloc, Kmalloc, Vmalloc, VmallocPageIter};
7-
use super::{AllocError, Allocator, Flags, NumaNode};
8-
use core::alloc::Layout;
9-
use core::borrow::{Borrow, BorrowMut};
10-
use core::marker::PhantomData;
11-
use core::mem::ManuallyDrop;
12-
use core::mem::MaybeUninit;
13-
use core::ops::{Deref, DerefMut};
14-
use core::pin::Pin;
15-
use core::ptr::NonNull;
16-
use core::result::Result;
17-
18-
use crate::ffi::c_void;
19-
use crate::fmt;
20-
use crate::init::InPlaceInit;
21-
use crate::page::AsPageIter;
22-
use crate::types::ForeignOwnable;
23-
use pin_init::{InPlaceWrite, Init, PinInit, ZeroableOption};
6+
use super::allocator::{
7+
KVmalloc,
8+
Kmalloc,
9+
Vmalloc,
10+
VmallocPageIter, //
11+
};
12+
13+
use super::{
14+
AllocError,
15+
Allocator,
16+
Flags,
17+
NumaNode, //
18+
};
19+
20+
use crate::{
21+
fmt,
22+
page::AsPageIter,
23+
prelude::*,
24+
types::ForeignOwnable, //
25+
};
26+
27+
use core::{
28+
alloc::Layout,
29+
borrow::{
30+
Borrow,
31+
BorrowMut, //
32+
},
33+
marker::PhantomData,
34+
mem::{
35+
ManuallyDrop,
36+
MaybeUninit, //
37+
},
38+
ops::{
39+
Deref,
40+
DerefMut, //
41+
},
42+
ptr::NonNull,
43+
result::Result, //
44+
};
45+
46+
use pin_init::ZeroableOption;
2447

2548
/// The kernel's [`Box`] type -- a heap allocation for a single value of type `T`.
2649
///
@@ -274,7 +297,10 @@ where
274297
/// # Examples
275298
///
276299
/// ```
277-
/// use kernel::sync::{new_spinlock, SpinLock};
300+
/// use kernel::sync::{
301+
/// new_spinlock,
302+
/// SpinLock, //
303+
/// };
278304
///
279305
/// struct Inner {
280306
/// a: u32,
@@ -567,7 +593,6 @@ where
567593
///
568594
/// ```
569595
/// # use core::borrow::Borrow;
570-
/// # use kernel::alloc::KBox;
571596
/// struct Foo<B: Borrow<u32>>(B);
572597
///
573598
/// // Owned instance.
@@ -595,7 +620,6 @@ where
595620
///
596621
/// ```
597622
/// # use core::borrow::BorrowMut;
598-
/// # use kernel::alloc::KBox;
599623
/// struct Foo<B: BorrowMut<u32>>(B);
600624
///
601625
/// // Owned instance.
@@ -660,9 +684,13 @@ where
660684
/// # Examples
661685
///
662686
/// ```
663-
/// # use kernel::prelude::*;
664-
/// use kernel::alloc::allocator::VmallocPageIter;
665-
/// use kernel::page::{AsPageIter, PAGE_SIZE};
687+
/// use kernel::{
688+
/// alloc::allocator::VmallocPageIter,
689+
/// page::{
690+
/// AsPageIter,
691+
/// PAGE_SIZE, //
692+
/// }, //
693+
/// };
666694
///
667695
/// let mut vbox = VBox::new((), GFP_KERNEL)?;
668696
///

rust/kernel/alloc/kvec.rs

Lines changed: 63 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,52 @@
33
//! Implementation of [`Vec`].
44
55
use super::{
6-
allocator::{KVmalloc, Kmalloc, Vmalloc, VmallocPageIter},
6+
allocator::{
7+
KVmalloc,
8+
Kmalloc,
9+
Vmalloc,
10+
VmallocPageIter, //
11+
},
712
layout::ArrayLayout,
8-
AllocError, Allocator, Box, Flags, NumaNode,
13+
AllocError,
14+
Allocator,
15+
Box,
16+
Flags,
17+
NumaNode, //
918
};
19+
1020
use crate::{
1121
fmt,
1222
page::{
1323
AsPageIter,
1424
PAGE_SIZE, //
15-
},
25+
}, //
1626
};
27+
1728
use core::{
18-
borrow::{Borrow, BorrowMut},
29+
borrow::{
30+
Borrow,
31+
BorrowMut, //
32+
},
1933
marker::PhantomData,
20-
mem::{ManuallyDrop, MaybeUninit},
21-
ops::Deref,
22-
ops::DerefMut,
23-
ops::Index,
24-
ops::IndexMut,
25-
ptr,
26-
ptr::NonNull,
27-
slice,
28-
slice::SliceIndex,
34+
mem::{
35+
ManuallyDrop,
36+
MaybeUninit, //
37+
},
38+
ops::{
39+
Deref,
40+
DerefMut,
41+
Index,
42+
IndexMut, //
43+
},
44+
ptr::{
45+
self,
46+
NonNull, //
47+
},
48+
slice::{
49+
self,
50+
SliceIndex, //
51+
}, //
2952
};
3053

3154
mod errors;
@@ -614,7 +637,7 @@ where
614637
///
615638
/// v.reserve(10, GFP_KERNEL)?;
616639
/// let cap = v.capacity();
617-
/// assert!(cap >= 10);
640+
/// assert!(cap >= v.len() + 10);
618641
///
619642
/// v.reserve(10, GFP_KERNEL)?;
620643
/// let new_cap = v.capacity();
@@ -849,6 +872,24 @@ impl<T> Vec<T, KVmalloc> {
849872

850873
impl<T: Clone, A: Allocator> Vec<T, A> {
851874
/// Extend the vector by `n` clones of `value`.
875+
///
876+
/// # Examples
877+
///
878+
/// ```
879+
/// let mut v = KVec::new();
880+
/// v.push(1, GFP_KERNEL)?;
881+
///
882+
/// v.extend_with(3, 5, GFP_KERNEL)?;
883+
/// assert_eq!(&v, &[1, 5, 5, 5]);
884+
///
885+
/// v.extend_with(2, 8, GFP_KERNEL)?;
886+
/// assert_eq!(&v, &[1, 5, 5, 5, 8, 8]);
887+
///
888+
/// v.extend_with(0, 3, GFP_KERNEL)?;
889+
/// assert_eq!(&v, &[1, 5, 5, 5, 8, 8]);
890+
///
891+
/// # Ok::<(), Error>(())
892+
/// ```
852893
pub fn extend_with(&mut self, n: usize, value: T, flags: Flags) -> Result<(), AllocError> {
853894
if n == 0 {
854895
return Ok(());
@@ -866,7 +907,7 @@ impl<T: Clone, A: Allocator> Vec<T, A> {
866907
spare[n - 1].write(value);
867908

868909
// SAFETY:
869-
// - `self.len() + n < self.capacity()` due to the call to reserve above,
910+
// - `self.len() + n <= self.capacity()` due to the call to reserve above,
870911
// - the loop and the line above initialized the next `n` elements.
871912
unsafe { self.inc_len(n) };
872913

@@ -1146,9 +1187,13 @@ where
11461187
/// # Examples
11471188
///
11481189
/// ```
1149-
/// # use kernel::prelude::*;
1150-
/// use kernel::alloc::allocator::VmallocPageIter;
1151-
/// use kernel::page::{AsPageIter, PAGE_SIZE};
1190+
/// use kernel::{
1191+
/// alloc::allocator::VmallocPageIter,
1192+
/// page::{
1193+
/// AsPageIter,
1194+
/// PAGE_SIZE, //
1195+
/// }, //
1196+
/// };
11521197
///
11531198
/// let mut vec = VVec::<u8>::new();
11541199
///

rust/kernel/alloc/kvec/errors.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
//! Errors for the [`Vec`] type.
44
5-
use kernel::fmt;
6-
use kernel::prelude::*;
5+
use crate::{
6+
fmt,
7+
prelude::*, //
8+
};
79

810
/// Error type for [`Vec::push_within_capacity`].
911
pub struct PushError<T>(pub T);

rust/kernel/alloc/layout.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
//!
55
//! Custom layout types extending or improving [`Layout`].
66
7-
use core::{alloc::Layout, marker::PhantomData};
7+
use core::{
8+
alloc::Layout,
9+
marker::PhantomData, //
10+
};
811

912
/// Error when constructing an [`ArrayLayout`].
1013
pub struct LayoutError;
@@ -47,7 +50,10 @@ impl<T> ArrayLayout<T> {
4750
/// # Examples
4851
///
4952
/// ```
50-
/// # use kernel::alloc::layout::{ArrayLayout, LayoutError};
53+
/// # use kernel::alloc::layout::{
54+
/// # ArrayLayout,
55+
/// # LayoutError, //
56+
/// # };
5157
/// let layout = ArrayLayout::<i32>::new(15)?;
5258
/// assert_eq!(layout.len(), 15);
5359
///

0 commit comments

Comments
 (0)