-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgc.c
2000 lines (1835 loc) · 51.4 KB
/
gc.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
/*
* The Emerald garbage collector - a generational copying collector
*
* The new generation is divided into two semispaces, but the boundary
* between them is flexible. Assume the new space is of size N, normally
* divided into two N/2 byte semispaces. We set the boundary based on the
* number of objects that survived the previous garbage collection. Suppose
* that S bytes of objects survived. Then we know that next time those
* objects will be promoted into the old generation (if they are still
* alive), so we can safely allocate half of the remaining space (N-S)/2
* since at the next collection all of the S bytes of objects that are now
* of age two will be promoted and the age 1 survivors of the collection are
* guaranteed to fit into the free (N-S)/2 bytes of free space.
*
* The old generation is a single space copying collector, because we can't
* afford to waste the space of having a semispace completely empty almost
* all of the time. However, we want to compact all the live objects into
* the front of the old generation so that we don't have to deal with a free
* list for allocation. However, the concrete type objects that describe
* the format of all the other objects are stored in the old generation, so
* no live object can be overwritten until it has been safely copied to a
* new location and all references to it have been update to the new
* location. The first time that this invariant is true is at the end of
* the collection. Therefore, we collect in the following manner:
*
* 1. Mark all reachable objects. At the same time, keep a histogram of
* all of the sizes of reachable objects (and their total size).
* 2. Find the line (called the boundary) between live objects and dead
* objects after the collection. This process is described in detail in
* the header comment to the function findBoundary.
* 3. Copy each live object after the boundary into the hole that was found
* for it before the boundary.
* 4. Fixup all references to moved objects.
*
* Note that the presence of a number of data structures in the run time
* that contain pointers to objects seriously complicates the problem.
* Every such reference must be considered a root of the garbage
* collection. In addition, the presence of the object table
* (ObjectTable) makes life even worse because
* every object that has ever been allocated an OID is referenced from these
* tables, and therefore can't be collected. We currently handle the object
* table specially during old generation collection, not marking from the
* pointers that it holds to replicated or stub objects so that they can be
* collected locally since the lack of a local reference to these objects
* means that they are not useful.
*
* This collector makes use of a number of functions defined in misc.c, most
* notably the sizeOf collection and doToExternalRoots.
*
* The distributed collector is in distgc.c, and contains yet another copy
* of a function very much like the three traversal functions that are in
* this file. Any time one of these functions is modified, care should be
* taken that the other similar functions are also modified.
*/
#include "system.h"
#include "types.h"
#include "builtins.h"
#include "globals.h"
#include "gc.h"
#include "assert.h"
#include "trace.h"
#include "iixsc.h"
#include "iisc.h"
#include "squeue.h"
#include "vm_i.h"
#include "move.h"
#include "misc.h"
/*
* This garbage collector implements both a copying collector for the newest
* generation as well as a copying, compacting collector in the old
* generation. The biggest thing left to do is to allow the size of the old
* generation to grow as required. The right way to do this is to create an
* array of old_start and old_end pointers, and allow the old generation to
* be broken into multiple segments. This would require the following
* changes:
* . nextGen would need to move to the next segment when the segment
* boundary was hit
* . Whenever we go through the entire old generation we would have to
* do it to each segment.
* . We would have to also keep track of the end of each segment, to
* account for the internal fragmentation at the end, so we don't
* expect another object in the little hole that is left.
* Alternatively we could put a little object in the hole, like we
* already do in the old generation hole filler. Check out the
* function recordSize.
*/
Object *rem_set;
int remNum;
static word *toSpace, *new_start, *fromSpace, *new_end, *new_lb, *new_ub;
static word *nextGen, *old_start, *old_end;
static int allocatingForward = 1;
int old_size;
static int guaranteeInterGcInterval;
static Object boundary;
int p_old_size = 2 * 1024 * 1024,
p_guaranteeInterGcInterval = 0,
p_bytesPerGeneration = 128 * 1024,
p_copyCount = 2;
static int remember_msize;
int stack_top;
static int wordsLeftInThisGeneration, wordsToBePromotedNextGC;
static int wordsPerGeneration;
int bytesPerGeneration, copyCount;
int inhibit_gc;
int move_stack_size = 0;
static int total_gc_time, old_gc_time;
static int nGCs, nOGCs;
int nDGCs, nDGCremoved;
static word *survivors_start, *survivors_end;
static inline int ageOf(Object o)
{
return (survivors_start <= (word *)o && (word *)o < survivors_end) + 1;
}
static inline int moved(Object o)
{
return o->flags & 1;
}
static inline Object forwardingPtr(Object o)
{
return (Object)(o->flags & ~1);
}
static inline void forward(Object o, Object new)
{
OID oid;
TRACE(memory, 5, ("Forwarding %#x to %#x", o, new));
new->flags = o->flags;
o->flags = (unsigned int)new | 1;
oid = OIDOf(o);
if ((HASOID(new->flags) && 1) != !isNoOID(oid)) {
TRACE(memory, 0, ("Object %#x has oid %s, HASOID == %s", o, OIDString(oid),
HASOID(new->flags) ? "true" : "false"));
if (isNoOID(oid)) {
CLEARHASOID(new->flags);
} else {
SETHASOID(new->flags);
}
}
if (HASOID(new->flags)) UpdateObjectLocation(o, new);
}
extern int sizeOf (Object o), sizeOfX(Object o, Object new, ConcreteType ct);
static inline int in_old (Object p)
{
return (word *)p >= old_start && (word *)p < old_end;
}
/*
* The definition of this function is critical to the correct operation of
* the garbage collector, because we have to be able to tell the difference
* between objects in the from and to half of new space when copying.
*
* In normal operation (during allocation) this function is set to say that
* anything in the new region is considered new, but during copying, we set
* it up so that only things in the fromSpace are considered new, as objects
* that have already been copied to toSpace can (indeed, must) be ignored.
*
* Check out the setting of new_lb and new_ub in swap and in synchBounds.
*/
static inline int in_new (Object p)
{
return ((word *)p >= new_start) && ((word *)p < new_end);
}
static inline int in_from (Object p)
{
return ((word *)p >= new_lb) && ((word *)p < new_ub);
}
Object get_new_addr_in_new (int psize)
{
word *new_addr;
if (allocatingForward) {
toSpace -= psize;
new_addr = toSpace;
assert(toSpace >= fromSpace);
} else {
new_addr = toSpace;
toSpace += psize;
assert(toSpace <= fromSpace);
}
return (Object) new_addr;
}
Object get_new_addr_in_old (int psize)
{
word *new_addr;
new_addr = nextGen;
nextGen += psize;
#ifdef GCPARANOID
if (nextGen > old_end) {
TRACE(memory, 0, ("nextGen overflow botch"));
abort();
}
#endif
return (Object) new_addr;
}
void gc_stats (int *tg, int *og, int *n, int *no, int *nd, int *ndr)
{
*tg = total_gc_time;
*og = old_gc_time;
*n = nGCs;
*no = nOGCs;
*nd = nDGCs;
*ndr = nDGCremoved;
}
#ifndef NDEBUG
static void checkOut(word *from, word *to)
{
Object o;
for (o = (Object) from;
(word *)o < to;
o = (Object) ((word *)o + sizeOf(o))) {
ConcreteType ct = CODEPTR(o->flags);
TRACE(memory, 9, ("0x%08x is a %s%.*s with size %d", (int)o,
!RESDNT(o->flags) ? "Stub " : "",
ct->d.name->d.items, (char *)ct->d.name->d.data, sizeOf(o)));
}
}
#else
# define checkOut(a, b)
#endif
static inline int liveWords(void)
{
return allocatingForward ? fromSpace - new_start : new_end - fromSpace;
}
static inline void nilOut(word *from, word *to)
{
#ifdef GCPARANOID
while (from < to) *from++ = (word)JNIL;
#endif
}
static void swap (void)
{
fromSpace = toSpace;
new_lb = new_start;
new_ub = new_end;
if (allocatingForward) {
toSpace = new_start;
nilOut(toSpace, fromSpace);
survivors_start = fromSpace;
survivors_end = new_end;
} else {
toSpace = new_end;
nilOut(fromSpace, toSpace);
survivors_start = new_start;
survivors_end = fromSpace;
}
IFTRACE(memory, 9) {
if (allocatingForward) {
checkOut(fromSpace, new_end);
} else {
checkOut(new_start, fromSpace);
}
}
allocatingForward = !allocatingForward;
}
static void synchBounds (void)
{
if (allocatingForward) {
new_lb = new_start;
new_ub = fromSpace;
} else {
new_lb = fromSpace;
new_ub = new_end;
}
IFTRACE(memory, 9) checkOut(new_lb, new_ub);
}
void push_ms (Object p)
{
if (stack_top >= move_stack_size) {
int new_move_stack_size;
Object *new_move_stack;
if (move_stack_size == 0) {
new_move_stack_size = INITIAL_MOVE_STACK_SIZE;
new_move_stack = (Object *)vmMalloc(new_move_stack_size * sizeof(Object));
} else {
new_move_stack_size = move_stack_size + INITIAL_MOVE_STACK_SIZE;
new_move_stack = (Object *)vmRealloc((char *)move_stack, new_move_stack_size * sizeof(Object));
}
TRACE(memory, 3, ("Growing move_stack from %d to %d words",
move_stack_size, new_move_stack_size));
if (new_move_stack == NULL) {
TRACE(memory, 0, ("Move stack overflow!"));
exit(1);
} else {
move_stack_size = new_move_stack_size;
move_stack = new_move_stack;
}
}
move_stack[stack_top++] = p;
}
/* Records objects that may have pointers pointing to objects in new */
/* generation */
void new_rem_set (Object elem)
{
if (!REMSET(elem->flags)) {
if (remNum >= remember_msize) {
remember_msize += REM_INCR;
rem_set = rem_set ?
(Object *) vmRealloc (rem_set, remember_msize * sizeof (word)) :
(Object *) vmMalloc (remember_msize * sizeof (word));
assert(rem_set);
}
rem_set[remNum++] = elem;
SETREMSET(elem->flags);
}
}
Object checkAndFindNew_new(Object p)
{
if (ISNIL(p)) return p;
if (in_from(p)) {
if (moved(p)) {
TRACE(memory, 8, ("The %.*s at 0x%08x has moved to 0x%08x",
CODEPTR(forwardingPtr(p)->flags)->d.name->d.items,
CODEPTR(forwardingPtr(p)->flags)->d.name->d.data,
p, forwardingPtr(p)));
return forwardingPtr(p);
} else {
int promotion = ageOf(p) >= copyCount;
int size = sizeOf(p);
Object newplace = promotion ?
(Object) get_new_addr_in_old(size):
(Object) get_new_addr_in_new(size);
/*
* Book-keeping - count the number of words that are guaranteed to be
* promoted next time.
*/
if (ageOf(p) == copyCount - 1) {
wordsToBePromotedNextGC += size;
}
TRACE(memory, 8, ("New Forwarding 0x%08x to 0x%08x, a %.*s", p, newplace,
CODEPTR(p->flags)->d.name->d.items,
CODEPTR(p->flags)->d.name->d.data));
forward(p, newplace);
push_ms(p);
return newplace;
}
} else if (wasGCMalloced(p)) {
TRACE(memory, 10, ("The %.*s at 0x%08x is old",
CODEPTR(p->flags)->d.name->d.items,
CODEPTR(p->flags)->d.name->d.data,
p));
return p;
} else {
TRACE(memory, 10, ("The thing at 0x%08x was not gcmalloced"));
return p;
}
}
int varContainsPointer(ConcreteType ct)
{
return !ISNIL(ct) && HASODP(ct->d.instanceFlags);
}
/**************************************************************************
* move_fields copies p into another address pointed by new_addr
* This assumes:
* new_p has had its age and flags field set (which means that we can
* use it to find the concrete type).
* p can be either the old object, now a forwarding pointer, or the
* real object
*
* move_fields returns true (1) if the object contains any objects that get
* moved and are still in the new generation.
*************************************************************************/
static int misdigit(int c)
{
return ('0' <= c && c <= '9');
}
int move_fields (Object p, Object new_p, Object (*checkAndFindNew)(Object))
{
word *p_ptr, *n_ptr;
Template temp;
char *brands;
char c;
int count;
int star;
ConcreteType ct, nct;
Object old, new;
int answer = 0;
p_ptr = (word *)p + 1;
n_ptr = (word *)new_p + 1;
ct = CODEPTR(new_p->flags);
nct = (ConcreteType) checkAndFindNew((Object)ct);
if (nct != ct && in_new((Object)nct)) answer = 1;
SETCODEPTR(new_p->flags, nct);
if (!RESDNT(new_p->flags)) {
/*
* This is a stub, so all we need to do is copy the pointer to the remote
* location (which is not garbage collectable). A stub has
* a firstThing and a pointer in the next field, and must be maintained.
*/
TRACE(memory, 5, ("Moving the fields of %#x, a stub for a %.*s to %#x",
p, ct->d.name->d.items, ct->d.name->d.data, new_p));
((Object *)new_p)[1] = ((Object *)p)[1];
/*
* If the object is a vector, and it is not zero length, then we have to
* copy its size, too.
*/
if (ct->d.instanceSize < 0 && !VZEROL(p->flags)) {
((Object *)new_p)[2] = ((Object *)p)[2];
}
return answer;
}
temp = ct->d.template;
TRACE(memory, 7, ("Moving the fields of %#x, a %.*s to %#x",
p, ct->d.name->d.items, ct->d.name->d.data, new_p));
/* nothing following the first thing of object p */
if (ISNIL(temp)) return answer;
brands = (char *) temp->d.data;
while (1) {
switch (*brands++) {
case '%':
count = 0;
star = 0;
while (misdigit (c = *brands++)) {
count = count * 10 + c - '0';
}
if (!count) count = 1;
if (c == '*') {
*n_ptr++ = count = *p_ptr++;
star = 1;
c = *brands++;
}
switch (c) {
case 'm':
while (count--) {
*n_ptr++ = *p_ptr++;
*n_ptr++ = *p_ptr++;
}
break;
case 'x':
case 'X':
while (count--) {
old = (Object) *p_ptr++;
new = checkAndFindNew(old);
*n_ptr++ = (word)new;
if (old != new && in_new(new)) answer = 1;
}
break;
case 'v':
case 'V':
while (count--) {
#ifdef USEABCONS
AbCon abcon = (AbCon)p_ptr[1];
ct = ISNIL(abcon) ? (ConcreteType)JNIL : abcon->d.con;
#else
ct = (ConcreteType) p_ptr[1];
#endif
if (varContainsPointer(ct)) {
old = (Object) *p_ptr++;
new = checkAndFindNew(old);
assert(ISNIL(new) || CODEPTR(new->flags) == ct ||
(moved((Object)ct) && CODEPTR(new->flags) == (ConcreteType)forwardingPtr((Object)ct)));
*n_ptr++ = (word)new;
if (old != new && in_new(new)) answer = 1;
} else {
*n_ptr++ = *p_ptr++;
}
#ifdef USEABCONS
*n_ptr++ = (word)abcon;
#else
nct = (ConcreteType)checkAndFindNew((Object)ct);
*n_ptr++ = (word)nct;
if (ct != nct && in_new((Object)nct)) answer = 1;
#endif
p_ptr++;
}
break;
case 'l':
case 'L':
count /= 4;
TRACE(memory, 5, ("Literals at %#x, now %#x", p, new_p));
while (count --) {
*n_ptr++ = *p_ptr++;
*n_ptr++ = *p_ptr++;
*n_ptr++ = *p_ptr++;
old = (Object) *p_ptr++;
new = checkAndFindNew(old);
*n_ptr++ = (word)new;
TRACE(memory, 6, ("Literal: %#x -> %#x", old, new));
if (old != new && in_new(new)) answer = 1;
}
break;
case 'q':
TRACE(memory, 5, ("An squeue in a condition at %#x, now %#x", p, new_p));
assert(count == 1);
*n_ptr++ = *p_ptr++;
break;
case 'd':
case 'D':
case 'f':
case 'F':
while (count--) {
*n_ptr++ = *p_ptr++;
}
break;
case 'c':
case 'C':
case 'b':
case 'B':
if (star) count = (count + 3) / 4;
while (count--) {
*n_ptr++ = *p_ptr++;
}
break;
default:
TRACE(memory, 0, ("cannot figure out brand"));
break;
}
break;
case '\0':
return answer;
default:
TRACE(memory, 0, ("what is '%c' doing in a template?\n", brands[-1]));
break;
}
}
return answer;
}
void flushStack(void)
{
Object p, new_p;
while (stack_top != 0) {
p = move_stack[--stack_top];
assert(moved(p));
new_p = forwardingPtr(p);
if (move_fields (p, new_p, checkAndFindNew_new) && ageOf(p) >= copyCount) {
new_rem_set(new_p);
}
}
}
void move_variable(Object *p_ptr, ConcreteType *ct_ptr)
{
ConcreteType ct;
#ifdef USEABCONS
{
AbCon abcon = (AbCon)*ct_ptr;
ct = ISNIL(abcon) ? (ConcreteType) JNIL : abcon->d.con;
}
#else
ct = *ct_ptr;
#endif
if (varContainsPointer(ct)) {
*p_ptr = checkAndFindNew_new(*p_ptr);
}
assert (!in_new ((Object)ct));
flushStack();
}
/* move_variables moves the variables between p and (p + count) */
void move_variables (int count, word *p_ptr)
{
ConcreteType ct;
while (count--) {
#ifdef USEABCONS
{
AbCon abcon = (AbCon)p_ptr[1];
ct = ISNIL(abcon) ? (ConcreteType) JNIL : abcon->d.con;
}
#else
ct = (ConcreteType) p_ptr[1];
#endif
if (varContainsPointer(ct)) {
*p_ptr = (word)checkAndFindNew_new((Object) *p_ptr);
}
assert (!in_new ((Object)ct));
p_ptr += 2;
}
flushStack();
}
/* move_pointers moves the pointers between p and (p + count) */
void move_pointers (int count, Object *p_ptr)
{
while (count-- > 0) {
*p_ptr = checkAndFindNew_new(*p_ptr);
}
flushStack();
}
/*
* Move all the objects in the object p.
*
* This function returns (Object)-1 if the object p contains no objects
* which reside in the new generation, and p otherwise.
*/
Object tl_move (Object p, Object (*checkAndFindNew)(Object))
{
word *p_ptr;
Template temp;
char *brands, c;
int count, star;
ConcreteType ct, nct;
Object old, new, answer = (Object) -1;
p_ptr = (word *)p + 1;
ct = CODEPTR(p->flags);
nct = (ConcreteType) checkAndFindNew((Object)ct);
temp = ct->d.template;
if (nct != ct) SETCODEPTR(p->flags, nct);
TRACE(memory, 7, ("Top level move of %#x, a %.*s",
p, ct->d.name->d.items, ct->d.name->d.data));
if (!RESDNT(p->flags)) {
/*
* This is a stub. A stub has a firstThing and a pointer in the
* next field, and must be maintained.
*/
/* Do nothing, as the pointer in field 1 cannot be to the heap */
TRACE(memory, 8, ("It is a stub"));
goto done;
}
/* nothing following the first thing of object p */
if (ISNIL(temp)) goto done;
brands = (char *) temp->d.data;
while (1) {
switch (*brands++) {
case '%':
count = 0;
star = 0;
while (misdigit (c = *brands++)) {
count = count * 10 + c - '0';
}
if (!count) count = 1;
if (c == '*') {
count = *p_ptr++;
star = 1;
c = *brands++;
}
switch (c) {
case 'm':
p_ptr += count * 2;
break;
case 'x':
case 'X':
while (count--) {
old = (Object) *p_ptr;
new = checkAndFindNew(old);
if (old != new) {
answer = p;
*p_ptr = (word)new;
}
p_ptr++;
}
break;
case 'v':
case 'V':
while (count--) {
#ifdef USEABCONS
AbCon abcon = (AbCon)p_ptr[1];
ct = ISNIL(abcon) ? (ConcreteType)JNIL : abcon->d.con;
#else
ct = (ConcreteType) p_ptr[1];
#endif
if (varContainsPointer(ct)) {
old = (Object) *p_ptr;
new = checkAndFindNew(old);
assert(ISNIL(new) || CODEPTR(new->flags) == ct ||
(moved((Object)ct) && CODEPTR(new->flags) == (ConcreteType)forwardingPtr((Object)ct)));
if (old != new) {
answer = p;
*p_ptr = (word)new;
}
}
p_ptr++;
#ifdef USEABCONS
p_ptr++;
#else
nct = (ConcreteType)checkAndFindNew((Object) ct);
*p_ptr++ = (word)nct;
#endif
}
break;
case 'l':
case 'L':
count /= 4;
TRACE(memory, 5, ("Literals at %#x", p));
while (count--) {
p_ptr += 3;
old = (Object) *p_ptr;
new = checkAndFindNew(old);
if (old != new) {
answer = p;
*p_ptr = (word)new;
}
TRACE(memory, 6, ("Literal: %#x -> %#x", old, new));
p_ptr++;
}
break;
case 'q':
assert(count == 1);
{
Object st;
TRACE(memory, 5, ("Updating squeue of condition at %#x", p));
assert(count == 1);
SQueueForEach((SQueue)*p_ptr, st) {
old = st;
new = checkAndFindNew(old);
if (old != new) {
((SQueue)*p_ptr)->table[SQueuexx_index] = (struct State *)new;
answer = p;
}
} SQueueNext();
p_ptr ++;
}
break;
case 'd':
case 'D':
case 'f':
case 'F':
p_ptr += count;
break;
case 'c':
case 'C':
case 'b':
case 'B':
if (star) count = (count + 3) / 4;
p_ptr += count;
break;
default:
TRACE(memory, 0, ("cannot figure out brand"));
break;
}
break;
case '\0':
goto done;
default:
TRACE(memory, 0, ("what is '%c' doing in a template?", brands[-1]));
break;
}
}
done:
flushStack();
return answer;
}
void moveFromRoots1 (void)
{
int limit = remNum, i;
Object o, new_o;
TRACE(memory, 5, ("Checking the remembered set"));
for (i = 0; i < limit; i++) {
o = rem_set[i];
TRACE(memory, 7, ("Doing the %.*s at 0x%08x",
CODEPTR(o->flags)->d.name->d.items,
CODEPTR(o->flags)->d.name->d.data,
o));
new_o = tl_move (o, checkAndFindNew_new);
if (new_o == (Object)-1) {
CLEARREMSET((o)->flags);
rem_set[i] = new_o;
} else {
assert(new_o == o);
}
}
}
static void (*_gc_b) (void), (*_gc_d) (void), (*_gc_a) (void), (*_gc_e) (void);
extern void doToExternalRoots(void (*pointers_f)(int, Object *),
void (*variable_f)(Object *, ConcreteType *),
void (*varibles_f)(int, word *),
int destructive,
int doFromObjectTable);
void ch_rem_set (void)
{
int i, j = 0;
for (i = 0; i < remNum; i++) {
if (rem_set[i] != (Object) -1) {
rem_set[j++] = rem_set[i];
}
}
remNum = j;
}
static int total_stores, total_interesting_stores;
extern void gcollect_old(void);
static int needDistGC(void)
{
/*
* Think about starting a distGC if the fraction of free space is small
* (how small?) and the number of objects with OIDs is large (how
* large?). The current settings of these two parameters is entirely a
* guess.
*/
#define DISTGCPERCENTTHRESHOLD 25
#define DISTGCGLOBALOBJECTTHRESHOLD 10000
if (!inDistGC()) {
int freepercent = (old_end - nextGen) * 100 / (old_end - old_start);
if (freepercent < DISTGCPERCENTTHRESHOLD) {
TRACE(distgc, 2, ("Starting distgc, free memory = %d", freepercent));
return 1;
} else if (OTableSize(ObjectTable) > DISTGCGLOBALOBJECTTHRESHOLD) {
TRACE(distgc, 2, ("Starting distgc, objects with oids = %d",
OTableSize(ObjectTable)));
return 1;
}
}
return 0;
}
void gcollect(void)
{
int start_time;
int done_time;
assert(!inhibit_gc);
nGCs++;
TRACE(memory, 2, ("Starting garbage collection #%d, remsetsize = %d",
nGCs, remNum));
IFTRACE(memory, 7) {
showAllProcesses(0, 1);
}
start_time = currentCpuTime();
wordsToBePromotedNextGC = 0;
synchBounds();
if (_gc_b) _gc_b();
/* Move objects in the remember set which records the objects that may
have pointers to objects in new generation - note that the
pointers in this set are all in the old generation */
moveFromRoots1();
ch_rem_set();
doToExternalRoots(move_pointers, move_variable, move_variables, 1, 1);
swap();
wordsLeftInThisGeneration =
guaranteeInterGcInterval ?
bytesPerGeneration / sizeof(word) :
(new_end - new_start + wordsToBePromotedNextGC) / (copyCount == 1?1:2)
- liveWords();
if (copyCount == 1) {
assert(wordsToBePromotedNextGC == 0);
assert(liveWords() == 0);
assert(wordsLeftInThisGeneration == new_end - new_start);
wordsToBePromotedNextGC = wordsLeftInThisGeneration;
}
if (old_end - nextGen < wordsToBePromotedNextGC) {
gcollect_old();
}
done_time = currentCpuTime();
total_gc_time += (done_time - start_time);
TRACE(memory, 3, ("Old = %d words, new = %d words, promo = %d words",
((unsigned int)nextGen - (unsigned int)old_start) / sizeof(int),
(allocatingForward ?
(unsigned int)fromSpace - (unsigned int)new_start :
(unsigned int)new_end - (unsigned int)fromSpace) / sizeof(int),
wordsToBePromotedNextGC));
TRACE(memory, 4, ("Total stores so far = %d, %d interesting",
total_stores, total_interesting_stores));
if (old_end - nextGen < wordsToBePromotedNextGC) {
TRACE(memory, 0, ("old_end = %#x, next_gen = %#x, diff = %d, promo = %d\n",
old_end, nextGen, old_end - nextGen,
wordsToBePromotedNextGC));
fprintf(stderr, "Out of memory. Try including the flag -O%dk on the command line!\n", old_size/1024 + 256);
abort();
}
fflush(stdout);
}
void stoCheck (Object intoObj, Object storedObj)
{
total_stores++;
if ((in_old (intoObj)) && (in_new (storedObj))) {
total_interesting_stores ++;
new_rem_set (intoObj);
}
}
void anticipateGC(int bytes)
{
if (!inhibit_gc) {
if (old_end - nextGen - wordsToBePromotedNextGC < BYTES_TO_WORDS(bytes))
gcollect();
/*
* Since the call to gcollect can sometimes call gcollect_old, then we
* only need to call it again if it is really necessary.
*/
if (old_end - nextGen - wordsToBePromotedNextGC < BYTES_TO_WORDS(bytes))
gcollect_old();
}
inhibit_gc++;
}
void ensureSpace(int bytes)
{
assert(!inhibit_gc);
while (wordsLeftInThisGeneration * 4 < bytes) {
gcollect();
}
if (old_end - nextGen - wordsToBePromotedNextGC < BYTES_TO_WORDS(bytes))
gcollect();
/*
* Since the call to gcollect can sometimes call gcollect_old, then we
* only need to call it again if it is really necessary.
*/
if (old_end - nextGen - wordsToBePromotedNextGC < BYTES_TO_WORDS(bytes))
gcollect_old();
}
/* allocate lb bytes of data in the old generation */
void *gc_malloc_old (int lb, int remember)
{
register word *op, *lp;
register int lw;
lw = BYTES_TO_WORDS (lb + sizeof(word) - 1);
if (old_end - nextGen - wordsToBePromotedNextGC < lw) {
if (inhibit_gc) {
TRACE(memory, 0, ("old_end = %#x, next_gen = %#x, diff = %d, promo = %d\n",
old_end, nextGen, old_end - nextGen,
wordsToBePromotedNextGC));
fprintf(stderr, "Out of memory. Try including the flag -O%dk on the command line!\n", old_size/1024 + 256);
abort();
} else {
gcollect();
gcollect_old();
if (old_end - nextGen -wordsToBePromotedNextGC < lw) {
TRACE(memory, 0, ("old_end = %#x, next_gen = %#x, diff = %d, promo = %d\n",
old_end, nextGen, old_end - nextGen,
wordsToBePromotedNextGC));
fprintf(stderr, "Out of memory. Try including the flag -O%dk on the command line!\n", old_size/1024 + 256);
abort();
}
}
}
op = nextGen;
nextGen += lw;
lp = op + lw;
while (--lp > op) *lp = JNIL;
*op = 0;
TRACE(memory, 10, ("Allocate old %d bytes returns %x", lb, op));
if (remember) new_rem_set((Object)op);
return op;
}
/* allocate lb bytes of data in the new generation */
void *gc_malloc(int lb)
{
register word *op, *lp;
register int lw;
lw = BYTES_TO_WORDS (lb + (sizeof (word)) - 1);
if (inhibit_gc || lw > wordsPerGeneration) return gc_malloc_old (lb, 0);