Rune - Fix array passing, optimize constant array indices
[rune.git] / tests / heap2.d
1 #!/usr/local/bin/rune -x
2 #
3 #       Test heap storage.  Rune does not garbage collect loops... that is
4 #       considered a run time error (which we will eventually detect).  Rune
5 #       will track references and delete heap objects recursively.  It is
6 #       recommended that you not rely on this for long singly-linked lists
7 #       because you may run out of stack.  Intead you should NULL the next
8 #       pointer out manually.
9 #
10 #       This example defaults to recursively cleaning up after your list.
11 #       If you change the 'i' loop to go to, say, 100000, and the (i == 30)
12 #       to be (i == 80000), you may see the stack overflow.
13
14 import "sys";
15 import <stdio>;
16
17 alias stdio.File *stdout = stdio.stdout;
18
19 class Test {
20         Test *next;
21         int index;
22 }
23
24 int
25 main(int ac, char **av)
26 {
27         Test *item;
28         int i;
29
30         for (i = 0; i < 1000000; ++i) {
31                 item.new();
32                 item->index = i;
33                 fubar(item);
34         }
35 }
36
37 void
38 fubar(Test *item)
39 {
40         global Test *base;
41         global Test **last = &base;
42         global int count = 0;
43
44         item = bleh(item);
45         *last = bleh(item);
46         last = &item->next;
47         if (item->index > 50000) {
48                 base = base->next;
49         }
50         if (item->index % 10000 == 0)
51                 stdout->show(item->index, (int)(item - base));
52 }
53
54 Test *
55 bleh(Test *next)
56 {
57         return(next);
58 }