Layout

Struct Layout 

Source
pub struct Layout { /* private fields */ }
Expand description

The layout of a block of memory in the form of its size and alignment in bytes.

Note that this is memapi’s custom type, not stdlib’s alloc::alloc::Layout. If a function you want does not exist, request it in an issue.

Implementations§

Source§

impl Layout

Source

pub const fn new<T>() -> Layout

Creates a layout for the given type.

This just delegates to <T as [SizedProps>::LAYOUT].

Source

pub const fn array<T>(n: usize) -> Result<Layout, Error>

Creates a layout representing an array of n T.

§Errors

Err(Error::InvalidLayout([T::SZ], [T::ALN], LayoutErr::ExceedsMax)) if the length of the computed array, in bytes, would exceed USIZE_MAX_NO_HIGH_BIT.

Source

pub const fn extend(&self, other: Layout) -> Result<(Layout, usize), Error>

Combines two layouts sequentially, returning the combined layout and the offset where other begins.

Given two layouts self and other, computes a layout describing a block of memory that can hold a value of layout self followed by a value of layout other, where other starts at an offset that satisfies its alignment. Returns the resulting combined layout and the offset at which other begins.

Note that this is only const on Rust versions 1.47 and above.

§Errors

Err(Error::InvalidLayout(self.size(), other.align(), LayoutErr::ExceedsMax)) if self.size() rounded up to the nearest multiple of other.align() would exceed USIZE_MAX_NO_HIGH_BIT.

Source

pub const fn dangling(&self) -> NonNull<u8>

Returns a valid, dangling pointer for this layout’s alignment.

The returned pointer is non-null and correctly aligned for types that use this layout’s alignment but should not be dereferenced.

Source

pub fn for_value<T: ?Sized>(val: &T) -> Layout

Creates a layout for the value behind the given reference

This just delegates to <&T as PtrProps>::layout().

Source

pub unsafe fn for_value_raw<T: ?Sized>(val: *const T) -> Layout

Creates a layout for the value behind the given reference

This just delegates to <*const T as PtrProps>::layout().

§Safety

The caller must ensure the pointer is:

  • non-null
  • non-dangling
  • aligned
Source

pub const fn from_size_align(size: usize, align: usize) -> Result<Layout, Error>

Creates a layout with the given size and alignment.

§Errors
Source

pub const fn aligned_alloc_compatible_from_size_align( size: usize, align: usize, ) -> Result<Layout, Error>

Creates a layout compatible with C’s aligned_alloc requirements from the given size and align.

C’s aligned_alloc(alignment, size) requires:

  • alignment is a power of two, non-zero, and a multiple of size_of::<*mut [c_void]>().
  • size is a multiple of alignment.

Therefore:

  • align will be rounded up to the nearest multiple of size_of::<*mut [c_void]>() if it isn’t already.
  • size will be rounded up to the nearest multiple of the resulting alignment.

This is semantically equivalent to Layout::from_size_align(size, align).and_then(|l| l.to_aligned_alloc_compatible()).

§Errors
§Examples
let l = Layout::aligned_alloc_compatible_from_size_align(10, 1).unwrap();

assert!(l.align() >= usize::SZ);
assert_eq!(l.size() % l.align(), 0);
assert!(l.size() >= 10);
// on 64-bit systems, l == Layout(size = 16, align = 8).
// 32-bit, l == Layout(size = 12, align = 4)
Source

pub const unsafe fn from_size_align_unchecked( size: usize, align: usize, ) -> Layout

Creates a layout with the given size and alignment.

In debug mode, this will panic if passed an invalid size or alignment.

§Safety

The caller must ensure:

Source

pub const fn size(&self) -> usize

Returns the size of this layout.

Source

pub const fn align(&self) -> usize

Returns the alignment of this layout.

Source

pub const fn padding_needed_for(&self, align: usize) -> Result<usize, Error>

Returns the amount of padding necessary after self to ensure that the following address will satisfy align.

§Errors
§Example

assert_eq!(unsafe { Layout::from_size_align_unchecked(6, 8) }.padding_needed_for(8), Ok(2));
Source

pub const fn pad_to_align(&self) -> Layout

Creates a layout by rounding the size of this layout up to a multiple of the layout’s alignment.

This is equivalent to adding the result of Layout::padding_needed_for to self.size().

Source

pub const fn repeat(&self, count: usize) -> Result<(Layout, usize), Error>

Creates a layout for count instances of the value described by layout, with padding between each to ensure that each instance is given its requested size and alignment.

On success, returns (l, offs) where l is the layout of the array and offs is the distance between the start of each element in the array (stride).

Note that this is only const on Rust versions 1.47 and above.

§Errors

Err(Error::ArithmeticError) if multiplying count by layout.size(), rounded up to the nearest multiple of layout.align(), would overflow.

Source

pub const fn repeat_packed(&self, count: usize) -> Result<Layout, Error>

Creates a layout for count instances of the value described by layout, with no padding between.

Note that, unlike Layout::repeat, repeat_packed doesn’t guarantee that repeated instances of the value described by layout will be properly aligned, even if layout is properly aligned.

In other words, if the layout returned byrepeat_packed is used to allocate an array, it isn’t guaranteed that all elements in the array will be properly aligned.

Note that this is only const on Rust versions 1.47 and above.

§Errors
Source

pub const fn align_to(&self, align: usize) -> Result<Layout, Error>

Creates a layout with the same size as self but an alignment meeting align. If self.align() >= align, returns self.

This method doesn’t modify the size of the new layout.

§Errors
Source

pub const fn align_to_multiple_of(&self, align: usize) -> Result<Layout, Error>

Returns a layout with the same size as self but whose alignment has been rounded up to the nearest multiple of align.

This differs from Layout::align_to: align_to sets the layout’s alignment to the provided alignment if that alignment is larger than the current one. This method instead rounds self.align() up to a multiple of the provided align.

§Errors
§Examples
// current alignment 8, round up to a multiple of 16 => next multiple is 16
let l = unsafe { Layout::from_size_align_unchecked(30, 8) };
let rounded = l.align_to_multiple_of(16).unwrap();
assert_eq!(rounded.align(), 16);
assert_eq!(rounded.size(), 30);
Source

pub const fn to_aligned_alloc_compatible(&self) -> Result<Layout, Error>

Converts this layout into one compatible with C’s aligned_alloc requirements.

C’s aligned_alloc(alignment, size) requires:

  • alignment is a power of two, non-zero, and a multiple of size_of::<*mut [c_void]>().
  • size is a multiple of alignment.

Therefore:

  • The alignment will be rounded up to the nearest multiple of size_of::<*mut [c_void]>() if it isn’t already.
  • The size will be rounded up to the nearest multiple of the resulting alignment.
§Errors

Err(Error::InvalidLayout(self.size(), self.align(), LayoutErr::CRoundUp)) if:

  • align == 0.
  • align is not a power of two.
  • align rounded up to size_of::<*mut [c_void]>() would exceed the maximum allowed alignment.
  • size rounded up to the new alignment would exceed USIZE_MAX_NO_HIGH_BIT.
§Examples
let l = Layout::from_size_align(10, 1).unwrap();
let compatible = l.to_aligned_alloc_compatible().unwrap();

assert!(compatible.align() >= usize::SZ);
assert_eq!(compatible.size() % compatible.align(), 0);
assert!(compatible.size() >= 10);
// on 64-bit systems, compatible == Layout(size = 16, align = 8).
// 32-bit, compatible == Layout(size = 12, align = 4)
Source

pub const fn to_stdlib(self) -> StdLayout

Converts this layout to an alloc::alloc::Layout.

Source

pub const fn from_stdlib(layout: StdLayout) -> Layout

Converts an alloc::alloc::Layout to a Layout.

Note that this is only const on Rust versions 1.50 and above.

Trait Implementations§

Source§

impl Clone for Layout

Source§

fn clone(&self) -> Layout

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Layout

Source§

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

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

impl From<Layout> for Layout

Source§

fn from(layout: StdLayout) -> Layout

Converts to this type from the input type.
Source§

impl From<Layout> for StdLayout

Source§

fn from(layout: Layout) -> StdLayout

Converts to this type from the input type.
Source§

impl Hash for Layout

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq<Layout> for Layout

Source§

fn eq(&self, other: &StdLayout) -> 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 PartialEq<Layout> for StdLayout

Source§

fn eq(&self, other: &Layout) -> 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 PartialEq for Layout

Source§

fn eq(&self, other: &Layout) -> 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 Copy for Layout

Source§

impl Eq for Layout

Source§

impl StructuralPartialEq for Layout

Auto Trait Implementations§

§

impl Freeze for Layout

§

impl RefUnwindSafe for Layout

§

impl Send for Layout

§

impl Sync for Layout

§

impl Unpin for Layout

§

impl UnwindSafe for Layout

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> SizedProps for T

Source§

const SZ: usize = _

The size of the type.
Source§

const ALN: usize = _

The alignment of the type.
Source§

const LAYOUT: Layout = _

The memory layout for the type.
Source§

const IS_ZST: bool = _

Whether the type is zero-sized.
Source§

const MAX_SLICE_LEN: usize = _

The largest safe length for a [Self].
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> UnsizedCopy for T
where T: Copy,