-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathm2dm.mod
272 lines (227 loc) · 5.27 KB
/
m2dm.mod
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
(*$Segment M2DM*)
(*$RangeCheck+*)
(*$OverflowCheck+*)
IMPLEMENTATION MODULE M2DM; (* NW 20.5.84; WH 19.8.86 *)
FROM InOut IMPORT WriteString, WriteCard, WriteHex, WriteLn;
FROM M2Lib IMPORT LoWORD, HighWORD;
FROM M2Shell IMPORT ShowProgress;
FROM SYSTEM IMPORT ADDRESS, ADR, TSIZE;
IMPORT Storage;
IMPORT EZStorage;
CONST
heapSize = 64000;
mainHeapSize = 64000;
TYPE
pHeap = POINTER TO aHeap;
aHeap = RECORD
buffer: ADDRESS;
size: CARDINAL;
current: ADDRESS;
last: ADDRESS;
next: pHeap;
END;
VAR
mainHeap: pHeap;
heaps: pHeap;
currentHeap: pHeap;
PROCEDURE ALLOCATE(VAR a: ADDRESS; n: CARDINAL);
BEGIN
IF currentHeap = NIL THEN
IF ShowProgress THEN
WriteLn;
WriteString("No heap created from which to allocate space");
END;
HALT;
END;
IF n >= Available() THEN
IF ShowProgress THEN
WriteLn;
WriteString("Unable to allocate heap space (wanted:");
WriteCard(n, 6);
WriteString(", got:");
WriteCard(Available(), 6);
WriteString(").");
WriteLn;
END;
HALT;
END;
WITH currentHeap^ DO
a := current;
current := current + VAL(ADDRESS, n);
END;
END ALLOCATE;
PROCEDURE Available(): CARDINAL;
BEGIN
IF currentHeap <> NIL THEN
WITH currentHeap^ DO
RETURN last - current;
END;
ELSE
RETURN 0;
END;
END Available;
PROCEDURE CloseHeaps;
VAR
current: pHeap;
previous: pHeap;
BEGIN
current := heaps;
WHILE current <> NIL DO
WITH current^ DO
EZStorage.DEALLOCATE(buffer, size);
END;
previous := current;
current := current^.next;
Storage.DEALLOCATE(previous, TSIZE(aHeap));
END;
heaps := NIL;
END CloseHeaps;
PROCEDURE CloseMainHeap;
BEGIN
WITH mainHeap^ DO
EZStorage.DEALLOCATE(buffer, size);
END;
Storage.DEALLOCATE(mainHeap, TSIZE(aHeap));
mainHeap := NIL;
END CloseMainHeap;
PROCEDURE NewHeap;
VAR
new: pHeap;
current: pHeap;
BEGIN
Storage.ALLOCATE(new, TSIZE(aHeap));
WITH new^ DO
EZStorage.ALLOCATE(buffer, heapSize);
IF buffer = NIL THEN
IF ShowProgress THEN
WriteLn;
WriteString("Unable to allocate a new heap.");
END;
HALT;
END;
(*
WriteString('Heap Address: ');
WriteHex(HighWORD(VAL(LONGINT, buffer)), 4);
WriteHex(LoWORD(VAL(LONGINT, buffer)), 4);
WriteString('H, Heap Length: ');
WriteHex(heapSize, 4);
WriteString('H');
WriteLn;
*)
size := heapSize;
current := buffer;
last := current + VAL(ADDRESS, heapSize);
next := NIL;
END;
IF heaps = NIL THEN
heaps := new;
ELSE
current := heaps;
WHILE current^.next <> NIL DO
current := current^.next;
END;
current^.next := new;
END;
currentHeap := new;
END NewHeap;
PROCEDURE ResetHeap(a: ADDRESS);
BEGIN
IF currentHeap = NIL THEN
IF ShowProgress THEN
WriteLn;
WriteString("Resetting a non-existant heap.");
END;
HALT;
END;
WITH currentHeap^ DO
current := a;
END;
END ResetHeap;
PROCEDURE ResizeHeap;
VAR
newSize: CARDINAL;
OK: BOOLEAN;
oldAdr: ADDRESS;
oldLen: CARDINAL;
BEGIN
IF currentHeap = mainHeap THEN
IF ShowProgress THEN
WriteLn;
WriteString("Resizing the main heap.");
END;
HALT;
END;
WITH currentHeap^ DO
oldAdr := buffer;
oldLen := size;
newSize := ((VAL(CARDINAL, current - buffer) DIV 256) + 1) * 256;
EZStorage.ChangeSize(buffer, newSize, OK);
IF OK THEN
last := buffer + VAL(ADDRESS, newSize);
size := newSize;
(*
WriteString('Changed heap size...');
WriteLn;
WriteString('Old Heap Address: ');
WriteHex(HighWORD(VAL(LONGINT, oldAdr)), 4);
WriteHex(LoWORD(VAL(LONGINT, oldAdr)), 4);
WriteString('H, Old Heap Length: ');
WriteHex(oldLen, 4);
WriteString('H');
WriteLn;
WriteString('New Heap Address: ');
WriteHex(HighWORD(VAL(LONGINT, buffer)), 4);
WriteHex(LoWORD(VAL(LONGINT, buffer)), 4);
WriteString('H, New Heap Length: ');
WriteHex(size, 4);
WriteString('H');
WriteLn;
*)
ELSIF ShowProgress THEN
WriteLn;
WriteString("Failed to resize a heap from ");
WriteCard(size, 5);
WriteString(" bytes to ");
WriteCard(newSize, 5);
WriteString(" bytes.");
HALT;
END;
END;
END ResizeHeap;
PROCEDURE RevertToMainHeap;
BEGIN
currentHeap := mainHeap;
END RevertToMainHeap;
PROCEDURE InitM2DM;
BEGIN
Storage.ALLOCATE(mainHeap, TSIZE(aHeap));
WITH mainHeap^ DO
EZStorage.ALLOCATE(buffer, mainHeapSize);
IF buffer = NIL THEN
IF ShowProgress THEN
WriteLn;
WriteString("Unable to allocate the main heap.");
END;
HALT;
END;
(*
WriteString('Main Heap Address: ');
WriteHex(HighWORD(VAL(LONGINT, buffer)), 4);
WriteHex(LoWORD(VAL(LONGINT, buffer)), 4);
WriteString('H, Main Heap Length: ');
WriteHex(mainHeapSize, 4);
WriteString('H');
WriteLn;
*)
size := mainHeapSize;
current := buffer;
last := current + VAL(ADDRESS, mainHeapSize);
next := NIL;
END;
currentHeap := mainHeap;
END InitM2DM;
BEGIN
mainHeap := NIL;
heaps := NIL;
currentHeap := NIL;
END M2DM.