| 1 | (* Map.done: the slots given back to the allocator, through one that |
| 2 | counts its blocks; the map empty after, and used again. *) |
| 3 | module MapDone; |
| 4 | import Fmt, Heap, Mem, Str, Names := Map([]const U8, INT, Str.hash, Str.eq); |
| 5 | import SYSTEM; |
| 6 | |
| 7 | var counting: Mem.Allocator; live: INT; |
| 8 | |
| 9 | proc alloc(_a: ^Mem.Allocator; size, align: INT): ^U8; |
| 10 | begin |
| 11 | INC(live); |
| 12 | return Mem.alloc(Heap.heap, size, align); |
| 13 | end; |
| 14 | |
| 15 | proc free(_a: ^Mem.Allocator; p: ^U8); |
| 16 | begin |
| 17 | DEC(live); |
| 18 | Mem.free(Heap.heap, p); |
| 19 | end; |
| 20 | |
| 21 | proc main*; |
| 22 | var m: Names.Map; i, v: INT; found, _ok: BOOL; keys: [3][]const U8; |
| 23 | begin |
| 24 | counting := {alloc: alloc, free: free}; |
| 25 | keys := ["node_modules", "target", "build"]; |
| 26 | Names.init(m, SYSTEM.ADR(counting)); |
| 27 | Names.done(m); (* nothing taken yet *) |
| 28 | for i := 0 to 99 do _ok := Names.put(m, keys[i mod 3], i); end; |
| 29 | Fmt.print("{} entries, {} block\n", [Fmt.int(m.len), Fmt.int(live)]); |
| 30 | Names.done(m); |
| 31 | v, found := Names.get(m, "target"); |
| 32 | Fmt.print("{} entries, {} blocks, found {}\n", [Fmt.int(m.len), Fmt.int(live), Fmt.bool(found)]); |
| 33 | _ok := Names.put(m, "target", 7); |
| 34 | v, found := Names.get(m, "target"); |
| 35 | Fmt.print("{} entry, {}\n", [Fmt.int(m.len), Fmt.int(v)]); |
| 36 | Names.done(m); |
| 37 | Fmt.print("{} blocks\n", [Fmt.int(live)]); |
| 38 | end; |
| 39 | |
| 40 | end MapDone. |