1 | /* $Id: tstkLdrHeap.c 29 2009-07-01 20:30:29Z bird $ */
|
---|
2 | /** @file
|
---|
3 | * kLdr - Heap testcase.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 2006-2007 Knut St. Osmundsen <[email protected]>
|
---|
8 | *
|
---|
9 | * Permission is hereby granted, free of charge, to any person
|
---|
10 | * obtaining a copy of this software and associated documentation
|
---|
11 | * files (the "Software"), to deal in the Software without
|
---|
12 | * restriction, including without limitation the rights to use,
|
---|
13 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
14 | * copies of the Software, and to permit persons to whom the
|
---|
15 | * Software is furnished to do so, subject to the following
|
---|
16 | * conditions:
|
---|
17 | *
|
---|
18 | * The above copyright notice and this permission notice shall be
|
---|
19 | * included in all copies or substantial portions of the Software.
|
---|
20 | *
|
---|
21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
28 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
29 | */
|
---|
30 |
|
---|
31 | /*******************************************************************************
|
---|
32 | * Header Files *
|
---|
33 | *******************************************************************************/
|
---|
34 | #include <k/kLdr.h>
|
---|
35 | #include <k/kHlp.h>
|
---|
36 |
|
---|
37 | #include <stdio.h>
|
---|
38 | #include <stdlib.h>
|
---|
39 |
|
---|
40 |
|
---|
41 | /*******************************************************************************
|
---|
42 | * Defined Constants And Macros *
|
---|
43 | *******************************************************************************/
|
---|
44 | #define CHECK_FATAL(expr) \
|
---|
45 | do { if (!(expr)) { printf("tstkLdrHeap(%d): FATAL FAILURE - %s\n", __LINE__, #expr); return 1; } \
|
---|
46 | } while (0)
|
---|
47 |
|
---|
48 | #define CHECK(expr) \
|
---|
49 | do { if (!(expr)) { printf("tstkLdrHeap(%d): ERROR - %s\n", __LINE__, #expr); cErrors++; kHlpAssertBreakpoint();} \
|
---|
50 | } while (0)
|
---|
51 |
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Get a random size.
|
---|
55 | * @returns random size.
|
---|
56 | */
|
---|
57 | static unsigned RandSize(void)
|
---|
58 | {
|
---|
59 | unsigned i = (unsigned)rand() % (256*1024 - 1);
|
---|
60 | return i ? i : 1;
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Get a random index.
|
---|
65 | * @returns random index.
|
---|
66 | * @param cEntries The number of entries in the table.
|
---|
67 | */
|
---|
68 | static unsigned RandIdx(unsigned cEntries)
|
---|
69 | {
|
---|
70 | unsigned i = (unsigned)rand();
|
---|
71 | while (i >= cEntries)
|
---|
72 | i >>= 1;
|
---|
73 | return i;
|
---|
74 | }
|
---|
75 |
|
---|
76 | #if 0
|
---|
77 | # define kHlpAlloc(a) malloc(a)
|
---|
78 | # define kHlpFree(a) free(a)
|
---|
79 | #endif
|
---|
80 |
|
---|
81 | int main()
|
---|
82 | {
|
---|
83 | int cErrors = 0;
|
---|
84 | int rc;
|
---|
85 | #define MAX_ALLOCS 256
|
---|
86 | static struct
|
---|
87 | {
|
---|
88 | void *pv;
|
---|
89 | unsigned cb;
|
---|
90 | } s_aAllocs[MAX_ALLOCS];
|
---|
91 | unsigned cAllocs;
|
---|
92 | unsigned i;
|
---|
93 | unsigned j;
|
---|
94 |
|
---|
95 | /*
|
---|
96 | * Some simple init / term.
|
---|
97 | */
|
---|
98 | rc = kHlpHeapInit();
|
---|
99 | CHECK_FATAL(!rc);
|
---|
100 | kHlpHeapTerm();
|
---|
101 |
|
---|
102 | rc = kHlpHeapInit();
|
---|
103 | CHECK_FATAL(!rc);
|
---|
104 | kHlpHeapTerm();
|
---|
105 |
|
---|
106 |
|
---|
107 | /*
|
---|
108 | * Simple alloc all, free all in FIFO order.
|
---|
109 | */
|
---|
110 | rc = kHlpHeapInit();
|
---|
111 | CHECK_FATAL(!rc);
|
---|
112 |
|
---|
113 | /* 1. allocate all slots. */
|
---|
114 | for (i = 0; i < MAX_ALLOCS; i++)
|
---|
115 | {
|
---|
116 | s_aAllocs[i].cb = RandSize();
|
---|
117 | s_aAllocs[i].pv = kHlpAlloc(s_aAllocs[i].cb);
|
---|
118 | CHECK(s_aAllocs[i].pv);
|
---|
119 | }
|
---|
120 |
|
---|
121 | /* 2. free all slots. */
|
---|
122 | for (i = 0; i < MAX_ALLOCS; i++)
|
---|
123 | kHlpFree(s_aAllocs[i].pv);
|
---|
124 |
|
---|
125 | /* terminate */
|
---|
126 | kHlpHeapTerm();
|
---|
127 |
|
---|
128 |
|
---|
129 | /*
|
---|
130 | * Simple alloc all, free all in LIFO order.
|
---|
131 | */
|
---|
132 | rc = kHlpHeapInit();
|
---|
133 | CHECK_FATAL(!rc);
|
---|
134 |
|
---|
135 | /* 1. allocate all slots. */
|
---|
136 | for (i = 0; i < MAX_ALLOCS; i++)
|
---|
137 | {
|
---|
138 | s_aAllocs[i].cb = RandSize();
|
---|
139 | s_aAllocs[i].pv = kHlpAlloc(s_aAllocs[i].cb);
|
---|
140 | CHECK(s_aAllocs[i].pv);
|
---|
141 | }
|
---|
142 |
|
---|
143 | /* 2. free all slots. */
|
---|
144 | i = MAX_ALLOCS;
|
---|
145 | while (i-- > 0)
|
---|
146 | kHlpFree(s_aAllocs[i].pv);
|
---|
147 |
|
---|
148 | /* terminate */
|
---|
149 | kHlpHeapTerm();
|
---|
150 |
|
---|
151 |
|
---|
152 | /*
|
---|
153 | * Bunch of allocations, free half, allocate and free in pairs, free all.
|
---|
154 | */
|
---|
155 | rc = kHlpHeapInit();
|
---|
156 | CHECK_FATAL(!rc);
|
---|
157 |
|
---|
158 | /* 1. allocate all slots. */
|
---|
159 | for (i = 0; i < MAX_ALLOCS; i++)
|
---|
160 | {
|
---|
161 | s_aAllocs[i].cb = RandSize();
|
---|
162 | s_aAllocs[i].pv = kHlpAlloc(s_aAllocs[i].cb);
|
---|
163 | CHECK(s_aAllocs[i].pv);
|
---|
164 | }
|
---|
165 | cAllocs = MAX_ALLOCS;
|
---|
166 |
|
---|
167 | /* 2. free half (random order). */
|
---|
168 | while (cAllocs > MAX_ALLOCS / 2)
|
---|
169 | {
|
---|
170 | i = RandIdx(cAllocs);
|
---|
171 | kHlpFree(s_aAllocs[i].pv);
|
---|
172 | cAllocs--;
|
---|
173 | if (i != cAllocs)
|
---|
174 | s_aAllocs[i] = s_aAllocs[cAllocs];
|
---|
175 | }
|
---|
176 |
|
---|
177 | /* 3. lots of alloc and free activity. */
|
---|
178 | for (j = 0; j < MAX_ALLOCS * 32; j++)
|
---|
179 | {
|
---|
180 | /* allocate */
|
---|
181 | unsigned cMax = RandIdx(MAX_ALLOCS / 4) + 1;
|
---|
182 | while (cAllocs < MAX_ALLOCS && cMax-- > 0)
|
---|
183 | {
|
---|
184 | i = cAllocs;
|
---|
185 | s_aAllocs[i].cb = RandSize();
|
---|
186 | s_aAllocs[i].pv = kHlpAlloc(s_aAllocs[i].cb);
|
---|
187 | CHECK(s_aAllocs[i].pv);
|
---|
188 | cAllocs++;
|
---|
189 | }
|
---|
190 |
|
---|
191 | /* free */
|
---|
192 | cMax = RandIdx(MAX_ALLOCS / 4) + 1;
|
---|
193 | while (cAllocs > MAX_ALLOCS / 2 && cMax-- > 0)
|
---|
194 | {
|
---|
195 | i = RandIdx(cAllocs);
|
---|
196 | kHlpFree(s_aAllocs[i].pv);
|
---|
197 | cAllocs--;
|
---|
198 | if (i != cAllocs)
|
---|
199 | s_aAllocs[i] = s_aAllocs[cAllocs];
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | /* 4. free all */
|
---|
204 | while (cAllocs > 0)
|
---|
205 | {
|
---|
206 | i = RandIdx(cAllocs);
|
---|
207 | kHlpFree(s_aAllocs[i].pv);
|
---|
208 | cAllocs--;
|
---|
209 | if (i != cAllocs)
|
---|
210 | s_aAllocs[i] = s_aAllocs[cAllocs];
|
---|
211 | }
|
---|
212 |
|
---|
213 | /* terminate */
|
---|
214 | kHlpHeapTerm();
|
---|
215 |
|
---|
216 |
|
---|
217 | /* summary */
|
---|
218 | if (!cErrors)
|
---|
219 | printf("tstkLdrHeap: SUCCESS\n");
|
---|
220 | else
|
---|
221 | printf("tstkLdrHeap: FAILURE - %d errors\n", cErrors);
|
---|
222 | return !!cErrors;
|
---|
223 | }
|
---|