1 | /* $Id: tstHandleTable.cpp 10789 2008-07-21 17:22:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - Handle Tables.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.215389.xyz. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | *
|
---|
26 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 | /*******************************************************************************
|
---|
32 | * Header Files *
|
---|
33 | *******************************************************************************/
|
---|
34 | #include <iprt/handletable.h>
|
---|
35 | #include <iprt/stream.h>
|
---|
36 | #include <iprt/initterm.h>
|
---|
37 | #include <iprt/err.h>
|
---|
38 | #include <iprt/getopt.h>
|
---|
39 |
|
---|
40 |
|
---|
41 | /*******************************************************************************
|
---|
42 | * Global Variables *
|
---|
43 | *******************************************************************************/
|
---|
44 | static unsigned g_cErrors;
|
---|
45 |
|
---|
46 | static DECLCALLBACK(void) tstHandleTableTest1Delete(RTHANDLETABLE hHandleTable, uint32_t h, void *pvObj, void *pvCtx, void *pvUser)
|
---|
47 | {
|
---|
48 | uint32_t *pcCalls = (uint32_t *)pvUser;
|
---|
49 | (*pcCalls)++;
|
---|
50 | }
|
---|
51 |
|
---|
52 | static DECLCALLBACK(int) tstHandleTableTest1Retain(RTHANDLETABLE hHandleTable, void *pvObj, void *pvCtx, void *pvUser)
|
---|
53 | {
|
---|
54 | uint32_t *pcCalls = (uint32_t *)pvUser;
|
---|
55 | (*pcCalls)++;
|
---|
56 | return VINF_SUCCESS;
|
---|
57 | }
|
---|
58 |
|
---|
59 | static int tstHandleTableTest1(uint32_t uBase, uint32_t cMax, uint32_t cDelta, uint32_t cUnitsPerDot, bool fCallbacks, uint32_t fFlags)
|
---|
60 | {
|
---|
61 | const char *pszWithCtx = fFlags & RTHANDLETABLE_FLAGS_CONTEXT ? "WithCtx" : "";
|
---|
62 | uint32_t cRetainerCalls = 0;
|
---|
63 | int rc;
|
---|
64 |
|
---|
65 | RTPrintf("tstHandleTable: TESTING RTHandleTableCreateEx(, 0");
|
---|
66 | if (fFlags & RTHANDLETABLE_FLAGS_LOCKED) RTPrintf(" | LOCKED");
|
---|
67 | if (fFlags & RTHANDLETABLE_FLAGS_CONTEXT) RTPrintf(" | CONTEXT");
|
---|
68 | RTPrintf(", %#x, %#x,,)...\n", uBase, cMax);
|
---|
69 |
|
---|
70 | RTHANDLETABLE hHT;
|
---|
71 | rc = RTHandleTableCreateEx(&hHT, fFlags, uBase, cMax,
|
---|
72 | fCallbacks ? tstHandleTableTest1Retain : NULL,
|
---|
73 | fCallbacks ? &cRetainerCalls : NULL);
|
---|
74 | if (RT_FAILURE(rc))
|
---|
75 | {
|
---|
76 | RTPrintf("\ntstHandleTable: FAILURE - RTHandleTableCreateEx failed, %Rrc!\n", rc);
|
---|
77 | return 1;
|
---|
78 | }
|
---|
79 |
|
---|
80 | /* fill it */
|
---|
81 | RTPrintf("tstHandleTable: TESTING RTHandleTableAlloc%s..", pszWithCtx); RTStrmFlush(g_pStdOut);
|
---|
82 | uint32_t i = uBase;
|
---|
83 | for (;; i++)
|
---|
84 | {
|
---|
85 | uint32_t h;
|
---|
86 | if (fFlags & RTHANDLETABLE_FLAGS_CONTEXT)
|
---|
87 | rc = RTHandleTableAllocWithCtx(hHT, (void *)((uintptr_t)&i + (uintptr_t)i * 4), NULL, &h);
|
---|
88 | else
|
---|
89 | rc = RTHandleTableAlloc(hHT, (void *)((uintptr_t)&i + (uintptr_t)i * 4), &h);
|
---|
90 | if (RT_SUCCESS(rc))
|
---|
91 | {
|
---|
92 | if (h != i)
|
---|
93 | {
|
---|
94 | RTPrintf("\ntstHandleTable: FAILURE (%d) - h=%d, expected %d!\n", __LINE__, h, i);
|
---|
95 | g_cErrors++;
|
---|
96 | }
|
---|
97 | }
|
---|
98 | else if (rc == VERR_NO_MORE_HANDLES)
|
---|
99 | {
|
---|
100 | if (i < cMax)
|
---|
101 | {
|
---|
102 | RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, expected > 65534!\n", __LINE__, i);
|
---|
103 | g_cErrors++;
|
---|
104 | }
|
---|
105 | break;
|
---|
106 | }
|
---|
107 | else
|
---|
108 | {
|
---|
109 | RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, rc=%Rrc!\n", __LINE__, i, rc);
|
---|
110 | g_cErrors++;
|
---|
111 | }
|
---|
112 | if (!(i % cUnitsPerDot))
|
---|
113 | {
|
---|
114 | RTPrintf(".");
|
---|
115 | RTStrmFlush(g_pStdOut);
|
---|
116 | }
|
---|
117 | }
|
---|
118 | uint32_t const c = i;
|
---|
119 | RTPrintf(" c=%#x\n", c);
|
---|
120 | if (fCallbacks && cRetainerCalls != 0)
|
---|
121 | {
|
---|
122 | RTPrintf("tstHandleTable: FAILURE (%d) - cRetainerCalls=%#x expected 0!\n", __LINE__, i, cRetainerCalls);
|
---|
123 | g_cErrors++;
|
---|
124 | }
|
---|
125 |
|
---|
126 | /* look up all the entries */
|
---|
127 | RTPrintf("tstHandleTable: TESTING RTHandleTableLookup%s..", pszWithCtx); RTStrmFlush(g_pStdOut);
|
---|
128 | cRetainerCalls = 0;
|
---|
129 | for (i = uBase; i < c; i++)
|
---|
130 | {
|
---|
131 | void *pvExpect = (void *)((uintptr_t)&i + (uintptr_t)i * 4);
|
---|
132 | void *pvObj;
|
---|
133 | if (fFlags & RTHANDLETABLE_FLAGS_CONTEXT)
|
---|
134 | pvObj = RTHandleTableLookupWithCtx(hHT, i, NULL);
|
---|
135 | else
|
---|
136 | pvObj = RTHandleTableLookup(hHT, i);
|
---|
137 | if (!pvObj)
|
---|
138 | {
|
---|
139 | RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableLookup%s failed!\n", __LINE__, i, pszWithCtx);
|
---|
140 | g_cErrors++;
|
---|
141 | }
|
---|
142 | else if (pvObj != pvExpect)
|
---|
143 | {
|
---|
144 | RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, pvObj=%p expected %p\n", __LINE__, i, pvObj, pvExpect);
|
---|
145 | g_cErrors++;
|
---|
146 | }
|
---|
147 | if (!(i % cUnitsPerDot))
|
---|
148 | {
|
---|
149 | RTPrintf(".");
|
---|
150 | RTStrmFlush(g_pStdOut);
|
---|
151 | }
|
---|
152 | }
|
---|
153 | RTPrintf("\n");
|
---|
154 | if (fCallbacks && cRetainerCalls != c - uBase)
|
---|
155 | {
|
---|
156 | RTPrintf("tstHandleTable: FAILURE (%d) - cRetainerCalls=%#x expected %#x!\n", __LINE__, cRetainerCalls, c - uBase);
|
---|
157 | g_cErrors++;
|
---|
158 | }
|
---|
159 |
|
---|
160 | /* remove all the entries (in order) */
|
---|
161 | RTPrintf("tstHandleTable: TESTING RTHandleTableFree%s..", pszWithCtx); RTStrmFlush(g_pStdOut);
|
---|
162 | cRetainerCalls = 0;
|
---|
163 | for (i = uBase; i < c; i++)
|
---|
164 | {
|
---|
165 | void *pvExpect = (void *)((uintptr_t)&i + (uintptr_t)i * 4);
|
---|
166 | void *pvObj;
|
---|
167 | if (fFlags & RTHANDLETABLE_FLAGS_CONTEXT)
|
---|
168 | pvObj = RTHandleTableFreeWithCtx(hHT, i, NULL);
|
---|
169 | else
|
---|
170 | pvObj = RTHandleTableFree(hHT, i);
|
---|
171 | if (!pvObj)
|
---|
172 | {
|
---|
173 | RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableLookup%s failed!\n", __LINE__, i, pszWithCtx);
|
---|
174 | g_cErrors++;
|
---|
175 | }
|
---|
176 | else if (pvObj != pvExpect)
|
---|
177 | {
|
---|
178 | RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, pvObj=%p expected %p\n", __LINE__, i, pvObj, pvExpect);
|
---|
179 | g_cErrors++;
|
---|
180 | }
|
---|
181 | else if ( fFlags & RTHANDLETABLE_FLAGS_CONTEXT
|
---|
182 | ? RTHandleTableLookupWithCtx(hHT, i, NULL)
|
---|
183 | : RTHandleTableLookup(hHT, i))
|
---|
184 | {
|
---|
185 | RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableLookup%s succeeded after free!\n", __LINE__, i, pszWithCtx);
|
---|
186 | g_cErrors++;
|
---|
187 | }
|
---|
188 | if (!(i % cUnitsPerDot))
|
---|
189 | {
|
---|
190 | RTPrintf(".");
|
---|
191 | RTStrmFlush(g_pStdOut);
|
---|
192 | }
|
---|
193 | }
|
---|
194 | RTPrintf("\n");
|
---|
195 | if (fCallbacks && cRetainerCalls != c - uBase)
|
---|
196 | {
|
---|
197 | RTPrintf("tstHandleTable: FAILURE (%d) - cRetainerCalls=%#x expected %#x!\n", __LINE__, cRetainerCalls, c - uBase);
|
---|
198 | g_cErrors++;
|
---|
199 | }
|
---|
200 |
|
---|
201 | /* do a mix of alloc, lookup and free where there is a constant of cDelta handles in the table. */
|
---|
202 | RTPrintf("tstHandleTable: TESTING Alloc,Lookup,Free mix [cDelta=%#x]..", cDelta); RTStrmFlush(g_pStdOut);
|
---|
203 | for (i = uBase; i < c * 2; i++)
|
---|
204 | {
|
---|
205 | /* alloc */
|
---|
206 | uint32_t hExpect = ((i - uBase) % (c - uBase)) + uBase;
|
---|
207 | uint32_t h;
|
---|
208 | if (fFlags & RTHANDLETABLE_FLAGS_CONTEXT)
|
---|
209 | rc = RTHandleTableAllocWithCtx(hHT, (void *)((uintptr_t)&i + (uintptr_t)hExpect * 4), NULL, &h);
|
---|
210 | else
|
---|
211 | rc = RTHandleTableAlloc(hHT, (void *)((uintptr_t)&i + (uintptr_t)hExpect * 4), &h);
|
---|
212 | if (RT_FAILURE(rc))
|
---|
213 | {
|
---|
214 | RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableAlloc%s: rc=%Rrc!\n", __LINE__, i, pszWithCtx, rc);
|
---|
215 | g_cErrors++;
|
---|
216 | }
|
---|
217 | else if (h != hExpect)
|
---|
218 | {
|
---|
219 | RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableAlloc%s: h=%u hExpect=%u! - abort sub-test\n", __LINE__, i, pszWithCtx, h, hExpect);
|
---|
220 | g_cErrors++;
|
---|
221 | break;
|
---|
222 | }
|
---|
223 |
|
---|
224 | if (i >= cDelta + uBase)
|
---|
225 | {
|
---|
226 | /* lookup */
|
---|
227 | for (uint32_t j = i - cDelta; j <= i; j++)
|
---|
228 | {
|
---|
229 | uint32_t hLookup = ((j - uBase) % (c - uBase)) + uBase;
|
---|
230 | void *pvExpect = (void *)((uintptr_t)&i + (uintptr_t)hLookup * 4);
|
---|
231 | void *pvObj;
|
---|
232 | if (fFlags & RTHANDLETABLE_FLAGS_CONTEXT)
|
---|
233 | pvObj = RTHandleTableLookupWithCtx(hHT, hLookup, NULL);
|
---|
234 | else
|
---|
235 | pvObj = RTHandleTableLookup(hHT, hLookup);
|
---|
236 | if (pvObj != pvExpect)
|
---|
237 | {
|
---|
238 | RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, j=%d, RTHandleTableLookup%s(,%u,): pvObj=%p expected %p!\n",
|
---|
239 | __LINE__, i, j, pszWithCtx, hLookup, pvObj, pvExpect);
|
---|
240 | g_cErrors++;
|
---|
241 | }
|
---|
242 | else if ( (fFlags & RTHANDLETABLE_FLAGS_CONTEXT)
|
---|
243 | && RTHandleTableLookupWithCtx(hHT, hLookup, &i))
|
---|
244 | {
|
---|
245 | RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, j=%d, RTHandleTableLookupWithCtx: succeeded with bad context\n",
|
---|
246 | __LINE__, i, j, pvObj, pvExpect);
|
---|
247 | g_cErrors++;
|
---|
248 | }
|
---|
249 | }
|
---|
250 |
|
---|
251 | /* free */
|
---|
252 | uint32_t hFree = ((i - uBase - cDelta) % (c - uBase)) + uBase;
|
---|
253 | void *pvExpect = (void *)((uintptr_t)&i + (uintptr_t)hFree * 4);
|
---|
254 | void *pvObj;
|
---|
255 | if (fFlags & RTHANDLETABLE_FLAGS_CONTEXT)
|
---|
256 | pvObj = RTHandleTableFreeWithCtx(hHT, hFree, NULL);
|
---|
257 | else
|
---|
258 | pvObj = RTHandleTableFree(hHT, hFree);
|
---|
259 | if (pvObj != pvExpect)
|
---|
260 | {
|
---|
261 | RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableFree%s: pvObj=%p expected %p!\n",
|
---|
262 | __LINE__, i, pszWithCtx, pvObj, pvExpect);
|
---|
263 | g_cErrors++;
|
---|
264 | }
|
---|
265 | else if (fFlags & RTHANDLETABLE_FLAGS_CONTEXT
|
---|
266 | ? RTHandleTableLookupWithCtx(hHT, hFree, NULL)
|
---|
267 | || RTHandleTableFreeWithCtx(hHT, hFree, NULL)
|
---|
268 | : RTHandleTableLookup(hHT, hFree)
|
---|
269 | || RTHandleTableFree(hHT, hFree))
|
---|
270 | {
|
---|
271 | RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableLookup/Free%s: succeeded after free\n",
|
---|
272 | __LINE__, i, pszWithCtx);
|
---|
273 | g_cErrors++;
|
---|
274 | }
|
---|
275 | }
|
---|
276 | if (!(i % (cUnitsPerDot * 2)))
|
---|
277 | {
|
---|
278 | RTPrintf(".");
|
---|
279 | RTStrmFlush(g_pStdOut);
|
---|
280 | }
|
---|
281 | }
|
---|
282 | RTPrintf("\n");
|
---|
283 |
|
---|
284 | /* finally, destroy the table (note that there are 128 entries in it). */
|
---|
285 | cRetainerCalls = 0;
|
---|
286 | uint32_t cDeleteCalls = 0;
|
---|
287 | rc = RTHandleTableDestroy(hHT,
|
---|
288 | fCallbacks ? tstHandleTableTest1Delete : NULL,
|
---|
289 | fCallbacks ? &cDeleteCalls : NULL);
|
---|
290 | if (RT_FAILURE(rc))
|
---|
291 | {
|
---|
292 | RTPrintf("tstHandleTable: FAILURE (%d) - RTHandleTableDestroy failed, %Rrc!\n", __LINE__, rc);
|
---|
293 | g_cErrors++;
|
---|
294 | }
|
---|
295 |
|
---|
296 | return 0;
|
---|
297 | }
|
---|
298 |
|
---|
299 | int main(int argc, char **argv)
|
---|
300 | {
|
---|
301 | /*
|
---|
302 | * Init the runtime and parse the arguments.
|
---|
303 | */
|
---|
304 | RTR3Init(false, 0);
|
---|
305 |
|
---|
306 | static RTOPTIONDEF const s_aOptions[] =
|
---|
307 | {
|
---|
308 | { "--base", 'b', RTGETOPT_REQ_UINT32 },
|
---|
309 | { "--max", 'm', RTGETOPT_REQ_UINT32 },
|
---|
310 | };
|
---|
311 |
|
---|
312 | uint32_t uBase = 0;
|
---|
313 | uint32_t cMax = 0;
|
---|
314 |
|
---|
315 | int ch;
|
---|
316 | int iArg = 1;
|
---|
317 | RTOPTIONUNION Value;
|
---|
318 | while ((ch = RTGetOpt(argc,argv, &s_aOptions[0], RT_ELEMENTS(s_aOptions), &iArg, &Value)))
|
---|
319 | switch (ch)
|
---|
320 | {
|
---|
321 | case 'b':
|
---|
322 | uBase = Value.u32;
|
---|
323 | break;
|
---|
324 |
|
---|
325 | case 'm':
|
---|
326 | cMax = Value.u32;
|
---|
327 | break;
|
---|
328 |
|
---|
329 | case '?':
|
---|
330 | case 'h':
|
---|
331 | RTPrintf("syntax: tstIntNet-1 [-pSt] [-d <secs>] [-f <file>] [-r <size>] [-s <size>]\n");
|
---|
332 | return 1;
|
---|
333 |
|
---|
334 | default:
|
---|
335 | if (RT_SUCCESS(ch))
|
---|
336 | RTPrintf("tstHandleTable: invalid argument (%#x): %s\n", ch, Value.psz);
|
---|
337 | else
|
---|
338 | RTPrintf("tstHandleTable: invalid argument: %Rrc - \n", ch, Value.pDef->pszLong);
|
---|
339 | return 1;
|
---|
340 | }
|
---|
341 | if (iArg < argc)
|
---|
342 | {
|
---|
343 | RTPrintf("tstHandleTable: invalid argument: %s\n", argv[iArg]);
|
---|
344 | return 1;
|
---|
345 | }
|
---|
346 |
|
---|
347 | /*
|
---|
348 | * Do a simple warmup / smoke test first.
|
---|
349 | */
|
---|
350 | tstHandleTableTest1(1, 65534, 128, 2048, false, 0);
|
---|
351 | tstHandleTableTest1(1, 65534, 128, 2048, false, RTHANDLETABLE_FLAGS_CONTEXT);
|
---|
352 | tstHandleTableTest1(1, 65534, 63, 2048, false, RTHANDLETABLE_FLAGS_LOCKED);
|
---|
353 | tstHandleTableTest1(1, 65534, 63, 2048, false, RTHANDLETABLE_FLAGS_CONTEXT | RTHANDLETABLE_FLAGS_LOCKED);
|
---|
354 | /* Test that the retain and delete functions work. */
|
---|
355 | tstHandleTableTest1(1, 1024, 256, 256, true, RTHANDLETABLE_FLAGS_LOCKED);
|
---|
356 | tstHandleTableTest1(1, 1024, 256, 256, true, RTHANDLETABLE_FLAGS_CONTEXT | RTHANDLETABLE_FLAGS_LOCKED);
|
---|
357 | /* check that the base works. */
|
---|
358 | tstHandleTableTest1(0x7ffff000, 65534, 4, 2048, false, RTHANDLETABLE_FLAGS_CONTEXT | RTHANDLETABLE_FLAGS_LOCKED);
|
---|
359 | tstHandleTableTest1(0xeffff000, 65534, 4, 2048, false, RTHANDLETABLE_FLAGS_CONTEXT | RTHANDLETABLE_FLAGS_LOCKED);
|
---|
360 | tstHandleTableTest1(0, 4097, 4, 256, false, RTHANDLETABLE_FLAGS_CONTEXT | RTHANDLETABLE_FLAGS_LOCKED);
|
---|
361 | tstHandleTableTest1(0, 1024, 4, 128, false, RTHANDLETABLE_FLAGS_CONTEXT | RTHANDLETABLE_FLAGS_LOCKED);
|
---|
362 | /* For testing 1st level expansion / reallocation. */
|
---|
363 | tstHandleTableTest1(1, 1024*1024*8, 3, 150000, false, 0);
|
---|
364 | tstHandleTableTest1(1, 1024*1024*8, 3, 150000, false, RTHANDLETABLE_FLAGS_CONTEXT);
|
---|
365 |
|
---|
366 | /*
|
---|
367 | * Threaded tests.
|
---|
368 | */
|
---|
369 | /** @todo threaded test for checking out the locking and expansion races. */
|
---|
370 |
|
---|
371 | /*
|
---|
372 | * Summary.
|
---|
373 | */
|
---|
374 | if (!g_cErrors)
|
---|
375 | RTPrintf("tstHandleTable: SUCCESS\n");
|
---|
376 | else
|
---|
377 | RTPrintf("tstHandleTable: FAILURE - %d errors\n", g_cErrors);
|
---|
378 |
|
---|
379 | return !!g_cErrors;
|
---|
380 | }
|
---|