14 years
edited 8 years
1 library AllocQ /* v1.0.1.0
2 *************************************************************************************3 *4 * */ uses /*
5 *6 * */ ErrorMessage /*
7 *8 *************************************************************************************9 *10 * Maximizes speed by reducing local variable declarations and removing11 * if-statement.12 *13 * Uses a queue instead of a stack for recycler. Using a queue requires one14 * extra variable declaration.15 *16 * set alloc = recycler[0]17 * set recycler[0] = recycler[alloc]18 *19 ************************************************************************************20 *21 * module AllocQFast22 *23 * Fields24 * -------------------------25 *26 * readonly boolean isAllocated27 *28 * Methods29 * -------------------------30 *31 * static method allocate takes nothing returns thistype32 * method deallocate takes nothing returns nothing33 *34 * debug static method calculateMemoryUsage takes nothing returns integer35 * debug static method getAllocatedMemoryAsString takes nothing returns string36 *37 ************************************************************************************/38 module AllocQ39 /*40 * stack41 */42 private static integer array recycler
43 private static integer alloc
44 private static integer last = 8191
45
46 /*47 * list of allocated memory48 */49 debug private static integer array allocatedNext
50 debug private static integer array allocatedPrev
51
52 /*53 * free memory counter54 */55 debug private static integer usedMemory = 0
56
57 /*58 * allocation59 */60 static method allocate takes nothing returns thistype
61 set alloc = recycler[0]
62
63 debug call ThrowError(alloc == 0, "AllocQ", "allocate", "thistype", 0, "Overflow.")
64
65 set recycler[0] = recycler[alloc]
66
67 set recycler[alloc] = -1
68
69 debug set usedMemory = usedMemory + 1
70
71 debug set allocatedNext[alloc] = 0
72 debug set allocatedPrev[alloc] = allocatedPrev[0]
73 debug set allocatedNext[allocatedPrev[0]] = alloc
74 debug set allocatedPrev[0] = alloc
75
76 return alloc77 endmethod78
79 method deallocate takes nothing returns nothing
80 debug call ThrowError(recycler[this] != -1, "AllocQ", "deallocate", "thistype", this, "Attempted To Deallocate Null Instance.")
81
82 set recycler[last] = this
83 set recycler[this] = 0
84 set last = this
85
86 debug set usedMemory = usedMemory - 1
87
88 debug set allocatedNext[allocatedPrev[this]] = allocatedNext[this]
89 debug set allocatedPrev[allocatedNext[this]] = allocatedPrev[this]
90 endmethod91
92 /*93 * analysis94 */95 method operator isAllocated takes nothing returns boolean
96 return recycler[this] == -1
97 endmethod98
99 static if DEBUG_MODE then
100 static method calculateMemoryUsage takes nothing returns integer
101 return usedMemory102 endmethod103
104 static method getAllocatedMemoryAsString takes nothing returns string
105 local integer memoryCell = allocatedNext[0]
106 local string memoryRepresentation = null
107
108 loop109 exitwhen memoryCell == 0
110
111 if (memoryRepresentation == null) then
112 set memoryRepresentation = I2S(memoryCell)
113 else114 set memoryRepresentation = memoryRepresentation + ", " + I2S(memoryCell)
115 endif116
117 set memoryCell = allocatedNext[memoryCell]
118 endloop119
120 return memoryRepresentation121 endmethod122 endif123
124 /*125 * initialization126 */127 private static method onInit takes nothing returns nothing
128 local integer i = 0
129 130 set recycler[8191] = 0 //so that the array doesn't reallocate over and over again
131
132 loop133 set recycler[i] = i + 1
134
135 exitwhen i == 8190
136 set i = i + 1
137 endloop138 endmethod139 endmodule140 endlibrary