Skip to content

Instantly share code, notes, and snippets.

@raingloom
Created May 5, 2016 21:34
Show Gist options
  • Save raingloom/41ba47d4cb366825e20710eb88c095d0 to your computer and use it in GitHub Desktop.
Save raingloom/41ba47d4cb366825e20710eb88c095d0 to your computer and use it in GitHub Desktop.
table allocation management for Lua
local free, free_l = {}, 0
local _print = print
local function dealloc( t )
free_l = free_l + 1
free[ free_l ] = t
return true
end
local mt = { __metatable = false, __gc = dealloc }
local function alloc()
if free_l > 0 then
free_l = free_l -1
return free[ free_l + 1 ]
else
return setmetatable( {}, mt )
end
end
local function purge( t )
for k in next, t do
rawset( t, k, nil )
end
end
return {
alloc = alloc,
purge = purge,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment