-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathgasman.c
2263 lines (1977 loc) · 86.8 KB
/
gasman.c
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
/****************************************************************************
**
*W gasman.c GAP source Martin Schönert
**
**
*Y Copyright (C) 1996, Lehrstuhl D für Mathematik, RWTH Aachen, Germany
*Y (C) 1998 School Math and Comp. Sci., University of St Andrews, Scotland
*Y Copyright (C) 2002 The GAP Group
**
** This file contains the functions of Gasman, the GAP storage manager.
**
** {\Gasman} is a storage manager for applications written in C. That means
** that an application using {\Gasman} requests blocks of storage from
** {\Gasman}. Those blocks of storage are called *bags*. Then the
** application writes data into and reads data from the bags. Finally a bag
** is no longer needed and the application simply forgets it. We say that
** such a bag that is no longer needed is *dead*. {\Gasman} cares about the
** allocation of bags and deallocation of dead bags. Thus these operations
** are transparent to the application, enabling the programmer to
** concentrate on algorithms instead of caring about storage allocation and
** deallocation.
**
** {\Gasman} implements an automatic, cooperating, compacting, generational,
** conservative storage management
**
** *Automatic* means that the application only allocates bags. It need not
** explicitly deallocate them. {\Gasman} automatically determines which
** bags are dead and deallocates them. This is done by a process called
** *garbage collection*. Garbage refers to the bags that are dead, and
** collection refers to the process of deallocating them.
**
** *Cooperating* means that the application must cooperate with {\Gasman},
** that is it must follow two rules. One rule is that it must not remember
** the addresses of the data area of a bag for too long. The other rule is
** that it must inform {\Gasman} when it has changed a bag.
**
** *Compacting* means that after a garbage collection {\Gasman} compacts the
** bags that are still live, so that the storage made available by
** deallocating the dead bags becomes one large contiguous block. This
** helps to avoid *fragmentation* of the free storage. The downside is that
** the address of the data area of a bag may change during a garbage
** collection, which is the reason why the application must not remember
** this address for too long, i.e., must not keep pointers to or into the
** data area of a bag over a garbage collection.
**
** *Generational* means that {\Gasman} distinguishes between old and young
** bags. Old bags have been allocated some time ago, i.e., they survived at
** least one garbage collection. During a garbage collection {\Gasman} will
** first find and deallocate the dead young bags. Only if that does not
** produce enough free storage, {\Gasman} will find and deallocate the dead
** old bags. The idea behind this is that usually most bags have a very
** short life, so that they will die young. The downside is that this
** requires {\Gasman} to quickly find the young bags that are referenced
** from old bags, which is the reason why an application must inform
** {\Gasman} when it has changed a bag.
**
** *Conservative* means that there are situations in which {\Gasman} cannot
** decide with absolute certainty whether a bag is still live or already
** dead. In these situations {\Gasman} has to assume that the bag is still
** live, and may thus keep a bag longer than it is necessary.
**
** What follows describes the reasons for this design, and at the same time
** the assumptions that were made about the application. This is given so
** you can make an educated guess as to whether {\Gasman} is an appropriate
** storage manager for your application.
**
** {\Gasman} is automatic, because this makes it easier to use in large or
** complex applications. Namely in with a non-automatic storage manager the
** application must decide when to deallocate a bag. This requires in
** general global knowledge, i.e., it is not sufficient to know whether the
** current function may still need the bag, one also needs to know whether
** any other function may still need the bag. With growing size or
** complexity of the application it gets harder to obtain this knowledge.
**
** {\Gasman} is cooperating, because this is a requirement for compaction
** and generations (at least without cooperation, compaction and generations
** are very difficult). As described below, the former is important for
** storage efficiency, the latter for time efficiency. Note that the
** cooperation requires only local knowledge, i.e., whether or not a certain
** function of the application follows the two rules can be decided by just
** looking at the function without any knowledge about the rest of the
** application.
**
** {\Gasman} is compacting, because this allows the efficient usage of the
** available storage by applications where the ratio between the size of the
** smallest and the largest bag is large. Namely with a non-compacting
** storage manager, a part of the free storage may become unavailable
** because it is fragmented into many small pieces, each of which is too
** small to serve an allocation.
**
** {\Gasman} is generational, because this makes it very much faster, at
** least for those applications where most bags will indeed die young.
** Namely a non-generational storage manager must test for each bag whether
** or not it is still live during each garbage collection. However with
** many applications the probability that an old bag, i.e., one that
** survived at least one garbage collection, will also survive the next
** garbage collection is high. A generational storage manager simply
** assumes that each old bag is still live during most garbage collections.
** Thereby it avoids the expensive tests for most bags during most garbage
** collections.
**
** {\Gasman} is conservative, because for most applications only few bags
** are incorrectly assumed to be still live and the additional cooperation
** required to make {\Gasman} (more) precise would slow down the
** application. Note that the problem appears since the C compiler provides
** not enough information to distinguish between true references to bags and
** other values that just happen to look like references. Thus {\Gasman}
** has to assume that everything that could be interpreted as a reference to
** a bag is indeed such a reference, and that this bag is still live.
** Therefore some bags may be kept by {\Gasman}, even though they are
** already dead.
*/
#include <string.h>
#include <src/system.h> /* Ints, UInts */
#include <src/gasman.h> /* garbage collector */
#include <src/objects.h> /* objects */
#include <src/io.h> /* for Pr */
#include <src/gaputils.h>
#include <stddef.h>
/****************************************************************************
**
*F WORDS_BAG( <size> ) . . . . . . . . . . words used by a bag of given size
**
** The structure of a bag is a follows{\:}
**
** <identifier>
** __/
** /
** V
** +---------+
** |<masterp>|
** +---------+
** \________________________
** \
** V
** +------+-------+------+---------+--------------------------------+----+
** |<size>|<flags>|<type>| <link> | . . ...| pad|
** +------+-------+------+---------+--------------------------------+----+
**
** A bag consists of a masterpointer, and a body.
**
** The *masterpointer* is a pointer to the data area of the bag. During a
** garbage collection the masterpointer is the only active pointer to the
** data area of the bag, because of the rule that no pointers to or into the
** data area of a bag may be remembered over calls to functions that may
** cause a garbage collection. It is the job of the garbage collection to
** update the masterpointer of a bag when it moves the bag.
**
** The *identifier* of the bag is a pointer to (the address of) the
** masterpointer of the bag. Thus 'PTR_BAG(<bag>)' is simply '\*<bag>'
** plus a cast.
**
** The *body* of a bag consists of a header, the data area, and the padding.
**
** The header in turn consists of the *type byte*, *flags byte* and the
** *size field*, which is either 32 bits or 48 bits (on 32 resp. 64 bit systems),
** followed by a link word. The 'BagHeader' struct describes the exact
** structure of the header.
**
** The *link word* usually contains the identifier of the bag, i.e., a
** pointer to the masterpointer of the bag. Thus the garbage collection can
** find the masterpointer of a bag through the link word if it knows the
** address of the data area of the bag. The link word is also used by
** {\Gasman} to keep bags on two linked lists (see "ChangedBags" and
** "MarkedBags").
**
** The *data area* of a bag is the area that contains the data stored by
** the application in this bag.
**
** The *padding* consists of up to 'sizeof(Bag)-1' bytes and pads the body
** so that the size of a body is always a multiple of 'sizeof(Bag)'. This
** is to ensure that bags are always aligned. The macro 'WORDS_BAG(<size>)'
** returns the number of words occupied by the data area and padding of a
** bag of size <size>.
**
** A body in the workspace whose type byte contains the value 255 is the
** remainder of a 'ResizeBag'. That is it consists either of the unused words
** after a bag has been shrunk, or of the old body of the bag after the
** contents of the body have been copied elsewhere for an extension. The
** size field in the bag header contains the number of bytes in
** this area excluding the first word itself. Note that such a body has no
** link word, because such a remainder does not correspond to a bag (see
** "Implementation of ResizeBag").
**
** A masterpointer with a value congruent to 1 mod 4 is the relic of an
** object that was weakly but not strongly marked in a recent garbage
** collection. These persist until after the next full garbage collection
** by which time all references to them should have been removed.
**
*/
#define SIZE_MPTR_BAGS 1
#define WORDS_BAG(size) (((size) + (sizeof(Bag)-1)) / sizeof(Bag))
/* This could be 65536, but would waste memory in various tables */
#define NTYPES 256
static inline Bag *DATA(BagHeader *bag)
{
return (Bag *)(((char *)bag) + sizeof(BagHeader));
}
/****************************************************************************
**
*V MptrBags . . . . . . . . . . . . . . beginning of the masterpointer area
*V OldBags . . . . . . . . . . . . . . . . . beginning of the old bags area
*V YoungBags . . . . . . . . . . . . . . . beginning of the young bags area
*V AllocBags . . . . . . . . . . . . . . . beginning of the allocation area
*V AllocSizeBags . . . . . . . . . . . . . . . . size of the allocation area
*V EndBags . . . . . . . . . . . . . . . . . . . . . . end of the workspace
**
** {\Gasman} manages one large block of storage called the *workspace*. The
** layout of the workspace is as follows{\:}
**
** +-----------------+-----------------+-----------------+-----------------+
** | masterpointer | old bags | young bags | allocation |
** | area | area | area | area |
** +-----------------+-----------------+-----------------+-----------------+
** ^ ^ ^ ^ ^
** MptrBags OldBags YoungBags AllocBags EndBags
**
** The *masterpointer area* contains all the masterpointers of the bags.
** 'MptrBags' points to the beginning of this area and 'OldBags' to the end.
**
** The *old bags area* contains the bodies of all the bags that survived at
** least one garbage collection. This area is only scanned for dead bags
** during a full garbage collection. 'OldBags' points to the beginning of
** this area and 'YoungBags' to the end.
**
** The *young bags area* contains the bodies of all the bags that have been
** allocated since the last garbage collection. This area is scanned for
** dead bags during each garbage collection. 'YoungBags' points to the
** beginning of this area and 'AllocBags' to the end.
**
** The *allocation area* is the storage that is available for allocation of
** new bags. When a new bag is allocated the storage for the body is taken
** from the beginning of this area, and this area is correspondingly
** reduced. If the body does not fit in the allocation area a garbage
** collection is performed. 'AllocBags' points to the beginning of this
** area and 'EndBags' to the end.
**
** Note that the borders between the areas are not static. In particular
** each allocation increases the size of the young bags area and reduces the
** size of the allocation area. On the other hand each garbage collection
** empties the young bags area.
*/
Bag * MptrBags;
Bag * OldBags;
Bag * YoungBags;
Bag * AllocBags;
UInt AllocSizeBags;
Bag * EndBags;
#if defined(MEMORY_CANARY)
#include <valgrind/valgrind.h>
#include <valgrind/memcheck.h>
Int canary_size() {
Int bufsize = (Int)EndBags - (Int)AllocBags;
return bufsize<4096?bufsize:4096;
}
void ADD_CANARY() {
VALGRIND_MAKE_MEM_NOACCESS(AllocBags, canary_size());
}
void CLEAR_CANARY() {
VALGRIND_MAKE_MEM_DEFINED(AllocBags, canary_size());
}
#define CANARY_DISABLE_VALGRIND() VALGRIND_DISABLE_ERROR_REPORTING
#define CANARY_ENABLE_VALGRIND() VALGRIND_ENABLE_ERROR_REPORTING
void CHANGED_BAG(Bag bag) {
CANARY_DISABLE_VALGRIND();
if (PTR_BAG(bag) <= YoungBags && LINK_BAG(bag) == bag) {
LINK_BAG(bag) = ChangedBags;
ChangedBags = bag;
}
CANARY_ENABLE_VALGRIND();
}
#else
#define ADD_CANARY()
#define CLEAR_CANARY()
#define CANARY_DISABLE_VALGRIND()
#define CANARY_ENABLE_VALGRIND()
#endif
/* These macros, are (a) for more readable code, but more importantly
(b) to ensure that unsigned subtracts and divides are used (since
we know the ordering of the pointers. This is needed on > 2GB
workspaces on 32 but systems. The Size****Area functions return an
answer in units of a word (ie sizeof(UInt) bytes), which should
therefore be small enough not to cause problems. */
#define SpaceBetweenPointers(a,b) (((UInt)((UInt)(a) - (UInt)(b)))/sizeof(Bag))
#define SizeMptrsArea SpaceBetweenPointers(OldBags, MptrBags)
#define SizeOldBagsArea SpaceBetweenPointers(YoungBags, OldBags)
#define SizeYoungBagsArea SpaceBetweenPointers(AllocBags, YoungBags)
#define SizeAllocationArea SpaceBetweenPointers(EndBags, AllocBags)
#define SizeAllBagsArea SpaceBetweenPointers(AllocBags, OldBags)
#define SizeWorkspace SpaceBetweenPointers(EndBags, MptrBags)
/****************************************************************************
**
*V FreeMptrBags . . . . . . . . . . . . . . . list of free bag identifiers
**
** 'FreeMptrBags' is the first free bag identifier, i.e., it points to the
** first available masterpointer. If 'FreeMptrBags' is 0 there are no
** available masterpointers. The available masterpointers are managed in a
** forward linked list, i.e., each available masterpointer points to the
** next available masterpointer, except for the last, which contains 0.
**
** When a new bag is allocated it gets the identifier 'FreeMptrBags', and
** 'FreeMptrBags' is set to the value stored in this masterpointer, which is
** the next available masterpointer. When a bag is deallocated by a garbage
** collection its masterpointer is added to the list of available
** masterpointers again.
*/
Bag FreeMptrBags;
/****************************************************************************
**
*V ChangedBags . . . . . . . . . . . . . . . . . . list of changed old bags
**
** 'ChangedBags' holds a list of old bags that have been changed since the
** last garbage collection, i.e., for which either 'CHANGED_BAG' was called
** or which have been resized.
**
** This list starts with the bag whose identifier is 'ChangedBags', and the
** link word of each bag on the list contains the identifier of the next bag
** on the list. The link word of the last bag on the list contains 0. If
** 'ChangedBags' has the value 0, the list is empty.
**
** The garbage collection needs to know which young bags are subbags of old
** bags, since it must not throw those away in a partial garbage
** collection. Only those old bags that have been changed since the last
** garbage collection can contain references to young bags, which have been
** allocated since the last garbage collection. The application cooperates
** by informing {\Gasman} with 'CHANGED_BAG' which bags it has changed. The
** list of changed old bags is scanned by a partial garbage collection and
** the young subbags of the old bags on this list are marked with 'MarkBag'
** (see "MarkedBags"). Without this list 'CollectBags' would have to scan
** all old bags for references to young bags, which would take too much time
** (see "Implementation of CollectBags").
**
** 'CHANGED_BAG' puts a bag on the list of changed old bags. 'CHANGED_BAG'
** first checks that <bag> is an old bag by checking that 'PTR_BAG( <bag> )'
** is smaller than 'YoungBags'. Then it checks that the bag is not already
** on the list of changed bags by checking that the link word still contains
** the identifier of <bag>. If <bag> is an old bag that is not already on
** the list of changed bags, 'CHANGED_BAG' puts <bag> on the list of changed
** bags, by setting the link word of <bag> to the current value of
** 'ChangedBags' and then setting 'ChangedBags' to <bag>.
*/
Bag ChangedBags;
/****************************************************************************
**
*V MarkedBags . . . . . . . . . . . . . . . . . . . . . list of marked bags
**
** 'MarkedBags' holds a list of bags that have already been marked during a
** garbage collection by 'MarkBag'. This list is only used during garbage
** collections, so it is always empty outside of garbage collections (see
** "Implementation of CollectBags").
**
** This list starts with the bag whose identifier is 'MarkedBags', and the
** link word of each bag on the list contains the identifier of the next bag
** on the list. The link word of the last bag on the list contains 0. If
** 'MarkedBags' has the value 0, the list is empty.
**
** Note that some other storage managers do not use such a list during the
** mark phase. Instead they simply let the marking functions call each
** other. While this is somewhat simpler it may use an unbound amount of
** space on the stack. This is particularly bad on systems where the stack
** is not in a separate segment of the address space, and thus may grow into
** the workspace, causing disaster.
**
** 'MarkBag' puts a bag <bag> onto this list. 'MarkBag' has to be
** careful, because it can be called with an argument that is not really a
** bag identifier, and may point outside the programs address space. So
** 'MarkBag' first checks that <bag> points to a properly aligned location
** between 'MptrBags' and 'OldBags'. Then 'MarkBag' checks that <bag> is
** the identifier of a young bag by checking that the masterpointer points
** to a location between 'YoungBags' and 'AllocBags' (if <bag> is the
** identifier of an old bag, the masterpointer will point to a location
** between 'OldBags' and 'YoungBags', and if <bag> only appears to be an
** identifier, the masterpointer could be on the free list of masterpointers
** and point to a location between 'MptrBags' and 'OldBags'). Then
** 'MarkBag' checks that <bag> is not already marked by checking that the
** link word of <bag> contains the identifier of the bag. If any of the
** checks fails, 'MarkBag' does nothing. If all checks succeed, 'MarkBag'
** puts <bag> onto the list of marked bags by putting the current value of
** 'ChangedBags' into the link word of <bag> and setting 'ChangedBags' to
** <bag>. Note that since bags are always placed at the front of the list,
** 'CollectBags' will mark the bags in a depth-first order. This is
** probably good to improve the locality of reference.
*/
Bag MarkedBags;
/****************************************************************************
**
*V NrAllBags . . . . . . . . . . . . . . . . . number of all bags allocated
*V SizeAllBags . . . . . . . . . . . . . . total size of all bags allocated
*V NrLiveBags . . . . . . . . . . number of bags that survived the last gc
*V SizeLiveBags . . . . . . . total size of bags that survived the last gc
*V NrDeadBags . . . . . . . number of bags that died since the last full gc
*V SizeDeadBags . . . . total size of bags that died since the last full gc
*V NrHalfDeadBags . . . . . number of bags that died since the last full gc
** but may still be weakly pointed to
*/
UInt NrAllBags;
UInt8 SizeAllBags;
UInt NrLiveBags;
UInt SizeLiveBags;
UInt NrDeadBags;
UInt8 SizeDeadBags;
UInt NrHalfDeadBags;
/****************************************************************************
**
*V InfoBags[<type>] . . . . . . . . . . . . . . . . . information for bags
*/
TNumInfoBags InfoBags [ NTYPES ];
/****************************************************************************
**
*F IS_BAG -- check if a value looks like a masterpointer reference.
*/
static inline UInt IS_BAG (
UInt bid )
{
return (((UInt)MptrBags <= bid)
&& (bid < (UInt)OldBags)
&& (bid & (sizeof(Bag)-1)) == 0);
}
/****************************************************************************
**
*F InitMsgsFuncBags(<msgs-func>) . . . . . . . . . install message function
**
** 'InitMsgsFuncBags' simply stores the printing function in a global
** variable.
*/
TNumMsgsFuncBags MsgsFuncBags;
void InitMsgsFuncBags (
TNumMsgsFuncBags msgs_func )
{
MsgsFuncBags = msgs_func;
}
/****************************************************************************
**
*F InitSweepFuncBags(<type>,<mark-func>) . . . . install sweeping function
*/
TNumSweepFuncBags TabSweepFuncBags [ NTYPES ];
void InitSweepFuncBags (
UInt type,
TNumSweepFuncBags sweep_func )
{
#ifdef CHECK_FOR_CLASH_IN_INIT_SWEEP_FUNC
char str[256];
if ( TabSweepFuncBags[type] != 0 ) {
str[0] = 0;
strncat( str, "warning: sweep function for type ", 33 );
str[33] = '0' + ((type/100) % 10);
str[34] = '0' + ((type/ 10) % 10);
str[35] = '0' + ((type/ 1) % 10);
str[36] = 0;
strncat( str, " already installed\n", 19 );
SyFputs( str, 0 );
}
#endif
TabSweepFuncBags[type] = sweep_func;
}
/****************************************************************************
**
*F InitMarkFuncBags(<type>,<mark-func>) . . . . . install marking function
*F MarkNoSubBags(<bag>) . . . . . . . . marking function that marks nothing
*F MarkOneSubBags(<bag>) . . . . . . marking function that marks one subbag
*F MarkTwoSubBags(<bag>) . . . . . . marking function that marks two subbags
*F MarkThreeSubBags(<bag>) . . . . marking function that marks three subbags
*F MarkFourSubBags(<bag>) . . . . marking function that marks four subbags
*F MarkAllSubBags(<bag>) . . . . . . marking function that marks everything
**
** 'InitMarkFuncBags', 'MarkNoSubBags', 'MarkOneSubBags', 'MarkTwoSubBags',
** and 'MarkAllSubBags' are really too simple for an explanation.
**
** 'MarkAllSubBagsDefault' is the same as 'MarkAllSubBags' but is only used
** by GASMAN as default. This will allow to catch type clashes.
*/
TNumMarkFuncBags TabMarkFuncBags [ NTYPES ];
void InitMarkFuncBags (
UInt type,
TNumMarkFuncBags mark_func )
{
#ifdef CHECK_FOR_CLASH_IN_INIT_MARK_FUNC
char str[256];
if ( TabMarkFuncBags[type] != MarkAllSubBagsDefault ) {
str[0] = 0;
strncat( str, "warning: mark function for type ", 32 );
str[32] = '0' + ((type/100) % 10);
str[33] = '0' + ((type/ 10) % 10);
str[34] = '0' + ((type/ 1) % 10);
str[35] = 0;
strncat( str, " already installed\n", 19 );
SyFputs( str, 0 );
}
#endif
TabMarkFuncBags[type] = mark_func;
}
#define MARKED_DEAD(x) (x)
#define MARKED_ALIVE(x) ((Bag)(((Char *)(x))+1))
#define MARKED_HALFDEAD(x) ((Bag)(((Char *)(x))+2))
#define IS_MARKED_ALIVE(bag) ((LINK_BAG(bag)) == MARKED_ALIVE(bag))
#define IS_MARKED_DEAD(bag) ((LINK_BAG(bag)) == MARKED_DEAD(bag))
#define IS_MARKED_HALFDEAD(bag) ((LINK_BAG(bag)) == MARKED_HALFDEAD(bag))
#define UNMARKED_DEAD(x) (x)
#define UNMARKED_ALIVE(x) ((Bag)(((Char *)(x))-1))
#define UNMARKED_HALFDEAD(x) ((Bag)(((Char *)(x))-2))
void MarkNoSubBags( Bag bag )
{
}
void MarkOneSubBags( Bag bag )
{
Bag sub; /* one subbag identifier */
sub = PTR_BAG(bag)[0];
MarkBag( sub );
}
void MarkTwoSubBags( Bag bag )
{
Bag sub; /* one subbag identifier */
sub = PTR_BAG(bag)[0];
MarkBag( sub );
sub = PTR_BAG(bag)[1];
MarkBag( sub );
}
void MarkThreeSubBags( Bag bag )
{
Bag sub; /* one subbag identifier */
sub = PTR_BAG(bag)[0];
MarkBag( sub );
sub = PTR_BAG(bag)[1];
MarkBag( sub );
sub = PTR_BAG(bag)[2];
MarkBag( sub );
}
void MarkFourSubBags( Bag bag )
{
Bag sub; /* one subbag identifier */
sub = PTR_BAG(bag)[0];
MarkBag( sub );
sub = PTR_BAG(bag)[1];
MarkBag( sub );
sub = PTR_BAG(bag)[2];
MarkBag( sub );
sub = PTR_BAG(bag)[3];
MarkBag( sub );
}
inline void MarkArrayOfBags( Bag array[], int count )
{
int i;
for (i = 0; i < count; i++) {
MarkBag( array[i] );
}
}
void MarkAllSubBags( Bag bag )
{
MarkArrayOfBags( PTR_BAG( bag ), SIZE_BAG(bag)/sizeof(Bag) );
}
void MarkAllSubBagsDefault( Bag bag )
{
MarkArrayOfBags( PTR_BAG( bag ), SIZE_BAG(bag)/sizeof(Bag) );
}
// We define MarkBag as a inline function here so that
// the compiler can optimize the marking functions using it in the
// "current translation unit", i.e. inside gasman.c.
// Other marking functions don't get to inline MarkBag calls anymore,
// but luckily these are rare (and usually not performance critical
// to start with).
inline void MarkBag( Bag bag )
{
if ( (((UInt)bag) & (sizeof(Bag)-1)) == 0 /* really looks like a pointer */
&& (Bag)MptrBags <= bag /* in plausible range */
&& bag < (Bag)OldBags /* " " " */
&& YoungBags < PTR_BAG(bag) /* points to a young bag */
&& PTR_BAG(bag) <= AllocBags /* " " " " " */
&& (IS_MARKED_DEAD(bag) || IS_MARKED_HALFDEAD(bag)) )
{
LINK_BAG(bag) = MarkedBags;
MarkedBags = bag;
}
}
void MarkBagWeakly( Bag bag )
{
if ( (((UInt)bag) & (sizeof(Bag)-1)) == 0 /* really looks like a pointer */
&& (Bag)MptrBags <= bag /* in plausible range */
&& bag < (Bag)OldBags /* " " " */
&& YoungBags < PTR_BAG(bag) /* points to a young bag */
&& PTR_BAG(bag) <= AllocBags /* " " " " " */
&& IS_MARKED_DEAD(bag) ) /* and not marked already */
{
LINK_BAG(bag) = (Bag)MARKED_HALFDEAD(bag); /* mark it now as we
don't have to recurse */
}
}
/****************************************************************************
**
*F CallbackForAllBags( <func> ) call a C function on all non-zero mptrs
**
** This calls a C function on every bag, including garbage ones, by simply
** walking the masterpointer area. Not terribly safe.
**
*/
void CallbackForAllBags(
void (*func)(Bag) )
{
Bag ptr;
for (ptr = (Bag)MptrBags; ptr < (Bag)OldBags; ptr ++)
if (*ptr != 0 && !IS_WEAK_DEAD_BAG(ptr) && (Bag)(*ptr) >= (Bag)OldBags)
{
(*func)(ptr);
}
}
/****************************************************************************
**
*V GlobalBags . . . . . . . . . . . . . . . . . . . . . list of global bags
*/
TNumGlobalBags GlobalBags;
/****************************************************************************
**
*F InitGlobalBag(<addr>, <cookie>) inform Gasman about global bag identifier
**
** 'InitGlobalBag' simply leaves the address <addr> in a global array, where
** it is used by 'CollectBags'. <cookie> is also recorded to allow things to
** be matched up after loading a saved workspace.
*/
Int WarnInitGlobalBag;
static UInt GlobalSortingStatus;
extern TNumAbortFuncBags AbortFuncBags;
void ClearGlobalBags ( void )
{
UInt i;
for (i = 0; i < GlobalBags.nr; i++)
{
GlobalBags.addr[i] = 0L;
GlobalBags.cookie[i] = 0L;
}
GlobalBags.nr = 0;
GlobalSortingStatus = 0;
WarnInitGlobalBag = 0;
}
void InitGlobalBag (
Bag * addr,
const Char * cookie )
{
if ( GlobalBags.nr == NR_GLOBAL_BAGS ) {
(*AbortFuncBags)(
"Panic: Gasman cannot handle so many global variables" );
}
#ifdef DEBUG_GLOBAL_BAGS
{
UInt i;
if (cookie != (Char *)0)
for (i = 0; i < GlobalBags.nr; i++)
if ( 0 == strcmp(GlobalBags.cookie[i], cookie) )
if (GlobalBags.addr[i] == addr)
Pr("Duplicate global bag entry %s\n", (Int)cookie, 0L);
else
Pr("Duplicate global bag cookie %s\n", (Int)cookie, 0L);
}
#endif
if ( WarnInitGlobalBag ) {
Pr( "#W global bag '%s' initialized\n", (Int)cookie, 0L );
}
GlobalBags.addr[GlobalBags.nr] = addr;
GlobalBags.cookie[GlobalBags.nr] = cookie;
GlobalBags.nr++;
GlobalSortingStatus = 0;
}
static Int IsLessGlobal (
const Char * cookie1,
const Char * cookie2,
UInt byWhat )
{
if (byWhat != 2)
{
(*AbortFuncBags)("can only sort globals by cookie");
}
if (cookie1 == 0L && cookie2 == 0L)
return 0;
if (cookie1 == 0L)
return -1;
if (cookie2 == 0L)
return 1;
return strcmp(cookie1, cookie2) < 0;
}
void SortGlobals( UInt byWhat )
{
const Char *tmpcookie;
Bag * tmpaddr;
UInt len, h, i, k;
if (byWhat != 2)
{
(*AbortFuncBags)("can only sort globals by cookie");
}
if (GlobalSortingStatus == byWhat)
return;
len = GlobalBags.nr;
h = 1;
while ( 9*h + 4 < len )
{ h = 3*h + 1; }
while ( 0 < h ) {
for ( i = h; i < len; i++ ) {
tmpcookie = GlobalBags.cookie[i];
tmpaddr = GlobalBags.addr[i];
k = i;
while ( h <= k && IsLessGlobal(tmpcookie,
GlobalBags.cookie[k-h],
byWhat))
{
GlobalBags.cookie[k] = GlobalBags.cookie[k-h];
GlobalBags.addr[k] = GlobalBags.addr[k-h];
k -= h;
}
GlobalBags.cookie[k] = tmpcookie;
GlobalBags.addr[k] = tmpaddr;
}
h = h / 3;
}
GlobalSortingStatus = byWhat;
}
Bag * GlobalByCookie(
const Char * cookie )
{
UInt i,top,bottom,middle;
Int res;
if (cookie == 0L)
{
Pr("Panic -- 0L cookie passed to GlobalByCookie\n",0L,0L);
SyExit(2);
}
if (GlobalSortingStatus != 2)
{
for (i = 0; i < GlobalBags.nr; i++)
{
if (strcmp(cookie, GlobalBags.cookie[i]) == 0)
return GlobalBags.addr[i];
}
return (Bag *)0L;
}
else
{
top = GlobalBags.nr;
bottom = 0;
while (top >= bottom) {
middle = (top + bottom)/2;
res = strcmp(cookie,GlobalBags.cookie[middle]);
if (res < 0)
top = middle-1;
else if (res > 0)
bottom = middle+1;
else
return GlobalBags.addr[middle];
}
return (Bag *)0L;
}
}
static Bag NextMptrRestoring;
extern TNumAllocFuncBags AllocFuncBags;
void StartRestoringBags( UInt nBags, UInt maxSize)
{
UInt target;
Bag *newmem;
/*Bag *ptr; */
target = (8*nBags)/7 + (8*maxSize)/7; /* ideal workspace size */
target = (target * sizeof (Bag) + (512L*1024L) - 1)/(512L*1024L)*(512L*1024L)/sizeof (Bag);
/* make sure that the allocated amount of memory is divisible by 512 * 1024 */
if (SizeWorkspace < target)
{
newmem = (*AllocFuncBags)(sizeof(Bag)*(target- SizeWorkspace)/1024, 0);
if (newmem == 0)
{
target = nBags + maxSize; /* absolute requirement */
target = (target * sizeof (Bag) + (512L*1024L) - 1)/(512L*1024L)*(512L*1024L)/sizeof (Bag);
/* make sure that the allocated amount of memory is divisible by 512 * 1024 */
if (SizeWorkspace < target)
(*AllocFuncBags)(sizeof(Bag)*(target- SizeWorkspace)/1024, 1);
}
EndBags = MptrBags + target;
}
OldBags = MptrBags + nBags + (SizeWorkspace - nBags - maxSize)/8;
AllocBags = OldBags;
NextMptrRestoring = (Bag)MptrBags;
SizeAllBags = 0;
NrAllBags = 0;
}
Bag NextBagRestoring( UInt type, UInt flags, UInt size )
{
Bag bag;
UInt i;
BagHeader * header = (BagHeader *)AllocBags;
*(Bag **)NextMptrRestoring = AllocBags = DATA(header);
bag = NextMptrRestoring;
header->type = type;
header->flags = flags;
header->size = size;
header->link = NextMptrRestoring;
NextMptrRestoring++;
#ifdef DEBUG_LOADING
if ((Bag *)NextMptrRestoring >= OldBags)
(*AbortFuncBags)("Overran Masterpointer area");
#endif
for (i = 0; i < WORDS_BAG(size); i++)
*AllocBags++ = (Bag)0;
#ifdef DEBUG_LOADING
if (AllocBags > EndBags)
(*AbortFuncBags)("Overran data area");
#endif
#ifdef COUNT_BAGS
InfoBags[type].nrLive += 1;
InfoBags[type].nrAll += 1;
InfoBags[type].sizeLive += size;
InfoBags[type].sizeAll += size;
#endif
SizeAllBags += size;
NrAllBags ++;
return bag;
}
void FinishedRestoringBags( void )
{
Bag p;
/* Bag *ptr; */
YoungBags = AllocBags;
FreeMptrBags = NextMptrRestoring;
for (p = NextMptrRestoring; p +1 < (Bag)OldBags; p++)
*(Bag *)p = p+1;
*p = 0;
NrLiveBags = NrAllBags;
SizeLiveBags = SizeAllBags;
NrDeadBags = 0;
SizeDeadBags = 0;
NrHalfDeadBags = 0;
ChangedBags = 0;
}
/****************************************************************************
**
*F InitFreeFuncBag(<type>,<free-func>) . . . . . . install freeing function
**
** 'InitFreeFuncBag' is really too simple for an explanation.
*/
TNumFreeFuncBags TabFreeFuncBags [ 256 ];
void InitFreeFuncBag (
UInt type,
TNumFreeFuncBags free_func )
{
TabFreeFuncBags[type] = free_func;
}
/****************************************************************************
**
*F InitCollectFuncBags(<bfr-func>,<aft-func>) . install collection functions
**
** 'InitCollectFuncBags' is really too simple for an explanation.
*/
TNumCollectFuncBags BeforeCollectFuncBags;
TNumCollectFuncBags AfterCollectFuncBags;
void InitCollectFuncBags (
TNumCollectFuncBags before_func,
TNumCollectFuncBags after_func )
{
BeforeCollectFuncBags = before_func;
AfterCollectFuncBags = after_func;
}
/****************************************************************************
**
*F FinishBags() . . . . . . . . . . . . . . . . . . . . . . .finalize GASMAN
**
** `FinishBags()' ends GASMAN and returns all memory to the OS pool
**
*/
void FinishBags( void )
{
(*AllocFuncBags)(-(sizeof(Bag)*SizeWorkspace/1024),2);
}
/****************************************************************************
**
*F InitBags(...) . . . . . . . . . . . . . . . . . . . . . initialize Gasman
**
** 'InitBags' remembers <alloc-func>, <stack-func>, <stack-bottom>,
** <stack-align> and <abort-func> in global variables. It also allocates the
** initial workspace, and sets up the linked list of available masterpointer.
*/
TNumAllocFuncBags AllocFuncBags;
TNumStackFuncBags StackFuncBags;
TNumExtraMarkFuncBags ExtraMarkFuncBags;
Bag * StackBottomBags;
UInt StackAlignBags;
TNumAbortFuncBags AbortFuncBags;
void InitBags (
TNumAllocFuncBags alloc_func,
UInt initial_size,
TNumStackFuncBags stack_func,
Bag * stack_bottom,
UInt stack_align,
TNumAbortFuncBags abort_func )
{
Bag * p; /* loop variable */
UInt i; /* loop variable */
ClearGlobalBags();
WarnInitGlobalBag = 0;
/* install the allocator and the abort function */
AllocFuncBags = alloc_func;
AbortFuncBags = abort_func;
ExtraMarkFuncBags = 0;
/* install the stack marking function and values */
StackFuncBags = stack_func;
StackBottomBags = stack_bottom;
StackAlignBags = stack_align;