| 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 (iterative-div2 l) |
| 82 |
(do ((l l (cddr l)) |
| 83 |
(a '() (cons (car l) a))) |
| 84 |
((null? l) a))) |
| 85 |
|
| 86 |
(define (main . args) |
| 87 |
(run-benchmark |
| 88 |
"diviter" |
| 89 |
diviter-iters |
| 90 |
(lambda () (iterative-div2 *ll*)) |
| 91 |
(lambda (result) |
| 92 |
(equal? result |
| 93 |
'(() () () () () () () () () () () () () () () () () () () () |
| 94 |
() () () () () () () () () () () () () () () () () () () () |
| 95 |
() () () () () () () () () () () () () () () () () () () () |
| 96 |
() () () () () () () () () () () () () () () () () () () () |
| 97 |
() () () () () () () () () () () () () () () () () () () ()))))) |
| 98 |
|
| 99 |
*/ |
| 100 |
|
| 101 |
int * ll; |
| 102 |
|
| 103 |
int *create_n(n) |
| 104 |
int n; |
| 105 |
{ |
| 106 |
int *result = NULL; |
| 107 |
while (n > 0) { |
| 108 |
n = n - 1; |
| 109 |
result = CONS( 0, result ); |
| 110 |
} |
| 111 |
return result; |
| 112 |
} |
| 113 |
|
| 114 |
int *iterative_div2 (int * l) |
| 115 |
{ |
| 116 |
int * a = NULL; |
| 117 |
for (l = l; l != NULL; l = CDR(CDR(l))) |
| 118 |
a = CONS(CAR(l), a); |
| 119 |
return a; |
| 120 |
} |
| 121 |
|
| 122 |
void reclaim_list ( int * x ) { |
| 123 |
|
| 124 |
int * y = x; |
| 125 |
while (y != NULL) { |
| 126 |
x = y; |
| 127 |
y = CDR(y); |
| 128 |
RECLAIM(x); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
#define LENGTH_BODY \ |
| 133 |
{ \ |
| 134 |
int n = 0; \ |
| 135 |
while (l != 0) { \ |
| 136 |
l = CDR(l); \ |
| 137 |
n++; \ |
| 138 |
} \ |
| 139 |
return n; \ |
| 140 |
} |
| 141 |
|
| 142 |
int list_length( int * l ) |
| 143 |
LENGTH_BODY |
| 144 |
|
| 145 |
/*===========================================================================*/ |
| 146 |
|
| 147 |
int main (argc, argv) |
| 148 |
int argc; |
| 149 |
char *argv[]; |
| 150 |
{ |
| 151 |
int i; |
| 152 |
int *result; |
| 153 |
int *ll; |
| 154 |
|
| 155 |
init(); |
| 156 |
ll = create_n (200); |
| 157 |
|
| 158 |
result = iterative_div2 (ll); |
| 159 |
for (i=1; i<400000; i++) { |
| 160 |
reclaim_list (result); |
| 161 |
result = iterative_div2 (ll); |
| 162 |
} |
| 163 |
|
| 164 |
if (list_length (result) != 100) |
| 165 |
printf ("*** wrong result ***\n"); |
| 166 |
return 0; |
| 167 |
} |