lua

A copy of the Lua development repository
Log | Files | Refs | README

tracegc.lua (680B)


      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