VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstHandleTable.cpp@ 10788

Last change on this file since 10788 was 10788, checked in by vboxsync, 17 years ago

IPRT: Implemented the context variant of the handle table.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 12.0 KB
Line 
1/* $Id: tstHandleTable.cpp 10788 2008-07-21 16:44:33Z 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*******************************************************************************/
44static unsigned g_cErrors;
45
46static 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
52static 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
59static int tstHandleTableTest1(uint32_t fFlags, uint32_t uBase, uint32_t cMax, uint32_t cDelta, uint32_t cUnitsPerDot, bool fCallbacks)
60{
61 int rc;
62 uint32_t cRetainerCalls = 0;
63
64 RTPrintf("tstHandleTable: TESTING RTHandleTableCreateEx(, 0");
65 fFlags |= RTHANDLETABLE_FLAGS_CONTEXT;
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 RTHandleTableAllocWithCtx.."); RTStrmFlush(g_pStdOut);
82 uint32_t i = uBase;
83 for (;; i++)
84 {
85 uint32_t h;
86 rc = RTHandleTableAllocWithCtx(hHT, (void *)((uintptr_t)&i + (uintptr_t)i * 4), NULL, &h);
87 if (RT_SUCCESS(rc))
88 {
89 if (h != i)
90 {
91 RTPrintf("\ntstHandleTable: FAILURE (%d) - h=%d, expected %d!\n", __LINE__, h, i);
92 g_cErrors++;
93 }
94 }
95 else if (rc == VERR_NO_MORE_HANDLES)
96 {
97 if (i < cMax)
98 {
99 RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, expected > 65534!\n", __LINE__, i);
100 g_cErrors++;
101 }
102 break;
103 }
104 else
105 {
106 RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, rc=%Rrc!\n", __LINE__, i, rc);
107 g_cErrors++;
108 }
109 if (!(i % cUnitsPerDot))
110 {
111 RTPrintf(".");
112 RTStrmFlush(g_pStdOut);
113 }
114 }
115 uint32_t const c = i;
116 RTPrintf(" c=%#x\n", c);
117 if (fCallbacks && cRetainerCalls != 0)
118 {
119 RTPrintf("tstHandleTable: FAILURE (%d) - cRetainerCalls=%#x expected 0!\n", __LINE__, i, cRetainerCalls);
120 g_cErrors++;
121 }
122
123 /* look up all the entries */
124 RTPrintf("tstHandleTable: TESTING RTHandleTableLookupWithCtx.."); RTStrmFlush(g_pStdOut);
125 cRetainerCalls = 0;
126 for (i = uBase; i < c; i++)
127 {
128 void *pvExpect = (void *)((uintptr_t)&i + (uintptr_t)i * 4);
129 void *pvObj = RTHandleTableLookupWithCtx(hHT, i, NULL);
130 if (!pvObj)
131 {
132 RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableLookupWithCtx failed!\n", __LINE__, i);
133 g_cErrors++;
134 }
135 else if (pvObj != pvExpect)
136 {
137 RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, pvObj=%p expected %p\n", __LINE__, i, pvObj, pvExpect);
138 g_cErrors++;
139 }
140 if (!(i % cUnitsPerDot))
141 {
142 RTPrintf(".");
143 RTStrmFlush(g_pStdOut);
144 }
145 }
146 RTPrintf("\n");
147 if (fCallbacks && cRetainerCalls != c - uBase)
148 {
149 RTPrintf("tstHandleTable: FAILURE (%d) - cRetainerCalls=%#x expected %#x!\n", __LINE__, cRetainerCalls, c - uBase);
150 g_cErrors++;
151 }
152
153 /* remove all the entries (in order) */
154 RTPrintf("tstHandleTable: TESTING RTHandleTableFreeWithCtx.."); RTStrmFlush(g_pStdOut);
155 cRetainerCalls = 0;
156 for (i = 1; i < c; i++)
157 {
158 void *pvExpect = (void *)((uintptr_t)&i + (uintptr_t)i * 4);
159 void *pvObj = RTHandleTableFreeWithCtx(hHT, i, NULL);
160 if (!pvObj)
161 {
162 RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableLookupWithCtx failed!\n", __LINE__, i);
163 g_cErrors++;
164 }
165 else if (pvObj != pvExpect)
166 {
167 RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, pvObj=%p expected %p\n", __LINE__, i, pvObj, pvExpect);
168 g_cErrors++;
169 }
170 else if (RTHandleTableLookupWithCtx(hHT, i, NULL))
171 {
172 RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableLookupWithCtx succeeded after free!\n", __LINE__, i);
173 g_cErrors++;
174 }
175 if (!(i % cUnitsPerDot))
176 {
177 RTPrintf(".");
178 RTStrmFlush(g_pStdOut);
179 }
180 }
181 RTPrintf("\n");
182 if (fCallbacks && cRetainerCalls != c - uBase)
183 {
184 RTPrintf("tstHandleTable: FAILURE (%d) - cRetainerCalls=%#x expected %#x!\n", __LINE__, cRetainerCalls, c - uBase);
185 g_cErrors++;
186 }
187
188 /* do a mix of alloc, lookup and free where there is a constant of cDelta handles in the table. */
189 RTPrintf("tstHandleTable: TESTING Alloc,Lookup,Free mix [cDelta=%#x]..", cDelta); RTStrmFlush(g_pStdOut);
190 for (i = 1; i < c * 2; i++)
191 {
192 /* alloc */
193 uint32_t hExpect = ((i - 1) % (c - 1)) + 1;
194 uint32_t h;
195 rc = RTHandleTableAllocWithCtx(hHT, (void *)((uintptr_t)&i + (uintptr_t)hExpect * 4), NULL, &h);
196 if (RT_FAILURE(rc))
197 {
198 RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableAllocWithCtx: rc=%Rrc!\n", __LINE__, i, rc);
199 g_cErrors++;
200 }
201 else if (h != hExpect)
202 {
203 RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableAllocWithCtx: rc=%Rrc!\n", __LINE__, i, rc);
204 g_cErrors++;
205 }
206
207 if (i > cDelta)
208 {
209 /* lookup */
210 for (uint32_t j = i - cDelta; j <= i; j++)
211 {
212 uint32_t hLookup = ((j - 1) % (c - 1)) + 1;
213 void *pvExpect = (void *)((uintptr_t)&i + (uintptr_t)hLookup * 4);
214 void *pvObj = RTHandleTableLookupWithCtx(hHT, hLookup, NULL);
215 if (pvObj != pvExpect)
216 {
217 RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, j=%d, RTHandleTableLookupWithCtx(,%u,): pvObj=%p expected %p!\n",
218 __LINE__, i, j, hLookup, pvObj, pvExpect);
219 g_cErrors++;
220 }
221 else if (RTHandleTableLookupWithCtx(hHT, hLookup, &i))
222 {
223 RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, j=%d, RTHandleTableLookupWithCtx: succeeded with bad context\n",
224 __LINE__, i, j, pvObj, pvExpect);
225 g_cErrors++;
226 }
227 }
228
229 /* free */
230 uint32_t hFree = ((i - 1 - cDelta) % (c - 1)) + 1;
231 void *pvExpect = (void *)((uintptr_t)&i + (uintptr_t)hFree * 4);
232 void *pvObj = RTHandleTableFreeWithCtx(hHT, hFree, NULL);
233 if (pvObj != pvExpect)
234 {
235 RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableFreeWithCtx: pvObj=%p expected %p!\n",
236 __LINE__, i, pvObj, pvExpect);
237 g_cErrors++;
238 }
239 else if ( RTHandleTableLookupWithCtx(hHT, hFree, NULL)
240 || RTHandleTableFreeWithCtx(hHT, hFree, NULL))
241 {
242 RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableLookup/FreeWithCtx: succeeded after free\n",
243 __LINE__, i);
244 g_cErrors++;
245 }
246 }
247 if (!(i % (cUnitsPerDot * 2)))
248 {
249 RTPrintf(".");
250 RTStrmFlush(g_pStdOut);
251 }
252 }
253 RTPrintf("\n");
254
255 /* finally, destroy the table (note that there are 128 entries in it). */
256 cRetainerCalls = 0;
257 uint32_t cDeleteCalls = 0;
258 rc = RTHandleTableDestroy(hHT,
259 fCallbacks ? tstHandleTableTest1Delete : NULL,
260 fCallbacks ? &cDeleteCalls : NULL);
261 if (RT_FAILURE(rc))
262 {
263 RTPrintf("tstHandleTable: FAILURE (%d) - RTHandleTableDestroy failed, %Rrc!\n", __LINE__, rc);
264 g_cErrors++;
265 }
266
267 return 0;
268}
269
270int main(int argc, char **argv)
271{
272 /*
273 * Init the runtime and parse the arguments.
274 */
275 RTR3Init(false, 0);
276
277 static RTOPTIONDEF const s_aOptions[] =
278 {
279 { "--base", 'b', RTGETOPT_REQ_UINT32 },
280 { "--max", 'm', RTGETOPT_REQ_UINT32 },
281 };
282
283 uint32_t uBase = 0;
284 uint32_t cMax = 0;
285
286 int ch;
287 int iArg = 1;
288 RTOPTIONUNION Value;
289 while ((ch = RTGetOpt(argc,argv, &s_aOptions[0], RT_ELEMENTS(s_aOptions), &iArg, &Value)))
290 switch (ch)
291 {
292 case 'b':
293 uBase = Value.u32;
294 break;
295
296 case 'm':
297 cMax = Value.u32;
298 break;
299
300 case '?':
301 case 'h':
302 RTPrintf("syntax: tstIntNet-1 [-pSt] [-d <secs>] [-f <file>] [-r <size>] [-s <size>]\n");
303 return 1;
304
305 default:
306 if (RT_SUCCESS(ch))
307 RTPrintf("tstHandleTable: invalid argument (%#x): %s\n", ch, Value.psz);
308 else
309 RTPrintf("tstHandleTable: invalid argument: %Rrc - \n", ch, Value.pDef->pszLong);
310 return 1;
311 }
312 if (iArg < argc)
313 {
314 RTPrintf("tstHandleTable: invalid argument: %s\n", argv[iArg]);
315 return 1;
316 }
317
318 /*
319 * Do a simple warmup / smoke test first.
320 */
321 /* these two are for the default case. */
322 tstHandleTableTest1(0, 1, 65534, 128, 2048, false);
323 tstHandleTableTest1(RTHANDLETABLE_FLAGS_LOCKED, 1, 65534, 63, 2048, false);
324 /* Test that the retain and delete functions work. */
325 tstHandleTableTest1(RTHANDLETABLE_FLAGS_LOCKED, 1, 1024, 256, 256, true);
326 /* For testing 1st level expansion / reallocation. */
327 tstHandleTableTest1(0, 1, 1024*1024*8, 3, 150000, false);
328
329 /*
330 * Threaded tests.
331 */
332
333
334 /*
335 * Summary.
336 */
337 if (!g_cErrors)
338 RTPrintf("tstHandleTable: SUCCESS\n");
339 else
340 RTPrintf("tstHandleTable: FAILURE - %d errors\n", g_cErrors);
341
342 return !!g_cErrors;
343}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette