Layout structv0.1.0
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
| Name | Type | Description |
|---|---|---|
size | uint | The number of bytes asked for, before any rounding. |
alignment | uint | The 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 parameter | Description |
|---|---|
T | the type to describe |
Returns: T's own size and alignment
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 parameter | Description |
|---|---|
T | the element type |
| Name | Type | Description |
|---|---|---|
count | uint | how many elements |
Returns: the array layout, or none if its size cannot be represented
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.
| Name | Type | Description |
|---|---|---|
size | uint | how many bytes are wanted |
alignment | uint | the boundary the address must be a multiple of |
Returns: the layout, or none when alignment is not a usable power of two
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.
| Name | Type | Description |
|---|---|---|
size | uint | how many bytes are wanted |
Returns: a layout of that size at its natural alignment, or none when there is none
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
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.
| Name | Type | Description |
|---|---|---|
address | uint | the address to test |
Returns: whether address meets this layout's alignment
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.
| Name | Type | Description |
|---|---|---|
other | Layout | the layout to compare against |
Returns: whether both the size and the alignment match
!=
pub func !=(self: &Layout, other: Layout) -> bool
Whether two layouts ask for different things.
| Name | Type | Description |
|---|---|---|
other | Layout | the layout to compare against |
Returns: whether either the size or the alignment differs
Related
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.
Parameters
| Name | Type | Description |
|---|---|---|
size | uint | the size to choose an alignment for |
Returns: the largest useful power-of-two alignment for that size