VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/misc/handletable.h@ 18100

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

IPRT: Implemented the simple handle table variant too.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.0 KB
Line 
1/* $Id: handletable.h 10789 2008-07-21 17:22:32Z vboxsync $ */
2/** @file
3 * IPRT - Handle Tables, internal header.
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/*******************************************************************************
33* Defined Constants And Macros *
34*******************************************************************************/
35/** The number of entries in the 2nd level lookup table. */
36#define RTHT_LEVEL2_ENTRIES 2048
37
38/** The number of (max) 1st level entries requiring dynamic allocation of the
39 * 1st level table. If the max number is below this threshold, the 1st level
40 * table will be allocated as part of the handle table structure. */
41#define RTHT_LEVEL1_DYN_ALLOC_THRESHOLD 256
42
43/** Checks whether a object pointer is really a free entry or not. */
44#define RTHT_IS_FREE(pvObj) ( ((uintptr_t)(pvObj) & 3) == 3 )
45
46/** Sets RTHTENTRYFREE::iNext. */
47#define RTHT_SET_FREE_IDX(pFree, idx) \
48 do { \
49 (pFree)->iNext = ((uintptr_t)((uint32_t)(idx)) << 2) | 3U; \
50 } while (0)
51
52/** Gets the index part of RTHTENTRYFREE::iNext. */
53#define RTHT_GET_FREE_IDX(pFree) ( (uint32_t)((pFree)->iNext >> 2) )
54
55/** @def NIL_RTHT_INDEX
56 * The NIL handle index for use in the free list. (The difference between
57 * 32-bit and 64-bit hosts here comes down to the shifting performed for
58 * RTHTENTRYFREE::iNext.) */
59#if ARCH_BITS == 32
60# define NIL_RTHT_INDEX ( UINT32_C(0x3fffffff) )
61#elif ARCH_BITS >= 34
62# define NIL_RTHT_INDEX ( UINT32_C(0xffffffff) )
63#else
64# error "Missing or unsupported ARCH_BITS."
65#endif
66
67
68/*******************************************************************************
69* Structures and Typedefs *
70*******************************************************************************/
71
72/**
73 * Handle table entry, simple variant.
74 */
75typedef struct RTHTENTRY
76{
77 /** The object. */
78 void *pvObj;
79} RTHTENTRY;
80/** Pointer to a handle table entry, simple variant. */
81typedef RTHTENTRY *PRTHTENTRY;
82
83
84/**
85 * Handle table entry, context variant.
86 */
87typedef struct RTHTENTRYCTX
88{
89 /** The object. */
90 void *pvObj;
91 /** The context. */
92 void *pvCtx;
93} RTHTENTRYCTX;
94/** Pointer to a handle table entry, context variant. */
95typedef RTHTENTRYCTX *PRTHTENTRYCTX;
96
97
98/**
99 * Free handle table entry, shared by all variants.
100 */
101typedef struct RTHTENTRYFREE
102{
103 /** The index of the next handle, special format.
104 * In order to distinguish free and used handle table entries we exploit
105 * the heap alignment and use the lower two bits to do this. Used entries
106 * will have these bits set to 0, while free entries will have tem set
107 * to 3. Use the RTHT_GET_FREE_IDX and RTHT_SET_FREE_IDX macros to access
108 * this field. */
109 uintptr_t iNext;
110} RTHTENTRYFREE;
111/** Pointer to a free handle table entry. */
112typedef RTHTENTRYFREE *PRTHTENTRYFREE;
113
114AssertCompile(sizeof(RTHTENTRYFREE) <= sizeof(RTHTENTRY));
115AssertCompile(sizeof(RTHTENTRYFREE) <= sizeof(RTHTENTRYCTX));
116AssertCompileMemberOffset(RTHTENTRYFREE, iNext, 0);
117AssertCompileMemberOffset(RTHTENTRY, pvObj, 0);
118AssertCompileMemberOffset(RTHTENTRYCTX, pvObj, 0);
119
120
121/**
122 * Internal handle table structure.
123 */
124typedef struct RTHANDLETABLEINT
125{
126 /** Magic value (RTHANDLETABLE_MAGIC). */
127 uint32_t u32Magic;
128 /** The handle table flags specified to RTHandleTableCreateEx. */
129 uint32_t fFlags;
130 /** The base handle value (i.e. the first handle). */
131 uint32_t uBase;
132 /** The current number of handle table entries. */
133 uint32_t cCur;
134 /** The spinlock handle (NIL if RTHANDLETABLE_FLAGS_LOCKED wasn't used). */
135 RTSPINLOCK hSpinlock;
136 /** The level one lookup table. */
137 void **papvLevel1;
138 /** The retainer callback. Can be NULL. */
139 PFNRTHANDLETABLERETAIN pfnRetain;
140 /** The user argument to the retainer. */
141 void *pvRetainUser;
142 /** The max number of handles. */
143 uint32_t cMax;
144 /** The number of handles currently allocated. (for optimizing destruction) */
145 uint32_t cCurAllocated;
146 /** The current number of 1st level entries. */
147 uint32_t cLevel1;
148 /** Head of the list of free handle entires (index). */
149 uint32_t iFreeHead;
150 /** Tail of the list of free handle entires (index). */
151 uint32_t iFreeTail;
152} RTHANDLETABLEINT;
153/** Pointer to an handle table structure. */
154typedef RTHANDLETABLEINT *PRTHANDLETABLEINT;
155
156
157/**
158 * Looks up a simple index.
159 *
160 * @returns Pointer to the handle table entry on success, NULL on failure.
161 * @param pThis The handle table structure.
162 * @param i The index to look up.
163 */
164DECLINLINE(PRTHTENTRY) rtHandleTableLookupSimpleIdx(PRTHANDLETABLEINT pThis, uint32_t i)
165{
166 if (i < pThis->cCur)
167 {
168 PRTHTENTRY paTable = (PRTHTENTRY)pThis->papvLevel1[i / RTHT_LEVEL2_ENTRIES];
169 if (paTable)
170 return &paTable[i % RTHT_LEVEL2_ENTRIES];
171 }
172 return NULL;
173}
174
175
176/**
177 * Looks up a simple handle.
178 *
179 * @returns Pointer to the handle table entry on success, NULL on failure.
180 * @param pThis The handle table structure.
181 * @param h The handle to look up.
182 */
183DECLINLINE(PRTHTENTRY) rtHandleTableLookupSimple(PRTHANDLETABLEINT pThis, uint32_t h)
184{
185 return rtHandleTableLookupSimpleIdx(pThis, h - pThis->uBase);
186}
187
188
189/**
190 * Looks up a context index.
191 *
192 * @returns Pointer to the handle table entry on success, NULL on failure.
193 * @param pThis The handle table structure.
194 * @param i The index to look up.
195 */
196DECLINLINE(PRTHTENTRYCTX) rtHandleTableLookupWithCtxIdx(PRTHANDLETABLEINT pThis, uint32_t i)
197{
198 if (i < pThis->cCur)
199 {
200 PRTHTENTRYCTX paTable = (PRTHTENTRYCTX)pThis->papvLevel1[i / RTHT_LEVEL2_ENTRIES];
201 if (paTable)
202 return &paTable[i % RTHT_LEVEL2_ENTRIES];
203 }
204 return NULL;
205}
206
207
208/**
209 * Looks up a context handle.
210 *
211 * @returns Pointer to the handle table entry on success, NULL on failure.
212 * @param pThis The handle table structure.
213 * @param h The handle to look up.
214 */
215DECLINLINE(PRTHTENTRYCTX) rtHandleTableLookupWithCtx(PRTHANDLETABLEINT pThis, uint32_t h)
216{
217 return rtHandleTableLookupWithCtxIdx(pThis, h - pThis->uBase);
218}
219
220
221/**
222 * Locks the handle table.
223 *
224 * @param pThis The handle table structure.
225 * @param pTmp The spinlock temp variable.
226 */
227DECLINLINE(void) rtHandleTableLock(PRTHANDLETABLEINT pThis, PRTSPINLOCKTMP pTmp)
228{
229 if (pThis->hSpinlock != NIL_RTSPINLOCK)
230 {
231 RTSPINLOCKTMP const Tmp = RTSPINLOCKTMP_INITIALIZER;
232 *pTmp = Tmp;
233 RTSpinlockAcquire(pThis->hSpinlock, pTmp);
234 }
235}
236
237
238/**
239 * Locks the handle table.
240 *
241 * @param pThis The handle table structure.
242 * @param pTmp The spinlock temp variable.
243 */
244DECLINLINE(void) rtHandleTableUnlock(PRTHANDLETABLEINT pThis, PRTSPINLOCKTMP pTmp)
245{
246 if (pThis->hSpinlock != NIL_RTSPINLOCK)
247 RTSpinlockRelease(pThis->hSpinlock, pTmp);
248}
249
250
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