Browse the docs

silk.platform.linux.mapping.

Anonymous memory mappings from the Linux kernel, for programs that run without libc.

When to use#

Use Mapping.allocate in a raw Linux program, after startup reads the page size from the AT_PAGESZ auxiliary vector entry. Other programs get memory from the Allocator effect.

Details#

This module exists only on Linux on x86_64 and aarch64, with the libc profile set to none. Each Mapping owns its whole mapping. Dropping the mapping releases it with munmap. Errors are Linux error numbers, such as 22 for EINVAL.

Gotchas#

A raw pointer from Mapping.pointer does not keep the mapping alive. If the program traps, the mapping is not always released.

Import as Mapping with import silk.platform.linux.mapping { Mapping }.

Public declarations: 1.

Mapping#

silk
pub struct Mapping

One anonymous mapping from the Linux kernel that starts as zeros, and an aligned range inside it. It exists only on Linux on x86_64 and aarch64, with the libc profile set to none.

Details#

Drop is automatic and releases the mapping. Use Mapping.release to get the error from munmap. Errors are Linux error numbers, such as 22 for EINVAL.

Gotchas#

A raw pointer from Mapping.pointer does not keep the mapping alive. If the program traps, the mapping is not always released.

Implementation Drop for Mapping#

silk
impl Drop for Mapping

Operation drop#

silk
fn drop(self: &mut Mapping) -> ()

Implementation Mapping#

silk
impl Mapping

Associated function allocate#

silk
pub fn allocate(length: usize, alignment: usize, pageSize: usize) -> Result<Mapping, usize>

Maps zeroed memory with at least length bytes that start at an address aligned to alignment.

Details#

pageSize must be the page size of the process, from AT_PAGESZ. The mapping is larger than length when it needs space to align the start.

Errors#

  • 22 (EINVAL) if length, alignment, or pageSize is 0, if alignment or pageSize is not a power of two, or if the size computation overflows.

  • The Linux error number from mmap if the kernel cannot make the mapping.

Method length#

silk
pub fn length(self: &Mapping) -> usize

Returns the usable byte count that was requested. It does not include the alignment space.

Method pointer#

silk
pub unsafe fn pointer(self: &mut Mapping) -> ?[*]mut u8

Returns a pointer to the start of the aligned usable range.

Gotchas#

The pointer does not own the mapping and does not keep it alive. Keep the Mapping alive and access at most Mapping.length bytes. The bytes are zero at first.

Associated function release#

silk
pub fn release(value: Mapping) -> usize

Releases the whole mapping now and returns the result of munmap.

Details#

Returns 0 on success or the Linux error number. After release, drop does not try to release the mapping again.

Category: Native platform.

Profiles: aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu-no-libc.

View the source of this page