-
Notifications
You must be signed in to change notification settings - Fork 2
/
MEMORY.PAS
622 lines (560 loc) · 14.8 KB
/
MEMORY.PAS
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
{/////////////////////////////////////////////////////////////////////////
//
// Dos Navigator Version 1.51 Copyright (C) 1991-99 RIT Research Labs
//
// This programs is free for commercial and non-commercial use as long as
// the following conditions are aheared to.
//
// Copyright remains RIT Research Labs, and as such any Copyright notices
// in the code are not to be removed. If this package is used in a
// product, RIT Research Labs should be given attribution as the RIT Research
// Labs of the parts of the library used. This can be in the form of a textual
// message at program startup or in documentation (online or textual)
// provided with the package.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 3. All advertising materials mentioning features or use of this software
// must display the following acknowledgement:
// "Based on Dos Navigator by RIT Research Labs."
//
// THIS SOFTWARE IS PROVIDED BY RIT RESEARCH LABS "AS IS" AND ANY EXPRESS
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The licence and distribution terms for any publically available
// version or derivative of this code cannot be changed. i.e. this code
// cannot simply be copied and put under another distribution licence
// (including the GNU Public Licence).
//
//////////////////////////////////////////////////////////////////////////}
unit Memory;
{-----------------------------------------------------}
{ This module is based on Turbo Vision Memry Unit }
{ Copyright (c) 1990 by Borland International }
{-----------------------------------------------------}
interface
const
MaxHeapSize: Word = 655360 div 16; { 640K }
LowMemSize: Word = 4096; { 4K }
MaxBufMem: Word = 65536 div 16; { 64K }
procedure InitMemory;
procedure DoneMemory;
procedure InitDosMem;
procedure DoneDosMem;
function LowMemory: Boolean;
function MemAlloc(Size: Word): Pointer;
function MemAllocSeg(Size: Word): Pointer;
procedure NewCache(var P: Pointer; Size: Word);
procedure DisposeCache(P: Pointer);
procedure NewBuffer(var P: Pointer; Size: Word);
procedure DisposeBuffer(P: Pointer);
function GetBufferSize(P: Pointer): Word;
function SetBufferSize(P: Pointer; Size: Word): Boolean;
{$IFNDEF DPMI}
procedure GetBufMem(var P: Pointer; Size: Word);
procedure FreeBufMem(P: Pointer);
procedure SetMemTop(MemTop: Pointer);
{$ENDIF}
implementation
type
PtrRec = record
Ofs, Seg: Word;
end;
{$IFDEF DPMI}
type
PCache = ^TCache;
TCache = record
Next: PCache;
Master: ^Pointer;
Data: record end;
end;
const
CacheList: PCache = nil;
SafetyPool: Pointer = nil;
SafetyPoolSize: Word = 8192;
DisablePool: Boolean = False;
function MemAllocateBlock(HeapHandle, Size, Attributes: Word;
EventProc: Pointer; var Selector: Word): Integer; far;
external 'RTM' index $0005;
function MemFreeBlock(Selector: Word): Integer; far;
external 'RTM' index $0006;
function MemResizeBlock(Selector: Word; Size: Word): Integer; far;
external 'RTM' index $0007;
function MemGetBlockSize(Selector: Word; var Size: Longint): Integer; far;
external 'RTM' index $0014;
function FreeCache: Boolean;
begin
FreeCache := False;
if CacheList <> nil then
begin
DisposeCache(CacheList^.Next^.Master^);
FreeCache := True;
end;
end;
function FreeSafetyPool: Boolean;
begin
FreeSafetyPool := False;
if SafetyPool <> nil then
begin
FreeMem(SafetyPool, SafetyPoolSize);
SafetyPool := nil;
FreeSafetyPool := True;
end;
end;
function HeapNotify(Size: Word): Integer; far;
begin
if FreeCache then HeapNotify := 2 else
if DisablePool then HeapNotify := 1 else
if FreeSafetyPool then HeapNotify := 2 else HeapNotify := 0;
end;
procedure InitMemory;
begin
HeapError := @HeapNotify;
SafetyPoolSize := LowMemSize * 16;
LowMemory;
end;
procedure DoneMemory;
begin
while FreeCache do;
FreeSafetyPool;
end;
procedure InitDosMem;
begin
end;
procedure DoneDosMem;
begin
end;
function LowMemory: Boolean;
begin
LowMemory := False;
if SafetyPool = nil then
begin
SafetyPool := MemAlloc(SafetyPoolSize);
if SafetyPool = nil then LowMemory := True;
end;
end;
function MemAlloc(Size: Word): Pointer;
var
P: Pointer;
begin
DisablePool := True;
GetMem(P, Size);
DisablePool := False;
MemAlloc := P;
end;
function MemAllocSeg(Size: Word): Pointer;
var
Selector: Word;
begin
Selector := 0;
if Size <> 0 then
repeat
if MemAllocateBlock(0, Size, 2, nil, Selector) <> 0 then
Selector := 0;
until (Selector <> 0) or not FreeCache;
MemAllocSeg := Ptr(Selector, 0);
end;
procedure NewCache(var P: Pointer; Size: Word);
var
Cache: PCache;
begin
Inc(Size, SizeOf(TCache));
PtrRec(Cache).Ofs := 0;
if MemAllocateBlock(0, Size, 4, nil, PtrRec(Cache).Seg) <> 0 then
PtrRec(Cache).Seg := 0;
if Cache <> nil then
begin
if CacheList = nil then Cache^.Next := Cache else
begin
Cache^.Next := CacheList^.Next;
CacheList^.Next := Cache;
end;
CacheList := Cache;
Cache^.Master := @P;
Inc(PtrRec(Cache).Ofs, SizeOf(TCache));
end;
P := Cache;
end;
procedure DisposeCache(P: Pointer);
var
Cache, C: PCache;
begin
PtrRec(Cache).Ofs := PtrRec(P).Ofs - SizeOf(TCache);
PtrRec(Cache).Seg := PtrRec(P).Seg;
C := CacheList;
while (C^.Next <> Cache) and (C^.Next <> CacheList) do C := C^.Next;
if C^.Next = Cache then
begin
if C = Cache then CacheList := nil else
begin
if CacheList = Cache then CacheList := C;
C^.Next := Cache^.Next;
end;
Cache^.Master^ := nil;
MemFreeBlock(PtrRec(Cache).Seg);
end;
end;
procedure NewBuffer(var P: Pointer; Size: Word);
begin
P := MemAllocSeg(Size);
end;
procedure DisposeBuffer(P: Pointer);
begin
MemFreeBlock(PtrRec(P).Seg);
end;
function GetBufferSize(P: Pointer): Word;
var
Size: Longint;
begin
if MemGetBlockSize(PtrRec(P).Seg, Size) <> 0 then Size := 0;
GetBufferSize := Size;
end;
function SetBufferSize(P: Pointer; Size: Word): Boolean;
begin
SetBufferSize := MemResizeBlock(PtrRec(P).Seg, Size) = 0;
end;
{$ELSE}
type
PCache = ^TCache;
TCache = record
Size: Word;
Master: ^Pointer;
Data: record end;
end;
type
PBuffer = ^TBuffer;
TBuffer = record
Size: Word;
Master: ^Word;
end;
const
CachePtr: Pointer = nil;
HeapResult: Integer = 0;
BufHeapPtr: Word = 0;
BufHeapEnd: Word = 0;
SafetyPoolSize = 5120;
function HeapNotify(Size: Word): Integer; far; assembler;
asm
CMP Size,0
JNE @@3
@@1: MOV AX,CachePtr.Word[2]
CMP AX,HeapPtr.Word[2]
JA @@3
JB @@2
MOV AX,CachePtr.Word[0]
CMP AX,HeapPtr.Word[0]
JAE @@3
@@2: XOR AX,AX
PUSH AX
PUSH AX
CALL DisposeCache
JMP @@1
@@3: MOV AX,HeapResult
end;
procedure FreeCacheMem;
begin
while CachePtr <> HeapEnd do DisposeCache(CachePtr);
end;
procedure InitMemory;
var
HeapSize: Word;
begin
HeapError := @HeapNotify;
if BufHeapPtr = 0 then
begin
HeapSize := PtrRec(HeapEnd).Seg - PtrRec(HeapOrg).Seg;
if HeapSize > MaxHeapSize then HeapSize := MaxHeapSize;
BufHeapEnd := PtrRec(HeapEnd).Seg;
PtrRec(HeapEnd).Seg := PtrRec(HeapOrg).Seg + HeapSize;
BufHeapPtr := PtrRec(HeapEnd).Seg;
end;
CachePtr := HeapEnd;
end;
procedure DoneMemory;
begin
FreeCacheMem;
end;
procedure InitDosMem;
begin
SetMemTop(Ptr(BufHeapEnd, 0));
end;
procedure DoneDosMem;
var
MemTop: Pointer;
begin
MemTop := Ptr(BufHeapPtr, 0);
if BufHeapPtr = PtrRec(HeapEnd).Seg then
begin
FreeCacheMem;
MemTop := HeapPtr;
end;
SetMemTop(MemTop);
end;
function LowMemory: Boolean;
begin
LowMemory := MaxAvail < SafetyPoolSize
end;
function MemAlloc(Size: Word): Pointer;
var
P: Pointer;
begin
HeapResult := 1;
GetMem(P, Size);
HeapResult := 0;
if (P <> nil) and LowMemory then
begin
FreeMem(P, Size);
P := nil;
end;
MemAlloc := P;
end;
function MemAllocSeg(Size: Word): Pointer;
var
P, T: Pointer;
begin
Size := (Size + 7) and $FFF8;
P := MemAlloc(Size + 8);
if P <> nil then
begin
if PtrRec(P).Ofs = 0 then
begin
PtrRec(T).Ofs := Size and 15;
PtrRec(T).Seg := PtrRec(P).Seg + Size shr 4;
end else
begin
T := P;
PtrRec(P).Ofs := 0;
Inc(PtrRec(P).Seg);
end;
FreeMem(T, 8);
end;
MemAllocSeg := P;
end;
procedure NewCache(var P: Pointer; Size: Word); assembler;
asm
LES DI,P
MOV AX,Size
ADD AX,(TYPE TCache)+15
MOV CL,4
SHR AX,CL
MOV DX,CachePtr.Word[2]
SUB DX,AX
JC @@1
CMP DX,HeapPtr.Word[2]
JBE @@1
MOV CX,HeapEnd.Word[2]
SUB CX,DX
CMP CX,MaxBufMem
JA @@1
MOV CachePtr.Word[2],DX
PUSH DS
MOV DS,DX
XOR SI,SI
MOV DS:[SI].TCache.Size,AX
MOV DS:[SI].TCache.Master.Word[0],DI
MOV DS:[SI].TCache.Master.Word[2],ES
POP DS
MOV AX,OFFSET TCache.Data
JMP @@2
@@1: XOR AX,AX
CWD
@@2: CLD
STOSW
XCHG AX,DX
STOSW
end;
procedure DisposeCache(P: Pointer); assembler;
asm
MOV AX,CachePtr.Word[2]
XOR BX,BX
XOR CX,CX
MOV DX,P.Word[2]
@@1: MOV ES,AX
CMP AX,DX
JE @@2
ADD AX,ES:[BX].TCache.Size
CMP AX,HeapEnd.Word[2]
JE @@2
PUSH ES
INC CX
JMP @@1
@@2: PUSH ES
LES DI,ES:[BX].TCache.Master
XOR AX,AX
CLD
STOSW
STOSW
POP ES
MOV AX,ES:[BX].TCache.Size
JCXZ @@4
@@3: POP DX
PUSH DS
PUSH CX
MOV DS,DX
ADD DX,AX
MOV ES,DX
MOV SI,DS:[BX].TCache.Size
MOV CL,3
SHL SI,CL
MOV CX,SI
SHL SI,1
DEC SI
DEC SI
MOV DI,SI
STD
REP MOVSW
LDS SI,ES:[BX].TCache.Master
MOV DS:[SI].Word[2],ES
POP CX
POP DS
LOOP @@3
@@4: ADD CachePtr.Word[2],AX
end;
procedure MoveSeg(Source, Dest, Size: Word); near; assembler;
asm
PUSH DS
MOV AX,Source
MOV DX,Dest
MOV BX,Size
CMP AX,DX
JB @@3
CLD
@@1: MOV CX,0FFFH
CMP CX,BX
JB @@2
MOV CX,BX
@@2: MOV DS,AX
MOV ES,DX
ADD AX,CX
ADD DX,CX
SUB BX,CX
SHL CX,1
SHL CX,1
SHL CX,1
XOR SI,SI
XOR DI,DI
REP MOVSW
OR BX,BX
JNE @@1
JMP @@6
@@3: ADD AX,BX
ADD DX,BX
STD
@@4: MOV CX,0FFFH
CMP CX,BX
JB @@5
MOV CX,BX
@@5: SUB AX,CX
SUB DX,CX
SUB BX,CX
MOV DS,AX
MOV ES,DX
SHL CX,1
SHL CX,1
SHL CX,1
MOV SI,CX
DEC SI
SHL SI,1
MOV DI,SI
REP MOVSW
OR BX,BX
JNE @@4
@@6: POP DS
end;
function GetBufSize(P: PBuffer): Word;
begin
GetBufSize := (P^.Size + 15) shr 4 + 1;
end;
procedure SetBufSize(P: PBuffer; NewSize: Word);
var
CurSize: Word;
begin
CurSize := GetBufSize(P);
MoveSeg(PtrRec(P).Seg + CurSize, PtrRec(P).Seg + NewSize,
BufHeapPtr - PtrRec(P).Seg - CurSize);
Inc(BufHeapPtr, NewSize - CurSize);
Inc(PtrRec(P).Seg, NewSize);
while PtrRec(P).Seg < BufHeapPtr do
begin
Inc(P^.Master^, NewSize - CurSize);
Inc(PtrRec(P).Seg, (P^.Size + 15) shr 4 + 1);
end;
end;
procedure NewBuffer(var P: Pointer; Size: Word);
var
BufSize: Word;
Buffer: PBuffer;
begin
BufSize := (Size + 15) shr 4 + 1;
if BufHeapPtr + BufSize > BufHeapEnd then P := nil else
begin
Buffer := Ptr(BufHeapPtr, 0);
Buffer^.Size := Size;
Buffer^.Master := @PtrRec(P).Seg;
P := Ptr(BufHeapPtr + 1, 0);
Inc(BufHeapPtr, BufSize);
end;
end;
procedure DisposeBuffer(P: Pointer);
begin
Dec(PtrRec(P).Seg);
SetBufSize(P, 0);
end;
function GetBufferSize(P: Pointer): Word;
begin
Dec(PtrRec(P).Seg);
GetBufferSize := PBuffer(P)^.Size;
end;
function SetBufferSize(P: Pointer; Size: Word): Boolean;
var
NewSize: Word;
begin
Dec(PtrRec(P).Seg);
NewSize := (Size + 15) shr 4 + 1;
SetBufferSize := False;
if BufHeapPtr + NewSize - GetBufSize(P) <= BufHeapEnd then
begin
SetBufSize(P, NewSize);
PBuffer(P)^.Size := Size;
SetBufferSize := True;
end;
end;
procedure GetBufMem(var P: Pointer; Size: Word);
begin
NewCache(P, Size);
end;
procedure FreeBufMem(P: Pointer);
begin
DisposeCache(P);
end;
procedure SetMemTop(MemTop: Pointer); assembler;
asm
MOV BX,MemTop.Word[0]
ADD BX,15
MOV CL,4
SHR BX,CL
ADD BX,MemTop.Word[2]
MOV AX,PrefixSeg
SUB BX,AX
MOV ES,AX
MOV AH,4AH
INT 21H
end;
{$ENDIF}
end.