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/markswp.h>
14 #include <AD/gc/gcobject.h>
15 #include <AD/gc/gcheaps.h>
18 #define UNIT (16 * 64)
19 #define HEAP_POINTERS 100
24 //////////////////////////////////////////////////////////////////////////////
25 // The list class defined below is made a garbage collectable class by
26 // deriving from GCObject and adding a tracing method ``trace.''
27 //////////////////////////////////////////////////////////////////////////////
28 class LIST
: public GCObject
{
30 char c
; // some position dependent storage
31 int x
[20]; // some other position independent junk
32 LIST
* next
; // the link
35 LIST(LIST
* n
, char c1
) : next(n
), c(c1
)
36 { for (int i
= 0; i
< 20; i
++) x
[i
] = i
; }
39 // The tracing method.
40 void trace(GC
* gc
) { next
= (LIST
*)gc
->trace(next
); }
43 //////////////////////////////////////////////////////////////////////////////
44 // Method to print a list, currently unused.
45 //////////////////////////////////////////////////////////////////////////////
47 { if (x
) { cout
<< x
->c
; print(x
->next
); }
50 //////////////////////////////////////////////////////////////////////////////
51 // This method checks that a list is well and okay.
52 //////////////////////////////////////////////////////////////////////////////
53 void verify (LIST
* x
)
55 // cout << "Verifying list\n" << flush;
56 for ( ; x
; x
= x
->next
, count
++) {
58 // Make sure that it lies within the collectable heap.
60 assert (HM::is_mapped(x
) && HM::page_gc(x
) == my_heap
.gc_id());
61 assert (HM::get_object_map().is_marked(x
));
64 for (int i
= 0; i
< 20; i
++)
65 assert (x
->x
[i
] == i
);
66 assert (x
->c
== (LENGTH
- count
- 1) / UNIT
+ 'a');
69 // Make sure that it has the right length.
70 assert (count
== LENGTH
);
74 void do_some_stuff(int trial
)
76 LIST
** heap
= new LIST
* [HEAP_POINTERS
];
80 for (j
= 0; j
< HEAP_POINTERS
; j
++) {
82 for (int i
= 0; i
< LENGTH
; i
++) {
83 x
= new LIST (x
,i
/ UNIT
+ 'a');
84 // The following line generates some garbage.
85 new LIST (x
, i
/ UNIT
+ 'a');
90 cout
<< "List allocated\n";
91 // cout << "Address of heap[] = " << (void*)heap << '\n';
92 // GCHeapManager::print_report(cout);
93 for (j
= 0; j
< HEAP_POINTERS
; j
++) {
94 // cout << "[" << j << "]" << flush;
98 GC::get_default_gc().collect();
99 for (j
= 0; j
< HEAP_POINTERS
; j
++) {
100 // cout << "[" << j << "]" << flush;
103 GC::get_default_gc().collect();
105 for (j
= 0; j
< HEAP_POINTERS
; j
++) {
106 // cout << "[" << j << "]" << flush;
109 for (j
= 0; j
< HEAP_POINTERS
; j
++) {
112 cout
<< "Trial = " << trial
<< " List is okay\n" << flush
;
113 GC::get_default_gc().collect();
119 // GC::get_default_gc().set_verbosity(
120 // GC::gc_print_debugging_info | GC::get_default_gc().verbosity());
121 GC::set_default_gc(my_heap
);
122 GC::get_default_gc().set_initial_heap_size(1024*1024);
123 GC::Statistics stats
= GC::get_default_gc().statistics();
124 cout
<< "Algorithm: " << stats
.algorithm
<< '\n'
125 << "Version: " << stats
.version
<< '\n';
127 for (int i
= 0; i
< TRIALS
; i
++)
130 GC::get_default_gc().collect();
132 stats
= GC::get_default_gc().statistics();
133 size_t memory_allocated
= sizeof(LIST
) * LENGTH
* 2 * HEAP_POINTERS
;
136 "Memory allocated: " << memory_allocated
<< '\n'
137 << "Final heap size: "
138 << stats
.bytes_managed
+ GCHeapManager::bytes_free() << '\n'
139 << "Bytes retained: " << stats
.bytes_used
<< '\n'
140 << "Retention rate: "
141 << (stats
.bytes_used
* 100 / memory_allocated
) << "%\n"
143 "If you don't see a crash by now then it's probably ok :-)\n"
144 "Ideally, the heap should be completely reclaimed at this\n"
145 "point, but since the collector is conservative, retention\n"
148 "Actually this test is bit unfair, since we are allocating\n"
149 "a long linked list, a strongly connected data structure, and if\n"
150 "any one part of the list is incorrectly promoted then a large\n"
151 "portion of the storage will be retained.\n"