VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/misc/handletable.cpp@ 10768

Last change on this file since 10768 was 10768, checked in by vboxsync, 16 years ago

Some early commit of the handle table code (home -> work).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.8 KB
Line 
1/* $Id: handletable.cpp 10768 2008-07-21 02:16:19Z vboxsync $ */
2/** @file
3 * IPRT - 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.virtualbox.org. 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/mem.h>
36#include <iprt/spinlock.h>
37#include <iprt/err.h>
38#include <iprt/assert.h>
39#include <iprt/param.h>
40#include <iprt/string.h>
41#include "internal/magics.h"
42
43
44/*******************************************************************************
45* Defined Constants And Macros *
46*******************************************************************************/
47/** The number of entries in the 2nd level lookup table. */
48#define RTHT_LEVEL2_ENTRIES 2048
49
50/** The number of (max) 1st level entries requiring dynamic allocation of the
51 * 1st level table. If the max number is below this threshold, the 1st level
52 * table will be allocated as part of the handle table structure. */
53#define RTHT_LEVEL1_DYN_ALLOC_THRESHOLD 256
54
55/** Checks whether a object pointer is really a free entry or not. */
56#define RTHT_IS_FREE(pvObj) ( ((uintptr_t)(pvObj) & 3) == 3 )
57
58/** Sets RTHTENTRYFREE::iNext. */
59#define RTHT_SET_FREE_IDX(pFree, idx) \
60 do { \
61 (pFree)->iNext = ((uintptr_t)(idx) << 2) | 3; \
62 } while (0)
63
64/** Gets the index part of RTHTENTRYFREE::iNext. */
65#define RTHT_GET_FREE_IDX(pFree) ( (uint32_t)((pFree)->iNext >> 2) )
66
67/** @def NIL_RTHT_INDEX
68 * The NIL handle index for use in the free list. (The difference between
69 * 32-bit and 64-bit hosts here comes down to the shifting performed for
70 * RTHTENTRYFREE::iNext.) */
71#if ARCH_BITS == 32
72# define NIL_RTHT_INDEX ( UINT32_C(0x3fffffff) )
73#elif ARCH_BITS >= 34
74# define NIL_RTHT_INDEX ( UINT32_C(0xffffffff) )
75#else
76# error "Missing or unsupported ARCH_BITS."
77#endif
78
79
80/*******************************************************************************
81* Structures and Typedefs *
82*******************************************************************************/
83
84/**
85 * Handle table entry, simple variant.
86 */
87typedef struct RTHTENTRY
88{
89 /** The object. */
90 void *pvObj;
91} RTHTENTRY;
92/** Pointer to a handle table entry, simple variant. */
93typedef RTHTENTRY *PRTHTENTRY;
94
95
96/**
97 * Handle table entry, context variant.
98 */
99typedef struct RTHTENTRYCTX
100{
101 /** The context. */
102 void *pvCtx;
103 /** The object. */
104 void *pvObj;
105} RTHTENTRYCTX;
106/** Pointer to a handle table entry, context variant. */
107typedef RTHTENTRYCTX *PRTHTENTRYCTX;
108
109
110/**
111 * Free handle table entry, shared by all variants.
112 */
113typedef struct RTHTENTRYFREE
114{
115 /** The index of the next handle, special format.
116 * In order to distinguish free and used handle table entries we exploit
117 * the heap alignment and use the lower two bits to do this. Used entries
118 * will have these bits set to 0, while free entries will have tem set
119 * to 3. Use the RTHT_GET_FREE_IDX and RTHT_SET_FREE_IDX macros to access
120 * this field. */
121 uintptr_t iNext;
122} RTHTENTRYFREE;
123/** Pointer to a free handle table entry. */
124typedef RTHTENTRYFREE *PRTHTENTRYFREE;
125
126AssertCompile(sizeof(RTHTENTRYFREE) <= sizeof(RTHTENTRY));
127AssertCompile(sizeof(RTHTENTRYFREE) <= sizeof(RTHTENTRYCTX));
128
129
130/**
131 * Internal handle table structure.
132 */
133typedef struct RTHANDLETABLEINT
134{
135 /** Magic value (RTHANDLETABLE_MAGIC). */
136 uint32_t u32Magic;
137 /** The handle table flags specified to RTHandleTableCreateEx. */
138 uint32_t fFlags;
139 /** The base handle value (i.e. the first handle). */
140 uint32_t uBase;
141 /** The current number of handles.
142 * This indirectly gives the size of the first level lookup table. */
143 uint32_t cCur;
144 /** The spinlock handle (NIL if RTHANDLETABLE_FLAGS_LOCKED wasn't used). */
145 RTSPINLOCK hSpinlock;
146 /** The level one lookup table. */
147 void **papvLevel1;
148 /** The retainer callback. Can be NULL. */
149 PFNRTHANDLETABLERETAIN pfnRetain;
150 /** The user argument to the retainer. */
151 void *pvRetainUser;
152 /** The max number of handles. */
153 uint32_t cMax;
154 /** The number of handles currently allocated. (for optimizing destruction) */
155 uint32_t cCurAllocated;
156 /** The current number of 1st level entries. */
157 uint32_t cLevel1;
158 /** Head of the list of free handle entires (index). */
159 uint32_t iFreeHead;
160 /** Tail of the list of free handle entires (index). */
161 uint32_t iFreeTail;
162} RTHANDLETABLEINT;
163/** Pointer to an handle table structure. */
164typedef RTHANDLETABLEINT *PRTHANDLETABLEINT;
165
166
167
168/**
169 * Looks up a simple handle.
170 *
171 * @returns Pointer to the handle table entry on success, NULL on failure.
172 * @param pThis The handle table structure.
173 * @param h The handle to look up.
174 */
175DECLINLINE(PRTHTENTRY) rtHandleTableLookupSimple(PRTHANDLETABLEINT pThis, uint32_t h)
176{
177 uint32_t i = h - pThis->uBase;
178 if (i < pThis->cCur)
179 {
180 PRTHTENTRY paTable = (PRTHTENTRY)pThis->papvLevel1[i / RTHT_LEVEL2_ENTRIES];
181 if (paTable)
182 return &paTable[i % RTHT_LEVEL2_ENTRIES];
183 }
184
185 return NULL;
186}
187
188
189/**
190 * Looks up a context handle.
191 *
192 * @returns Pointer to the handle table entry on success, NULL on failure.
193 * @param pThis The handle table structure.
194 * @param h The handle to look up.
195 */
196DECLINLINE(PRTHTENTRYCTX) rtHandleTableLookupWithCtx(PRTHANDLETABLEINT pThis, uint32_t h)
197{
198 uint32_t i = h - pThis->uBase;
199 if (i < pThis->cCur)
200 {
201 PRTHTENTRYCTX paTable = (PRTHTENTRYCTX)pThis->papvLevel1[i / RTHT_LEVEL2_ENTRIES];
202 if (paTable)
203 return &paTable[i % RTHT_LEVEL2_ENTRIES];
204 }
205
206 return NULL;
207}
208
209
210DECLINLINE(void) rtHandleTableLock(PRTHANDLETABLEINT pThis, PRTSPINLOCKTMP pTmp)
211{
212 if (pThis->hSpinlock != NIL_RTSPINLOCK)
213 RTSpinlockRelease(pThis->hSpinlock, pTmp);
214}
215
216
217DECLINLINE(void) rtHandleTableUnlock(PRTHANDLETABLEINT pThis, PRTSPINLOCKTMP pTmp)
218{
219 if (pThis->hSpinlock != NIL_RTSPINLOCK)
220 RTSpinlockAcquire(pThis->hSpinlock, pTmp);
221}
222
223
224RTDECL(int) RTHandleTableCreateEx(PRTHANDLETABLE phHandleTable, uint32_t fFlags, uint32_t uBase, uint32_t cMax,
225 PFNRTHANDLETABLERETAIN pfnRetain, void *pvUser)
226{
227 return VERR_NOT_IMPLEMENTED;
228}
229
230
231RTDECL(int) RTHandleTableCreate(PRTHANDLETABLE phHandleTable)
232{
233 return RTHandleTableCreateEx(phHandleTable, RTHANDLETABLE_FLAGS_LOCKED, 1, 65534, NULL, NULL);
234}
235
236
237RTDECL(int) RTHandleTableDestroy(RTHANDLETABLE hHandleTable, PFNRTHANDLETABLEDELETE pfnDelete, void *pvUser)
238{
239 return -1;
240}
241
242
243/**
244 * Allocates a handle from the handle table.
245 *
246 * @returns IPRT status code, almost any.
247 * @retval VINF_SUCCESS on success.
248 * @retval VERR_NO_MEMORY if we failed to extend the handle table.
249 * @retval VERR_NO_MORE_HANDLES if we're out of handles.
250 *
251 * @param hHandleTable The handle to the handle table.
252 * @param pvObj The object to associate with the new handle.
253 * @param ph Where to return the handle on success.
254 *
255 * @remarks Do not call this if RTHANDLETABLE_FLAGS_CONTEXT was used during creation.
256 */
257RTDECL(int) RTHandleTableAlloc(RTHANDLETABLE hHandleTable, void *pvObj, uint32_t *ph);
258
259/**
260 * Looks up a handle.
261 *
262 * @returns The object pointer on success. NULL on failure.
263 *
264 * @param hHandleTable The handle to the handle table.
265 * @param h The handle to lookup.
266 *
267 * @remarks Do not call this if RTHANDLETABLE_FLAGS_CONTEXT was used during creation.
268 */
269RTDECL(void *) RTHandleTableLookup(RTHANDLETABLE hHandleTable, uint32_t h);
270
271/**
272 * Looks up and frees a handle.
273 *
274 * @returns The object pointer on success. NULL on failure.
275 *
276 * @param hHandleTable The handle to the handle table.
277 * @param h The handle to lookup.
278 *
279 * @remarks Do not call this if RTHANDLETABLE_FLAGS_CONTEXT was used during creation.
280 */
281RTDECL(void *) RTHandleTableFree(RTHANDLETABLE hHandleTable, uint32_t h);
282
283
284RTDECL(int) RTHandleTableAllocWithCtx(RTHANDLETABLE hHandleTable, void *pvObj, void *pvCtx, uint32_t *ph)
285{
286 /* validate the input */
287 PRTHANDLETABLEINT pThis = (PRTHANDLETABLEINT)hHandleTable;
288 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
289 AssertReturn(pThis->u32Magic == RTHANDLETABLE_MAGIC, VERR_INVALID_HANDLE);
290 AssertReturn(pThis->fFlags & RTHANDLETABLE_FLAGS_CONTEXT, VERR_INVALID_FUNCTION);
291 AssertReturn(RTHT_IS_FREE(pvObj), VERR_INVALID_PARAMETER);
292 AssertPtrReturn(ph, VERR_INVALID_POINTER);
293 *ph = pThis->uBase - 1;
294
295 /*
296 * Allocation loop.
297 */
298 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
299 rtHandleTableLock(pThis, &Tmp);
300
301 int rc;
302 do
303 {
304 /*
305 * Try grab a free entry from the head of the free list.
306 */
307 uint32_t i = pThis->iFreeHead;
308 if (i != NIL_RTHT_INDEX)
309 {
310 PRTHTENTRYFREE pFree = (PRTHTENTRYFREE)rtHandleTableLookupWithCtx(pThis, i + pThis->uBase);
311 Assert(pFree);
312 if (i == pThis->iFreeTail)
313 pThis->iFreeTail = pThis->iFreeHead = NIL_RTHT_INDEX;
314 else
315 pThis->iFreeHead = RTHT_GET_FREE_IDX(pFree);
316 pThis->cCurAllocated++;
317 Assert(pThis->cCurAllocated <= pThis->cCur);
318
319 /*
320 * Setup the entry and return.
321 */
322 PRTHTENTRYCTX pEntry = (PRTHTENTRYCTX)pFree;
323 pEntry->pvObj = pvObj;
324 pEntry->pvCtx = pvCtx;
325 *ph = i + pThis->uBase;
326 rc = VINF_SUCCESS;
327 }
328 /*
329 * Must expand the handle table, unless it's full.
330 */
331 else if (pThis->cCur >= pThis->cMax)
332 {
333 rc = VERR_NO_MORE_HANDLES;
334 Assert(pThis->cCur == pThis->cCurAllocated);
335 }
336 else
337 {
338 /*
339 * Do we have to expand the 1st level table too?
340 */
341 uint32_t const iLevel1 = pThis->cCur / RTHT_LEVEL2_ENTRIES;
342 uint32_t cLevel1 = iLevel1 + 1 >= pThis->cLevel1
343 ? pThis->cLevel1 + PAGE_SIZE / sizeof(void *)
344 : 0;
345 if (cLevel1 > pThis->cMax / RTHT_LEVEL2_ENTRIES)
346 cLevel1 = pThis->cMax / RTHT_LEVEL2_ENTRIES;
347
348 /* leave the lock (never do fancy stuff from behind a spinlock). */
349 rtHandleTableUnlock(pThis, &Tmp);
350
351 /*
352 * Do the allocation(s).
353 */
354 rc = VERR_TRY_AGAIN;
355 void **papvLevel1 = NULL;
356 if (cLevel1)
357 {
358 papvLevel1 = (void **)RTMemAlloc(sizeof(void *) * cLevel1);
359 if (!papvLevel1)
360 return VERR_NO_MEMORY;
361 }
362
363 PRTHTENTRYCTX paTable = (PRTHTENTRYCTX)RTMemAlloc(sizeof(*paTable) * RTHT_LEVEL2_ENTRIES);
364 if (!paTable)
365 {
366 RTMemFree(papvLevel1);
367 return VERR_NO_MEMORY;
368 }
369
370 /* re-enter the lock. */
371 rtHandleTableLock(pThis, &Tmp);
372
373 /*
374 * Insert the new bits, but be a bit careful as someone might have
375 * raced us expanding the table.
376 */
377
378 if (cLevel1)
379 {
380 if (cLevel1 > pThis->cLevel1)
381 {
382 /* Replace the 1st level table. */
383 memcpy(papvLevel1, pThis->papvLevel1, sizeof(void *) * pThis->cLevel1);
384 memset(&papvLevel1[pThis->cLevel1], 0, sizeof(void *) * (cLevel1 - pThis->cLevel1));
385 void **papvTmp = pThis->papvLevel1;
386 pThis->papvLevel1 = papvLevel1;
387 papvLevel1 = papvTmp;
388 }
389
390 /* free the obsolete one (outside the lock of course) */
391 rtHandleTableLock(pThis, &Tmp);
392 RTMemFree(papvLevel1);
393 rtHandleTableUnlock(pThis, &Tmp);
394 }
395
396 /* insert the table we allocated */
397 if (pThis->cCur < pThis->cMax)
398 {
399 uint32_t iLevel1New = pThis->cCur / RTHT_LEVEL2_ENTRIES;
400 pThis->papvLevel1[iLevel1New] = paTable;
401
402 /* link all entries into a free list. */
403 Assert(!(pThis->cCur % RTHT_LEVEL2_ENTRIES));
404 for (uint32_t i = 0; i < RTHT_LEVEL2_ENTRIES - 1; i++)
405 {
406 RTHT_SET_FREE_IDX((PRTHTENTRYFREE)&paTable[i], i + 1);
407 paTable[i].pvCtx = (void *)~(uintptr_t)7;
408 }
409 RTHT_SET_FREE_IDX((PRTHTENTRYFREE)&paTable[RTHT_LEVEL2_ENTRIES - 1], NIL_RTHT_INDEX);
410 paTable[RTHT_LEVEL2_ENTRIES - 1].pvCtx = (void *)~(uintptr_t)7;
411
412 /* join the free list with the other. */
413 if (pThis->iFreeTail == NIL_RTHT_INDEX)
414 pThis->iFreeHead = pThis->cCur;
415 else
416 {
417 PRTHTENTRYFREE pPrev = (PRTHTENTRYFREE)rtHandleTableLookupWithCtx(pThis, pThis->iFreeTail);
418 Assert(pPrev);
419 RTHT_SET_FREE_IDX(pPrev, pThis->cCur);
420 }
421 pThis->iFreeTail = pThis->cCur + RTHT_LEVEL2_ENTRIES - 1;
422
423 pThis->cCur += RTHT_LEVEL2_ENTRIES;
424 }
425 else
426 {
427 /* free the table (raced someone, and we lost). */
428 rtHandleTableLock(pThis, &Tmp);
429 RTMemFree(paTable);
430 rtHandleTableUnlock(pThis, &Tmp);
431 }
432
433 rc = VERR_TRY_AGAIN;
434 }
435 } while (rc == VERR_TRY_AGAIN);
436
437 rtHandleTableUnlock(pThis, &Tmp);
438
439 return rc;
440}
441
442
443RTDECL(void *) RTHandleTableLookupWithCtx(RTHANDLETABLE hHandleTable, uint32_t h, void *pvCtx)
444{
445 /* validate the input */
446 PRTHANDLETABLEINT pThis = (PRTHANDLETABLEINT)hHandleTable;
447 AssertPtrReturn(pThis, NULL);
448 AssertReturn(pThis->u32Magic == RTHANDLETABLE_MAGIC, NULL);
449 AssertReturn(pThis->fFlags & RTHANDLETABLE_FLAGS_CONTEXT, NULL);
450
451 void *pvObj = NULL;
452
453 /* acquire the lock */
454 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
455 rtHandleTableLock(pThis, &Tmp);
456
457 /*
458 * Perform the lookup and retaining.
459 */
460 PRTHTENTRYCTX pEntry = rtHandleTableLookupWithCtx(pThis, h);
461 if (pEntry && pEntry->pvCtx == pvCtx)
462 {
463 pvObj = pEntry->pvObj;
464 if (!RTHT_IS_FREE(pvObj))
465 {
466 if (pThis->pfnRetain)
467 {
468 int rc = pThis->pfnRetain(hHandleTable, pEntry->pvObj, pvCtx, pThis->pvRetainUser);
469 if (RT_FAILURE(rc))
470 pvObj = NULL;
471 }
472 }
473 else
474 pvObj = NULL;
475 }
476
477 /* release the lock */
478 rtHandleTableUnlock(pThis, &Tmp);
479 return pvObj;
480}
481
482
483RTDECL(void *) RTHandleTableFreeWithCtx(RTHANDLETABLE hHandleTable, uint32_t h, void *pvCtx)
484{
485 /* validate the input */
486 PRTHANDLETABLEINT pThis = (PRTHANDLETABLEINT)hHandleTable;
487 AssertPtrReturn(pThis, NULL);
488 AssertReturn(pThis->u32Magic == RTHANDLETABLE_MAGIC, NULL);
489 AssertReturn(pThis->fFlags & RTHANDLETABLE_FLAGS_CONTEXT, NULL);
490
491 void *pvObj = NULL;
492
493 /* acquire the lock */
494 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
495 rtHandleTableLock(pThis, &Tmp);
496
497 /*
498 * Perform the lookup and retaining.
499 */
500 PRTHTENTRYCTX pEntry = rtHandleTableLookupWithCtx(pThis, h);
501 if (pEntry && pEntry->pvCtx == pvCtx)
502 {
503 pvObj = pEntry->pvObj;
504 if (!RTHT_IS_FREE(pvObj))
505 {
506 if (pThis->pfnRetain)
507 {
508 int rc = pThis->pfnRetain(hHandleTable, pEntry->pvObj, pvCtx, pThis->pvRetainUser);
509 if (RT_FAILURE(rc))
510 pvObj = NULL;
511 }
512
513 /*
514 * Link it into the free list.
515 */
516 if (pvObj)
517 {
518 pEntry->pvCtx = (void *)~(uintptr_t)7;
519
520 PRTHTENTRYFREE pFree = (PRTHTENTRYFREE)pEntry;
521 RTHT_SET_FREE_IDX(pFree, NIL_RTHT_INDEX);
522
523 uint32_t const i = h - pThis->uBase;
524 if (pThis->iFreeTail == NIL_RTHT_INDEX)
525 pThis->iFreeHead = pThis->iFreeTail = i;
526 else
527 {
528 PRTHTENTRYFREE pPrev = (PRTHTENTRYFREE)rtHandleTableLookupWithCtx(pThis, pThis->iFreeTail + pThis->uBase);
529 Assert(pPrev);
530 RTHT_SET_FREE_IDX(pPrev, i);
531 pThis->iFreeTail = i;
532 }
533
534 Assert(pThis->cCurAllocated > 0);
535 pThis->cCurAllocated--;
536 }
537 }
538 else
539 pvObj = NULL;
540 }
541
542 /* release the lock */
543 rtHandleTableUnlock(pThis, &Tmp);
544 return pvObj;
545}
546
Note: See TracBrowser for help on using the repository browser.

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