1 | /* $Id: $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * kLdr - Heap testcase.
|
---|
5 | *
|
---|
6 | * Copyright (c) 2006 knut st. osmundsen <bird-kbuild-src@anduin.net>
|
---|
7 | *
|
---|
8 | *
|
---|
9 | * This file is part of kLdr.
|
---|
10 | *
|
---|
11 | * kLdr is free software; you can redistribute it and/or modify
|
---|
12 | * it under the terms of the GNU General Public License as published by
|
---|
13 | * the Free Software Foundation; either version 2 of the License, or
|
---|
14 | * (at your option) any later version.
|
---|
15 | *
|
---|
16 | * kLdr is distributed in the hope that it will be useful,
|
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | * GNU General Public License for more details.
|
---|
20 | *
|
---|
21 | * You should have received a copy of the GNU General Public License
|
---|
22 | * along with kLdr; if not, write to the Free Software
|
---|
23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
24 | *
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*******************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *******************************************************************************/
|
---|
31 | #include <kLdr.h>
|
---|
32 | #include "kLdrHlp.h"
|
---|
33 |
|
---|
34 | #include <stdio.h>
|
---|
35 | #include <stdlib.h>
|
---|
36 |
|
---|
37 |
|
---|
38 | /*******************************************************************************
|
---|
39 | * Defined Constants And Macros *
|
---|
40 | *******************************************************************************/
|
---|
41 | #define CHECK_FATAL(expr) \
|
---|
42 | do { if (!(expr)) { printf("tstkLdrHeap(%d): FATAL FAILURE - %s\n", __LINE__, #expr); return 1; } \
|
---|
43 | } while (0)
|
---|
44 |
|
---|
45 | #define CHECK(expr) \
|
---|
46 | do { if (!(expr)) { printf("tstkLdrHeap(%d): ERROR - %s\n", __LINE__, #expr); cErrors++; __debugbreak();} \
|
---|
47 | } while (0)
|
---|
48 |
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Get a random size.
|
---|
52 | * @returns random size.
|
---|
53 | */
|
---|
54 | static unsigned RandSize(void)
|
---|
55 | {
|
---|
56 | unsigned i = (unsigned)rand() % (256*1024 - 1);
|
---|
57 | return i ? i : 1;
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Get a random index.
|
---|
62 | * @returns random index.
|
---|
63 | * @param cEntries The number of entries in the table.
|
---|
64 | */
|
---|
65 | static unsigned RandIdx(unsigned cEntries)
|
---|
66 | {
|
---|
67 | unsigned i = (unsigned)rand();
|
---|
68 | while (i >= cEntries)
|
---|
69 | i >>= 1;
|
---|
70 | return i;
|
---|
71 | }
|
---|
72 |
|
---|
73 | #if 0
|
---|
74 | # define kldrHlpAlloc(a) malloc(a)
|
---|
75 | # define kldrHlpFree(a) free(a)
|
---|
76 | #endif
|
---|
77 |
|
---|
78 | int main()
|
---|
79 | {
|
---|
80 | int cErrors = 0;
|
---|
81 | int rc;
|
---|
82 | #define MAX_ALLOCS 256
|
---|
83 | static struct
|
---|
84 | {
|
---|
85 | void *pv;
|
---|
86 | unsigned cb;
|
---|
87 | } s_aAllocs[MAX_ALLOCS];
|
---|
88 | unsigned cAllocs;
|
---|
89 | unsigned i;
|
---|
90 | unsigned j;
|
---|
91 |
|
---|
92 | /*
|
---|
93 | * Some simple init / term.
|
---|
94 | */
|
---|
95 | rc = kldrHlpHeapInit();
|
---|
96 | CHECK_FATAL(!rc);
|
---|
97 | kldrHlpHeapTerm();
|
---|
98 |
|
---|
99 | rc = kldrHlpHeapInit();
|
---|
100 | CHECK_FATAL(!rc);
|
---|
101 | kldrHlpHeapTerm();
|
---|
102 |
|
---|
103 |
|
---|
104 | /*
|
---|
105 | * Simple alloc all, free all in FIFO order.
|
---|
106 | */
|
---|
107 | rc = kldrHlpHeapInit();
|
---|
108 | CHECK_FATAL(!rc);
|
---|
109 |
|
---|
110 | /* 1. allocate all slots. */
|
---|
111 | for (i = 0; i < MAX_ALLOCS; i++)
|
---|
112 | {
|
---|
113 | s_aAllocs[i].cb = RandSize();
|
---|
114 | s_aAllocs[i].pv = kldrHlpAlloc(s_aAllocs[i].cb);
|
---|
115 | CHECK(s_aAllocs[i].pv);
|
---|
116 | }
|
---|
117 |
|
---|
118 | /* 2. free all slots. */
|
---|
119 | for (i = 0; i < MAX_ALLOCS; i++)
|
---|
120 | kldrHlpFree(s_aAllocs[i].pv);
|
---|
121 |
|
---|
122 | /* terminate */
|
---|
123 | kldrHlpHeapTerm();
|
---|
124 |
|
---|
125 |
|
---|
126 | /*
|
---|
127 | * Simple alloc all, free all in LIFO order.
|
---|
128 | */
|
---|
129 | rc = kldrHlpHeapInit();
|
---|
130 | CHECK_FATAL(!rc);
|
---|
131 |
|
---|
132 | /* 1. allocate all slots. */
|
---|
133 | for (i = 0; i < MAX_ALLOCS; i++)
|
---|
134 | {
|
---|
135 | s_aAllocs[i].cb = RandSize();
|
---|
136 | s_aAllocs[i].pv = kldrHlpAlloc(s_aAllocs[i].cb);
|
---|
137 | CHECK(s_aAllocs[i].pv);
|
---|
138 | }
|
---|
139 |
|
---|
140 | /* 2. free all slots. */
|
---|
141 | i = MAX_ALLOCS;
|
---|
142 | while (i-- > 0)
|
---|
143 | kldrHlpFree(s_aAllocs[i].pv);
|
---|
144 |
|
---|
145 | /* terminate */
|
---|
146 | kldrHlpHeapTerm();
|
---|
147 |
|
---|
148 |
|
---|
149 | /*
|
---|
150 | * Bunch of allocations, free half, allocate and free in pairs, free all.
|
---|
151 | */
|
---|
152 | rc = kldrHlpHeapInit();
|
---|
153 | CHECK_FATAL(!rc);
|
---|
154 |
|
---|
155 | /* 1. allocate all slots. */
|
---|
156 | for (i = 0; i < MAX_ALLOCS; i++)
|
---|
157 | {
|
---|
158 | s_aAllocs[i].cb = RandSize();
|
---|
159 | s_aAllocs[i].pv = kldrHlpAlloc(s_aAllocs[i].cb);
|
---|
160 | CHECK(s_aAllocs[i].pv);
|
---|
161 | }
|
---|
162 | cAllocs = MAX_ALLOCS;
|
---|
163 |
|
---|
164 | /* 2. free half (random order). */
|
---|
165 | while (cAllocs > MAX_ALLOCS / 2)
|
---|
166 | {
|
---|
167 | i = RandIdx(cAllocs);
|
---|
168 | kldrHlpFree(s_aAllocs[i].pv);
|
---|
169 | cAllocs--;
|
---|
170 | if (i != cAllocs)
|
---|
171 | s_aAllocs[i] = s_aAllocs[cAllocs];
|
---|
172 | }
|
---|
173 |
|
---|
174 | /* 3. lots of alloc and free activity. */
|
---|
175 | for (j = 0; j < MAX_ALLOCS * 32; j++)
|
---|
176 | {
|
---|
177 | /* allocate */
|
---|
178 | unsigned cMax = RandIdx(MAX_ALLOCS / 4) + 1;
|
---|
179 | while (cAllocs < MAX_ALLOCS && cMax-- > 0)
|
---|
180 | {
|
---|
181 | i = cAllocs;
|
---|
182 | s_aAllocs[i].cb = RandSize();
|
---|
183 | s_aAllocs[i].pv = kldrHlpAlloc(s_aAllocs[i].cb);
|
---|
184 | CHECK(s_aAllocs[i].pv);
|
---|
185 | cAllocs++;
|
---|
186 | }
|
---|
187 |
|
---|
188 | /* free */
|
---|
189 | cMax = RandIdx(MAX_ALLOCS / 4) + 1;
|
---|
190 | while (cAllocs > MAX_ALLOCS / 2 && cMax-- > 0)
|
---|
191 | {
|
---|
192 | i = RandIdx(cAllocs);
|
---|
193 | kldrHlpFree(s_aAllocs[i].pv);
|
---|
194 | cAllocs--;
|
---|
195 | if (i != cAllocs)
|
---|
196 | s_aAllocs[i] = s_aAllocs[cAllocs];
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | /* 4. free all */
|
---|
201 | while (cAllocs > 0)
|
---|
202 | {
|
---|
203 | i = RandIdx(cAllocs);
|
---|
204 | kldrHlpFree(s_aAllocs[i].pv);
|
---|
205 | cAllocs--;
|
---|
206 | if (i != cAllocs)
|
---|
207 | s_aAllocs[i] = s_aAllocs[cAllocs];
|
---|
208 | }
|
---|
209 |
|
---|
210 | /* terminate */
|
---|
211 | kldrHlpHeapTerm();
|
---|
212 |
|
---|
213 |
|
---|
214 | /* summary */
|
---|
215 | if (!cErrors)
|
---|
216 | printf("tstkLdrHeap: SUCCESS\n");
|
---|
217 | else
|
---|
218 | printf("tstkLdrHeap: FAILURE - %d errors\n", cErrors);
|
---|
219 | return !!cErrors;
|
---|
220 | }
|
---|