The context system in Odin
Every proc carries an implicit context; the temp allocator is its cheapest trick.
package main
import "core:fmt"
main :: proc() {
// Every proc carries an implicit context with two allocators.
fmt.println("default:", context.allocator != {})
// tprintf builds strings on the temp allocator -- no delete needed.
tag := fmt.tprintf("%s berth %d", "Valparaiso", 9)
fmt.println("tag:", tag)
// One call reclaims everything temporary at a natural boundary.
free_all(context.temp_allocator)
fmt.println("temp storage swept")
}
How it works
context.allocatorandcontext.temp_allocatorride along on every call.fmt.tprintfbuilds a string on temp storage — nothing to delete.free_all(context.temp_allocator)sweeps it all at a natural boundary.
Keywords and builtins used here
contextfree_allimportpackageproc
The run, in numbers
- Lines
- 16
- Characters to type
- 442
- Tokens
- 73
- Three-star pace
- 65 tpm
At the three-star pace of 65 tokens a minute, this run takes about 67 seconds.
Step 3 of 3 in Idioms, step 18 of 27 in Language basics.