- Joined
- Sep 9, 2007
- Messages
- 6,759
Hey guys, like always, please give comments/criticism/suggestions.
Basically, it's just Alloc using my Stack library.
Basically, it's just Alloc using my Stack library.
JASS:
// ---------------------------------------------------------------------------------------------------------------------------
//
// AllocateStack
// ===========
//
// Version: 1.0.0
// Author: Anachron
//
// Requirements:
// Stack [by Anachron]
// (New) Table [by Bribe] (v. 3.1)
//
// Description:
// AllocateStack is a custom struct allocator that
// features a sleek but useful way to allocate and deallocate instances.
//
// History:
// 1.0.0: Initial Release
//
// API:
// thistype.allocate() --> create a new instance
// thistype.deallocate() --> destroy current instance and recycle it
//
// ---------------------------------------------------------------------------------------------------------------------------
library AllocateStack requires Stack
private module InitAllocateStack
private static method onInit takes nothing returns nothing
set thistype.RecycleBin = Stack.create()
set thistype.InstanceBin = Table.create()
endmethod
endmodule
module IsAllocateStack
private static Stack RecycleBin = 0
private static Stack InstanceBin = 0
private static integer Allocated = 0
private static integer Max = 8191
implement InitAllocateStack
public static method allocate takes nothing returns thistype
local thistype this = thistype.RecycleBin.getFirst()
if this == 0 then
if thistype.Allocated < thistype.Max then
set thistype.Allocated = thistype.Allocated +1
set this = thistype.Allocated
endif
endif
if this != 0 then
set thistype.InstanceBin.add(this)
else
set this = 1/0
endif
return this
endmethod
public static method isAllocated takes thistype instance returns boolean
return thistype.InstanceBin.has(instance)
endmethod
public method deallocate takes nothing returns nothing
if not thistype.isAllocated(this) then
return
endif
call thistype.RecybleBin.add(this)
call thistype.InstanceBin.delete(this)
endmethod
endmodule
struct AllocateStack
implement IsAllocateStack
endstruct
endlibrary