simple.cc - generated code example
[prop.git] / tests / test_gc24.cc
blob2cd83b377003ce2477bc345ff8d456e85ffd4812
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/markswp.h>
14 #include <AD/gc/gcobject.h>
15 #include <AD/gc/gcheaps.h>
17 #define LENGTH (256)
18 #define UNIT (16 * 64)
19 #define HEAP_POINTERS 100
20 #define TRIALS 100
22 MarkSweepGC my_heap;
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 {
29 public:
30 char c; // some position dependent storage
31 int x[20]; // some other position independent junk
32 LIST * next; // the link
34 // The constructor
35 LIST(LIST * n, char c1) : next(n), c(c1)
36 { for (int i = 0; i < 20; i++) x[i] = i; }
38 private:
39 // The tracing method.
40 void trace(GC * gc) { next = (LIST *)gc->trace(next); }
43 //////////////////////////////////////////////////////////////////////////////
44 // Method to print a list, currently unused.
45 //////////////////////////////////////////////////////////////////////////////
46 void print(LIST * x)
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)
54 { int count = 0;
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));
63 // Check its contents
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];
78 // Allocate a list
79 int j;
80 for (j = 0; j < HEAP_POINTERS; j++) {
81 LIST * x = 0;
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');
87 heap[j] = x;
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;
95 verify(heap[j]);
98 GC::get_default_gc().collect();
99 for (j = 0; j < HEAP_POINTERS; j++) {
100 // cout << "[" << j << "]" << flush;
101 verify(heap[j]);
103 GC::get_default_gc().collect();
105 for (j = 0; j < HEAP_POINTERS; j++) {
106 // cout << "[" << j << "]" << flush;
107 verify(heap[j]);
109 for (j = 0; j < HEAP_POINTERS; j++) {
110 heap[j] = 0;
112 cout << "Trial = " << trial << " List is okay\n" << flush;
113 GC::get_default_gc().collect();
114 delete [] heap;
117 int main()
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++)
128 do_some_stuff(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;
135 cout << "\n"
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"
142 << "\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"
146 "may occur.\n"
147 "\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"
152 "\n"
153 "Happy GC!\n";
154 return 0;