kingkingyyk3
Visitor (Welcome to the Jungle, Baby!)
- Reaction score
- 216
SSA - Safety Struct Allocator
:thup:
JASS:
//////////////////////////////////
// ____ ____
// / __| / ___| /\ SAFETY STRUCT ALLOCATOR v1.0.1
// \ \ \ \ /__\ by kingking
// _\ \ _\ \ / || \
// |___/ |___/ /_| |_\
//
// What is SSA?
// It is a safety struct allocator.
//
// Why use this?
// This is safer than default struct allocator.
//
// Who should use this?
// Who want safety first.
//
// When to use this?
// Anywhere you like. =D
//
// How to use this?
// Put "implement CSA" in your struct.
// Replace vars.onDestroy with vars.customDestroy
// Replace struct.allocate with struct.get
// Replace vars.destroy with vars.release
// CSA featured structs must extends array for safety issues.
//
// Features :
// Recycle indexes
// Double-free protection
//
// Testing(If you want) :
// Run in debug mode.
// Release 2 same struct index. You can see error report.
//
// Requirements :
// Jasshelper (As new as possible)
///////////////////////////////////
library SafetyStructAllocator
public struct METHODS extends array
method customDestroy takes nothing returns nothing
endmethod
endstruct
endlibrary
module SSA
private static delegate SafetyStructAllocator_METHODS FUNCTIONS=0
private static boolean array CSA_instantiated
private static thistype array CSA_removedInstances
private static integer CSA_removedCount = 0
private static integer CSA_counter = 0
private static integer CSA_this
static method get takes nothing returns thistype
if thistype.CSA_removedCount == 0 then
set thistype.CSA_this = thistype.CSA_removedInstances[thistype.CSA_removedCount]
set thistype.CSA_instantiated[thistype.CSA_this] = true
set thistype.CSA_removedCount = thistype.CSA_removedCount - 1
return thistype.CSA_this
endif
set thistype.CSA_counter = thistype.CSA_counter + 1
return thistype.CSA_counter
endmethod
method release takes nothing returns nothing
if thistype.CSA_instantiated[this] then
call this.customDestroy()
set thistype.CSA_instantiated[this] = false
set thistype.CSA_removedCount = thistype.CSA_removedCount + 1
set thistype.CSA_removedInstances[thistype.CSA_removedCount] = this
debug else
debug call BJDebugMsg("Double Free founded on #" + I2S(this) "!")
endif
endmethod
endmodule
:thup: