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.
Details#
The values are for the target being checked or built. A static body, such as a static if
condition, can call it too, so a layout check against a C header fails at compile time.
Associated function offsetOf#
pub static fn offsetOf<T>(name: Intrinsic.FieldName<T>) -> usizeReturns the byte offset of the field name from the start of one value of the struct T.
When to use#
Use it with Layout.of in a static if to check, at compile time, that an extern "C"
struct matches the C header it mirrors. The answer is for the target being checked or built,
so a mismatch on one target fails only that target's check.
Details#
name must be a string literal that names a field of T, so a misspelled name is an error
at the literal. The function is static: it runs during static evaluation, such as in a
static if condition or a constant, and never at run time. The fields of an extern "C"
struct are at their C offsets; the fields of other structs are at offsets the compiler picks.
Examples#
import silk.mem.layout { Layout }
extern "C" struct Pair {
first: i64,
second: i32,
}
pub fn main() -> i32 {
static if Layout.of<Pair>().bytes != 16 || Layout.offsetOf<Pair>("second") != 8 {
compileError("Pair does not match its C header")
}
return 0
}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, x86_64-pc-windows-msvc, wasm64-unknown-unknown.