1 //////////////////////////////////////////////////////////////////////////////
2 // Testing the ability to trace cross heap pointers.
3 // We'll allocate a list in two different collectors.
4 // Garbage collection should preserve the shape of the list.
5 //////////////////////////////////////////////////////////////////////////////
10 #include <AD/gc/gcobject.h>
11 #include <AD/gc/gcheaps.h>
12 #include <AD/gc/markswp.h>
14 MarkSweepGC heap1
, heap2
;
16 #define TRIALS 5 // number of times to test
17 #define LENGTH 100 // length of list to allocate
19 class LIST
: public GCObject
{
24 LIST(LIST
* n
) : next(n
)
25 { for (int i
= 0; i
< 500; i
++) junk
[i
] = i
+ tag
; }
28 { for (int i
= 0; i
< 500; i
++) assert(junk
[i
] == i
+tag
);
29 if (next
) next
->verify();
31 void trace(GC
* gc
) { next
= (LIST
*)gc
->trace(next
); }
36 for (len
= 0; l
; l
= l
->next
)
44 for (int i
= 0; i
< LENGTH
; i
++) {
45 l
= new (heap1
) LIST(l
);
46 new (heap1
) LIST(l
); // some garbage for heap1
47 new (heap1
) LIST(l
); // some garbage for heap1
48 new (heap1
) LIST(l
); // some garbage for heap1
49 l
= new (heap2
) LIST(l
);
50 new (heap2
) LIST(l
); // some garbage for heap2
51 new (heap2
) LIST(l
); // some garbage for heap2
52 new (heap2
) LIST(l
); // some garbage for heap2
55 assert(length(l
) == LENGTH
* 2);
60 cout
<< "Testing crossheap pointers\n";
61 for (int trial
= 1; trial
<= TRIALS
; trial
++) {
62 cout
<< "Trial " << trial
<< "\n" << flush
;
64 cout
<< "Trial " << trial
<< " has passed\n" << flush
;
66 cout
<< "Now cleaning up\n";