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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
; Copyright 1996 Acorn Computers Ltd
;
; Licensed under the Apache License, Version 2.0 (the "License");
; you may not use this file except in compliance with the License.
; You may obtain a copy of the License at
;
; http://www.apache.org/licenses/LICENSE-2.0
;
; Unless required by applicable law or agreed to in writing, software
; distributed under the License is distributed on an "AS IS" BASIS,
; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
; See the License for the specific language governing permissions and
; limitations under the License.
;
TTL => HeapMan : Heap Allocation SWI
; Interruptible heap SWI.
; Look down the IRQ stack to see if anybody was in a heap operation.
; If they were, then (with IRQs off) the foreground call is done first, by
; picking up info from a fixed block. Patch the IRQ stack so that the heap SWI
; is returned to at a "it happened in the background" fixup routine. Current
; request can then be dealt with! Ta Nick.
; Also has an interlock on the register restore area; otherwise anybody
; with an IRQ process doing heap ops with interrupts enabled will cause
; trouble.
GBLL TubeInfo
TubeInfo SETL {FALSE}
GBLL debheap
debheap SETL 1=0
[ DebugHeaps
FreeSpaceDebugMask * &04000000
UsedSpaceDebugMask * &08000000
]
Nil * 0
hpd RN r1 ; The punter sees these
addr RN r2
size RN r3
HpTemp RN r10 ; But not these
tp RN r11
bp RN r12
work RN r4 ; This is the only one we have to save.
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; + H E A P O R G A N I S A T I O N +
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; A heap block descriptor (hpd) has the form
; +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ -+ -+ -+ -+
; | magic | free | base | end | debug |
; +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+- +- +- +- +
; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
^ 0, hpd
hpdmagic # 4
hpdfree # 4
hpdbase # 4
hpdend # 4 ; Needed for debugging heap, and top end validation
[ debheap
hpddebug # 4 ; 0 -> No debug, ~0 -> Debug
]
hpdsize * @-hpdmagic
magic_heap_descriptor * (((((("p":SHL:8)+"a"):SHL:8)+"e"):SHL:8)+"H")
; hpdmagic is a unique identification field
; hpdfree is the offset of the first block in the free space list
; hpdbase is the offset of the byte above the last one used
; hpdend is the offset of the byte above the last one usable
; | hpdbase
; \|/
; +---+--------------------+--------+
; low |hpd| heap blocks | unused | high
; +---+--------------------+---------+
; /|\ /|\
; | hpdfree | hpdend
; | in here somewhere.
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; Blocks in the free space list have the form :
; +--+--+--+--+--+--+--+--+--+ ~ -+--+
; | long link | long size | |
; +--+--+--+--+--+--+--+--+--+ ~ -+--+
; 0 1 2 3 4 5 6 7 8 (size-1)
;
; where the link field is an offset to the next free block
^ 0 ; Can't use register relative unfortunately as many regs used
frelink # 4
fresize # 4
freblksize # 0
; The link field is Nil (0) for the last block in the list
; Block sizes must be forced to a multiple of 8 bytes for subsequent link and
; size information to be stored in them if they are disposed of by the user.
; They must also be capable of storing a 4 byte size field while allocated.
; This field is used to size the block to free when FreeArea is called.
ALIGN
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; The Macros
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; Check hpd valid
MACRO
$label ValidateHpd $faildest
$label BL ValidateHpdSubr
BNE $faildest._badhpd
MEND
;****************************************************************************
; These bits of ExtendBlock are outside the IRQ HeapOp range because they
; don't update the heap structure, so we can safely restore old IRQ status
CopyBackwardsInSafeZone
LDR work, [stack, #3*4] ; get user link
ANDS work, work, #I_bit ; look at I_bit
WritePSRc SVC_mode, work, EQ ; if was clear then clear it now
ADD bp, bp, #4 ; new block pointer
STR bp, [stack] ; return to user
; copy wackbords: HpTemp-4 bytes from addr+4 to bp, in appropriate order!
cpe_prev
SUBS HpTemp, HpTemp, #4
LDRGT work, [addr, #4]!
STRGT work, [bp], #4
BGT cpe_prev
WritePSRc SVC_mode + I_bit, work; disable IRQs before we venture back
B GoodExtension ; into danger zone
ReallocateInSafeZone
LDR work, [addr, hpd]! ; get block size, set block addr
ADD size, size, work
SUB size, size, #4 ; block size to claim
ADD addr, addr, #4
MOV bp, addr ; address to copy from
Push addr ; save for later freeing
MOV R0, #HeapReason_Get
SWI XOS_Heap
Pull addr, VS
BVS SafeNaffExtension
[ debheap
LDR HpTemp, hpddebug
CMP HpTemp, #0
BEQ %FT06
DREG work, "got new block : copying "
06
]
STR addr, [stack, #4]
; claimed : copy work-4 bytes from bp to addr
CopyForExtension
SUBS work, work, #4
LDRGT HpTemp, [bp],#4
STRGT HpTemp, [addr],#4
BGT CopyForExtension
; free the old block!
[ debheap
LDR HpTemp, hpddebug
CMP HpTemp, #0
BEQ %FT08
WRLN "freeing old block"
08
]
; recursive SWI to free old block; we have invalidated any held information
MOV R0, #HeapReason_Free
Pull addr ; heap block addr
SWI XOS_Heap
MOV R0, #HeapReason_ExtendBlock
WritePSRc SVC_mode + I_bit,work ; disable IRQs before we venture back
B GoodExtension ; into danger zone
SafeNaffExtension
WritePSRc SVC_mode + I_bit,work ; disable IRQs before we venture back
B NaffExtension ; into danger zone
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; Here's the bit that gets returned to if the heap op was done in the
; background. Pick up the registers, look at the saved PSR to see if error
; return or OK.
; This bit musn't be in range of the IRQ Heap Op checking!!!
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
heapopdoneinbackground ROUT
LDR R12, =HeapReturnedReg_R0
[ TubeInfo
LDR R0, [R12]
BL TubeDumpR0
LDR R0, [R12, #4]
BL TubeDumpR0
LDR R0, [R12, #8]
BL TubeDumpR0
LDR R0, [R12, #24]
TST R0, #V_bit
MOVEQ R0, #"v"
MOVNE R0, #"V"
TubeChar R1, R2, "MOV R2, r0"
TubeChar R0, R1, "MOV R1, #10"
TubeChar R0, R1, "MOV R1, #13"
]
LDMIA R12, {R0-R4, R10, R11}
MOV stack, R10
MOV R10, #0
STR R10, [R12, #HeapReturnedReg_PSR-HeapReturnedReg_R0]
; clear the interlock
TST R11, #V_bit ; look at returned error
BEQ GoodHeapExit
B NaffHeapExit
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
; HeapEntry. SWI level entry
; =========
;
; Perform actions on the heap block described by r1(hpd)
; In r0 = heap action requested
; r1(hpd) -> heap block
; r2(addr) -> start of block
; r3(size) = size of block
; Out VClear -> Action performed
; VSet -> Something terrible has happened, error set
; Rest of universe ok
HeapEntry ROUT
Push lr
SavePSR lr ; hang on to interrupt state
; First check that we aren't in an interrupted Heap Op
WritePSRc SVC_mode+I_bit, R11
MOV R11, #IRQsema
inspect_IRQ_stack
LDR R11, [R11]
CMP R11, #0
BEQ iis_end
LDR R10, [R11, #4*8] ; Get LR from IRQ stack
ADR R12, first_heap_address_to_trap
CMP R10, R12
ADRGEL R12, HeapCode_end
CMPGE R12, R10
BLT inspect_IRQ_stack
; somebody's in the heap code! Time for perversion.
; Pick up registers, do foreground op, poke IRQstack return address
ADRL R10, heapopdoneinbackground
STR R10, [R11, #4*8] ; return address zapped
LDR R10, [R11, #4*6] ; get stored SPSR
BIC R10, R10, #&FF
ORR R10, R10, #I32_bit:OR:SVC2632
STR R10, [R11, #4*6] ; return into SVC26/32 mode with IRQs disabled
Push "R0-R3, lr"
LDR R10, =HeapSavedReg_R0
; This can't happen: heap ops are non-interruptible while foreground ops
; are waiting to complete
; LDR R12, [R10, #HeapReturnedReg_PSR-HeapSavedReg_R0]
; CMP R12, #0
; BNE HeapInUse
LDMIA R10, {R0-R3, R10, R11}
SWI XOS_Heap ; with interrupts off!
LDR R12, =HeapReturnedReg_R0
; Could we poke these into the IRQ stack too...?
; would allow interruptible IRQ processes to do heap ops!!!
MRS lr, CPSR
STMIA R12, {R0-R3, R10, R11, lr}
Pull "R0-R3, lr"
iis_end ; store the registers in the info block
LDR R12, =HeapSavedReg_R0
STMIA R12, {R0-R4, stack}
first_heap_address_to_trap ; because register saveblock now set.
LDR R12, [R12, #HeapReturnedReg_PSR-HeapSavedReg_R0]
CMP R12, #0
RestPSR lr, EQ ; restore callers interrupt state
; only if no foreground waiting to
; complete
CMP r0, #MaxHeapCode ; now despatch it.
ADDLS pc, pc, r0, LSL #2 ; Tutu : faster & shorter
B NaffHeapReason ; Return if unknown call reason
HeapJumpTable ; Check reason codes against Hdr:Heap defs
assert ((.-HeapJumpTable) :SHR: 2) = HeapReason_Init
B InitHeap
assert ((.-HeapJumpTable) :SHR: 2) = HeapReason_Desc
B DescribeHeap
assert ((.-HeapJumpTable) :SHR: 2) = HeapReason_Get
B GetArea
assert ((.-HeapJumpTable) :SHR: 2) = HeapReason_Free
B FreeArea
assert ((.-HeapJumpTable) :SHR: 2) = HeapReason_ExtendBlock
B ExtendBlock
assert ((.-HeapJumpTable) :SHR: 2) = HeapReason_ExtendHeap
B ExtendHeap
assert ((.-HeapJumpTable) :SHR: 2) = HeapReason_ReadBlockSize
B ReadBlockSize
[ debheap
B ShowHeap
]
MaxHeapCode * (.-HeapJumpTable-4) :SHR: 2 ; Largest valid reason code
NaffHeapReason
ADR R0, ErrorBlock_HeapBadReason
[ International
BL TranslateError
]
NaffHeapExit ; get here with R0 = error ptr
SETV
GoodHeapExit ; V cleared on entry to SWI dispatch
SETPSR I_bit, R12 ; IRQs off
Pull lr
ORRVS lr, lr, #V_bit ; VSet Exit
ExitSWIHandler ; Like all good SWI handlers
;HeapInUse
; $HeapBadAsModuleBRA
; SetBorder R10, R11, 15, 0, 0
; $HeapBadAsModuleKET
; ADR R0, ErrorBlock_HeapFail_HeapLocked
; B NaffHeapExit
; Errors
MakeErrorBlock HeapBadReason
MakeErrorBlock HeapFail_Init
MakeErrorBlock HeapFail_BadDesc
MakeErrorBlock HeapFail_BadLink
MakeErrorBlock HeapFail_Alloc
MakeErrorBlock HeapFail_NotABlock
MakeErrorBlock HeapFail_BadExtend
MakeErrorBlock HeapFail_ExcessiveShrink
; MakeErrorBlock HeapFail_HeapLocked
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; Subroutine to validate heap pointer
; checks hpd points at existing LogRam
; and also that internal offsets fall into the same block of RAM
ValidateHpdSubr
Push "R0-R3, lr"
SavePSR R3
WritePSRc SVC_mode+I_bit, R0 ; interrupts off for validation
MOV R0, hpd
ADD R1, hpd, #hpdsize+freblksize
SWI XOS_ValidateAddress
BCS vhpds_fail
TST R0, #3 ; check alignment
LDREQ HpTemp, =magic_heap_descriptor
LDREQ tp, [R0, #:INDEX: hpdmagic]
CMPEQ tp, HpTemp
BNE vhpds_fail ; failure
LDR R1, [R0, #:INDEX: hpdend]
ADD R1, R1, R0
SWI XOS_ValidateAddress
BCS vhpds_fail ; failure
ORR R3, R3, #Z_bit ; success
RestPSR R3
Pull "R0-R3, PC"
vhpds_fail
BIC R3, R3, #Z_bit ; NE returned ; fails
RestPSR R3
Pull "R0-R3, PC"
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
; InitHeap. Top level HeapEntry
; ========
;
; Initialise a heap descriptor block
; In : hpd -> block to initialise, size = size of block
; Out : VClear -> Block initialised
; VSet -> Something terrible has happened
; Rest of universe ok
; To initialise (or even reinitialise) a heap descriptor:
; $(
; hpd!magic := magic_heap_descriptor
; hpd!free := Nil
; hpd!base := hpdsize
; hpd!end := size
; $)
InitHeap ROUT
CMP size,#hpdsize+freblksize
BLT NaffHeapInitialise ; can't get hpd and 1 block in
Push "R0, R1"
MOV R0, hpd
ADD R1, hpd, size
SWI XOS_ValidateAddress
Pull "R0, R1"
BCS NaffHeapInitialise
[ DebugHeaps
ORR lr, hpd, #FreeSpaceDebugMask ; form word to store throughout heap
ADD HpTemp, hpd, size ; HpTemp -> end of heap
10
STR lr, [HpTemp, #-4]! ; store word, pre-decrementing
TEQ HpTemp, hpd ; until we get to start
BNE %BT10
]
LDR HpTemp, =magic_heap_descriptor
STR HpTemp, hpdmagic ; hpd!magic := magic_heap_desc
MOV HpTemp, #Nil
STR HpTemp, hpdfree ; hpd!free := Nil
MOV HpTemp, #hpdsize
STR HpTemp, hpdbase ; hpd!base := hpdsize
STR size, hpdend ; hpd!end := size
[ debheap
MOV HpTemp, #0 ; No debugging until the punter sets this Word
STR HpTemp, hpddebug
]
B GoodHeapExit
NaffHeapInitialise
[ debheap
WRLN "Unaligned/too big hpd/size: InitHeap failed"
]
ADR R0, ErrorBlock_HeapFail_Init
[ International
BL TranslateError
]
B NaffHeapExit ; VSet exit
LTORG
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
; DescribeHeap. Top level HeapEntry
; ============
;
; Return information about the heap whose descriptor is pointed to by hpd
; In : hpd -> heap descriptor
; Out : VClear -> addr = max block size claimable, size = total free store
; VSet -> Something wrong
; Rest of universe ok
DescribeHeap ROUT
ValidateHpd describefailed
[ debheap
LDR HpTemp, hpddebug
CMP HpTemp, #0
BEQ %FT00
Push link
WRLN "DescribeHeap"
BL iShowHeap
Pull link
00
]
LDR addr, hpdend
LDR HpTemp, hpdbase
SUB addr, addr, HpTemp ; unused area at base to end
MOV size, addr
LDR bp, hpdfree
ADR tp, hpdfree
ADD HpTemp, HpTemp, hpd ; address of end of allocated memory
B %FT20
; Main loop chaining up free space list. size = total, addr = maxvec
15 ADD tp, tp, bp ; get address of next
CMP tp, HpTemp
BHS describefailed_badlink ; points outside allocated memory
LDR bp, [tp, #fresize] ; Size of this block.
CMP bp, addr ; if size > maxvec then maxvec := size
MOVHI addr, bp
ADD size, size, bp ; tfree +:= size
LDR bp, [tp, #frelink] ; Get offset to next block
20 CMP bp,#Nil ; we know Nil is 0!
BLT describefailed_badlink ; -ve are naff
BNE %BT15
CMP addr, #0
SUBGT addr, addr, #4 ; max block claimable
B GoodHeapExit ; VClear Exit
describefailed_badhpd
[ debheap
WRLN "Invalid heap descriptor: DescribeHeap failed"
]
ADR R0, ErrorBlock_HeapFail_BadDesc
[ International
BL TranslateError
]
B NaffHeapExit ; VSet Exit
describefailed_badlink
[ debheap
WRLN "Invalid heap link: DescribeHeap failed"
]
ADR R0, ErrorBlock_HeapFail_BadLink
[ International
BL TranslateError
]
B NaffHeapExit ; VSet Exit
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
; GetArea. Top level HeapEntry
; =======
;
; Allocate a block of memory from the heap
; This will allocate the first block of sufficiently large size in the free
; list, with an oversize block being split.
; Failure to find a large enough block on the free list will try to claim
; space out of the heap block.
; Fails if requesting size = 0
; In : hpd -> heap pointer, size = size of block required
; Out : VClear : addr -> got a block
; VSet : addr = 0, couldn't get block
; Rest of universe ok
GetArea ROUT
Push "size"
ValidateHpd garfailed
[ debheap
; HpTemp not critical
LDR HpTemp, hpddebug
CMP HpTemp, #0
BEQ %FT00
Push "r0, link"
MOV r0, size
DREG r0, "GetArea "
BL iShowHeap
Pull "r0, link"
00
]
CMP size, #0 ; Can't deallocate 0, so there!
BLE garfailed_zero ; And -ve is invalid as well!
; note sizes of many megabytes thrown out by looking.
ADD size, size, #(freblksize-1)+4 ; Make block size granular
BIC size, size, #(freblksize-1) ; with size field added
ADR addr, hpdfree-frelink ; addr:= @(hpd!free)-frelink
garloop
LDR tp, [addr, #frelink] ; tp := addr!fre.link
CMP tp, #Nil ; Is this the end of the chain ?
BEQ garmore ; - so try main blk
ADD addr, addr, tp ; convert offset
LDR HpTemp, [addr, #fresize] ; If length < size then no good
SUBS HpTemp, HpTemp, size ; In case this works, for below split
BLO garloop
; Now addr -> a block on the free space list that our item will fit in
; If we have an exact fit (or as close as the granularity of the free list will
; allow), unlink this block and return it
BNE SplitFreeBlock
[ debheap
LDR HpTemp, hpddebug
CMP HpTemp, #0
BEQ %FT60
WRLN "Got an exact fit block"
60
]
LDR HpTemp, [addr, #frelink] ; Move this block's link field
CMP HpTemp, #Nil
ADDNE HpTemp, HpTemp, tp ; convert offset into offset from
; previous block
WritePSRc SVC_mode+I_bit, lr
ASSERT frelink=0
STR HpTemp, [addr, -tp] ; store in link of previous block
B ResultIsAddrPlus4
SplitFreeBlock
; Need to split the free block, returning the end portion to the caller
[ debheap
; HpTemp critical
Push HpTemp
LDR HpTemp, hpddebug
CMP HpTemp, #0
BEQ %FT70
WRLN "Splitting free block"
70
Pull HpTemp
]
WritePSRc SVC_mode+I_bit, lr
STR HpTemp, [addr, #fresize] ; Adjust size of free block remaining
ADD addr, addr, HpTemp ; addr -> free block just deallocated
ResultIsAddrPlus4
[ DebugHeaps
ORR lr, hpd, #UsedSpaceDebugMask ; form word to store throughout block
ADD HpTemp, addr, size ; HpTemp -> end of block
75
STR lr, [HpTemp, #-4]! ; store word, pre-decrementing
TEQ HpTemp, addr
BNE %BT75
]
STR size, [addr], #4 ; Store block size and increment addr
Pull "size" ; Return original value to the punter
; Note : real size got would be an option!
B GoodHeapExit ; RESULTIS addr
; Got no more free blocks of length >= size, so try to allocate more heap space
; out of the block described by hpd
garmore
[ debheap
; HpTemp not critical
LDR HpTemp, hpddebug
CMP HpTemp, #0
BEQ %FT80
WRLN "Trying to get more from main block"
80
]
LDR addr, hpdbase
ADD tp, addr, size ; addr := (hpd!base +:= size)
LDR HpTemp, hpdend
WritePSRc SVC_mode+I_bit, lr
CMP tp, HpTemp ; See if we'd fall out of the bottom
STRLS tp, hpdbase ; Only adjust hpdbase if valid alloc
ADDLS addr, addr, hpd ; offset conversion
BLS ResultIsAddrPlus4
[ debheap
STRIM "Not enough room to allocate in main block"
]
garfailed
ADR R0, ErrorBlock_HeapFail_Alloc
[ International
BL TranslateError
]
[ debheap
WRLN " : GetArea failed"
]
garfail_common
MOV addr, #0 ; addr := 0 if we couldn't allocate
Pull "size" ; RESULTIS 0
B NaffHeapExit ; VSet Exit
garfailed_badhpd
[ debheap
STRIM "Invalid heap descriptor"
]
ADR R0, ErrorBlock_HeapFail_BadDesc
[ International
BL TranslateError
]
B garfail_common
[ debheap
garfailed_zero
STRIM "Can't allocate 0 or less bytes"
B garfailed
|
garfailed_zero * garfailed
]
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
; FreeArea. Top level HeapEntry
; ========
;
; Return an area of store to the heap
; In : hpd -> heap descriptor, addr -> block to free
; Out : VClear -> block freed
; VSet -> failed to free block, size invalid
; Rest of universe ok
; The block to be freed is matched against those on the free list and inserted
; in it's correct place, with the list being maintained in ascending address
; order. If possible, the freed block is merged with contigous blocks above
; and below it to give less fragmentation, and if contiguous with main memory,
; is merged with that. If the latter, check to see if there is a block which
; would be made contiguous with main memory by the former's freeing, and if so,
; merge that with main memory too. Phew !
FreeArea ROUT
Push "addr, size, work"
[ debheap
; HpTemp not critical
LDR HpTemp, hpddebug
CMP HpTemp, #0
BEQ %FT00
Push "r0, link"
STRIM "FreeArea "
SUB r0, addr, hpd
SUB r0, r0, #4
BL PrintOffsetLine
BL iShowHeap
Pull "r0, link"
00
]
BL FindHeapBlock
BLVC FreeChunkWithConcatenation
Pull "addr, size, work"
BVC GoodHeapExit
B NaffHeapExit ; VSet Exit
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
; ExtendBlock. Top level HeapEntry
; ===========
;
; Extend or reallocate existing block
; In : hpd -> heap descriptor, addr -> block, size = size to change by
; Out : VClear -> block freed, addr new block pointer
; VSet -> failed to extend block
; Rest of universe ok
ExtendBlock
Push "addr, size, work"
[ debheap
; HpTemp not critical
LDR HpTemp, hpddebug
CMP HpTemp, #0
BEQ %FT00
Push "r0, link"
DREG size, "ExtendBlock by ",concat
STRIM " block at "
SUB r0, addr, hpd
SUB r0, r0, #4
BL PrintOffsetLine
BL iShowHeap
Pull "r0, link"
00
]
BL FindHeapBlock
BVS NaffExtension
ADD size, size, #freblksize-1 ; round size as appropriate :
BICS size, size, #freblksize-1 ; round up to nearest 8
BEQ GoodExtension ; get the easy case done.
BPL MakeBlockBigger
RSB size, size, #0
LDR bp, [addr, hpd] ; get block size
WritePSRc SVC_mode+I_bit, R14
SUBS bp, bp, size ; size of block left
[ debheap
; HpTemp not critical, GE/LT critical
BLE %FT01
LDR HpTemp, hpddebug
CMP HpTemp, #0
BEQ %FT01
WRLN "Freeing part of block"
01
CMP bp, #0 ; restore GE/Lt
]
MOVLE HpTemp, #-1 ; if discarding block, then
STRLE HpTemp, [stack] ; make pointer really naff.
STRGT bp, [addr, hpd] ; update size of block left
ADDGT addr, addr, bp ; offset of block to free
STRGT size, [addr, hpd] ; construct block for freeing
BL FreeChunkWithConcatenation ; work still set from block lookup
GoodExtension
Pull "addr, size, work"
[ DebugHeaps
ADD lr, size, #freblksize-1 ; work out how much we actually extended by
BICS lr, lr, #freblksize-1
BEQ %FT99 ; if zero or negative
BMI %FT99 ; then nothing to do
LDR HpTemp, [addr, #-4] ; get new block size
SUB HpTemp, HpTemp, #4 ; Exclude size word itself
ADD HpTemp, addr, HpTemp ; end of new block
SUB lr, HpTemp, lr ; start of new extension
ORR bp, hpd, #UsedSpaceDebugMask
98
STR bp, [HpTemp, #-4]! ; store word
TEQ HpTemp, lr
BNE %BT98
99
]
B GoodHeapExit
MakeBlockBigger
LDR HpTemp, [addr, hpd] ; get size
ADD HpTemp, HpTemp, addr ; block end
; TMD 01-Mar-89: FindHeapBlock now never returns tp=Nil, only tp=hpdfree,
; so no need for check
LDR bp, [tp, hpd] ; next free
CMP bp, #Nil
ADDNE bp, bp, tp
LDREQ bp, hpdbase
; bp is potential following block
CMP HpTemp, bp
BNE try_preceding_block
; now get size available, see if fits
LDR HpTemp, hpdbase
CMP bp, HpTemp
ADDNE HpTemp, bp, hpd
LDRNE HpTemp, [HpTemp, #fresize]
LDREQ HpTemp, hpdend
SUBEQ HpTemp, HpTemp, bp
BICEQ HpTemp, HpTemp, #(freblksize-1)
; force it to a sensible blocksize
MRS lr, CPSR ; save EQ/NE state
CMP HpTemp, size
BLT try_add_preceding_block
ORR lr, lr, #I32_bit ; disable IRQs
MSR CPSR_cf, lr
[ debheap
; HpTemp, EQ/NE critical
Push "HpTemp,lr"
LDR HpTemp, hpddebug
CMP HpTemp, #0
BEQ %FT02
STRIM "Extending block into "
02
Pull "HpTemp,lr"
msr ,CPSR_f, lr
]
LDR work, [addr, hpd] ; get size back
ADD work, work, size ; new size
STR work, [addr, hpd] ; block updated
; now see which we're extending into
BNE IntoFreeEntry
[ debheap
Push HpTemp
LDR HpTemp, hpddebug
CMP HpTemp, #0
BEQ %FT03
WRLN "base-end area"
03
Pull HpTemp
]
ADD work, work, addr
STR work, hpdbase
B GoodExtension
IntoFreeEntry
[ debheap
Push HpTemp
LDR HpTemp, hpddebug
CMP HpTemp, #0
BEQ %FT04
WRLN "free entry"
04
Pull HpTemp
]
CMP HpTemp, size
BNE SplitFreeBlockForExtend
; free entry just right size : remove from free list
LDR HpTemp, [bp, hpd] ; free link
CMP HpTemp, #Nil
ADDNE HpTemp, HpTemp, bp ; offset from heap start
SUBNE HpTemp, HpTemp, tp
STR HpTemp, [tp, hpd] ; free list updated
B GoodExtension
SplitFreeBlockForExtend
LDR work, [tp, hpd]
ADD work, work, size
STR work, [tp, hpd] ; prevnode points at right place
ADD work, work, tp ; offset of new free entry
ADD work, work, hpd
SUB HpTemp, HpTemp, size ; new freblk size
STR HpTemp, [work, #fresize]
LDR HpTemp, [bp, hpd]
CMP HpTemp, #Nil
SUBNE HpTemp, HpTemp, size ; reduced offset for free link
STR HpTemp, [work, #frelink]
B GoodExtension
try_preceding_block
; TMD 01-Mar-89: FindHeapBlock now never returns tp=Nil, only tp=hpdfree,
; so no need for check
CMP tp, #:INDEX: hpdfree ; no real preceder?
BEQ got_to_reallocate
ADD bp, tp, hpd
LDR bp, [bp, #fresize]
ADD bp, bp, tp ; end of preceding block
CMP addr, bp
BNE got_to_reallocate
; now get size available, see if fits
SUB bp, bp, tp ; freblk size
SUBS bp, bp, size ; compare, find free size left
BLT got_to_reallocate
[ debheap
Push "HpTemp,lr"
LDR HpTemp, hpddebug
CMP HpTemp, #0
BEQ %FT10
CMP bp, #0
BEQ %FT11
STRIM "Extending block into previous free"
B %FT12
11
STRIM "Previous free perfect fit"
12
SWI XOS_NewLine
10
Pull "HpTemp,lr"
]
WritePSRc SVC_mode+I_bit, HpTemp ; IRQs off
hack_preceder
; bp is new size of preceding block
; tp is prevfree offset
; work is prevprevfree offset
; size is amount block grows by
; addr is block offset
CMP bp, #0
ADDNE HpTemp, tp, hpd
STRNE bp, [HpTemp, #fresize] ; prevblock shrunk
BNE copy_backwards
; free freblk: work is still prevprevblk pointer
LDR HpTemp, [tp, hpd]
CMP HpTemp, #Nil
ADDNE HpTemp, HpTemp, tp ; offset from heap start
SUBNE HpTemp, HpTemp, work
STR HpTemp, [work, hpd] ; free list updated
copy_backwards
ADD bp, bp, tp
LDR HpTemp, [addr, hpd]! ; current block size
ADD size, HpTemp, size
STR size, [bp, hpd]! ; update blocksize
[ debheap
Push r0
LDR r0, hpddebug
CMP r0, #0
BEQ %FT06
DREG HpTemp, "copying -4+",concat
STRIM " from "
SUB R0, addr, hpd
BL PrintOffset
STRIM " to "
SUB R0, bp, hpd
BL PrintOffsetLine
06
Pull r0
]
; TMD 02-Mar-89: We've finished messing about with the heap structure
; so we can branch outside danger zone and restore IRQ status while doing copy
B CopyBackwardsInSafeZone
try_add_preceding_block
[ {TRUE}
; HpTemp is size of following block
CMP tp, #:INDEX: hpdfree ; no real preceder?
BEQ got_to_reallocate
Push "work, size" ; need prevprevblk ptr
SUB size, size, HpTemp ; size still needed
ADD HpTemp, tp, hpd
LDR HpTemp, [HpTemp, #fresize]
ADD HpTemp, HpTemp, tp ; end of preceding block
CMP addr, HpTemp
BNE got_to_reallocate2
; now get size available, see if fits
SUB HpTemp, HpTemp, tp ; freblk size
SUBS HpTemp, HpTemp, size
BLT got_to_reallocate2
[ debheap
Push "HpTemp,lr"
LDR HpTemp, hpddebug
CMP HpTemp, #0
BEQ %FT10
Pull HpTemp
CMP HpTemp, #0
BEQ %FT11
STRIM "Extending block into previous free and block after"
B %FT12
11
STRIM "Previous free+nextblock perfect fit"
12
SWI XOS_NewLine
10
Pull "lr"
]
WritePSRc SVC_mode+I_bit, work ; IRQs off
; delink block at bp
LDR work, hpdbase
CMP bp, work ; extend into free, or delink block?
BNE ext_delink
LDR work, hpdend
SUB work, work, bp ; get back real size
BIC work, work, #(freblksize-1)
ADD work, work, bp
STR work, hpdbase ; all free allocated
B ext_hack
ext_delink
LDR work, [bp, hpd]
CMP work, #Nil
ADDNE work, work, bp
SUBNE work, work, tp
STR work, [tp, hpd] ; block delinked
ext_hack
MOV bp, HpTemp
Pull "work, size"
; bp is new size of preceding block
; tp is prevfree offset
; work is prevprevfree offset
; size is amount block grows by
; addr is block offset
B hack_preceder
got_to_reallocate2
Pull "work, size"
]
got_to_reallocate
; claim block of new size ; copy data
; Done by recursive SWIs: somewhat inefficient, but simple.
[ debheap
LDR HpTemp, hpddebug
CMP HpTemp, #0
BEQ %FT05
WRLN "reallocating block"
05
]
B ReallocateInSafeZone
NaffExtension
Pull "addr, size, work"
B NaffHeapExit
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
; ExtendHeap. Top level HeapEntry
; ==========
;
; Extend or shrink heap
; In : hpd -> heap descriptor, size = size to change by
; Out : VClear -> heap size changed OK
; VSet -> failed to change by specified amount
; size = amount changed by
ExtendHeap ROUT
ValidateHpd ExtendHeap
CMP r3, #0
ADDMI r3, r3, #3 ; round towards 0
BIC R3, R3, #3 ; ensure word amount
LDR HpTemp, hpdend
ADD HpTemp, HpTemp, R3 ; HpTemp := new size
LDR tp, hpdbase
CMP tp, HpTemp
BGT ExtendHeap_badshrink
WritePSRc SVC_mode+I_bit, lr
Push "R0, R1"
MOV R0, hpd ; Ensure heap will be in valid area
ADD R1, hpd, HpTemp
SWI XOS_ValidateAddress
Pull "R0, R1"
BCS ExtendHeap_nafforf
[ DebugHeaps
CMP R3, #0 ; if shrunk or stayed same
BLE %FT15 ; then nothing to do
ADD tp, hpd, HpTemp ; tp -> end of heap
SUB bp, tp, R3 ; bp -> start of new bit
ORR lr, hpd, #FreeSpaceDebugMask
10
STR lr, [tp, #-4]! ; store word
TEQ tp, bp
BNE %BT10
15
]
STR HpTemp, hpdend ; uppy date him
B GoodHeapExit ; moved all the size asked for
ExtendHeap_badhpd
ADRL R0, ErrorBlock_HeapFail_BadDesc
[ International
BL TranslateError
]
MOV size, #0
B NaffHeapExit
ExtendHeap_nafforf
ADRL R0, ErrorBlock_HeapFail_BadExtend
[ International
BL TranslateError
]
MOV size, #0
B NaffHeapExit
ExtendHeap_badshrink
LDR HpTemp, hpdend
STR tp, hpdend ; update heap
SUB size, HpTemp, tp ; size managed to change by
ADRL R0, ErrorBlock_HeapFail_ExcessiveShrink
[ International
BL TranslateError
]
B NaffHeapExit ; and sort of fail
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
; ReadBlockSize. Top level HeapEntry
; =============
;
ReadBlockSize
Push "addr, work"
BL FindHeapBlock
LDRVC size, [addr, hpd]
Pull "addr, work"
BVC GoodHeapExit
B NaffHeapExit
;**************************************************************************
; Common routines for free/extend
FindHeapBlock ROUT
; Convert addr to address
; Validate heap
; check block is an allocated block
; return tp = free list entry before the block (hpdfree if none)
; work = free list before that (if exists)
; corrupts HpTemp, bp
Push lr
ValidateHpd findfailed
SUB addr, addr, hpd ; convert to offset
SUB addr, addr, #4 ; real block posn
; Find block in heap by chaining down freelist, stepping through blocks
; TMD 01-Mar-89
; no need to check explicitly for null free list, code drops thru OK
[ debheap
LDR HpTemp, hpddebug
CMP HpTemp, #0
BEQ %FT03
Push lr
WRLN "Scanning freelist"
Pull lr
03
]
; step down free list to find appropriate chunk
; get tp = free block before addr
; HpTemp = " " after "
; work = block before tp
MOV tp, #:INDEX: hpdfree
StepDownFreeList
LDR HpTemp, [hpd, tp] ; link offset
CMP HpTemp,#Nil
BEQ ListEnded ; EQ state used!
ADD HpTemp, HpTemp, tp
CMP HpTemp, addr
MOVLS work, tp
MOVLS tp, HpTemp
BLS StepDownFreeList
ListEnded
LDREQ HpTemp, hpdbase ; if EQ from CMP HpTemp, addr
; then bad block anyway
CMP tp, #:INDEX: hpdfree
MOVEQ bp, #hpdsize ; is this a fudge I see before me?
BEQ ScanAllocForAddr
ADD bp, tp, #fresize
LDR bp, [hpd, bp]
ADD bp, tp, bp
ScanAllocForAddr
; bp -> start of allocated chunk
; HpTemp -> end " " "
; scan to find addr, error if no in here
Push work ; keep prevlink ptr
[ debheap
; HpTemp critical
Push "HpTemp, R0, link"
LDR HpTemp, hpddebug
CMP HpTemp, #0
BEQ %FT02
STRIM "Scan for addr from "
MOV R0, bp
BL PrintOffset
STRIM " to "
LDR R0,[stack,#4] ; HpTemp
BL PrintOffsetLine
02
Pull "HpTemp, r0, link"
]
B CheckForNullAllocn
ScanAllocForAddrLoop
CMP bp, addr
BEQ ValidBlock
LDR work, [bp, hpd] ; get size
ADD bp, bp, work
CheckForNullAllocn
CMP bp, HpTemp
BLT ScanAllocForAddrLoop
[ debheap
Push lr
STRIM "Given pointer not a block"
Pull lr
]
ADRL R0, ErrorBlock_HeapFail_NotABlock
[ International
BL TranslateError
|
SETV
]
Pull "work, pc"
ValidBlock ; tp = free link offset, addr = block offset
CLRV
Pull "work, pc"
findfailed_badhpd
[ debheap
Push lr
STRIM "Invalid heap descriptor"
Pull lr
]
ADRL R0, ErrorBlock_HeapFail_BadDesc
[ International
BL TranslateError
|
SETV
]
Pull PC
;****************************************************************************
FreeChunkWithConcatenation ROUT
; in : addr -> block
; tp -> preceding free list entry
; out : block freed, concatenated with any free parts on either side,
; base reduced if can do
; corrupts HpTemp, bp, size, addr
; TMD 01-Mar-89: FindHeapBlock now never returns tp=Nil, only tp=hpdfree,
; so no need for check, code will get there eventually!
; attempt concatenation with free blocks on both/either side
[ debheap
Push "R0, lr"
LDR HpTemp, hpddebug
CMP HpTemp, #0
BEQ %FT04
STRIM "concatenation attempt with free ptr "
MOV R0,tp
BL PrintOffsetLine
04
Pull "R0, lr"
]
[ DebugHeaps
ORR bp, hpd, #FreeSpaceDebugMask
LDR size, [addr, hpd]!
ADD HpTemp, addr, size
SUB HpTemp, HpTemp, #4 ; HpTemp -> last word of block
10
STR bp, [HpTemp], #-4 ; store word, then go back
TEQ HpTemp, addr ; loop until done, but don't overwrite size field
BNE %BT10 ; otherwise we might get an IRQ with a duff heap
SUB addr, addr, hpd ; make addr an offset again
]
LDR size, [addr, hpd] ; block size
ADD bp, size, addr ; eob offset
LDR HpTemp, [tp, hpd] ; Nil doesn't matter here!
ADD HpTemp, HpTemp, tp ; offset of free block after ours
CMP HpTemp, bp ; if tp was hpdfree then <> bp
BNE NoConcatWithNext ; so will take branch
[ debheap
Push lr
LDR bp, hpddebug
CMP bp, #0
BEQ %FT05
WRLN "concatenating with block after"
05
Pull lr
]
ADD bp, hpd, HpTemp
LDR bp, [bp, #fresize]
ADD bp, bp, size
WritePSRc SVC_mode+I_bit, size
STR bp, [addr, hpd] ; enlarge our block
LDR bp, [HpTemp, hpd] ; offset in free list
CMP bp, #Nil
ADDNE bp, HpTemp, bp ; offset from heap start
SUBNE bp, bp, tp ; free list offset
STR bp, [tp, hpd] ; free list updated, our block bigger
; - but not in the free list yet!
NoConcatWithNext ; tp = free link offset, addr = block offset
; now try for concatenation with previous block
CMP tp, #:INDEX: hpdfree ; are we before any real free blocks?
BEQ NoConcatenation ; yup
ADD HpTemp, tp, hpd
LDR size, [HpTemp, #fresize]
ADD bp, size, tp
CMP bp, addr
BNE NoConcatenation
[ debheap
Push lr
LDR bp, hpddebug
CMP bp, #0
BEQ %FT06
WRLN "concatenating with block before"
STRIM "prevfree = "
Push R0
MOV R0, work
BL PrintOffsetLine
Pull R0
06
Pull lr
]
LDR bp, [addr, hpd] ; get block size
ADD size, bp, size ; new free block size
WritePSRc SVC_mode+I_bit, bp
STR size, [HpTemp, #fresize]
; now check for butts against base : work is still prevnode to tp
ADD HpTemp, size, tp
LDR bp, hpdbase
CMP bp, HpTemp
BNE %FT06 ; all done : exit keeping IRQs off
SUB bp, bp, size
STR bp, hpdbase ; step unused bit back
MOV bp, #Nil ; this MUST have been last free block!
STR bp, [work, hpd]
06
CLRV
MOV PC, lr ; Whew!
NoConcatenation ; check if block butts against base
; tp = previous freelink offset
LDR size, [addr, hpd]
ADD HpTemp, size, addr
LDR bp, hpdbase
CMP bp, HpTemp
BNE AddToFreeList
SUB bp, bp, size
WritePSRc SVC_mode+I_bit, HpTemp
STR bp, hpdbase
CLRV
MOV PC, lr
AddToFreeList ; block at addr, previous free at tp
[ debheap
Push "R0, lr"
LDR HpTemp, hpddebug
CMP HpTemp, #0
BEQ %FT07
STRIM "add to free list : free link "
MOV R0,tp
BL PrintOffset
STRIM ", block "
MOV R0, addr
BL PrintOffsetLine
07
Pull "R0, lr"
]
LDR size, [addr, hpd]!
WritePSRc SVC_mode+I_bit, HpTemp
STR size, [addr, #fresize]
SUB addr, addr, hpd
LDR size, [hpd, tp] ; prevlink
CMP size, #Nil
SUBNE size, size, addr
ADDNE size, size, tp ; form offset if not eolist
STR size, [addr, hpd]
SUB size, addr, tp
STR size, [tp, hpd]
CLRV
MOV PC, lr
;*****************************************************************************
[ debheap
;
; ShowHeap. Top level HeapEntry
; ========
;
; Dump the heap pointed to by hpd
ShowHeap
Push link
BL iShowHeap ; Needed to fudge link for SVC mode entry
Pull link
B GoodHeapExit
iShowHeap ROUT ; Internal entry point for debugging heap
Push "r0, hpd, addr, size, work, bp, tp, link"
ValidateHpd showfailed ; debugging heaps won't work interruptibly
LDR tp, hpdfree
CMP tp, #Nil
ADDNE tp, tp, #:INDEX: hpdfree
LDR bp, hpdbase
MOV addr, #hpdsize
LDR work, hpdend
SWI OS_NewLine ; Initial blurb about hpd contents
DREG hpd, "**** Heap map **** : hpd "
STRIM "-> free"
MOV r0, tp
BL PrintOffset
STRIM ", base"
MOV r0, bp
BL PrintOffsetLine
STRIM "-> start"
MOV r0, addr
BL PrintOffset
STRIM ", end"
MOV r0, work
BL PrintOffsetLine
SUB r0, work, bp ; hpdend-hpdbase
DREG r0,"Bytes free: ",concat, Word
SUB r0, bp, addr ; hpdbase-hpdsize
DREG r0,", bytes used: ",, Word
SWI XOS_NewLine
CMP tp, #Nil ; No free blocks at all ?
BNE %FT10
WRLN "No Free Blocks"
CMP bp, addr ; Is a block allocated at all ?
MOVNE r0, addr ; hpdsize
BNE %FT40
WRLN "No Used Blocks"
B %FT99
10 CMP tp, addr ; hpdsize ; Allocated block below first free ?
BEQ %FT15
MOV r0, addr ; hpdbase
BL HexUsedBlk
SUB r0, tp, addr ; hpdfree-hpdsize
DREG r0
SWI XOS_NewLine
; Main loop chaining up free space list
15 ADD addr, tp, hpd ; convert to address
LDR size, [addr, #fresize] ; Size of this block
LDR addr, [addr, #frelink] ; offset to next block
STRIM "Free Block "
MOV r0, tp
BL PrintOffset
DREG size, ", size "
ADD r0, tp, size ; r0 -> eob. Adjacent free blocks don't exist
CMP addr, #Nil ; If last block, then must we see if we're = hpdbase
BEQ %FT40
; Used block starts at r0, ends at addr+tp - so size = (addr+tp)-r0
BL HexUsedBlk
SUB r0, addr, r0 ; addr-r0
ADD r0, r0, tp ; used block size
DREG r0
SWI XOS_NewLine
ADD tp, addr, tp ; step down free list
B %BT15 ; And loop
40 CMP r0, bp ; Is there any allocated space after this block ?
BEQ %FT99
BL HexUsedBlk
SUB r0, bp, r0 ; hpdbase-sob
DREG r0
SWI XOS_NewLine
99
CLRV
GRAB "r0, hpd, addr, size, work, bp, tp, pc"
showfailed_badhpd
WRLN "Invalid heap descriptor : ShowHeap failed"
GRAB "r0, hpd, addr, size, work, bp, tp, pc"
HexUsedBlk
Push "lr"
STRIM "Used Block "
BL PrintOffset
STRIM ", size"
Pull "lr"
MOV PC, R14
PrintOffset
Push "r0, lr"
DREG r0
CMP R0, #0
ADDNE R0, R0, hpd
DREG r0," (",concat
STRIM ")"
GRAB "R0, PC"
PrintOffsetLine
Push "lr"
BL PrintOffset
SWI XOS_NewLine
Pull "PC"
]
[ TubeInfo
TubeDumpR0 ROUT
Push "R1, R2, lr"
TubeChar R0, R1, "MOV R1, #10"
TubeChar R0, R1, "MOV R1, #13"
MOV R1, #7
01 MOV R0, R0, ROR #28
AND R2, R0, #&F
TubeChar R0, R1, "ADD R1, R2, #""0"""
SUBS R1, R1, #1
BPL %BT01
Pull "R1, R2, PC"
]
HeapCode_end
; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
END