silk.mem.layout.
Byte size and alignment values that describe the memory to request from an allocator.
When to use#
Use Layout.of for memory that holds a Silk type. Use Layout.repeat for an array of
elements. Use Layout.make only when the code knows a byte size and alignment but no type,
such as at a foreign boundary.
Details#
An alignment is always a power of two and never zero. A Layout only describes memory. It
does not allocate or initialize memory. Layout.make and Layout.repeat check their
inputs and return an error value instead of an invalid layout.
Examples#
Size storage for repeated values#
import silk.mem.layout { Layout, LayoutOverflow }
import silk.num.usize
pub fn main() -> i32 {
let element = Layout.of<i32>()
let bytes = match move Layout.repeat(move element, 3) {
Layout { bytes, alignment } => bytes
LayoutOverflow {} => usize.ZERO
}
if bytes != 12 {
return 1
}
return 0
}Import as Layout with import silk.mem.layout { Layout }.
Public declarations: 3.
Layout#
pub struct LayoutA byte size and an alignment that describe one block of memory.
Details#
Make a layout with Layout.of, Layout.make, or Layout.repeat. These functions make
sure that alignment is a power of two and never zero. A struct literal does not check the
fields. A layout only describes memory. It does not allocate or initialize memory.
Field bytes#
pub bytes: usizeThe number of bytes in the block. It can be zero.
Field alignment#
pub alignment: usizeThe alignment of the block in bytes. The start address must be a multiple of this value. It is a power of two.
Implementation Layout#
impl LayoutAssociated function of#
pub fn of<T>() -> LayoutReturns the byte size and alignment of one value of type T.
Associated function make#
pub fn make(size: usize, alignment: usize) -> Layout | InvalidAlignmentReturns a layout with size bytes and the given alignment, or InvalidAlignment if
alignment is not valid.
When to use#
Use make when the code has a byte size and alignment but no Silk type. Use Layout.of
when a type is available.
Errors#
InvalidAlignmentifalignmentis zero or is not a power of two.
Associated function repeat#
pub fn repeat(layout: Layout, count: usize) -> Layout | LayoutOverflowReturns the layout of count consecutive blocks of layout, or LayoutOverflow if the
size is too large.
Details#
The result has layout.bytes * count bytes and the alignment of layout. A count of zero
gives a layout of zero bytes.
Errors#
LayoutOverflowiflayout.bytes * countis more than the maximumusize.
InvalidAlignment#
pub struct InvalidAlignmentThe error value of Layout.make for an alignment that is zero or not a power of two.
Field alignment#
pub alignment: usizeThe alignment that Layout.make did not accept.
LayoutOverflow#
pub struct LayoutOverflowThe error value of Layout.repeat when the total byte size is more than the maximum usize.
Category: Memory.
Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2.