Mmap v0.1.0

Source
Creates a virtual-memory mapping through the target-specific BSD thunk.

Package: Bsd

Signature

func Mmap(
    address: *opaque,
    length: uint,
    protection: int32,
    flags: int32,
    fd: int32,
    offset: uint64
) -> int64;

Parameters

NameTypeDescription
address*opaqueRequested address hint, or null.
lengthuintMapping length in bytes.
protectionint32Page protections, such as ProtectionRead.
flagsint32Mapping behavior, such as MapPrivate.
fdint32Backing descriptor, or -1 for anonymous memory.
offsetuint64Page-aligned offset in the backing object.

Returns

int64 - the mapped address encoded as an integer on success, or a negative errno value on failure.

For private anonymous memory, combine MapPrivate | MapAnonymous, pass -1i32 for fd, and use an offset of 0u64. The compiler thunk handles the target-specific mmap syscall details.

Check the result before casting it to a pointer. Release every successful mapping with Munmap using its correct base address and length.

Example

import Bsd::{ IsError, Mmap, Munmap, MapAnonymous, MapPrivate, ProtectionRead, ProtectionWrite };

func Main() -> int {
    let result = Mmap(null, 4096u, ProtectionRead | ProtectionWrite,
        MapPrivate | MapAnonymous, -1i32, 0u64);
    if IsError(result) {
        return 1;
    }
    let memory = result as *opaque;
    Munmap(memory, 4096u);
    return 0;
}

See also