Merge remote branch 'master'
[prop.git] / tests / test_gc1.cc
blob301de1c9ee837ad4664bf8c9d2957b5ec2050732
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/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 {
25 public:
26 char c; // some position dependent storage
27 int x[20]; // some other position independent junk
28 LIST * next; // the link
30 // The constructor
31 LIST(LIST * n, char c1) : next(n), c(c1)
32 { for (int i = 0; i < 20; i++) x[i] = i; }
34 private:
35 // The tracing method.
36 void trace(GC * gc) { next = (LIST *)gc->trace(next); }
39 //////////////////////////////////////////////////////////////////////////////
40 // Method to print a list, currently unused.
41 //////////////////////////////////////////////////////////////////////////////
42 void print(LIST * x)
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)
50 { int count = 0;
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));
59 // Check its contents
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;
71 void do_some_stuff()
73 LIST * x = 0;
75 // Allocate a list
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";
85 verify(x);
86 GC::get_default_gc().collect();
88 verify(x);
89 GC::get_default_gc().collect();
91 verify(x);
92 x = 0;
93 GC::get_default_gc().collect();
96 int main()
98 GC::Statistics stats = GC::get_default_gc().statistics();
99 cout << "Algorithm: " << stats.algorithm << '\n'
100 << "Version: " << stats.version << '\n';
102 do_some_stuff();
103 GC::get_default_gc().collect();
105 stats = GC::get_default_gc().statistics();
106 size_t memory_allocated = sizeof(LIST) * LENGTH * 2;
108 cout << "\n"
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"
115 << "\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"
119 "may occur.\n"
120 "\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"
125 "\n"
126 "Happy GC!\n";
127 return 0;