]> git.sur5r.net Git - cc65/blob - testcode/lib/heaptest.c
b5470bd2a956b42b4827d628aff63550ce55e350
[cc65] / testcode / lib / heaptest.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <time.h> 
5
6
7 /* From _heap.h */
8 extern unsigned _horg;          /* Bottom of heap */
9 extern unsigned _hptr;          /* Current top */
10 extern unsigned _hend;          /* Upper limit */
11 extern unsigned _hfirst;        /* First free block in list */
12 extern unsigned _hlast;         /* Last free block in list */
13
14
15 static unsigned char* V[256];
16
17
18
19 static char* Alloc (void)
20 /* Allocate a random sized chunk of memory */
21 {
22     /* Determine the size */
23     unsigned char Size = (((unsigned char)rand()) & 0x7F) + 1;
24
25     /* Allocate memory */
26     unsigned char* P = malloc (Size);
27
28     /* Set the string to a defined value. We use the size, since this will
29      * also allow us to retrieve it later.
30      */
31     if (P) {
32         memset (P, Size, Size);
33     } else {
34         printf ("Could not allocate %u bytes\n", Size);
35     }
36     return P;
37 }
38
39
40
41 static void Free (unsigned char* P)
42 /* Check a memory block and free it */
43 {
44     unsigned char I;
45
46     /* Get the size of the block */
47     unsigned char Size = P[0];
48
49     /* Scan the block */
50     for (I = 1; I < Size; ++I) {
51         if (P[I] != Size) {
52             printf ("Scan failed - expected %02X, got %02X\n",
53                     Size, P[I]);
54         }
55     }
56
57     /* Free the block */
58     free (P);
59 }
60
61
62
63 static void FillArray (void)
64 /* Fill the array with randomly allocated memory chunks */
65 {
66     unsigned char I = 0;
67     do {
68         V[I] = Alloc ();
69         ++I;
70     } while (I != 0);
71 }
72
73
74
75 static void ShowInfo (void)
76 /* Show heap info */
77 {
78     printf ("%04X  %04X  %04X  %04X  %04X\n",
79             _horg, _hptr, _hend, _hfirst, _hlast);
80 }
81
82
83
84 static void Test1 (void)
85 /* First test */
86 {
87     unsigned char I;
88     FillArray ();
89     for (I = 0; I < 0x80; ++I) {
90         Free (V[0x7F-I]);
91         Free (V[0x80+I]);
92     }
93     ShowInfo ();
94 }
95
96
97
98 static void Test2 (void)
99 /* Second test */
100 {
101     unsigned char I;
102     FillArray ();
103     I = 0;
104     do {
105         Free (V[I]);
106         I += 2;
107     } while (I != 0);
108     I = 1;
109     do {
110         Free (V[I]);
111         I += 2;
112     } while (I != 1);
113     ShowInfo ();
114 }
115
116
117
118 static void Test3 (void)
119 /* Third test */
120 {
121     unsigned char I;
122     FillArray ();
123     I = 0;
124     do {
125         Free (V[I]);
126         I += 2;
127     } while (I != 0);
128     do {
129         V[I] = Alloc ();
130         I += 2;
131     } while (I != 0);
132     I = 1;
133     do {
134         Free (V[I]);
135         I += 2;
136     } while (I != 1);
137     do {
138         V[I] = Alloc ();
139         I += 2;
140     } while (I != 1);
141     I = 0;
142     do {
143         Free (V[I]);
144         ++I;
145     } while (I != 0);
146     ShowInfo ();
147 }
148
149
150
151 static void Test4 (void)
152 /* Fourth test */
153 {
154     unsigned char I, J;
155     FillArray ();
156     I = J = 0;
157     do {
158         do {
159             Free (V[I]);
160             V[I] = Alloc ();
161             ++I;
162         } while (I != 0);
163         ++J;
164     } while (J < 5);
165     do {
166         Free (V[I]);
167         ++I;
168     } while (I != 0);
169     ShowInfo ();
170 }
171
172
173
174 int main (void)
175 {
176     unsigned long T;
177
178     /* Show info at start */
179     ShowInfo ();
180
181     /* Remember the time */
182     T = clock ();
183
184     /* Do the tests */
185     Test1 ();
186     Test2 ();
187     Test3 ();
188     Test4 ();
189
190     /* Calculate the time and print it */
191     T = clock () - T;
192     printf ("Time needed: %lu ticks\n", T);
193
194     /* Done */
195     return EXIT_SUCCESS;
196 }
197
198
199