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