VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/misc/handletablectx.cpp@ 10789

Last change on this file since 10789 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: 10.5 KB
Line 
1/* $Id: handletablectx.cpp 10789 2008-07-21 17:22:32Z 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 <iprt/asm.h>
42#include "internal/magics.h"
43#include "handletable.h"
44
45
46RTDECL(int) RTHandleTableAllocWithCtx(RTHANDLETABLE hHandleTable, void *pvObj, void *pvCtx, uint32_t *ph)
47{
48 /* validate the input */
49 PRTHANDLETABLEINT pThis = (PRTHANDLETABLEINT)hHandleTable;
50 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
51 AssertReturn(pThis->u32Magic == RTHANDLETABLE_MAGIC, VERR_INVALID_HANDLE);
52 AssertReturn(pThis->fFlags & RTHANDLETABLE_FLAGS_CONTEXT, VERR_INVALID_FUNCTION);
53 AssertReturn(!RTHT_IS_FREE(pvObj), VERR_INVALID_PARAMETER);
54 AssertPtrReturn(ph, VERR_INVALID_POINTER);
55 *ph = pThis->uBase - 1;
56
57 /*
58 * Allocation loop.
59 */
60 RTSPINLOCKTMP Tmp;
61 rtHandleTableLock(pThis, &Tmp);
62
63 int rc;
64 do
65 {
66 /*
67 * Try grab a free entry from the head of the free list.
68 */
69 uint32_t i = pThis->iFreeHead;
70 if (i != NIL_RTHT_INDEX)
71 {
72 PRTHTENTRYFREE pFree = (PRTHTENTRYFREE)rtHandleTableLookupWithCtxIdx(pThis, i);
73 Assert(pFree);
74 if (i == pThis->iFreeTail)
75 pThis->iFreeTail = pThis->iFreeHead = NIL_RTHT_INDEX;
76 else
77 pThis->iFreeHead = RTHT_GET_FREE_IDX(pFree);
78 pThis->cCurAllocated++;
79 Assert(pThis->cCurAllocated <= pThis->cCur);
80
81 /*
82 * Setup the entry and return.
83 */
84 PRTHTENTRYCTX pEntry = (PRTHTENTRYCTX)pFree;
85 pEntry->pvObj = pvObj;
86 pEntry->pvCtx = pvCtx;
87 *ph = i + pThis->uBase;
88 rc = VINF_SUCCESS;
89 }
90 /*
91 * Must expand the handle table, unless it's full.
92 */
93 else if (pThis->cCur >= pThis->cMax)
94 {
95 rc = VERR_NO_MORE_HANDLES;
96 Assert(pThis->cCur == pThis->cCurAllocated);
97 }
98 else
99 {
100 /*
101 * Do we have to expand the 1st level table too?
102 */
103 uint32_t const iLevel1 = pThis->cCur / RTHT_LEVEL2_ENTRIES;
104 uint32_t cLevel1 = iLevel1 >= pThis->cLevel1
105 ? pThis->cLevel1 + PAGE_SIZE / sizeof(void *)
106 : 0;
107 if (cLevel1 > pThis->cMax / RTHT_LEVEL2_ENTRIES)
108 cLevel1 = pThis->cMax / RTHT_LEVEL2_ENTRIES;
109 Assert(!cLevel1 || pThis->cMax / RTHT_LEVEL2_ENTRIES >= RTHT_LEVEL1_DYN_ALLOC_THRESHOLD);
110
111 /* leave the lock (never do fancy stuff from behind a spinlock). */
112 rtHandleTableUnlock(pThis, &Tmp);
113
114 /*
115 * Do the allocation(s).
116 */
117 rc = VERR_TRY_AGAIN;
118 void **papvLevel1 = NULL;
119 if (cLevel1)
120 {
121 papvLevel1 = (void **)RTMemAlloc(sizeof(void *) * cLevel1);
122 if (!papvLevel1)
123 return VERR_NO_MEMORY;
124 }
125
126 PRTHTENTRYCTX paTable = (PRTHTENTRYCTX)RTMemAlloc(sizeof(*paTable) * RTHT_LEVEL2_ENTRIES);
127 if (!paTable)
128 {
129 RTMemFree(papvLevel1);
130 return VERR_NO_MEMORY;
131 }
132
133 /* re-enter the lock. */
134 rtHandleTableLock(pThis, &Tmp);
135
136 /*
137 * Insert the new bits, but be a bit careful as someone might have
138 * raced us expanding the table.
139 */
140 /* deal with the 1st level lookup expansion first */
141 if (cLevel1)
142 {
143 Assert(papvLevel1);
144 if (cLevel1 > pThis->cLevel1)
145 {
146 /* Replace the 1st level table. */
147 memcpy(papvLevel1, pThis->papvLevel1, sizeof(void *) * pThis->cLevel1);
148 memset(&papvLevel1[pThis->cLevel1], 0, sizeof(void *) * (cLevel1 - pThis->cLevel1));
149 pThis->cLevel1 = cLevel1;
150 void **papvTmp = pThis->papvLevel1;
151 pThis->papvLevel1 = papvLevel1;
152 papvLevel1 = papvTmp;
153 }
154
155 /* free the obsolete one (outside the lock of course) */
156 rtHandleTableUnlock(pThis, &Tmp);
157 RTMemFree(papvLevel1);
158 rtHandleTableLock(pThis, &Tmp);
159 }
160
161 /* insert the table we allocated */
162 if (pThis->cCur < pThis->cMax)
163 {
164 uint32_t iLevel1New = pThis->cCur / RTHT_LEVEL2_ENTRIES;
165 pThis->papvLevel1[iLevel1New] = paTable;
166
167 /* link all entries into a free list. */
168 Assert(!(pThis->cCur % RTHT_LEVEL2_ENTRIES));
169 for (uint32_t i = 0; i < RTHT_LEVEL2_ENTRIES - 1; i++)
170 {
171 RTHT_SET_FREE_IDX((PRTHTENTRYFREE)&paTable[i], i + 1 + pThis->cCur);
172 paTable[i].pvCtx = (void *)~(uintptr_t)7;
173 }
174 RTHT_SET_FREE_IDX((PRTHTENTRYFREE)&paTable[RTHT_LEVEL2_ENTRIES - 1], NIL_RTHT_INDEX);
175 paTable[RTHT_LEVEL2_ENTRIES - 1].pvCtx = (void *)~(uintptr_t)7;
176
177 /* join the free list with the other. */
178 if (pThis->iFreeTail == NIL_RTHT_INDEX)
179 pThis->iFreeHead = pThis->cCur;
180 else
181 {
182 PRTHTENTRYFREE pPrev = (PRTHTENTRYFREE)rtHandleTableLookupWithCtxIdx(pThis, pThis->iFreeTail);
183 Assert(pPrev);
184 RTHT_SET_FREE_IDX(pPrev, pThis->cCur);
185 }
186 pThis->iFreeTail = pThis->cCur + RTHT_LEVEL2_ENTRIES - 1;
187
188 pThis->cCur += RTHT_LEVEL2_ENTRIES;
189 }
190 else
191 {
192 /* free the table (raced someone, and we lost). */
193 rtHandleTableUnlock(pThis, &Tmp);
194 RTMemFree(paTable);
195 rtHandleTableLock(pThis, &Tmp);
196 }
197
198 rc = VERR_TRY_AGAIN;
199 }
200 } while (rc == VERR_TRY_AGAIN);
201
202 rtHandleTableUnlock(pThis, &Tmp);
203
204 return rc;
205}
206
207
208RTDECL(void *) RTHandleTableLookupWithCtx(RTHANDLETABLE hHandleTable, uint32_t h, void *pvCtx)
209{
210 /* validate the input */
211 PRTHANDLETABLEINT pThis = (PRTHANDLETABLEINT)hHandleTable;
212 AssertPtrReturn(pThis, NULL);
213 AssertReturn(pThis->u32Magic == RTHANDLETABLE_MAGIC, NULL);
214 AssertReturn(pThis->fFlags & RTHANDLETABLE_FLAGS_CONTEXT, NULL);
215
216 void *pvObj = NULL;
217
218 /* acquire the lock */
219 RTSPINLOCKTMP Tmp;
220 rtHandleTableLock(pThis, &Tmp);
221
222 /*
223 * Perform the lookup and retaining.
224 */
225 PRTHTENTRYCTX pEntry = rtHandleTableLookupWithCtx(pThis, h);
226 if (pEntry && pEntry->pvCtx == pvCtx)
227 {
228 pvObj = pEntry->pvObj;
229 if (!RTHT_IS_FREE(pvObj))
230 {
231 if (pThis->pfnRetain)
232 {
233 int rc = pThis->pfnRetain(hHandleTable, pEntry->pvObj, pvCtx, pThis->pvRetainUser);
234 if (RT_FAILURE(rc))
235 pvObj = NULL;
236 }
237 }
238 else
239 pvObj = NULL;
240 }
241
242 /* release the lock */
243 rtHandleTableUnlock(pThis, &Tmp);
244 return pvObj;
245}
246
247
248RTDECL(void *) RTHandleTableFreeWithCtx(RTHANDLETABLE hHandleTable, uint32_t h, void *pvCtx)
249{
250 /* validate the input */
251 PRTHANDLETABLEINT pThis = (PRTHANDLETABLEINT)hHandleTable;
252 AssertPtrReturn(pThis, NULL);
253 AssertReturn(pThis->u32Magic == RTHANDLETABLE_MAGIC, NULL);
254 AssertReturn(pThis->fFlags & RTHANDLETABLE_FLAGS_CONTEXT, NULL);
255
256 void *pvObj = NULL;
257
258 /* acquire the lock */
259 RTSPINLOCKTMP Tmp;
260 rtHandleTableLock(pThis, &Tmp);
261
262 /*
263 * Perform the lookup and retaining.
264 */
265 PRTHTENTRYCTX pEntry = rtHandleTableLookupWithCtx(pThis, h);
266 if (pEntry && pEntry->pvCtx == pvCtx)
267 {
268 pvObj = pEntry->pvObj;
269 if (!RTHT_IS_FREE(pvObj))
270 {
271 if (pThis->pfnRetain)
272 {
273 int rc = pThis->pfnRetain(hHandleTable, pEntry->pvObj, pvCtx, pThis->pvRetainUser);
274 if (RT_FAILURE(rc))
275 pvObj = NULL;
276 }
277
278 /*
279 * Link it into the free list.
280 */
281 if (pvObj)
282 {
283 pEntry->pvCtx = (void *)~(uintptr_t)7;
284
285 PRTHTENTRYFREE pFree = (PRTHTENTRYFREE)pEntry;
286 RTHT_SET_FREE_IDX(pFree, NIL_RTHT_INDEX);
287
288 uint32_t const i = h - pThis->uBase;
289 if (pThis->iFreeTail == NIL_RTHT_INDEX)
290 pThis->iFreeHead = pThis->iFreeTail = i;
291 else
292 {
293 PRTHTENTRYFREE pPrev = (PRTHTENTRYFREE)rtHandleTableLookupWithCtxIdx(pThis, pThis->iFreeTail);
294 Assert(pPrev);
295 RTHT_SET_FREE_IDX(pPrev, i);
296 pThis->iFreeTail = i;
297 }
298
299 Assert(pThis->cCurAllocated > 0);
300 pThis->cCurAllocated--;
301 }
302 }
303 else
304 pvObj = NULL;
305 }
306
307 /* release the lock */
308 rtHandleTableUnlock(pThis, &Tmp);
309 return pvObj;
310}
311
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