Skip to content

Instantly share code, notes, and snippets.

@PProvost
Created November 3, 2010 19:31
Show Gist options
  • Select an option

  • Save PProvost/661562 to your computer and use it in GitHub Desktop.

Select an option

Save PProvost/661562 to your computer and use it in GitHub Desktop.
A simple resource pool implementation in lua
-- Replace this function with your "new" function
local function NewItem()
return { }
end
-- Storage of released items
local pool = {}
-- Get/create a new item
local function Acquire()
local item = next(pool)
if not item then
item = NewItem()
else
pool[item] = nil
end
return item
end
-- Release an item to the pool
local function Release(item)
if pool[item] then
error("Attempt to release a item that was already released")
end
pool[item] = true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment