- Joined
- May 9, 2014
- Messages
- 1,820
Introducing:
*TimerAlloc*
JASS:
library TimerAlloc
private struct TimerAlloc extends array
private static integer MAX_STACK = 500
private static constant integer SUPER_MAX_STACK = 8000
private static thistype current = 0
private static thistype last = 0
private thistype next
private thistype recNext
private timer TimerStack
static method getTimerType takes timer t returns thistype
local thistype this = thistype(0).next
local integer i = 1
loop
exitwhen this.TimerStack == t or i == MAX_STACK
set this = this.next
set i = i + 1
endloop
if this.TimerStack != t then
set this = last + 1
set MAX_STACK = this
set last.next = this
set last = last.next
set last.TimerStack = t
set last.next = 0
endif
return this
endmethod
static method dealloc takes timer t returns nothing
local thistype this = thistype.getTimerType(t)
call PauseTimer(this.TimerStack)
set this.recNext = recNext
set recNext = this
endmethod
static method alloc takes nothing returns timer
local thistype this
if thistype(0).recNext == 0 then
set current = current.next
set this = current
else
set this = thistype(0).recNext
set thistype(0).recNext = thistype(0).recNext.recNext
endif
return this.TimerStack
endmethod
private static method onInit takes nothing returns nothing
local thistype i = 1
loop
set i.TimerStack = CreateTimer()
call BJDebugMsg(I2S(i))
set i.next = 0 //next[1] == next[0] = 0, next[2] == 0
set thistype(0).next.next = i //next[next[0]] == next[0] = 1, next[next[0]] == next[1] = 2
set thistype(0).next = i //next[0] = 1
exitwhen i == MAX_STACK
set i = i + 1
endloop
set last = i
set thistype(0).next = 1 //Basically goes back to 1.
endmethod
endstruct
function DeallocateTimer takes timer whichTimer returns nothing
call TimerAlloc.dealloc(whichTimer)
endfunction
function AllocateTimer takes nothing returns timer
return TimerAlloc.alloc()
endfunction
endlibrary
A simple snippet of sorts, it allows you to use and recycle already created timers...
[At this point, this would be an unverified claim]
...so that you don't consume nearly as much memory as you do when creating timers and destroying them.
Last edited: