13.13. Runtime library API
libmorlocis the shared library every pool links. It owns what must be the same in every language: shared memory, schemas, packets, sockets, Arrow blocks, the result cache, stream files and the intrinsics. A language binding wraps it for its language and adds one thing of its own, a walker that writes native values into the voidstar layout and reads them back (Voidstar format and schema strings). This section specifies the C ABI declared inmorloc.hthat bindings are written against. Functions only the nexus or the daemon uses are out of scope.
Division of labour. The library never touches a language’s objects. It
parses a schema string into a Schema tree whose nodes carry each slot’s
width, alignment and field offsets, so no binding recomputes the layout and
none can disagree about it. The binding walks that tree, sizes the value,
allocates a block with shmalloc, and fills it. Reading is the reverse: the
library turns a packet into a pointer to a voidstar value, and the binding
walks it into native objects, resolving each relative pointer through
resolve_region.
ABI version. morloc.h defines MORLOC_ABI_VERSION, an integer distinct
from the release version, and the library reports the value it was built
with through morloc_abi_version(). It changes whenever a declaration in
the header or the packet format changes. A binding compiled against one
version is only correct with a library that reports the same one.
Errors. Every fallible function takes char** errmsg_ as its last
parameter (ERRMSG in the header). The callee sets *errmsg_ to NULL on
entry. On failure it sets it to a malloc’d, NUL-terminated message, which
the caller frees with `free(), and returns a failure value, usually NULL for
a pointer, false for a bool, -1 for a handle, RELNULL for a relative
pointer and non-zero for an int status. A caller tests *errmsg_, which is
set exactly when the call failed. One outcome is not a failure: a stream
write whose reader has gone returns MLC_RESULT_PIPE_CLOSED with no message,
so the run can end with the broken-pipe status. A failure carried in a
packet (status fail, Packet protocol) is data, not a failed call: reading
such a packet’s value fails with the packet’s message.
Ownership.
| What the library returns | Who releases it, and how |
|---|---|
|
The caller, with |
|
The caller, with |
|
The caller, with |
A shared-memory block |
Reference counted. |
A value ( |
A voidstar in a fresh shared-memory block the caller holds one reference to. |
A stream handle or fold cell ( |
The caller, with |
A data packet that names a shared-memory block carries a reference the
sender took on the receiver’s behalf (Shared memory layout). The receiver inherits
that reference and releases it once, when it is done with the value; it
never takes its own on arrival. An Arrow array passed to arrow_to_shm is
consumed: its release callback runs once the copy is made.
Panics. A panic in the library is an internal error, never a reported
failure. Unless a catch scope holds it, it ends the process at once with
exit status 70 (Error propagation). A binding installs the library’s hook once
at start (morloc_install_panic_hook, morloc_install_crash_handler).
Hosting a pool. A binding chooses one of two process models
(Worker model). A threaded pool calls pool_main with a
pool_config_t holding its dispatch callbacks; the library accepts
connections and runs each call on a worker thread. A forking pool calls
morloc_fork_pool_run with a morloc_fork_pool_t that says how to fork,
initialise and serve one connection; each worker reads the call with
stream_from_client and answers with send_reply_to_foreign_server.
Shared memory
| Function | Purpose |
|---|---|
|
Map the program’s shared memory, given its base name and initial size. |
|
Directory for file-backed volumes when the shared-memory filesystem is too small. |
|
Allocate a block holding one reference for the caller. |
|
Take / release one reference on a block. |
|
Convert between a relative pointer and an address in this process. |
|
|
|
Inline resolve of a relative pointer to |
|
The space a value’s pointers lead into: shared memory, or an inline packet payload of known length. |
|
Record and forget a native view (such as an array over a block) so a forked child keeps the block alive. |
|
Bytes of shared memory the program holds, when statistics are on. |
Schemas and encodings
| Function or type | Purpose |
|---|---|
|
A parsed schema node (type, parameters, keys, hint, width, alignment, offsets); a variable-length slot. |
|
Parse a schema string into a |
|
Render a |
|
Narrow a double to a float, failing on a finite value out of range. |
|
Describe the first string in a value holding a NUL byte, or NULL. |
Packets and calls
| Function | Purpose |
|---|---|
|
Check a buffer holds a whole packet / total size of a packet. |
|
Classify a received packet. |
|
The reply to a ping. |
|
Split a call packet into function index and argument packets / free it. |
|
Build a call packet from a function index and argument packets. |
|
Wrap a voidstar value: inline below the size threshold, else by reference to its block. |
|
Wrap a value inline whatever its size, for a value that must outlive its block. |
|
Wrap an Arrow block by reference. |
|
An error packet / one marking a closed output pipe. |
|
Whether a packet is the closed-pipe marker. |
|
The voidstar value a data packet carries, decompressed and loaded as needed. |
|
The message of a failed data packet, or NULL. |
|
A copy of an argument packet a pool can return as its result, with its own reference. |
|
Whether a path names a stream file rather than a single packet. |
|
Send a call to another pool’s socket and return its reply. |
|
Run a call on a cluster node through the scheduler, with its resources. |
Pools and processes
| Function or type | Purpose |
|---|---|
|
Run a threaded pool with the binding’s dispatch callbacks and per-dispatch release. |
|
Run a forking pool; workers never return from it. |
|
Fork a worker, refusing unless the process has one thread. |
|
Read one whole packet from a connection. |
|
Send a reply, running the release callback before it is sent. |
|
Close a socket / a pool’s listening socket and state. |
|
Fork generation at request time / exit if user code forked during the call. |
|
Install the panic exit / report the active function on a fatal signal. |
|
Tell user panics from runtime panics, for a pool whose own language panics. |
Arrow
| Function | Purpose |
|---|---|
|
Copy an Arrow C Data Interface array into a fresh table block; the typed form casts declared columns, refuses nulls in non-optional ones and puts declared columns first. |
|
The same for an Arrow stream, concatenating its batches. |
|
Check a received block against the declared columns. |
|
Import a block as Arrow C Data Interface structs, recording it so a table returned unchanged is passed on without a copy. |
|
Shared memory held by this process’s open table views, for languages whose collector cannot see it. |
Caching
| Function | Purpose |
|---|---|
|
The pool’s source fingerprint, mixed into every key. |
|
A key from a function index and its argument values (not their pointers). |
|
Read / atomically write a cached result packet under a label and key. |
|
Count events for the run summary. |
Intrinsics: values
| Function | Purpose |
|---|---|
|
Write a value as JSON or a voidstar packet (optionally compressed). |
|
Read a value from a file of any of those forms, or a stream file as a list. |
|
Parse a value from JSON / render it as JSON. |
|
Hash a value / order two values of one schema. |
|
Per-thread accumulators of a fold handler. |
Intrinsics: streams and handles
| Function | Purpose |
|---|---|
|
Open a stream file as a handle of a kind / close a handle / read a file’s schema. |
|
Open for reading, create for writing, reopen for appending. |
|
Standard streams, routed through the nexus. |
|
Release a standard stream a dispatch left open. |
|
Next batch as a list block / telling end from an empty batch / handed to a decoder in place. |
|
Write a batch / flush buffered elements / join stream files / elements written to standard output. |
|
Random access into a file: a path into its value, its length, a reader over it, its batch layout. |
|
A channel between pools, its producer, and its end. |
|
A temporary file removed when the call ends / remove it early. |
|
Write a handle into a value as its path, and open a handle from one, when a handle crosses pools. |
Utilities
| Function | Purpose |
|---|---|
|
The ABI version the library was built with. |
|
A call id / a formatted log line for a labeled function. |
|
Record one timing of a benchmarked function. |
|
Print bytes in hexadecimal, for debugging. |