1 //////////////////////////////////////////////////////////////////////////////
2 // This program tests the garbage collector classes.
3 // We'll allocate a long linked list in the collectable heap, then invoke
4 // garbage collection a few times.
6 // List cells of of random length.
8 // Note: In your application you'll only have to include <AD/gc/gcobject.h>.
9 // In this case we'll have to import some other implementation dependent
10 // information since we'll have do some poking around with the heap.
11 //////////////////////////////////////////////////////////////////////////////
17 #include <AD/gc/bgc.h>
18 #include <AD/gc/gcobject.h>
19 #include <AD/gc/gcheaps.h>
21 #define LENGTH (256 * 32)
22 #define UNIT (16 * 64)
25 //////////////////////////////////////////////////////////////////////////////
26 // The list class defined below is made a garbage collectable class by
27 // deriving from GCObject and adding a tracing method ``trace.''
28 //////////////////////////////////////////////////////////////////////////////
29 class LIST
: public GCObject
{
31 char c
; // some position dependent storage
32 LIST
* next
; // the link
33 int len
; // len of junk
34 int x
[1]; // some other junk
37 LIST(LIST
* n
, char c1
, int l
) : next(n
), c(c1
), len(l
)
38 { for (int i
= 0; i
< l
; i
++) x
[i
] = l
- i
; }
40 // The tracing method.
41 void trace(GC
* gc
) { next
= (LIST
*)gc
->trace(next
); }
44 //////////////////////////////////////////////////////////////////////////////
45 // Method to print a list, currently unused.
46 //////////////////////////////////////////////////////////////////////////////
48 { if (x
) { cout
<< x
->c
; print(x
->next
); }
51 //////////////////////////////////////////////////////////////////////////////
52 // This method checks that a list is well and okay.
53 //////////////////////////////////////////////////////////////////////////////
54 void verify (LIST
* x
)
56 cout
<< "Verifying list\n" << flush
;
57 for ( ; x
; x
= x
->next
, count
++) {
59 // Make sure that it lies within the collectable heap.
61 assert (HM::is_mapped(x
) && HM::page_gc(x
) == bgc
.gc_id());
62 assert (HM::get_object_map().is_marked(x
));
65 for (int i
= 0; i
< x
->len
; i
++)
66 assert (x
->x
[i
] == x
->len
- i
);
67 assert (x
->c
== (LENGTH
- count
- 1) / UNIT
+ 'a');
70 // Make sure that it has the right length.
71 assert (count
== LENGTH
);
72 cout
<< "List is okay\n" << flush
;
83 for (int i
= 0; i
< LENGTH
; i
++) {
84 int l
= rand() % JUNK
;
85 x
= new (sizeof(LIST
) + (l
-1) * sizeof(int)) LIST (x
,i
/ UNIT
+ 'a', l
);
86 // The following line generates some garbage.
87 new (sizeof(LIST
) + (l
-1) * sizeof(int)) LIST (x
, i
/ UNIT
+ 'a', l
);
90 // cout << "&x = " << (void*)&x << '\n';
91 // cout << "&x = " << (void*)&x << " x = " << (void*)x << '\n';
92 cout
<< "List allocated\n";
94 GC::get_default_gc().collect();
97 GC::get_default_gc().collect();
101 GC::get_default_gc().collect();
106 GC::Statistics stats
= GC::get_default_gc().statistics();
107 cout
<< "Algorithm: " << stats
.algorithm
<< '\n'
108 << "Version: " << stats
.version
<< '\n';
111 GC::get_default_gc().collect();
114 "If you don't see a crash by now then it's probably ok :-)\n"
115 "Ideally, the heap should be completely reclaimed at this\n"
116 "point, but since the collector is conservative, retention\n"
119 "Actually this test is bit unfair, since we are allocating\n"
120 "a long linked list, a strongly connected data structure, and if\n"
121 "any one part of the list is incorrectly promoted then a large\n"
122 "portion of the storage will be retained.\n"