Layout structv0.1.0

Source
A validated allocation request: a size, and the alignment its address must satisfy.
pub struct Layout {
    pub size: uint;
    pub alignment: uint;
}

Copy and owns nothing. Every instance is valid, because New refuses to build an invalid one: the alignment is a power of two, and the size rounded up to that alignment does not pass the end of the address space. A size of zero is valid and is not an allocation of nothing — see Allocate for what an allocator does with it.

Fields

NameTypeDescription
sizeuintThe number of bytes asked for, before any rounding.
alignmentuintThe alignment the address must be a multiple of. Always a power of two.

Associated functions

ForValue

pub func ForValue<T>() -> Layout

A layout for one value of T, using its actual size and alignment. No value is copied or consumed. Cannot fail, runs in constant time, and allocates nothing.

Type parameterDescription
Tthe type to describe

Returns: T's own size and alignment

Source

ForArray

pub func ForArray<T>(count: uint) -> Layout?

A layout for count consecutive values of T, or absent when the size would overflow. No value is copied or consumed. A zero count describes empty storage at T's alignment. Runs in constant time and allocates nothing.

Type parameterDescription
Tthe element type
NameTypeDescription
countuinthow many elements

Returns: the array layout, or none if its size cannot be represented

Source

New

pub func New(size: uint, alignment: uint) -> Layout?

A layout for size bytes at alignment, or nothing when the pair cannot be one. size and alignment are read by value; the layout is returned by copy. Reports none when alignment is zero or not a power of two, and when rounding size up to alignment would pass the end of the address space — which is the case that turns into an allocation far smaller than the caller asked for if it is not caught here. Runs in constant time and allocates nothing.

NameTypeDescription
sizeuinthow many bytes are wanted
alignmentuintthe boundary the address must be a multiple of

Returns: the layout, or none when alignment is not a usable power of two

Source

ForBytes

pub func ForBytes(size: uint) -> Layout?

A layout for size bytes at the alignment a machine word wants. size is read by value. Reports none only when rounding size up would pass the end of the address space, since the alignment is known good. This is the layout for storage whose contents are bytes rather than a typed value: an allocation aligned to a word suits anything the machine loads in one instruction. Runs in constant time and allocates nothing.

NameTypeDescription
sizeuinthow many bytes are wanted

Returns: a layout of that size at its natural alignment, or none when there is none

Source

Methods

PaddedSize

pub func PaddedSize(self: &Layout) -> uint

The size this layout occupies once rounded up to its own alignment. self is borrowed. This is the stride between two of these laid out one after another, and is what an arena advances by. Cannot overflow, because New refused every size for which it would. Runs in constant time and allocates nothing.

Returns: the size rounded up to a whole number of alignments

Source

IsSatisfiedBy

pub func IsSatisfiedBy(self: &Layout, address: uint) -> bool

Whether address satisfies this layout's alignment. self is borrowed and address is read by value and never dereferenced. Cannot fail. Runs in constant time and allocates nothing.

NameTypeDescription
addressuintthe address to test

Returns: whether address meets this layout's alignment

Source

Operators

==

pub func ==(self: &Layout, other: Layout) -> bool

Whether two layouts ask for the same thing. self is borrowed and other is read by value.

NameTypeDescription
otherLayoutthe layout to compare against

Returns: whether both the size and the alignment match

Source

!=

pub func !=(self: &Layout, other: Layout) -> bool

Whether two layouts ask for different things.

NameTypeDescription
otherLayoutthe layout to compare against

Returns: whether either the size or the alignment differs

Source

NaturalAlignment

pub func NaturalAlignment(size: uint) -> uint

The alignment a value of size bytes naturally wants. size is read by value. Returns the largest power of two that divides a value of this size, capped at a machine word, which is what every supported target's ABI requires of an ordinary value. A size of zero reports one, since a zero-sized value has nothing to align. Runs in time proportional to the word size and allocates nothing.

Source

Parameters

NameTypeDescription
sizeuintthe size to choose an alignment for

Returns: the largest useful power-of-two alignment for that size