Created
May 5, 2016 21:34
-
-
Save raingloom/41ba47d4cb366825e20710eb88c095d0 to your computer and use it in GitHub Desktop.
table allocation management for Lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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