Alloc

JASS:
//globals
//udg_AllocTable = hashtable
//udg_Alloc = boolean array
//udg_Alloc_R = integer array
//udg_Alloc_Count = integer

//local integer alloc = CreateAlloc()
//local integer index = Allocate(alloc)
//set someData[index] = 01101010
//set someData[index] = 0
//call Deallocate(index)

constant function Alloc takes nothing returns hashtable
    return udg_AllocTable
endfunction

function CreateAlloc takes nothing returns integer
    local integer i = udg_Alloc_R[0]
	if 0 == i then
	    set i = udg_Alloc_Count + 1
		set udg_Alloc_Count = i
	else
	    set udg_Alloc_R[0] = udg_Alloc_R[i]
	endif
	set udg_Alloc[i] = true
    return i
endfunction

function DestroyAlloc takes integer alloc returns nothing
    set udg_Alloc_R[alloc] = udg_Alloc_R[0]
	set udg_Alloc_R[0] = alloc
	set udg_Alloc[alloc] = false
endfunction

function Allocate takes integer alloc returns integer
    local integer i
	if null == udg_AllocTable then
	    set udg_AllocTable = InitHashtable()
	endif
	if udg_Alloc[alloc] then
	    set i = LoadInteger(Alloc(), alloc, 0)
		if 0 == i then
		    set i = LoadInteger(Alloc(), alloc, 8193) + 1
			call SaveInteger(Alloc(), alloc, 8193, i)
		else
		    call SaveInteger(Alloc(), alloc, 0, LoadInteger(Alloc(), alloc, i))
		endif
		return i
	endif
	return 0
endfunction

function Deallocate takes integer alloc, integer i returns nothing
    call SaveInteger(Alloc(), alloc, i, LoadInteger(Alloc(), alloc, 0))
	call SaveInteger(Alloc(), alloc, 0, i)
endfunction
Last edited:
Top