Created
November 3, 2010 19:31
-
-
Save PProvost/661562 to your computer and use it in GitHub Desktop.
A simple resource pool implementation in lua
This file contains hidden or 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
| -- 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