]> git.sur5r.net Git - cc65/blob - testcode/lib/heaptest.c
info about c1541 in docs, lowered highest available address to $6000 due to
[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         exit (EXIT_FAILURE);
36     }
37     return P;
38 }
39
40
41
42 static void Free (unsigned char* P)
43 /* Check a memory block and free it */
44 {
45     unsigned char I;
46
47     /* Get the size of the block */
48     unsigned char Size = P[0];
49
50     /* Scan the block */
51     for (I = 1; I < Size; ++I) {
52         if (P[I] != Size) {
53             printf ("Scan failed - expected %02X, got %02X\n",
54                     Size, P[I]);
55             exit (EXIT_FAILURE);
56         }
57     }
58
59     /* Free the block */
60     free (P);
61 }
62
63
64
65 static void FillArray (void)
66 /* Fill the array with randomly allocated memory chunks */
67 {
68     unsigned char I = 0;
69     do {
70         V[I] = Alloc ();
71         ++I;
72     } while (I != 0);
73 }
74
75
76
77 static void ShowInfo (void)
78 /* Show heap info */
79 {
80     /* Count free blocks */
81     unsigned Count = 0;
82     unsigned** P = (unsigned**) _hfirst;
83     while (P) {
84         ++Count;
85         P = P[1];
86     }
87     printf ("%04X  %04X  %04X  %04X  %04X %u\n",
88             _horg, _hptr, _hend, _hfirst, _hlast, Count);
89
90     if (Count) {
91         P = (unsigned**) _hfirst;
92         while (P) {
93             printf ("%04X  %04X  %04X %04X(%u)\n",
94                     (unsigned) P, P[2], P[1], P[0], P[0]);
95             P = P[1];
96         }
97         getchar ();
98     }
99 }
100
101
102
103 static void Test1 (void)
104 {
105     unsigned char I;
106     FillArray ();
107     for (I = 0; I < 0x80; ++I) {
108         Free (V[0x7F-I]);
109         Free (V[0x80+I]);
110     }
111     ShowInfo ();
112 }
113
114
115
116 static void Test2 (void)
117 {
118     unsigned char I;
119     FillArray ();
120     I = 0;
121     do {
122         Free (V[I]);
123         ++I;
124     } while (I != 0);
125     ShowInfo ();
126 }
127
128
129
130 static void Test3 (void)
131 {
132     unsigned char I;
133     FillArray ();
134     I = 0;
135     do {
136         --I;
137         Free (V[I]);
138     } while (I != 0);
139     ShowInfo ();
140 }
141
142
143
144 static void Test4 (void)
145 {
146     unsigned char I;
147     FillArray ();
148     I = 0;
149     do {
150         Free (V[I]);
151         I += 2;
152     } while (I != 0);
153     I = 1;
154     do {
155         Free (V[I]);
156         I += 2;
157     } while (I != 1);
158     ShowInfo ();
159 }
160
161
162
163 static void Test5 (void)
164 {
165     unsigned char I;
166     FillArray ();
167     I = 0;
168     do {
169         Free (V[I]);
170         I += 2;
171     } while (I != 0);
172     do {
173         V[I] = Alloc ();
174         I += 2;
175     } while (I != 0);
176     I = 1;
177     do {
178         Free (V[I]);
179         I += 2;
180     } while (I != 1);
181     do {
182         V[I] = Alloc ();
183         I += 2;
184     } while (I != 1);
185     I = 0;
186     do {
187         Free (V[I]);
188         ++I;
189     } while (I != 0);
190     ShowInfo ();
191 }
192
193
194
195 static void Test6 (void)
196 {
197     unsigned char I, J;
198     FillArray ();
199     I = J = 0;
200     do {
201         do {
202             Free (V[I]);
203             V[I] = Alloc ();
204             ++I;
205         } while (I != 0);
206         ++J;
207     } while (J < 5);
208     do {
209         Free (V[I]);
210         ++I;
211     } while (I != 0);
212     ShowInfo ();
213 }
214
215
216
217 int main (void)
218 {
219     unsigned long T;
220
221     /* Show info at start */
222     ShowInfo ();
223
224     /* Remember the time */
225     T = clock ();
226
227     /* Do the tests */
228     Test1 ();
229     Test2 ();
230     Test3 ();
231     Test4 ();
232     Test5 ();
233     Test6 ();
234
235     /* Calculate the time and print it */
236     T = clock () - T;
237     printf ("Time needed: %lu ticks\n", T);
238
239     /* Done */
240     return EXIT_SUCCESS;
241 }
242
243
244