diff options
Diffstat (limited to 'testes/tracegc.lua')
-rw-r--r-- | testes/tracegc.lua | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/testes/tracegc.lua b/testes/tracegc.lua new file mode 100644 index 00000000..9c5c1b3f --- /dev/null +++ b/testes/tracegc.lua | |||
@@ -0,0 +1,40 @@ | |||
1 | -- track collections | ||
2 | |||
3 | local M = {} | ||
4 | |||
5 | -- import list | ||
6 | local setmetatable, stderr, collectgarbage = | ||
7 | setmetatable, io.stderr, collectgarbage | ||
8 | |||
9 | _ENV = nil | ||
10 | |||
11 | local active = false | ||
12 | |||
13 | |||
14 | -- each time a table is collected, remark it for finalization on next | ||
15 | -- cycle | ||
16 | local mt = {} | ||
17 | function mt.__gc (o) | ||
18 | stderr:write'.' -- mark progress | ||
19 | if active then | ||
20 | setmetatable(o, mt) -- remark object for finalization | ||
21 | end | ||
22 | end | ||
23 | |||
24 | |||
25 | function M.start () | ||
26 | if not active then | ||
27 | active = true | ||
28 | setmetatable({}, mt) -- create initial object | ||
29 | end | ||
30 | end | ||
31 | |||
32 | |||
33 | function M.stop () | ||
34 | if active then | ||
35 | active = false | ||
36 | collectgarbage() -- call finalizer for the last time | ||
37 | end | ||
38 | end | ||
39 | |||
40 | return M | ||