Add simple memory pool implementation in OvalePool.
Johnny C. Lam [03-16-13 - 08:03]
Add simple memory pool implementation in OvalePool.
git-svn-id: svn://svn.curseforge.net/wow/ovale/mainline/trunk@775 d5049fe3-3747-40f7-a4b5-f36d6801af5f
diff --git a/Ovale.toc b/Ovale.toc
index 17e20cb..858c827 100644
--- a/Ovale.toc
+++ b/Ovale.toc
@@ -32,6 +32,7 @@ OvaleEnemies.lua
OvaleEquipement.lua
OvaleGUID.lua
OvalePaperDoll.lua
+OvalePool.lua
OvaleRecount.lua
OvaleSkada.lua
OvaleSpellDamage.lua
diff --git a/OvalePool.lua b/OvalePool.lua
new file mode 100644
index 0000000..2273396
--- /dev/null
+++ b/OvalePool.lua
@@ -0,0 +1,62 @@
+--[[--------------------------------------------------------------------
+ Ovale Spell Priority
+ Copyright (C) 2013 Johnny C. Lam
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License in the LICENSE
+ file accompanying this program.
+--]]--------------------------------------------------------------------
+
+-- Simple resource pool.
+local _, Ovale = ...
+local OvalePool = {}
+Ovale.poolPrototype = OvalePool
+
+--<private-static-properties>
+OvalePool.name = "OvalePool"
+OvalePool.pool = nil
+OvalePool.size = 0
+OvalePool.unused = 0
+--</private-static-properties>
+
+--<public-static-methods>
+function OvalePool:NewPool(name)
+ obj = { name = name, pool = {}, size = 0, unused = 0 }
+ setmetatable(obj, { __index = self })
+ return obj
+end
+
+function OvalePool:Get()
+ assert(self.pool)
+ local item = tremove(self.pool)
+ if item then
+ self.unused = self.unused - 1
+ else
+ self.size = self.size + 1
+ item = {}
+ end
+ return item
+end
+
+function OvalePool:Release(item)
+ assert(self.pool)
+ wipe(item)
+ tinsert(self.pool, item)
+ self.unused = self.unused + 1
+end
+
+function OvalePool:Drain()
+ assert(self.pool)
+ while true do
+ if not tremove(self.pool) then
+ break
+ end
+ end
+ self.size = self.size - self.unused
+ self.unused = 0
+end
+
+function OvalePool:Debug()
+ Ovale:Print("Pool " .. tostring(self.name) .. " has size " .. self.size .. " with " .. self.unused .. " item(s).")
+end
+--</public-static-methods>