From 99fc7b68449981c02e80d9f565d13e4f78ce88e6 Mon Sep 17 00:00:00 2001 From: "Johnny C. Lam" Date: Wed, 23 Oct 2013 07:18:58 +0000 Subject: [PATCH] Clean up table pool modules. Modify constructors so that prototype inheritance works properly. Remove the Reset() method and just call Drain() from the constructor to empty out the pool to start. git-svn-id: svn://svn.curseforge.net/wow/ovale/mainline/trunk@1092 d5049fe3-3747-40f7-a4b5-f36d6801af5f --- OvalePool.lua | 32 +++++++++++--------------------- OvalePoolGC.lua | 25 ++++++++++++------------- 2 files changed, 23 insertions(+), 34 deletions(-) diff --git a/OvalePool.lua b/OvalePool.lua index e150157..f6536d0 100644 --- a/OvalePool.lua +++ b/OvalePool.lua @@ -29,18 +29,19 @@ OvalePool.unused = 0 OvalePool.__index = OvalePool -- --- +-- do - local function NewPool(...) - local obj = setmetatable({ name = ... }, OvalePool) - obj:Reset() - return obj - end - setmetatable(OvalePool, { __call = function(_, ...) return NewPool(...) end }) + -- Class constructor + setmetatable(OvalePool, { __call = function(self, ...) return self:NewPool(...) end }) +end + +function OvalePool:NewPool(name) + name = name or self.name + local obj = setmetatable({ name = name }, self) + obj:Drain() + return obj end --- --- function OvalePool:Get() assert(self.pool) local item = tremove(self.pool) @@ -61,19 +62,8 @@ function OvalePool:Release(item) 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:Reset() self.pool = {} - self.size = 0 + self.size = self.size - self.unused self.unused = 0 end diff --git a/OvalePoolGC.lua b/OvalePoolGC.lua index 3f8d480..acb904d 100644 --- a/OvalePoolGC.lua +++ b/OvalePoolGC.lua @@ -23,29 +23,28 @@ OvalePoolGC.size = 0 OvalePoolGC.__index = OvalePoolGC -- --- +-- do - local function NewPool(...) - local obj = setmetatable({ name = ... }, OvalePoolGC) - obj:Reset() - return obj - end - setmetatable(OvalePoolGC, { __call = function(_, ...) return NewPool(...) end }) + -- Class constructor + setmetatable(OvalePoolGC, { __call = function(self, ...) return self:NewPool(...) end }) +end + +function OvalePoolGC:NewPool(name) + name = name or self.name + return setmetatable({ name = name }, self) end --- --- function OvalePoolGC:Get() -- Keep running count of total number of tables allocated. self.size = self.size + 1 return {} end --- The Release and Drain methods are no-ops. -function OvalePoolGC:Release(item) end -function OvalePoolGC:Drain() end +function OvalePoolGC:Release(item) + -- no-op +end -function OvalePoolGC:Reset() +function OvalePoolGC:Drain() self.size = 0 end -- 1.7.9.5