Molten

Memory and arenas

Cast has no garbage collector. Every allocation belongs to an arena (a region freed all at once) or to engine object storage, and you know its lifetime from where it was allocated.

Arena Lives until Use for
frame The end of the current frame Temporary lists, strings for this frame, query results. The default
level The current level unloads Level-wide caches, navigation data
game The game exits Long-lived systems, registries, settings
screen Its screen leaves the stack Menu state, per-screen data. Each screen on the stack has its own
heap You call free(x) The rare case nothing else fits. Leaks are reported in dev builds
Object storage The object is destroyed Component data. Never allocated directly

Choosing an arena

new allocates classes, arrays, maps and strings. Without an arena it uses frame:

class Note { text: string = ""; }

export function main(): void {
    const temp = new Note();                         // frame
    const kept = new(game) Note();                   // until exit
    const list = new(level) Array<int>();
    const names = new(screen) Map<string, int>();
    const label = new(game) string("Player ", 1);
    const owned = new(heap) Array<int>();
    owned.push(1, 2, 3);
    print(temp.text == "", kept.text == "", list.length, names.size, label, owned.length);
    free(owned);
}
true true 0 0 Player 1 3

An unknown arena name is an error (unknown arena 'scope'). In a standalone program (castc run) all arenas live until the program exits.

free

free(x) releases a heap class instance, string, Array or Map. Anything else is an error (free requires a heap allocation). Freeing a collection releases its storage, not the objects its elements refer to. Using a freed value is caught in dev builds.

Don't let memory escape its arena

Storing a short-lived allocation where something longer-lived can reach it would leave a dangling reference. The compiler rejects the cases it can see:

Storing Into Error
A frame value A component, a static field, or a level/game/screen/heap object frame allocation escapes into component storage / ... into game storage
A level or screen value game or heap memory level allocation escapes into game storage

level and screen lifetimes are unrelated, so one can't be stored in the other either. The analysis follows assignments, literals, constructors and slices within a function; it can't see through every function call. Dev builds check the rest at runtime: each arena reference carries a stamp, and using one whose arena has been reset is reported.

Where state lives in a game

Gameplay code is unloaded and replaced on every save, so it may not keep state of its own:

  • Components hold per-object state. This is the main place.
  • Class static fields hold module-wide state; their storage is in engine game memory. Point them at game, level or screen allocations.
  • Module-level let is a compile error.
  • Hand-written C uses api->game_memory("key", size, align) for named blocks that survive reloads.

Strings

String literals are static. Built strings (+, templates, string(x)) live in frame unless you write new(arena) string(...).

View this page's source on GitHub