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#

silk
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#

silk
pub struct Layout

A 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#

silk
pub bytes: usize

The number of bytes in the block. It can be zero.

Field alignment#

silk
pub alignment: usize

The alignment of the block in bytes. The start address must be a multiple of this value. It is a power of two.

Implementation Layout#

silk
impl Layout

Associated function of#

silk
pub fn of<T>() -> Layout

Returns 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#

silk
pub static fn offsetOf<T>(name: Intrinsic.FieldName<T>) -> usize

Returns 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#

silk
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#

silk
pub fn make(size: usize, alignment: usize) -> Layout | InvalidAlignment

Returns 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#

Associated function repeat#

silk
pub fn repeat(layout: Layout, count: usize) -> Layout | LayoutOverflow

Returns 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#

  • LayoutOverflow if layout.bytes * count is more than the maximum usize.

InvalidAlignment#

silk
pub struct InvalidAlignment

The error value of Layout.make for an alignment that is zero or not a power of two.

Field alignment#

silk
pub alignment: usize

The alignment that Layout.make did not accept.

LayoutOverflow#

silk
pub struct LayoutOverflow

The 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.

View the source of this page