raz / test / run / MapDone.raz

40 lines · 1.2 KiB · raw · history · last change 264716cb

1(* Map.done: the slots given back to the allocator, through one that
2 counts its blocks; the map empty after, and used again. *)
3module MapDone;
4import Fmt, Heap, Mem, Str, Names := Map([]const U8, INT, Str.hash, Str.eq);
5import SYSTEM;
6
7var counting: Mem.Allocator; live: INT;
8
9proc alloc(_a: ^Mem.Allocator; size, align: INT): ^U8;
10begin
11 INC(live);
12 return Mem.alloc(Heap.heap, size, align);
13end;
14
15proc free(_a: ^Mem.Allocator; p: ^U8);
16begin
17 DEC(live);
18 Mem.free(Heap.heap, p);
19end;
20
21proc main*;
22var m: Names.Map; i, v: INT; found, _ok: BOOL; keys: [3][]const U8;
23begin
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)]);
38end;
39
40end MapDone.