mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2026-07-22 03:27:30 -04:00
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
This commit is contained in:
@@ -22,8 +22,12 @@
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
||||
pub struct AllocError;
|
||||
|
||||
use crate::error::{code::EINVAL, Result};
|
||||
use core::{alloc::Layout, ptr::NonNull};
|
||||
use crate::prelude::*;
|
||||
|
||||
use core::{
|
||||
alloc::Layout,
|
||||
ptr::NonNull, //
|
||||
};
|
||||
|
||||
/// Flags to be used when allocating memory.
|
||||
///
|
||||
|
||||
@@ -8,14 +8,25 @@
|
||||
//!
|
||||
//! Reference: <https://docs.kernel.org/core-api/memory-allocation.html>
|
||||
|
||||
use super::Flags;
|
||||
use core::alloc::Layout;
|
||||
use core::ptr;
|
||||
use core::ptr::NonNull;
|
||||
use super::{
|
||||
AllocError,
|
||||
Allocator,
|
||||
Flags,
|
||||
NumaNode, //
|
||||
};
|
||||
|
||||
use crate::alloc::{AllocError, Allocator, NumaNode};
|
||||
use crate::bindings;
|
||||
use crate::page;
|
||||
use crate::{
|
||||
bindings,
|
||||
page, //
|
||||
};
|
||||
|
||||
use core::{
|
||||
alloc::Layout,
|
||||
ptr::{
|
||||
self,
|
||||
NonNull, //
|
||||
}, //
|
||||
};
|
||||
|
||||
const ARCH_KMALLOC_MINALIGN: usize = bindings::ARCH_KMALLOC_MINALIGN;
|
||||
|
||||
@@ -163,8 +174,11 @@ impl Vmalloc {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// # use core::ptr::{NonNull, from_mut};
|
||||
/// # use kernel::{page, prelude::*};
|
||||
/// # use core::ptr::{
|
||||
/// # from_mut,
|
||||
/// # NonNull, //
|
||||
/// # };
|
||||
/// # use kernel::page;
|
||||
/// use kernel::alloc::allocator::Vmalloc;
|
||||
///
|
||||
/// let mut vbox = VBox::<[u8; page::PAGE_SIZE]>::new_uninit(GFP_KERNEL)?;
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
// SPDX-License-Identifier: GPL-2.0
|
||||
|
||||
use super::Vmalloc;
|
||||
|
||||
use crate::page;
|
||||
use core::marker::PhantomData;
|
||||
use core::ptr::NonNull;
|
||||
|
||||
use core::{
|
||||
marker::PhantomData,
|
||||
ptr::NonNull, //
|
||||
};
|
||||
|
||||
/// An [`Iterator`] of [`page::BorrowedPage`] items owned by a [`Vmalloc`] allocation.
|
||||
///
|
||||
|
||||
@@ -3,24 +3,47 @@
|
||||
//! Implementation of [`Box`].
|
||||
|
||||
#[allow(unused_imports)] // Used in doc comments.
|
||||
use super::allocator::{KVmalloc, Kmalloc, Vmalloc, VmallocPageIter};
|
||||
use super::{AllocError, Allocator, Flags, NumaNode};
|
||||
use core::alloc::Layout;
|
||||
use core::borrow::{Borrow, BorrowMut};
|
||||
use core::marker::PhantomData;
|
||||
use core::mem::ManuallyDrop;
|
||||
use core::mem::MaybeUninit;
|
||||
use core::ops::{Deref, DerefMut};
|
||||
use core::pin::Pin;
|
||||
use core::ptr::NonNull;
|
||||
use core::result::Result;
|
||||
use super::allocator::{
|
||||
KVmalloc,
|
||||
Kmalloc,
|
||||
Vmalloc,
|
||||
VmallocPageIter, //
|
||||
};
|
||||
|
||||
use crate::ffi::c_void;
|
||||
use crate::fmt;
|
||||
use crate::init::InPlaceInit;
|
||||
use crate::page::AsPageIter;
|
||||
use crate::types::ForeignOwnable;
|
||||
use pin_init::{InPlaceWrite, Init, PinInit, ZeroableOption};
|
||||
use super::{
|
||||
AllocError,
|
||||
Allocator,
|
||||
Flags,
|
||||
NumaNode, //
|
||||
};
|
||||
|
||||
use crate::{
|
||||
fmt,
|
||||
page::AsPageIter,
|
||||
prelude::*,
|
||||
types::ForeignOwnable, //
|
||||
};
|
||||
|
||||
use core::{
|
||||
alloc::Layout,
|
||||
borrow::{
|
||||
Borrow,
|
||||
BorrowMut, //
|
||||
},
|
||||
marker::PhantomData,
|
||||
mem::{
|
||||
ManuallyDrop,
|
||||
MaybeUninit, //
|
||||
},
|
||||
ops::{
|
||||
Deref,
|
||||
DerefMut, //
|
||||
},
|
||||
ptr::NonNull,
|
||||
result::Result, //
|
||||
};
|
||||
|
||||
use pin_init::ZeroableOption;
|
||||
|
||||
/// The kernel's [`Box`] type -- a heap allocation for a single value of type `T`.
|
||||
///
|
||||
@@ -274,7 +297,10 @@ pub fn pin(x: T, flags: Flags) -> Result<Pin<Box<T, A>>, AllocError>
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use kernel::sync::{new_spinlock, SpinLock};
|
||||
/// use kernel::sync::{
|
||||
/// new_spinlock,
|
||||
/// SpinLock, //
|
||||
/// };
|
||||
///
|
||||
/// struct Inner {
|
||||
/// a: u32,
|
||||
@@ -567,7 +593,6 @@ fn deref_mut(&mut self) -> &mut T {
|
||||
///
|
||||
/// ```
|
||||
/// # use core::borrow::Borrow;
|
||||
/// # use kernel::alloc::KBox;
|
||||
/// struct Foo<B: Borrow<u32>>(B);
|
||||
///
|
||||
/// // Owned instance.
|
||||
@@ -595,7 +620,6 @@ fn borrow(&self) -> &T {
|
||||
///
|
||||
/// ```
|
||||
/// # use core::borrow::BorrowMut;
|
||||
/// # use kernel::alloc::KBox;
|
||||
/// struct Foo<B: BorrowMut<u32>>(B);
|
||||
///
|
||||
/// // Owned instance.
|
||||
@@ -660,9 +684,13 @@ fn drop(&mut self) {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// # use kernel::prelude::*;
|
||||
/// use kernel::alloc::allocator::VmallocPageIter;
|
||||
/// use kernel::page::{AsPageIter, PAGE_SIZE};
|
||||
/// use kernel::{
|
||||
/// alloc::allocator::VmallocPageIter,
|
||||
/// page::{
|
||||
/// AsPageIter,
|
||||
/// PAGE_SIZE, //
|
||||
/// }, //
|
||||
/// };
|
||||
///
|
||||
/// let mut vbox = VBox::new((), GFP_KERNEL)?;
|
||||
///
|
||||
|
||||
@@ -3,29 +3,52 @@
|
||||
//! Implementation of [`Vec`].
|
||||
|
||||
use super::{
|
||||
allocator::{KVmalloc, Kmalloc, Vmalloc, VmallocPageIter},
|
||||
allocator::{
|
||||
KVmalloc,
|
||||
Kmalloc,
|
||||
Vmalloc,
|
||||
VmallocPageIter, //
|
||||
},
|
||||
layout::ArrayLayout,
|
||||
AllocError, Allocator, Box, Flags, NumaNode,
|
||||
AllocError,
|
||||
Allocator,
|
||||
Box,
|
||||
Flags,
|
||||
NumaNode, //
|
||||
};
|
||||
|
||||
use crate::{
|
||||
fmt,
|
||||
page::{
|
||||
AsPageIter,
|
||||
PAGE_SIZE, //
|
||||
},
|
||||
}, //
|
||||
};
|
||||
|
||||
use core::{
|
||||
borrow::{Borrow, BorrowMut},
|
||||
borrow::{
|
||||
Borrow,
|
||||
BorrowMut, //
|
||||
},
|
||||
marker::PhantomData,
|
||||
mem::{ManuallyDrop, MaybeUninit},
|
||||
ops::Deref,
|
||||
ops::DerefMut,
|
||||
ops::Index,
|
||||
ops::IndexMut,
|
||||
ptr,
|
||||
ptr::NonNull,
|
||||
slice,
|
||||
slice::SliceIndex,
|
||||
mem::{
|
||||
ManuallyDrop,
|
||||
MaybeUninit, //
|
||||
},
|
||||
ops::{
|
||||
Deref,
|
||||
DerefMut,
|
||||
Index,
|
||||
IndexMut, //
|
||||
},
|
||||
ptr::{
|
||||
self,
|
||||
NonNull, //
|
||||
},
|
||||
slice::{
|
||||
self,
|
||||
SliceIndex, //
|
||||
}, //
|
||||
};
|
||||
|
||||
mod errors;
|
||||
@@ -614,7 +637,7 @@ pub fn clear(&mut self) {
|
||||
///
|
||||
/// v.reserve(10, GFP_KERNEL)?;
|
||||
/// let cap = v.capacity();
|
||||
/// assert!(cap >= 10);
|
||||
/// assert!(cap >= v.len() + 10);
|
||||
///
|
||||
/// v.reserve(10, GFP_KERNEL)?;
|
||||
/// let new_cap = v.capacity();
|
||||
@@ -849,6 +872,24 @@ pub fn shrink_to(&mut self, min_capacity: usize, flags: Flags) -> Result<(), All
|
||||
|
||||
impl<T: Clone, A: Allocator> Vec<T, A> {
|
||||
/// Extend the vector by `n` clones of `value`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// let mut v = KVec::new();
|
||||
/// v.push(1, GFP_KERNEL)?;
|
||||
///
|
||||
/// v.extend_with(3, 5, GFP_KERNEL)?;
|
||||
/// assert_eq!(&v, &[1, 5, 5, 5]);
|
||||
///
|
||||
/// v.extend_with(2, 8, GFP_KERNEL)?;
|
||||
/// assert_eq!(&v, &[1, 5, 5, 5, 8, 8]);
|
||||
///
|
||||
/// v.extend_with(0, 3, GFP_KERNEL)?;
|
||||
/// assert_eq!(&v, &[1, 5, 5, 5, 8, 8]);
|
||||
///
|
||||
/// # Ok::<(), Error>(())
|
||||
/// ```
|
||||
pub fn extend_with(&mut self, n: usize, value: T, flags: Flags) -> Result<(), AllocError> {
|
||||
if n == 0 {
|
||||
return Ok(());
|
||||
@@ -866,7 +907,7 @@ pub fn extend_with(&mut self, n: usize, value: T, flags: Flags) -> Result<(), Al
|
||||
spare[n - 1].write(value);
|
||||
|
||||
// SAFETY:
|
||||
// - `self.len() + n < self.capacity()` due to the call to reserve above,
|
||||
// - `self.len() + n <= self.capacity()` due to the call to reserve above,
|
||||
// - the loop and the line above initialized the next `n` elements.
|
||||
unsafe { self.inc_len(n) };
|
||||
|
||||
@@ -1146,9 +1187,13 @@ fn into_iter(self) -> Self::IntoIter {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// # use kernel::prelude::*;
|
||||
/// use kernel::alloc::allocator::VmallocPageIter;
|
||||
/// use kernel::page::{AsPageIter, PAGE_SIZE};
|
||||
/// use kernel::{
|
||||
/// alloc::allocator::VmallocPageIter,
|
||||
/// page::{
|
||||
/// AsPageIter,
|
||||
/// PAGE_SIZE, //
|
||||
/// }, //
|
||||
/// };
|
||||
///
|
||||
/// let mut vec = VVec::<u8>::new();
|
||||
///
|
||||
|
||||
@@ -2,8 +2,10 @@
|
||||
|
||||
//! Errors for the [`Vec`] type.
|
||||
|
||||
use kernel::fmt;
|
||||
use kernel::prelude::*;
|
||||
use crate::{
|
||||
fmt,
|
||||
prelude::*, //
|
||||
};
|
||||
|
||||
/// Error type for [`Vec::push_within_capacity`].
|
||||
pub struct PushError<T>(pub T);
|
||||
|
||||
@@ -4,7 +4,10 @@
|
||||
//!
|
||||
//! Custom layout types extending or improving [`Layout`].
|
||||
|
||||
use core::{alloc::Layout, marker::PhantomData};
|
||||
use core::{
|
||||
alloc::Layout,
|
||||
marker::PhantomData, //
|
||||
};
|
||||
|
||||
/// Error when constructing an [`ArrayLayout`].
|
||||
pub struct LayoutError;
|
||||
@@ -47,7 +50,10 @@ pub const fn empty() -> Self {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// # use kernel::alloc::layout::{ArrayLayout, LayoutError};
|
||||
/// # use kernel::alloc::layout::{
|
||||
/// # ArrayLayout,
|
||||
/// # LayoutError, //
|
||||
/// # };
|
||||
/// let layout = ArrayLayout::<i32>::new(15)?;
|
||||
/// assert_eq!(layout.len(), 15);
|
||||
///
|
||||
|
||||
Reference in New Issue
Block a user