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 // Note: In your application you'll only have to include <AD/gc/gcobject.h>.
7 // In this case we'll have to import some other implementation dependent
8 // information since we'll have do some poking around with the heap.
9 //////////////////////////////////////////////////////////////////////////////
13 #include <AD/gc/bgc.h>
14 #include <AD/gc/gcobject.h>
15 #include <AD/gc/gcheaps.h>
17 #define LENGTH (256 * 64)
18 #define UNIT (16 * 64)
20 //////////////////////////////////////////////////////////////////////////////
21 // The list class defined below is made a garbage collectable class by
22 // deriving from GCObject and adding a tracing method ``trace.''
23 //////////////////////////////////////////////////////////////////////////////
24 class LIST
: public GCObject
{
26 char c
; // some position dependent storage
27 int x
[20]; // some other position independent junk
28 LIST
* next
; // the link
31 LIST(LIST
* n
, char c1
) : next(n
), c(c1
)
32 { for (int i
= 0; i
< 20; i
++) x
[i
] = i
; }
35 // The tracing method.
36 void trace(GC
* gc
) { next
= (LIST
*)gc
->trace(next
); }
39 //////////////////////////////////////////////////////////////////////////////
40 // Method to print a list, currently unused.
41 //////////////////////////////////////////////////////////////////////////////
43 { if (x
) { cout
<< x
->c
; print(x
->next
); }
46 //////////////////////////////////////////////////////////////////////////////
47 // This method checks that a list is well and okay.
48 //////////////////////////////////////////////////////////////////////////////
49 void verify (LIST
* x
)
51 cout
<< "Verifying list\n" << flush
;
52 for ( ; x
; x
= x
->next
, count
++) {
54 // Make sure that it lies within the collectable heap.
56 assert (HM::is_mapped(x
) && HM::page_gc(x
) == bgc
.gc_id());
57 assert (HM::get_object_map().is_marked(x
));
60 for (int i
= 0; i
< 20; i
++)
61 assert (x
->x
[i
] == i
);
62 assert (x
->c
== (LENGTH
- count
- 1) / UNIT
+ 'a');
65 // Make sure that it has the right length.
66 assert (count
== LENGTH
);
67 cout
<< "List is okay\n" << flush
;
76 for (int i
= 0; i
< LENGTH
; i
++) {
77 x
= new LIST (x
,i
/ UNIT
+ 'a');
78 // The following line generates some garbage.
79 new LIST (x
, i
/ UNIT
+ 'a');
82 // cout << "&x = " << (void*)&x << '\n';
83 // cout << "&x = " << (void*)&x << " x = " << (void*)x << '\n';
84 cout
<< "List allocated\n";
86 GC::get_default_gc().collect();
89 GC::get_default_gc().collect();
93 GC::get_default_gc().collect();
98 GC::Statistics stats
= GC::get_default_gc().statistics();
99 cout
<< "Algorithm: " << stats
.algorithm
<< '\n'
100 << "Version: " << stats
.version
<< '\n';
103 GC::get_default_gc().collect();
105 stats
= GC::get_default_gc().statistics();
106 size_t memory_allocated
= sizeof(LIST
) * LENGTH
* 2;
109 "Memory allocated: " << memory_allocated
<< '\n'
110 << "Final heap size: "
111 << stats
.bytes_managed
+ GCHeapManager::bytes_free() << '\n'
112 << "Bytes retained: " << stats
.bytes_used
<< '\n'
113 << "Retention rate: "
114 << (stats
.bytes_used
* 100 / memory_allocated
) << "%\n"
116 "If you don't see a crash by now then it's probably ok :-)\n"
117 "Ideally, the heap should be completely reclaimed at this\n"
118 "point, but since the collector is conservative, retention\n"
121 "Actually this test is bit unfair, since we are allocating\n"
122 "a long linked list, a strongly connected data structure, and if\n"
123 "any one part of the list is incorrectly promoted then a large\n"
124 "portion of the storage will be retained.\n"