- Joined
- Dec 4, 2006
- Messages
- 110
Alloc
Version 1.09
Version 1.09
Requirements
- JASS NewGen
JASS:
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~ Alloc ~~ By Sevion ~~ Version 1.09 ~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// What is Alloc?
// - Alloc implements an intuitive allocation method for array structs
//
// =Pros=
// - Efficient.
// - Simple.
// - Less overhead than regular structs.
//
// =Cons=
// - Must use array structs (hardly a con).
// - Must manually call OnDestroy.
// - Must use Delegates for inheritance.
// - No default values for variables (use onInit instead).
// - No array members (use another Alloc struct as a linked list or type declaration).
//
// Methods:
// - struct.allocate()
// - struct.deallocate()
//
// These methods are used just as they should be used in regular structs.
//
// Modules:
// - Alloc
// Implements the most basic form of Alloc. Includes only create and destroy
// methods.
//
// Details:
// - Less overhead than regular structs
//
// - Use array structs when using Alloc. Put the implement at the top of the struct.
//
// - Alloc operates almost exactly the same as default structs in debug mode with the exception of onDestroy.
//
// How to import:
// - Create a trigger named Alloc.
// - Convert it to custom text and replace the whole trigger text with this.
//
// Thanks:
// - Nestharus for the method of allocation and suggestions on further merging.
// - Bribe for suggestions like the static if and method names.
// - PurgeandFire111 for some suggestions like the merging of Alloc and AllocX as well as OnDestroy stuff.
//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
library Alloc
module Alloc
private static integer instanceCount = 0
private thistype recycle
static method allocate takes nothing returns thistype
local thistype this
if (thistype(0).recycle == 0) then
debug if (instanceCount == 8190) then
debug call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "Alloc ERROR: Attempted to allocate too many instances!")
debug return 0
debug endif
set instanceCount = instanceCount + 1
set this = instanceCount
else
set this = thistype(0).recycle
set thistype(0).recycle = thistype(0).recycle.recycle
endif
debug set this.recycle = -1
return this
endmethod
method deallocate takes nothing returns nothing
debug if (this.recycle != -1) then
debug call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "Alloc ERROR: Attempted to deallocate an invalid instance at [" + I2S(this) + "]!")
debug return
debug endif
set this.recycle = thistype(0).recycle
set thistype(0).recycle = this
endmethod
endmodule
endlibrary
JASS:
scope test initializer dotest
private struct teststruct extends array
implement Alloc
private string data
public static method create takes nothing returns thistype
//call BJDebugMsg("Created")
return thistype.allocate()
endmethod
public method test takes nothing returns nothing
call BJDebugMsg("Instance: " + I2S(this))
endmethod
public method destroy takes nothing returns nothing
call BJDebugMsg("Destroying: " + I2S(this))
call this.deallocate()
endmethod
endstruct
private function dotest takes nothing returns nothing
local teststruct a = teststruct.create()
local teststruct b = teststruct.create()
local teststruct c = teststruct.create()
local teststruct d = teststruct.create()
call a.test()
call b.test()
call c.test()
call d.test()
call a.destroy()
call b.destroy()
call c.destroy()
call d.destroy()
set a = teststruct.create()
set b = teststruct.create()
set c = teststruct.create()
set d = teststruct.create()
call a.test()
call b.test()
call c.test()
call d.test()
endfunction
endscope
Should be faster than all other structs. This should become the new struct standard
Updates
- Version 1.09: Moved the overflow check for some efficiency.
- Version 1.08: More debug fixes.
- Version 1.07: Fixed some debug things.
- Version 1.06: Reverted debug changes.
- Version 1.05: Fixed some debug things.
- Version 1.04: Removed OnDestroy.
- Version 1.03: Removed a boolean and made things a bit more efficient.
- Version 1.02: Minor updates. Removed AllocX and AllocXS.
- Version 1.01: Minor updates and renamed functions.
- Version 1.00: Release.
Last edited by a moderator: