Debian Codegraph @

benchmarks.larceny/src/divrec.c

raw
1 #include <stdio.h>
2
3 /*---------------------------------------------------------------------------*/
4
5 /* Support for Scheme like stuff */
6 /* Assumes sizeof(int) == sizeof(int *). */
7
8 #ifdef FAST_ALLOCATOR
9 #define CUSTOM_ALLOC 1 /* Set to 1 for custom allocation */
10 #else
11 #define CUSTOM_ALLOC 0
12 #endif
13
14 int *alloc;
15
16 #define CAR(pair) *(pair)
17 #define CDR(pair) ((int *) *((pair)+1))
18
19 /* This custom allocator is moby fragile. */
20
21 #if CUSTOM_ALLOC
22
23 #define CONS(car,cdr) ( *(--alloc) = (int) cdr, *(--alloc) = car, alloc )
24
25 #define RECLAIM(x) if (x == alloc) alloc = alloc + 2
26
27 #define HEAP_SIZE 1000000
28
29 void init()
30 {
31 /* char *heap = (char *) malloc( HEAP_SIZE ); */
32 int heap_size_in_bytes = HEAP_SIZE * sizeof(int);
33 int * heap = (int *) malloc( heap_size_in_bytes );
34
35 if (heap == NULL)
36 {
37 printf( "Not enough memory (%d bytes needed)\n", heap_size_in_bytes );
38 exit(1);
39 }
40
41 alloc = heap + HEAP_SIZE; /* alloc from end */
42 }
43
44 #else
45
46 #define CONS(car,cdr) ( alloc = (int *) malloc(2*sizeof(int)), \
47 *(alloc) = (car), \
48 *(alloc+1) = (int) (cdr), \
49 alloc)
50
51 #define RECLAIM(x) free(x)
52
53 void init()
54 {
55 return;
56 }
57
58 #endif
59
60 /* Other globals */
61
62 int result;
63
64 /*=============================================================================
65
66 THE BENCHMARKS
67
68 =============================================================================*/
69
70 /*-----------------------------------------------------------------------------
71
72 ;;; DIVITER -- Benchmark which divides by 2 using lists of n ()'s.
73
74 (define (create-n n)
75 (do ((n n (- n 1))
76 (a '() (cons '() a)))
77 ((= n 0) a)))
78
79 (define *ll* (create-n 200))
80
81 (define (recursive-div2 l)
82 (cond ((null? l) '())
83 (else (cons (car l) (recursive-div2 (cddr l))))))
84
85 (define (main . args)
86 (run-benchmark
87 "diviter"
88 diviter-iters
89 (lambda () (iterative-div2 *ll*))
90 (lambda (result)
91 (equal? result
92 '(() () () () () () () () () () () () () () () () () () () ()
93 () () () () () () () () () () () () () () () () () () () ()
94 () () () () () () () () () () () () () () () () () () () ()
95 () () () () () () () () () () () () () () () () () () () ()
96 () () () () () () () () () () () () () () () () () () () ())))))
97
98 */
99
100 int * ll;
101
102 int *create_n(n)
103 int n;
104 {
105 int *result = NULL;
106 while (n > 0) {
107 n = n - 1;
108 result = CONS( 0, result );
109 }
110 return result;
111 }
112
113 int *recursive_div2 (int * l)
114 {
115 if (l == NULL)
116 return NULL;
117 else {
118 int * y = recursive_div2 (CDR(CDR( l )));
119 return CONS( CAR(l), y );
120 }
121 }
122
123 void reclaim_list ( int * x ) {
124
125 int * y = x;
126 while (y != NULL) {
127 x = y;
128 y = CDR(y);
129 RECLAIM(x);
130 }
131 }
132
133 #define LENGTH_BODY \
134 { \
135 int n = 0; \
136 while (l != 0) { \
137 l = CDR(l); \
138 n++; \
139 } \
140 return n; \
141 }
142
143 int list_length( int * l )
144 LENGTH_BODY
145
146 /*===========================================================================*/
147
148 int main (argc, argv)
149 int argc;
150 char *argv[];
151 {
152 int i;
153 int *result;
154 int *ll;
155
156 init();
157 ll = create_n (200);
158
159 result = recursive_div2 (ll);
160 for (i=1; i<400000; i++) {
161 reclaim_list (result);
162 result = recursive_div2 (ll);
163 }
164
165 if (list_length (result) != 100)
166 printf ("*** wrong result ***\n");
167 return 0;
168 }