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