rt
rt: low-level runtime support
The rt (runtime) module provides support code for the Hare compiler. It is not guaranteed to be portable between different Hare compilers.
Index
Types
type abort_handler;
type chunk;
type meta;
type arch_jmp_buf;
type jmp_buf;
type memory_heap;
type slice;
Constants
def EMPTY_HEAP = memory_heap {
cur_allocs = 0,
bins = [null...],
cur_chunk = (null: *chunk, CHUNKSZ),
};
Globals
let argc: size;
let argv: *[*]*u8;
let envp: *[*]nullable *u8;
Functions
fn fini() void;
fn free_(p: nullable *opaque) void;
fn getmeta(p: *opaque) *meta;
fn init() void;
fn malloc(n: size) nullable *opaque;
fn onabort(handler: *abort_handler) *abort_handler;
fn realloc(p: nullable *opaque, n: size) nullable *opaque;
fn setheap(new_heap: *memory_heap) *memory_heap;
fn _abort(path: *str, line: u64, col: u64, msg: str) never;
fn abort_fixed(path: *str, line: u64, col: u64, i: u64) void;
fn ensure(s: *slice, membsz: size) bool;
fn longjmp(buf: *jmp_buf, n: int) never;
fn memcpy(dest: *opaque, src: *opaque, n: size) void;
fn memmove(dest: *opaque, src: *opaque, n: size) void;
fn memset(dest: *opaque, val: u8, n: size) void;
fn setjmp(buf: *jmp_buf) int;
fn start_ha() never;
fn start_linux(iv: *[*]uintptr) never;
fn strcmp(_a: str, _b: str) bool;
fn unensure(s: *slice, membsz: size) void;
Types
type abort_handler
type abort_handler = fn(path: *str, line: u64, col: u64, msg: str) never;
Signature for abort handler function.
type chunk
type chunk = union {
padding: size,
data: [*]u8,
};
A group of blocks that were allocated together.
type meta = struct {
union {
sz: size,
next: uintptr,
},
user: [*]u8,
};
Metadata for a block.
type arch_jmp_buf
Show undocumented member
type arch_jmp_buf = [8]u64;
type jmp_buf
Show undocumented member
type jmp_buf = struct {
__jb: arch_jmp_buf,
__fl: size,
__ss: [128 / size(size)]size,
};
type memory_heap
Show undocumented member
type memory_heap = struct {
cur_allocs: size,
bins: [9]nullable *meta,
cur_chunk: (*chunk, size),
};
type slice
Show undocumented member
type slice = struct {
data: nullable *opaque,
length: size,
capacity: size,
};
Constants
def EMPTY_HEAP
def EMPTY_HEAP = memory_heap {
cur_allocs = 0,
bins = [null...],
cur_chunk = (null: *chunk, CHUNKSZ),
};
An empty memory heap, used to initialize a memory_heap for use with setheap.
Globals
let argc
Show undocumented member
let argc: size;
let argv
Show undocumented member
let argv: *[*]*u8;
let envp
Show undocumented member
let envp: *[*]nullable *u8;
Functions
fn fini
fn fini() void;
Run all global finalization functions.
fn free_
fn free_(p: nullable *opaque) void;
Frees an allocation returned by malloc. Freeing any other pointer, or freeing a pointer that's already been freed, will cause an abort.
fn getmeta(p: *opaque) *meta;
Gets the metadata for a given allocation. The provided pointer must have been returned by malloc or realloc and must not have been freed.
fn init
fn init() void;
Run all global initialization functions.
fn malloc
fn malloc(n: size) nullable *opaque;
Allocates n bytes of memory and returns a pointer to them, or null if there is insufficient memory.
fn onabort
fn onabort(handler: *abort_handler) *abort_handler;
Sets a new global runtime abort handler, returning the previous handler.
fn realloc
fn realloc(p: nullable *opaque, n: size) nullable *opaque;
Changes the allocation size of a pointer to n bytes. If n is smaller than the prior allocation, it is truncated; otherwise the allocation is expanded and the values of the new bytes are undefined. May return a different pointer than the one given if there is insufficient space to expand the pointer in-place. Returns null if there is insufficient memory to support the request.
fn setheap
fn setheap(new_heap: *memory_heap) *memory_heap;
Switches the internal runtime allocator to a new memory heap. The caller should provision a memory_heap initialized to EMPTY_HEAP somehow (statically, or in a second memory_heap, or even on the stack if you're brave enough) and pass it to this function to enable it. Returns a pointer to the heap which was previously in use, should you wish to restore it later.
The caller is responsible for ensuring that any use of free() or delete() makes use of an object which was allocated (via alloc(), insert(), or append()) from the same heap.
This function is designed for debugging use, and exists in particular to satisfy the needs of debug::.
fn _abort
Show undocumented member
fn _abort(path: *str, line: u64, col: u64, msg: str) never;
fn abort_fixed
Show undocumented member
fn abort_fixed(path: *str, line: u64, col: u64, i: u64) void;
fn ensure
Show undocumented member
fn ensure(s: *slice, membsz: size) bool;
fn longjmp
Show undocumented member
fn longjmp(buf: *jmp_buf, n: int) never;
fn memcpy
Show undocumented member
fn memcpy(dest: *opaque, src: *opaque, n: size) void;
fn memmove
Show undocumented member
fn memmove(dest: *opaque, src: *opaque, n: size) void;
fn memset
Show undocumented member
fn memset(dest: *opaque, val: u8, n: size) void;
fn setjmp
Show undocumented member
fn setjmp(buf: *jmp_buf) int;
fn start_ha
Show undocumented member
fn start_ha() never;
fn start_linux
Show undocumented member
fn start_linux(iv: *[*]uintptr) never;
fn strcmp
Show undocumented member
fn strcmp(_a: str, _b: str) bool;
fn unensure
Show undocumented member
fn unensure(s: *slice, membsz: size) void;