kernel::alloc::kvec

Type Alias KVec

Source
pub type KVec<T> = Vec<T, Kmalloc>;
Expand description

Type alias for Vec with a Kmalloc allocator.

§Examples

let mut v = KVec::new();
v.push(1, GFP_KERNEL)?;
assert_eq!(&v, &[1]);

Aliased Type§

struct KVec<T> { /* private fields */ }

Implementations

Source§

impl<T, A> Vec<T, A>
where A: Allocator,

Source

pub fn capacity(&self) -> usize

Returns the number of elements that can be stored within the vector without allocating additional memory.

Source

pub fn len(&self) -> usize

Returns the number of elements stored within the vector.

Source

pub unsafe fn set_len(&mut self, new_len: usize)

Forcefully sets self.len to new_len.

§Safety
  • new_len must be less than or equal to Self::capacity.
  • If new_len is greater than self.len, all elements within the interval [self.len,new_len) must be initialized.
Source

pub fn as_slice(&self) -> &[T]

Returns a slice of the entire vector.

Source

pub fn as_mut_slice(&mut self) -> &mut [T]

Returns a mutable slice of the entire vector.

Source

pub fn as_mut_ptr(&mut self) -> *mut T

Returns a mutable raw pointer to the vector’s backing buffer, or, if T is a ZST, a dangling raw pointer.

Source

pub fn as_ptr(&self) -> *const T

Returns a raw pointer to the vector’s backing buffer, or, if T is a ZST, a dangling raw pointer.

Source

pub fn is_empty(&self) -> bool

Returns true if the vector contains no elements, false otherwise.

§Examples
let mut v = KVec::new();
assert!(v.is_empty());

v.push(1, GFP_KERNEL);
assert!(!v.is_empty());
Source

pub const fn new() -> Self

Creates a new, empty Vec<T, A>.

This method does not allocate by itself.

Source

pub fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit<T>]

Returns a slice of MaybeUninit<T> for the remaining spare capacity of the vector.

Source

pub fn push(&mut self, v: T, flags: Flags) -> Result<(), AllocError>

Appends an element to the back of the Vec instance.

§Examples
let mut v = KVec::new();
v.push(1, GFP_KERNEL)?;
assert_eq!(&v, &[1]);

v.push(2, GFP_KERNEL)?;
assert_eq!(&v, &[1, 2]);
Source

pub fn with_capacity(capacity: usize, flags: Flags) -> Result<Self, AllocError>

Creates a new Vec instance with at least the given capacity.

§Examples
let v = KVec::<u32>::with_capacity(20, GFP_KERNEL)?;

assert!(v.capacity() >= 20);
Source

pub unsafe fn from_raw_parts( ptr: *mut T, length: usize, capacity: usize, ) -> Self

Creates a Vec<T, A> from a pointer, a length and a capacity using the allocator A.

§Examples
let mut v = kernel::kvec![1, 2, 3]?;
v.reserve(1, GFP_KERNEL)?;

let (mut ptr, mut len, cap) = v.into_raw_parts();

// SAFETY: We've just reserved memory for another element.
unsafe { ptr.add(len).write(4) };
len += 1;

// SAFETY: We only wrote an additional element at the end of the `KVec`'s buffer and
// correspondingly increased the length of the `KVec` by one. Otherwise, we construct it
// from the exact same raw parts.
let v = unsafe { KVec::from_raw_parts(ptr, len, cap) };

assert_eq!(v, [1, 2, 3, 4]);
§Safety

If T is a ZST:

  • ptr must be a dangling, well aligned pointer.

Otherwise:

  • ptr must have been allocated with the allocator A.
  • ptr must satisfy or exceed the alignment requirements of T.
  • ptr must point to memory with a size of at least size_of::<T>() * capacity bytes.
  • The allocated size in bytes must not be larger than isize::MAX.
  • length must be less than or equal to capacity.
  • The first length elements must be initialized values of type T.

It is also valid to create an empty Vec passing a dangling pointer for ptr and zero for cap and len.

Source

pub fn into_raw_parts(self) -> (*mut T, usize, usize)

Consumes the Vec<T, A> and returns its raw components pointer, length and capacity.

This will not run the destructor of the contained elements and for non-ZSTs the allocation will stay alive indefinitely. Use Vec::from_raw_parts to recover the Vec, drop the elements and free the allocation, if any.

Source

pub fn reserve( &mut self, additional: usize, flags: Flags, ) -> Result<(), AllocError>

Ensures that the capacity exceeds the length by at least additional elements.

§Examples
let mut v = KVec::new();
v.push(1, GFP_KERNEL)?;

v.reserve(10, GFP_KERNEL)?;
let cap = v.capacity();
assert!(cap >= 10);

v.reserve(10, GFP_KERNEL)?;
let new_cap = v.capacity();
assert_eq!(new_cap, cap);
Source§

impl<T: Clone, A: Allocator> Vec<T, A>

Source

pub fn extend_with( &mut self, n: usize, value: T, flags: Flags, ) -> Result<(), AllocError>

Extend the vector by n clones of value.

Source

pub fn extend_from_slice( &mut self, other: &[T], flags: Flags, ) -> Result<(), AllocError>

Pushes clones of the elements of slice into the Vec instance.

§Examples
let mut v = KVec::new();
v.push(1, GFP_KERNEL)?;

v.extend_from_slice(&[20, 30, 40], GFP_KERNEL)?;
assert_eq!(&v, &[1, 20, 30, 40]);

v.extend_from_slice(&[50, 60], GFP_KERNEL)?;
assert_eq!(&v, &[1, 20, 30, 40, 50, 60]);
Source

pub fn from_elem(value: T, n: usize, flags: Flags) -> Result<Self, AllocError>

Create a new Vec<T, A> and extend it by n clones of value.

Trait Implementations§

Source§

impl<T> Default for KVec<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T: Debug, A: Allocator> Debug for Vec<T, A>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T, A> Deref for Vec<T, A>
where A: Allocator,

Source§

type Target = [T]

The resulting type after dereferencing.
Source§

fn deref(&self) -> &[T]

Dereferences the value.
Source§

impl<T, A> DerefMut for Vec<T, A>
where A: Allocator,

Source§

fn deref_mut(&mut self) -> &mut [T]

Mutably dereferences the value.
Source§

impl<T, A> Drop for Vec<T, A>
where A: Allocator,

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T, A, const N: usize> From<Box<[T; N], A>> for Vec<T, A>
where A: Allocator,

Source§

fn from(b: Box<[T; N], A>) -> Vec<T, A>

Converts to this type from the input type.
Source§

impl<T, I: SliceIndex<[T]>, A> Index<I> for Vec<T, A>
where A: Allocator,

Source§

type Output = <I as SliceIndex<[T]>>::Output

The returned type after indexing.
Source§

fn index(&self, index: I) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T, I: SliceIndex<[T]>, A> IndexMut<I> for Vec<T, A>
where A: Allocator,

Source§

fn index_mut(&mut self, index: I) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<T, A> IntoIterator for Vec<T, A>
where A: Allocator,

Source§

fn into_iter(self) -> Self::IntoIter

Consumes the Vec<T, A> and creates an Iterator, which moves each value out of the vector (from start to end).

§Examples
let v = kernel::kvec![1, 2]?;
let mut v_iter = v.into_iter();

let first_element: Option<u32> = v_iter.next();

assert_eq!(first_element, Some(1));
assert_eq!(v_iter.next(), Some(2));
assert_eq!(v_iter.next(), None);
let v = kernel::kvec![];
let mut v_iter = v.into_iter();

let first_element: Option<u32> = v_iter.next();

assert_eq!(first_element, None);
Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T, A>

Which kind of iterator are we turning this into?
Source§

impl<T, U, A: Allocator> PartialEq<&[U]> for Vec<T, A>
where T: PartialEq<U>,

Source§

fn eq(&self, other: &&[U]) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, U, A: Allocator, const N: usize> PartialEq<&[U; N]> for Vec<T, A>
where T: PartialEq<U>,

Source§

fn eq(&self, other: &&[U; N]) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, U, A: Allocator> PartialEq<&mut [U]> for Vec<T, A>
where T: PartialEq<U>,

Source§

fn eq(&self, other: &&mut [U]) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, U, A: Allocator> PartialEq<[U]> for Vec<T, A>
where T: PartialEq<U>,

Source§

fn eq(&self, other: &[U]) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, U, A: Allocator, const N: usize> PartialEq<[U; N]> for Vec<T, A>
where T: PartialEq<U>,

Source§

fn eq(&self, other: &[U; N]) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, U, A1: Allocator, A2: Allocator> PartialEq<Vec<U, A2>> for Vec<T, A1>
where T: PartialEq<U>,

Source§

fn eq(&self, other: &Vec<U, A2>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Eq, A> Eq for Vec<T, A>
where A: Allocator,

Source§

impl<T, A> Send for Vec<T, A>
where T: Send, A: Allocator,

Source§

impl<T, A> Sync for Vec<T, A>
where T: Sync, A: Allocator,