Mmap v0.1.0

Source
Creates a virtual-memory mapping.

Package: Linux

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; must be greater than 0.
protectionint32Page protections, such as ProtectionRead.
flagsint32Mapping behavior, such as MapPrivate.
fdint32Backing file descriptor, or -1 for an anonymous mapping.
offsetuint64Page-aligned offset in the backing file.

Returns

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

Description

Combine protection and mapping flags with bitwise OR. For an anonymous private mapping, pass MapPrivate | MapAnonymous, -1i32 for fd, and 0u64 for offset. Release a successful mapping with Munmap.

Call IsError before casting the result to a pointer. Casting a negative errno result produces an invalid address.

Example

import Linux::{ 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;
    // Use memory within the mapped 4096-byte range.
    Munmap(memory, 4096u);
    return 0;
}

See also

  • Constants — available protection and mapping flags
  • Munmap — remove a mapping