std.memory.Space owns a named group of mostly static MiniLang values and
binary assets. Values live in stable native blocks outside the managed heap.
Blocks grow automatically; close() releases them all and removes the global
name. Code in another module can retrieve the same space with memory.get(name).
import std.memory as memory
level = memory.Space.new("hollowkeep/crypt")
world = level.load("world", buildLevel) // buildLevel() returns structs/arrays
texture = level.loadBytes("wall", "assets/wall.png")
sound = level.addBytes("ambient", decodedSound)
level.seal()
// In another module:
wall = memory.get("hollowkeep/crypt").get("wall")
print len(wall) // ordinary MiniLang bytes
level.save("crypt.mlspace")
level.close()
restored = memory.Space.fromFile("crypt.mlspace")| Call | Result |
|---|---|
Space.new(name) |
Register a new space; its first block starts at 1 MiB. |
Space.withCapacity(name, bytes) |
Choose the first block's minimum capacity. |
space.add(name, value) |
Clone a MiniLang value graph into the space and register the root. |
space.load(name, loader) |
Call loader(), then clone its result. |
space.addBytes(name, data) |
Return an ordinary region-owned bytes copy. |
space.loadBytes(name, path) |
Read a file directly into an ordinary region-owned bytes value. |
space.addBlob(name, data) / space.loadBlob(name, path) |
Store raw native payloads for pointer-based APIs. Return Blob views. |
space.get(name) / space.has(name) |
Find a named value, or return void / false. |
space.remove(name) |
Remove a name. The occupied storage is reclaimed by close(). |
space.seal() |
Stop additions and protect completed blocks as read-only. |
space.save(path) / Space.fromFile(path) |
Save and restore names, supported graphs, bytes and blobs. |
space.close() |
Release every block and unregister the space. |
Names are unique within each live space. After remove, the name may be reused.
Adding a duplicate or adding to a sealed or closed space returns an error.
Space.close() is idempotent. Access through a value retained after close() is
invalid; the program must stop all users of the space before closing it.
Space.add supports strings, arrays, structs, bytes, floats, primitive values,
shared references and cycles. It clones the reachable graph so the GC does not
need to trace region contents. It rejects functions, closures, threads and space
metadata. Do not insert native pointer fields into a value graph; a pointer to
unmapped memory cannot be safely inspected. A failed clone rolls back its native
allocations. Graphs already in another space can be cloned into an independent
space. Treat cloned values as immutable after insertion: direct field or
array writes can insert managed references that the GC cannot see. seal()
physically rejects writes on Windows and Linux; a write through a retained value
after sealing can terminate the process.
binary struct and binary class values can also be cloned. Their fixed-width
field bytes include zeroed alignment padding. Declared bytes, string and
array fields (including typed arrays) hold MiniLang references; the graph walker copies and relocates
those referenced objects and ignores scalar bytes. The returned value retains
ordinary field and method access. See the
binary layout rules.
If a reference field in a region-owned object is later assigned a managed
object outside the space, save() rejects that external pointer. Build the
complete graph before add() when a portable snapshot is needed.
loadBytes reads in chunks directly into the final region-owned bytes
object, so APIs such as std.fs.writeAllBytes can use it without Blob.toBytes().
add and load first build a managed source graph and then clone it, so their
peak memory includes both copies during loading. For very large individual
binary assets, prefer loadBytes.
save() writes a versioned binary format. It preserves root names, object
identity, cycles, primitive values, bytes and blobs. Reachable objects are
streamed in 1 MiB chunks; object references are stored as relocations and
patched during fromFile(). Removed and unreachable objects are omitted. A
snapshot rejects unsupported external pointers before it creates the file.
The legacy binary-only MLSPACE1 format remains readable; mixed and graph
snapshots use MLSPACE2.
Restoring structs requires the same MiniLang struct definitions and compatible compiler build: the snapshot stores runtime struct IDs, not a schema. Binary objects additionally require the same binary layout size and runtime type ID. Native GPU or audio handles are not serialized. Each object or blob is limited to 1 GiB, and a snapshot is limited to one million names and one million graph objects. Malformed and truncated snapshots return an error and release partially loaded blocks.
On Windows and Linux, seal() uses OS read-only page protection. MinUX currently
supports the logical seal but not physical page protection. loadBytes and
loadBlob use native reads on all three targets.