LEVIATHAN vd52e79c4 · d52e79c486

Types

Ptr — the foreign address

The primitive that holds an address of C memory or code; it is never dereferenced, freed or kept alive by the runtime.

since 0.1.0-alpha.1linuxwindowswasm

Description

Ptr is the primitive for an address that belongs to C: memory a C library allocated, a function pointer, a handle that is really a pointer. It is a plain value, like an int: copying a Ptr copies the address, two Ptrs are equal when their addresses are equal, and a Ptr can be a field, an array element, a map key or a value sent to another thread. The runtime never reads through a Ptr, frees it or keeps anything alive for it, so whether the address is still valid is the program's responsibility, exactly as in C.

Ptr has no literal and no ordering (< and > do not apply). Make one with ffi::nullPtr() or ffi::ptr(address), or receive one from a foreign function. Its methods are isNull(), addr() (the address as an int), offset(bytes) (pointer arithmetic, unchecked) and toString() (0x and lowercase hexadecimal, 0x0 for null).

Ptr? is a Ptr that may be absent. At a foreign-function boundary None is passed as NULL, and a NULL result arrives as None, so declaring a C function's result as Ptr? makes the null case visible in the type. A plain Ptr that happens to be null is passed to C unchanged.

Reading and writing the memory behind a pointer goes through a Block: ffi::view(p, len) returns a block over len bytes at p, without copying, and every Block accessor then works on the foreign memory, bounds-checked against len. ffi::alloc(n) and ffi::free(p) use C's allocator, and a block's own bytes have an address too: block.ptr().

Foreign memory through a view

uses ffi;
use ffi;
use io::console;

void main() {
    Ptr p = ffi::alloc(16);
    Block v = ffi::view(p, 16);
    v.setInt32(0, 7);
    v.setFloat64(8, 2.5);
    console.writeln(v.int32At(0));
    console.writeln(v.float64At(8));
    console.writeln(p.offset(8) == v.slice(8, 8).ptr());
    ffi::free(p);
}
main();
7
2.5e0
true

Rules

  1. Ptr is a primitive type available without an import; the functions that create and use pointers are in the ffi namespace.
  2. == and != compare addresses. No other operator applies to a Ptr; an ordering comparison is no operator '<' on 'Ptr' (Ptr is not ordered).
  3. ffi::view and ffi::toString throw FfiException for a null pointer; nothing else checks one.
  4. A view does not own its bytes: using it after the foreign memory is released is the program's error.
  5. ffi::alloc(n) with a negative n throws FfiException (ffi::alloc: negative size <n>), and so does ffi::toString(p, len) with a negative len (ffi::toString: negative length <len>). ffi::alloc(0) is valid: it returns a non-null address of its own, which ffi::free releases like any other.
  6. The types Ptr? and string? are the only optional types that can cross to C.

Examples

An optional pointer is None until a foreign function provides an address:

An absent pointer

use io::console;

Ptr? p = None;
console.writeln(p == None);
true

A C function that returns NULL for "not found" is declared with a Ptr? result:

An optional pointer result

uses ffi;
use ffi;
use io::console;

@extern("c")
Ptr? getenv(string name);

@extern("c", "strerror")
Ptr? cStrerror(int32 code);

string envOr(string name, string fallback) {
    Ptr? p = getenv(name);
    if (p != None) {
        return ffi::toString(p);
    }
    return fallback;
}

void main() {
    console.writeln(envOr("LEVIATHAN_DOCS_SURELY_UNSET_VARIABLE", "(unset)"));
    Ptr? text = cStrerror(2);
    if (text != None) {
        console.writeln(ffi::toString(text));
    }
}
main();
(unset)
No such file or directory

Notes

  • A Ptr field makes a struct ineligible for the column-wise array layout; arrays of such structs are stored row by row.
  • Block.ptr() is valid while the block is alive: the runtime never moves a block's bytes.

See also

  • Foreign functions (@extern, @export) — Declare a C function with @extern and call it like any other function; which types cross to C and how, errors, variadic functions, exports and the build permission.
  • Ptr — An opaque address of foreign (C) memory or code.
  • view — A Block over len bytes of foreign memory starting at p.
  • nullPtr — The null address.
  • Primitives (the object mask) — The built-in value types, their default values, the methods they carry, and the void unit rules.