initial
[prop.git] / tests / test_gc15.cc
blobcb2e28989b08470634f71e67eaf9bc6e2a147a12
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 // List cells of of random length.
7 //
8 // Note: In your application you'll only have to include <AD/gc/gcobject.h>.
9 // In this case we'll have to import some other implementation dependent
10 // information since we'll have do some poking around with the heap.
11 //////////////////////////////////////////////////////////////////////////////
13 #include <assert.h>
14 #include <unistd.h>
15 #include <stdlib.h>
16 #include <iostream.h>
17 #include <AD/gc/bgc.h>
18 #include <AD/gc/gcobject.h>
19 #include <AD/gc/gcheaps.h>
21 #define LENGTH (256 * 32)
22 #define UNIT (16 * 64)
23 #define JUNK 100
25 //////////////////////////////////////////////////////////////////////////////
26 // The list class defined below is made a garbage collectable class by
27 // deriving from GCObject and adding a tracing method ``trace.''
28 //////////////////////////////////////////////////////////////////////////////
29 class LIST : public GCObject {
30 public:
31 char c; // some position dependent storage
32 LIST * next; // the link
33 int len; // len of junk
34 int x[1]; // some other junk
36 // The constructor
37 LIST(LIST * n, char c1, int l) : next(n), c(c1), len(l)
38 { for (int i = 0; i < l; i++) x[i] = l - i; }
40 // The tracing method.
41 void trace(GC * gc) { next = (LIST*)gc->trace(next); }
44 //////////////////////////////////////////////////////////////////////////////
45 // Method to print a list, currently unused.
46 //////////////////////////////////////////////////////////////////////////////
47 void print(LIST * x)
48 { if (x) { cout << x->c; print(x->next); }
51 //////////////////////////////////////////////////////////////////////////////
52 // This method checks that a list is well and okay.
53 //////////////////////////////////////////////////////////////////////////////
54 void verify (LIST * x)
55 { int count = 0;
56 cout << "Verifying list\n" << flush;
57 for ( ; x; x = x->next, count++) {
59 // Make sure that it lies within the collectable heap.
61 assert (HM::is_mapped(x) && HM::page_gc(x) == bgc.gc_id());
62 assert (HM::get_object_map().is_marked(x));
64 // Check its contents
65 for (int i = 0; i < x->len; i++)
66 assert (x->x[i] == x->len - i);
67 assert (x->c == (LENGTH - count - 1) / UNIT + 'a');
70 // Make sure that it has the right length.
71 assert (count == LENGTH);
72 cout << "List is okay\n" << flush;
76 void do_some_stuff()
78 LIST * x = 0;
80 srand(getpid());
82 // Allocate a list
83 for (int i = 0; i < LENGTH; i++) {
84 int l = rand() % JUNK;
85 x = new (sizeof(LIST) + (l-1) * sizeof(int)) LIST (x,i / UNIT + 'a', l);
86 // The following line generates some garbage.
87 new (sizeof(LIST) + (l-1) * sizeof(int)) LIST (x, i / UNIT + 'a', l);
90 // cout << "&x = " << (void*)&x << '\n';
91 // cout << "&x = " << (void*)&x << " x = " << (void*)x << '\n';
92 cout << "List allocated\n";
93 verify(x);
94 GC::get_default_gc().collect();
96 verify(x);
97 GC::get_default_gc().collect();
99 verify(x);
100 x = 0;
101 GC::get_default_gc().collect();
104 int main()
106 GC::Statistics stats = GC::get_default_gc().statistics();
107 cout << "Algorithm: " << stats.algorithm << '\n'
108 << "Version: " << stats.version << '\n';
110 do_some_stuff();
111 GC::get_default_gc().collect();
113 cout << "\n"
114 "If you don't see a crash by now then it's probably ok :-)\n"
115 "Ideally, the heap should be completely reclaimed at this\n"
116 "point, but since the collector is conservative, retention\n"
117 "may occur.\n"
118 "\n"
119 "Actually this test is bit unfair, since we are allocating\n"
120 "a long linked list, a strongly connected data structure, and if\n"
121 "any one part of the list is incorrectly promoted then a large\n"
122 "portion of the storage will be retained.\n"
123 "\n"
124 "Happy GC!\n";
125 return 0;