VirtualBox

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

Last change on this file since 10789 was 10789, checked in by vboxsync, 17 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.1 KB
Line 
1/* $Id: handletablesimple.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.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/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) RTHandleTableAlloc(RTHANDLETABLE hHandleTable, void *pvObj, 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)rtHandleTableLookupSimpleIdx(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 PRTHTENTRY pEntry = (PRTHTENTRY)pFree;
85 pEntry->pvObj = pvObj;
86 *ph = i + pThis->uBase;
87 rc = VINF_SUCCESS;
88 }
89 /*
90 * Must expand the handle table, unless it's full.
91 */
92 else if (pThis->cCur >= pThis->cMax)
93 {
94 rc = VERR_NO_MORE_HANDLES;
95 Assert(pThis->cCur == pThis->cCurAllocated);
96 }
97 else
98 {
99 /*
100 * Do we have to expand the 1st level table too?
101 */
102 uint32_t const iLevel1 = pThis->cCur / RTHT_LEVEL2_ENTRIES;
103 uint32_t cLevel1 = iLevel1 >= pThis->cLevel1
104 ? pThis->cLevel1 + PAGE_SIZE / sizeof(void *)
105 : 0;
106 if (cLevel1 > pThis->cMax / RTHT_LEVEL2_ENTRIES)
107 cLevel1 = pThis->cMax / RTHT_LEVEL2_ENTRIES;
108 Assert(!cLevel1 || pThis->cMax / RTHT_LEVEL2_ENTRIES >= RTHT_LEVEL1_DYN_ALLOC_THRESHOLD);
109
110 /* leave the lock (never do fancy stuff from behind a spinlock). */
111 rtHandleTableUnlock(pThis, &Tmp);
112
113 /*
114 * Do the allocation(s).
115 */
116 rc = VERR_TRY_AGAIN;
117 void **papvLevel1 = NULL;
118 if (cLevel1)
119 {
120 papvLevel1 = (void **)RTMemAlloc(sizeof(void *) * cLevel1);
121 if (!papvLevel1)
122 return VERR_NO_MEMORY;
123 }
124
125 PRTHTENTRY paTable = (PRTHTENTRY)RTMemAlloc(sizeof(*paTable) * RTHT_LEVEL2_ENTRIES);
126 if (!paTable)
127 {
128 RTMemFree(papvLevel1);
129 return VERR_NO_MEMORY;
130 }
131
132 /* re-enter the lock. */
133 rtHandleTableLock(pThis, &Tmp);
134
135 /*
136 * Insert the new bits, but be a bit careful as someone might have
137 * raced us expanding the table.
138 */
139 /* deal with the 1st level lookup expansion first */
140 if (cLevel1)
141 {
142 Assert(papvLevel1);
143 if (cLevel1 > pThis->cLevel1)
144 {
145 /* Replace the 1st level table. */
146 memcpy(papvLevel1, pThis->papvLevel1, sizeof(void *) * pThis->cLevel1);
147 memset(&papvLevel1[pThis->cLevel1], 0, sizeof(void *) * (cLevel1 - pThis->cLevel1));
148 pThis->cLevel1 = cLevel1;
149 void **papvTmp = pThis->papvLevel1;
150 pThis->papvLevel1 = papvLevel1;
151 papvLevel1 = papvTmp;
152 }
153
154 /* free the obsolete one (outside the lock of course) */
155 rtHandleTableUnlock(pThis, &Tmp);
156 RTMemFree(papvLevel1);
157 rtHandleTableLock(pThis, &Tmp);
158 }
159
160 /* insert the table we allocated */
161 if (pThis->cCur < pThis->cMax)
162 {
163 uint32_t iLevel1New = pThis->cCur / RTHT_LEVEL2_ENTRIES;
164 pThis->papvLevel1[iLevel1New] = paTable;
165
166 /* link all entries into a free list. */
167 Assert(!(pThis->cCur % RTHT_LEVEL2_ENTRIES));
168 for (uint32_t i = 0; i < RTHT_LEVEL2_ENTRIES - 1; i++)
169 RTHT_SET_FREE_IDX((PRTHTENTRYFREE)&paTable[i], i + 1 + pThis->cCur);
170 RTHT_SET_FREE_IDX((PRTHTENTRYFREE)&paTable[RTHT_LEVEL2_ENTRIES - 1], NIL_RTHT_INDEX);
171
172 /* join the free list with the other. */
173 if (pThis->iFreeTail == NIL_RTHT_INDEX)
174 pThis->iFreeHead = pThis->cCur;
175 else
176 {
177 PRTHTENTRYFREE pPrev = (PRTHTENTRYFREE)rtHandleTableLookupSimpleIdx(pThis, pThis->iFreeTail);
178 Assert(pPrev);
179 RTHT_SET_FREE_IDX(pPrev, pThis->cCur);
180 }
181 pThis->iFreeTail = pThis->cCur + RTHT_LEVEL2_ENTRIES - 1;
182
183 pThis->cCur += RTHT_LEVEL2_ENTRIES;
184 }
185 else
186 {
187 /* free the table (raced someone, and we lost). */
188 rtHandleTableUnlock(pThis, &Tmp);
189 RTMemFree(paTable);
190 rtHandleTableLock(pThis, &Tmp);
191 }
192
193 rc = VERR_TRY_AGAIN;
194 }
195 } while (rc == VERR_TRY_AGAIN);
196
197 rtHandleTableUnlock(pThis, &Tmp);
198
199 return rc;
200}
201
202
203RTDECL(void *) RTHandleTableLookup(RTHANDLETABLE hHandleTable, uint32_t h)
204{
205 /* validate the input */
206 PRTHANDLETABLEINT pThis = (PRTHANDLETABLEINT)hHandleTable;
207 AssertPtrReturn(pThis, NULL);
208 AssertReturn(pThis->u32Magic == RTHANDLETABLE_MAGIC, NULL);
209 AssertReturn(!(pThis->fFlags & RTHANDLETABLE_FLAGS_CONTEXT), NULL);
210
211 void *pvObj = NULL;
212
213 /* acquire the lock */
214 RTSPINLOCKTMP Tmp;
215 rtHandleTableLock(pThis, &Tmp);
216
217 /*
218 * Perform the lookup and retaining.
219 */
220 PRTHTENTRY pEntry = rtHandleTableLookupSimple(pThis, h);
221 if (pEntry)
222 {
223 pvObj = pEntry->pvObj;
224 if (!RTHT_IS_FREE(pvObj))
225 {
226 if (pThis->pfnRetain)
227 {
228 int rc = pThis->pfnRetain(hHandleTable, pEntry->pvObj, NULL, pThis->pvRetainUser);
229 if (RT_FAILURE(rc))
230 pvObj = NULL;
231 }
232 }
233 else
234 pvObj = NULL;
235 }
236
237 /* release the lock */
238 rtHandleTableUnlock(pThis, &Tmp);
239 return pvObj;
240}
241
242
243RTDECL(void *) RTHandleTableFree(RTHANDLETABLE hHandleTable, uint32_t h)
244{
245 /* validate the input */
246 PRTHANDLETABLEINT pThis = (PRTHANDLETABLEINT)hHandleTable;
247 AssertPtrReturn(pThis, NULL);
248 AssertReturn(pThis->u32Magic == RTHANDLETABLE_MAGIC, NULL);
249 AssertReturn(!(pThis->fFlags & RTHANDLETABLE_FLAGS_CONTEXT), NULL);
250
251 void *pvObj = NULL;
252
253 /* acquire the lock */
254 RTSPINLOCKTMP Tmp;
255 rtHandleTableLock(pThis, &Tmp);
256
257 /*
258 * Perform the lookup and retaining.
259 */
260 PRTHTENTRY pEntry = rtHandleTableLookupSimple(pThis, h);
261 if (pEntry)
262 {
263 pvObj = pEntry->pvObj;
264 if (!RTHT_IS_FREE(pvObj))
265 {
266 if (pThis->pfnRetain)
267 {
268 int rc = pThis->pfnRetain(hHandleTable, pEntry->pvObj, NULL, pThis->pvRetainUser);
269 if (RT_FAILURE(rc))
270 pvObj = NULL;
271 }
272
273 /*
274 * Link it into the free list.
275 */
276 if (pvObj)
277 {
278 PRTHTENTRYFREE pFree = (PRTHTENTRYFREE)pEntry;
279 RTHT_SET_FREE_IDX(pFree, NIL_RTHT_INDEX);
280
281 uint32_t const i = h - pThis->uBase;
282 if (pThis->iFreeTail == NIL_RTHT_INDEX)
283 pThis->iFreeHead = pThis->iFreeTail = i;
284 else
285 {
286 PRTHTENTRYFREE pPrev = (PRTHTENTRYFREE)rtHandleTableLookupSimpleIdx(pThis, pThis->iFreeTail);
287 Assert(pPrev);
288 RTHT_SET_FREE_IDX(pPrev, i);
289 pThis->iFreeTail = i;
290 }
291
292 Assert(pThis->cCurAllocated > 0);
293 pThis->cCurAllocated--;
294 }
295 }
296 else
297 pvObj = NULL;
298 }
299
300 /* release the lock */
301 rtHandleTableUnlock(pThis, &Tmp);
302 return pvObj;
303}
304
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