]> git.sur5r.net Git - cc65/blob - testcode/lib/strdup-test.c
added optimization for indexed 16-bit array load of form (array[i & 0x7f])
[cc65] / testcode / lib / strdup-test.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 void ShowInfo (void)
20 /* Show heap info */
21 {
22     /* Count free blocks */
23     unsigned Count = 0;
24     unsigned** P = (unsigned**) _hfirst;
25     while (P) {
26         ++Count;
27         P = P[1];
28     }
29     printf ("%04X  %04X  %04X  %04X  %04X %u\n",
30             _horg, _hptr, _hend, _hfirst, _hlast, Count);
31
32     if (Count) {
33         P = (unsigned**) _hfirst;
34         while (P) {
35             printf ("%04X  %04X  %04X %04X(%u)\n",
36                     (unsigned) P, P[2], P[1], P[0], P[0]);
37             P = P[1];
38         }
39         getchar ();
40     }
41 }
42
43
44
45 static const char* RandStr (void)
46 /* Create a random string */
47 {
48     static char S [300];
49     unsigned Len = (rand () & 0xFF) + (sizeof (S) - 0xFF - 1);
50     unsigned I;
51     char C;
52
53     for (I = 0; I < Len; ++I) {
54         do {
55             C = rand() & 0xFF;
56         } while (C == 0);
57         S[I] = C;
58     }
59     S[Len] = '\0';
60
61     return S;
62 }
63
64
65
66 static void FillArray (void)
67 /* Fill the string array */
68 {
69     unsigned char I = 0;
70     do {
71         V[I] = strdup (RandStr ());
72         ++I;
73     } while (I != 0);
74 }
75
76
77
78 static void FreeArray (void)
79 /* Free all strings in the array */
80 {
81     unsigned char I = 0;
82     do {
83         free (V[I]);
84         ++I;
85     } while (I != 0);
86 }
87
88
89
90 int main (void)
91 {
92     unsigned long T;
93
94     /* Show info at start */
95     ShowInfo ();
96
97     /* Remember the time */
98     T = clock ();
99
100     /* Do the tests */
101     FillArray ();
102     ShowInfo ();
103     FreeArray ();
104     ShowInfo ();
105
106     /* Calculate the time and print it */
107     T = clock () - T;
108     printf ("Time needed: %lu ticks\n", T);
109
110     /* Done */
111     return EXIT_SUCCESS;
112 }
113
114
115