Map.done: its slots given back, leaving it empty Met in rwalk, whose table of plain-name rules lives for one directory of a walk: Buf and Vec had done, Map had none, so its memory could only be dropped. done frees the slots, if any, and leaves the map as init does, to be used again. Library growth that breaks no program; test/run/MapDone counts the blocks through an allocator of its own.
| @@ -491,29 +491,6 @@ | ||
| 491 | 491 | breaks a `match` on `Status` that names all four, so it is left out: |
| 492 | 492 | the shell knows when it continues a job itself. |
| 493 | 493 | |
| 494 | -## `Map.done` | |
| 495 | - | |
| 496 | -`Map` takes memory from its allocator as it grows and frees the old | |
| 497 | -slots, but never gives the last ones back: a map made over `Heap` for | |
| 498 | -a while, as a table kept for one directory of a walk, can't be | |
| 499 | -emptied of its memory, only dropped, and the memory is lost. `Buf` | |
| 500 | -and `Vec` each have a `done`; `Map` has none, and is the odd one out. | |
| 501 | - | |
| 502 | -``` | |
| 503 | -proc done*(m: ^Map); (* its memory given back, leaving it empty *) | |
| 504 | -``` | |
| 505 | - | |
| 506 | -As `Vec.done`: free the slots if there are any, then `cap` and `len` | |
| 507 | -0, so the map can be used again and grows anew. `init` already leaves | |
| 508 | -it so, with no memory taken until the first `put`. | |
| 509 | - | |
| 510 | -Met in rwalk's `Rwignore`, which keeps a table of the rules that are a | |
| 511 | -plain name (`node_modules`, `target`) for each ignore file, made as the | |
| 512 | -walk enters a directory and dropped as it leaves. Without `done` it | |
| 513 | -used a `Vec` of rule numbers instead, searched in turn. Library growth | |
| 514 | -that breaks no program; about ten lines, with a test in `Map`'s | |
| 515 | -`main`. | |
| 516 | - | |
| 517 | 494 | ## Enumerations' names, each its own |
| 518 | 495 | |
| 519 | 496 | An enumeration's names are declared in the scope around its type |
| @@ -520,6 +520,7 @@ | ||
| 520 | 520 | proc remove*(m: ^Map; k: K): BOOL; (* false if k wasn't there *) |
| 521 | 521 | proc get*(m: ^Map; k: K): (V, BOOL); (* what k maps to, and whether it maps to anything *) |
| 522 | 522 | proc next*(m: ^Map; i: INT): (INT, K, V); (* the entry after i *) |
| 523 | +proc done*(m: ^Map); (* its memory given back, leaving it empty *) | |
| 523 | 524 | |
| 524 | 525 | module Vec(T); |
| 525 | 526 | type Vec* = record len*: INT; ... end; |
| @@ -115,4 +115,14 @@ | ||
| 115 | 115 | return -1, k, v; |
| 116 | 116 | end; |
| 117 | 117 | |
| 118 | +(* Gives the map's memory back to its allocator, leaving it empty, as | |
| 119 | + init does: it may be used again, and grows anew. *) | |
| 120 | +proc done*(m: ^Map); | |
| 121 | +begin | |
| 122 | + if m^.slots # nil then Mem.free(m^.a, ^U8(m^.slots)); end; | |
| 123 | + m^.slots := nil; | |
| 124 | + m^.cap := 0; | |
| 125 | + m^.len := 0; | |
| 126 | +end; | |
| 127 | + | |
| 118 | 128 | end Map. |
| @@ -0,0 +1,4 @@ | ||
| 1 | +3 entries, 1 block | |
| 2 | +0 entries, 0 blocks, found false | |
| 3 | +1 entry, 7 | |
| 4 | +0 blocks |
| @@ -0,0 +1,39 @@ | ||
| 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([]U8, INT, Str.hash, Str.eq); | |
| 5 | + | |
| 6 | +var counting: Mem.Allocator; live: INT; | |
| 7 | + | |
| 8 | +proc alloc(_a: ^Mem.Allocator; size, align: INT): ^U8; | |
| 9 | +begin | |
| 10 | + INC(live); | |
| 11 | + return Mem.alloc(Heap.heap, size, align); | |
| 12 | +end; | |
| 13 | + | |
| 14 | +proc free(_a: ^Mem.Allocator; p: ^U8); | |
| 15 | +begin | |
| 16 | + DEC(live); | |
| 17 | + Mem.free(Heap.heap, p); | |
| 18 | +end; | |
| 19 | + | |
| 20 | +proc main*; | |
| 21 | +var m: Names.Map; i, v: INT; found, _ok: BOOL; keys: [3][]U8; | |
| 22 | +begin | |
| 23 | + counting := {alloc: alloc, free: free}; | |
| 24 | + keys := ["node_modules", "target", "build"]; | |
| 25 | + Names.init(ADR(m), ADR(counting)); | |
| 26 | + Names.done(ADR(m)); (* nothing taken yet *) | |
| 27 | + for i := 0 to 99 do _ok := Names.put(ADR(m), keys[i mod 3], i); end; | |
| 28 | + Fmt.print("{} entries, {} block\n", [Fmt.int(m.len), Fmt.int(live)]); | |
| 29 | + Names.done(ADR(m)); | |
| 30 | + v, found := Names.get(ADR(m), "target"); | |
| 31 | + Fmt.print("{} entries, {} blocks, found {}\n", [Fmt.int(m.len), Fmt.int(live), Fmt.bool(found)]); | |
| 32 | + _ok := Names.put(ADR(m), "target", 7); | |
| 33 | + v, found := Names.get(ADR(m), "target"); | |
| 34 | + Fmt.print("{} entry, {}\n", [Fmt.int(m.len), Fmt.int(v)]); | |
| 35 | + Names.done(ADR(m)); | |
| 36 | + Fmt.print("{} blocks\n", [Fmt.int(live)]); | |
| 37 | +end; | |
| 38 | + | |
| 39 | +end MapDone. |