- Joined
- Nov 30, 2007
- Messages
- 1,202
Will this work or am I missing something? I'm trying to create a hash based allocation and I'm wondering if I've failed to take something into account.
Secondly, whats the correct way to check for double free? I would like to raise an exception if that happens.
The purpose is if you want to use Table based allocation and be given a unique key, but don't want to be slowed down by using method wrappers from Bribe's Table, such as when you're creating Table based data structures.
Example Usage:
Forgot to update changes: the free method should flush the child key and the method operators should be private.
Secondly, whats the correct way to check for double free? I would like to raise an exception if that happens.
JASS:
library HashRecyler
struct HashRecyler extends array
readonly static hashtable table = InitHashtable()
static method operator[] takes integer k returns integer
return LoadInteger(table, -1, k)
endmethod
static method operator []= takes integer k, integer tb returns nothing
call SaveInteger(table, -1, k, tb)
endmethod
private static method onInit takes nothing returns nothing
set thistype[0] = 1
endmethod
static method alloc takes nothing returns integer
local integer t = thistype[0]
if (thistype[t] == 0) then
set thistype[0] = t + 1
else
set thistype[0] = thistype[t]
endif
call BJDebugMsg("Allocated: " + I2S(t))
return t
endmethod
static method free takes integer t returns nothing
call BJDebugMsg("deallocated: " + I2S(t))
set thistype[t] = thistype[0]
set thistype[0] = t
endmethod
endstruct
endlibrary
The purpose is if you want to use Table based allocation and be given a unique key, but don't want to be slowed down by using method wrappers from Bribe's Table, such as when you're creating Table based data structures.
Example Usage:
JASS:
struct List extends array
private static hashtable ht
private static method onInit takes nothing returns nothing
set ht = HashRecyler.table
endmethod
static method create takes nothing returns thistype
local thistype this = HashRecyler .alloc()
return this
endmethod
method destroy takes nothing returns nothing
call HashRecyler .free(this)
endmethod
method operator length= takes integer index returns nothing
call SaveInteger(ht, this, -1, index)
endmethod
method operator length takes nothing returns integer
return LoadInteger(ht, this, -1)
endmethod
method clear takes nothing returns nothing
call FlushChildHashtable(ht, this)
set .length = 0
endmethod
....
endstruct
JASS:
local thistype this = Table(this).create() ---> local thistype this = HashRecyler.alloc()
Table(this).destroy() ---> HashRecyler.free(this)
Table(this).integer[index] = value --> SaveInteger(ht, this, index, value)
etc
Forgot to update changes: the free method should flush the child key and the method operators should be private.
Last edited: