14 years
edited 8 years
1 library AllocT /* v1.0.2.0
2 *************************************************************************************3 *4 * */uses/*
5 *6 * */ ErrorMessage /* https://wc3modding.info/4608/errormessage/
7 * */ Table /* https://wc3modding.info/4611/snippet-new-table/
8 *9 *************************************************************************************10 *11 * Minimizes code generation and global variables while maintaining12 * excellent performance.13 *14 * Uses hashtable instead of array, which drastically reduces performance15 * but uncaps the instance limit. Should use with table fields instead of16 * array fields.17 *18 * local thistype this = recycler[0]19 *20 * if (recycler[this] == 0) then21 * set recycler[0] = this + 122 * else23 * set recycler[0] = recycler[this]24 * endif25 *26 ************************************************************************************27 *28 * module AllocT29 *30 * static method allocate takes nothing returns thistype31 * method deallocate takes nothing returns nothing32 *33 * readonly boolean isAllocated34 *35 * debug static method calculateMemoryUsage takes nothing returns integer36 * debug static method getAllocatedMemoryAsString takes nothing returns string37 *38 ************************************************************************************/39 module AllocT40 /*41 * stack42 */43 private static Table recycler
44
45 /*46 * list of allocated memory47 */48 debug private static Table allocatedNext
49 debug private static Table allocatedPrev
50
51 /*52 * free memory counter53 */54 debug private static integer usedMemory = 0
55
56 /*57 * allocation58 */59 static method allocate takes nothing returns thistype
60 local thistype this = recycler[0]
61
62 debug call ThrowError(this < 0, "AllocT", "allocate", "thistype", 0, "Overflow.")
63
64 if (recycler[this] == 0) then
65 set recycler[0] = this + 1
66 else67 set recycler[0] = recycler[this]
68 endif69
70 set recycler[this] = -1
71
72 debug set usedMemory = usedMemory + 1
73
74 debug set allocatedNext[this] = 0
75 debug set allocatedPrev[this] = allocatedPrev[0]
76 debug set allocatedNext[allocatedPrev[0]] = this
77 debug set allocatedPrev[0] = this
78
79 return this
80 endmethod81
82 method deallocate takes nothing returns nothing
83 debug call ThrowError(recycler[this] != -1, "AllocT", "deallocate", "thistype", this, "Attempted To Deallocate Null Instance.")
84
85 set recycler[this] = recycler[0]
86 set recycler[0] = this
87
88 debug set usedMemory = usedMemory - 1
89
90 debug set allocatedNext[allocatedPrev[this]] = allocatedNext[this]
91 debug set allocatedPrev[allocatedNext[this]] = allocatedPrev[this]
92 endmethod93
94 /*95 * analysis96 */97 method operator isAllocated takes nothing returns boolean
98 return recycler[this] == -1
99 endmethod100
101 static if DEBUG_MODE then
102 static method calculateMemoryUsage takes nothing returns integer
103 return usedMemory104 endmethod105
106 static method getAllocatedMemoryAsString takes nothing returns string
107 local integer memoryCell = allocatedNext[0]
108 local string memoryRepresentation = null
109
110 loop111 exitwhen memoryCell == 0
112
113 if (memoryRepresentation == null) then
114 set memoryRepresentation = I2S(memoryCell)
115 else116 set memoryRepresentation = memoryRepresentation + ", " + I2S(memoryCell)
117 endif118
119 set memoryCell = allocatedNext[memoryCell]
120 endloop121
122 return memoryRepresentation123 endmethod124 endif125
126 /*127 * initialization128 */129 private static method onInit takes nothing returns nothing
130 set recycler = Table.create()
131 debug set allocatedNext = Table.create()
132 debug set allocatedPrev = Table.create()
133
134 set recycler[0] = 1
135 endmethod136 endmodule137 endlibrary