Merge branch 'makefiles'
[prop.git] / tests / test_gc8.cc
blobb2f8a1ab2d588e5ab57d409053d8f6ef6e351ce2
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.
5 //
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 //////////////////////////////////////////////////////////////////////////////
11 #include <assert.h>
12 #include <iostream.h>
13 #include <AD/gc/gcobject.h>
14 #include <AD/gc/gcheaps.h>
15 #include <AD/gc/markswp.h>
17 MarkSweepGC marksweep_gc;
19 #define LENGTH (256 * 64)
20 #define UNIT (16 * 64)
22 //////////////////////////////////////////////////////////////////////////////
23 // The list class defined below is made a garbage collectable class by
24 // deriving from GCObject and adding a tracing method ``trace.''
25 //////////////////////////////////////////////////////////////////////////////
26 class LIST : public GCObject {
27 public:
28 char c; // some position dependent storage
29 int x[20]; // some other position independent junk
30 LIST * next; // the link
32 // The constructor
33 LIST(LIST * n, char c1) : next(n), c(c1)
34 { for (int i = 0; i < 20; i++) x[i] = i; }
36 // The tracing method.
37 void trace(GC * gc) { next = (LIST*)gc->trace(next); }
40 //////////////////////////////////////////////////////////////////////////////
41 // Method to print a list, currently unused.
42 //////////////////////////////////////////////////////////////////////////////
43 void print(LIST * x)
44 { if (x) { cout << x->c; print(x->next); }
47 //////////////////////////////////////////////////////////////////////////////
48 // This method checks that a list is well and okay.
49 //////////////////////////////////////////////////////////////////////////////
50 void verify (LIST * x)
51 { int count = 0;
52 cout << "Verifying list\n" << flush;
53 for ( ; x; x = x->next, count++) {
55 // Make sure that it lies within the collectable heap.
57 assert (HM::is_mapped(x) && HM::page_gc(x) == marksweep_gc.gc_id());
58 //assert (HM::get_object_map().is_marked(x));
59 if (! HM::get_object_map().is_marked(x))
60 { cerr << "count = " << count << ' '
61 << (void*)HM::get_object_map().starting_addr(x) << ' '
62 << (void*)x << '\n';
64 assert (HM::get_object_map().is_marked(x));
66 // Check its contents
67 for (int i = 0; i < 20; i++)
68 assert (x->x[i] == i);
69 assert (x->c == (LENGTH - count - 1) / UNIT + 'a');
72 // Make sure that it has the right length.
73 assert (count == LENGTH);
74 cout << "List is okay\n" << flush;
78 void do_some_stuff()
80 LIST * x = 0;
82 // Allocate a list
83 for (int j = 0; j < 5; j++)
85 for (int i = 0; i < LENGTH; i++) {
86 x = new LIST (x,i / UNIT + 'a');
87 // The following line generates some garbage.
88 new LIST (x, i / UNIT + 'a');
91 // cout << "&x = " << (void*)&x << '\n';
92 // cout << "&x = " << (void*)&x << " x = " << (void*)x << '\n';
93 cout << "List allocated\n";
94 verify(x);
95 GC::get_default_gc().collect();
97 verify(x);
98 GC::get_default_gc().collect();
100 verify(x);
101 x = 0;
102 GC::get_default_gc().collect();
106 int main()
108 GC::set_default_gc(marksweep_gc);
109 GC::Statistics stats = GC::get_default_gc().statistics();
110 cout << "Algorithm: " << stats.algorithm << '\n'
111 << "Version: " << stats.version << '\n';
113 do_some_stuff();
114 GC::get_default_gc().collect();
116 stats = GC::get_default_gc().statistics();
117 size_t memory_allocated = sizeof(LIST) * LENGTH * 2;
119 cout << "\n"
120 "Memory allocated: " << memory_allocated << '\n'
121 << "Final heap size: "
122 << stats.bytes_managed + GCHeapManager::bytes_free() << '\n'
123 << "Bytes retained: " << stats.bytes_used << '\n'
124 << "Retention rate: "
125 << (stats.bytes_used * 100 / memory_allocated) << "%\n"
126 << "\n"
127 "If you don't see a crash by now then it's probably ok :-)\n"
128 "Ideally, the heap should be completely reclaimed at this\n"
129 "point, but since the collector is conservative, retention\n"
130 "may occur.\n"
131 "\n"
132 "Actually this test is bit unfair, since we are allocating\n"
133 "a long linked list, a strongly connected data structure, and if\n"
134 "any one part of the list is incorrectly promoted then a large\n"
135 "portion of the storage will be retained.\n"
136 "\n"
137 "Happy GC!\n";
138 return 0;