New test module 'tracegc'

New module easies the inclusion of GC tracing in individual test files.
This commit is contained in:
Roberto Ierusalimschy
2021-03-01 13:54:29 -03:00
parent f9d857a81b
commit 5276973224
4 changed files with 54 additions and 18 deletions

40
testes/tracegc.lua Normal file
View File

@@ -0,0 +1,40 @@
-- track collections
local M = {}
-- import list
local setmetatable, stderr, collectgarbage =
setmetatable, io.stderr, collectgarbage
_ENV = nil
local active = false
-- each time a table is collected, remark it for finalization on next
-- cycle
local mt = {}
function mt.__gc (o)
stderr:write'.' -- mark progress
if active then
setmetatable(o, mt) -- remark object for finalization
end
end
function M.start ()
if not active then
active = true
setmetatable({}, mt) -- create initial object
end
end
function M.stop ()
if active then
active = false
collectgarbage() -- call finalizer for the last time
end
end
return M