1 | /* $Id: handletable.cpp 33540 2010-10-28 09:27:05Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Handle Tables.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008 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 | * Header Files *
|
---|
30 | *******************************************************************************/
|
---|
31 | #include <iprt/handletable.h>
|
---|
32 | #include "internal/iprt.h"
|
---|
33 |
|
---|
34 | #include <iprt/mem.h>
|
---|
35 | #include <iprt/spinlock.h>
|
---|
36 | #include <iprt/err.h>
|
---|
37 | #include <iprt/assert.h>
|
---|
38 | #include <iprt/param.h>
|
---|
39 | #include <iprt/string.h>
|
---|
40 | #include <iprt/asm.h>
|
---|
41 | #include "internal/magics.h"
|
---|
42 | #include "handletable.h"
|
---|
43 |
|
---|
44 |
|
---|
45 |
|
---|
46 | RTDECL(int) RTHandleTableCreateEx(PRTHANDLETABLE phHandleTable, uint32_t fFlags, uint32_t uBase, uint32_t cMax,
|
---|
47 | PFNRTHANDLETABLERETAIN pfnRetain, void *pvUser)
|
---|
48 | {
|
---|
49 | PRTHANDLETABLEINT pThis;
|
---|
50 | uint32_t cLevel1;
|
---|
51 | size_t cb;
|
---|
52 |
|
---|
53 | /*
|
---|
54 | * Validate input.
|
---|
55 | */
|
---|
56 | AssertPtrReturn(phHandleTable, VERR_INVALID_POINTER);
|
---|
57 | *phHandleTable = NIL_RTHANDLETABLE;
|
---|
58 | AssertPtrNullReturn(pfnRetain, VERR_INVALID_POINTER);
|
---|
59 | AssertReturn(!(fFlags & ~RTHANDLETABLE_FLAGS_MASK), VERR_INVALID_PARAMETER);
|
---|
60 | AssertReturn(cMax > 0, VERR_INVALID_PARAMETER);
|
---|
61 | AssertReturn(UINT32_MAX - cMax >= uBase, VERR_INVALID_PARAMETER);
|
---|
62 |
|
---|
63 | /*
|
---|
64 | * Adjust the cMax value so it is a multiple of the 2nd level tables.
|
---|
65 | */
|
---|
66 | if (cMax >= UINT32_MAX - RTHT_LEVEL2_ENTRIES)
|
---|
67 | cMax = UINT32_MAX - RTHT_LEVEL2_ENTRIES + 1;
|
---|
68 | cMax = ((cMax + RTHT_LEVEL2_ENTRIES - 1) / RTHT_LEVEL2_ENTRIES) * RTHT_LEVEL2_ENTRIES;
|
---|
69 |
|
---|
70 | cLevel1 = cMax / RTHT_LEVEL2_ENTRIES;
|
---|
71 | Assert(cLevel1 * RTHT_LEVEL2_ENTRIES == cMax);
|
---|
72 |
|
---|
73 | /*
|
---|
74 | * Allocate the structure, include the 1st level lookup table
|
---|
75 | * if it's below the threshold size.
|
---|
76 | */
|
---|
77 | cb = sizeof(RTHANDLETABLEINT);
|
---|
78 | if (cLevel1 < RTHT_LEVEL1_DYN_ALLOC_THRESHOLD)
|
---|
79 | cb = RT_ALIGN(cb, sizeof(void *)) + cLevel1 * sizeof(void *);
|
---|
80 | pThis = (PRTHANDLETABLEINT)RTMemAllocZ(cb);
|
---|
81 | if (!pThis)
|
---|
82 | return VERR_NO_MEMORY;
|
---|
83 |
|
---|
84 | /*
|
---|
85 | * Initialize it.
|
---|
86 | */
|
---|
87 | pThis->u32Magic = RTHANDLETABLE_MAGIC;
|
---|
88 | pThis->fFlags = fFlags;
|
---|
89 | pThis->uBase = uBase;
|
---|
90 | pThis->cCur = 0;
|
---|
91 | pThis->hSpinlock = NIL_RTSPINLOCK;
|
---|
92 | if (cLevel1 < RTHT_LEVEL1_DYN_ALLOC_THRESHOLD)
|
---|
93 | pThis->papvLevel1 = (void **)((uint8_t *)pThis + RT_ALIGN(sizeof(*pThis), sizeof(void *)));
|
---|
94 | else
|
---|
95 | pThis->papvLevel1 = NULL;
|
---|
96 | pThis->pfnRetain = pfnRetain;
|
---|
97 | pThis->pvRetainUser = pvUser;
|
---|
98 | pThis->cMax = cMax;
|
---|
99 | pThis->cCurAllocated = 0;
|
---|
100 | pThis->cLevel1 = cLevel1 < RTHT_LEVEL1_DYN_ALLOC_THRESHOLD ? cLevel1 : 0;
|
---|
101 | pThis->iFreeHead = NIL_RTHT_INDEX;
|
---|
102 | pThis->iFreeTail = NIL_RTHT_INDEX;
|
---|
103 | if (fFlags & RTHANDLETABLE_FLAGS_LOCKED)
|
---|
104 | {
|
---|
105 | int rc = RTSpinlockCreate(&pThis->hSpinlock);
|
---|
106 | if (RT_FAILURE(rc))
|
---|
107 | {
|
---|
108 | RTMemFree(pThis);
|
---|
109 | return rc;
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | *phHandleTable = pThis;
|
---|
114 | return VINF_SUCCESS;
|
---|
115 | }
|
---|
116 | RT_EXPORT_SYMBOL(RTHandleTableCreateEx);
|
---|
117 |
|
---|
118 |
|
---|
119 | RTDECL(int) RTHandleTableCreate(PRTHANDLETABLE phHandleTable)
|
---|
120 | {
|
---|
121 | return RTHandleTableCreateEx(phHandleTable, RTHANDLETABLE_FLAGS_LOCKED, 1, 65534, NULL, NULL);
|
---|
122 | }
|
---|
123 | RT_EXPORT_SYMBOL(RTHandleTableCreate);
|
---|
124 |
|
---|
125 |
|
---|
126 | RTDECL(int) RTHandleTableDestroy(RTHANDLETABLE hHandleTable, PFNRTHANDLETABLEDELETE pfnDelete, void *pvUser)
|
---|
127 | {
|
---|
128 | PRTHANDLETABLEINT pThis;
|
---|
129 | RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
|
---|
130 | uint32_t i1;
|
---|
131 | uint32_t i;
|
---|
132 |
|
---|
133 | /*
|
---|
134 | * Validate input, quietly ignore the NIL handle.
|
---|
135 | */
|
---|
136 | if (hHandleTable == NIL_RTHANDLETABLE)
|
---|
137 | return VINF_SUCCESS;
|
---|
138 | pThis = (PRTHANDLETABLEINT)hHandleTable;
|
---|
139 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
140 | AssertReturn(pThis->u32Magic == RTHANDLETABLE_MAGIC, VERR_INVALID_HANDLE);
|
---|
141 | AssertPtrNullReturn(pfnDelete, VERR_INVALID_POINTER);
|
---|
142 |
|
---|
143 | /*
|
---|
144 | * Mark the thing as invalid / deleted.
|
---|
145 | * Then kill the lock.
|
---|
146 | */
|
---|
147 | rtHandleTableLock(pThis, &Tmp);
|
---|
148 | ASMAtomicWriteU32(&pThis->u32Magic, ~RTHANDLETABLE_MAGIC);
|
---|
149 | rtHandleTableUnlock(pThis, &Tmp);
|
---|
150 |
|
---|
151 | if (pThis->hSpinlock != NIL_RTSPINLOCK)
|
---|
152 | {
|
---|
153 | rtHandleTableLock(pThis, &Tmp);
|
---|
154 | rtHandleTableUnlock(pThis, &Tmp);
|
---|
155 |
|
---|
156 | RTSpinlockDestroy(pThis->hSpinlock);
|
---|
157 | pThis->hSpinlock = NIL_RTSPINLOCK;
|
---|
158 | }
|
---|
159 |
|
---|
160 | if (pfnDelete)
|
---|
161 | {
|
---|
162 | /*
|
---|
163 | * Walk all the tables looking for used handles.
|
---|
164 | */
|
---|
165 | uint32_t cLeft = pThis->cCurAllocated;
|
---|
166 | if (pThis->fFlags & RTHANDLETABLE_FLAGS_CONTEXT)
|
---|
167 | {
|
---|
168 | for (i1 = 0; cLeft > 0 && i1 < pThis->cLevel1; i1++)
|
---|
169 | {
|
---|
170 | PRTHTENTRYCTX paTable = (PRTHTENTRYCTX)pThis->papvLevel1[i1];
|
---|
171 | if (paTable)
|
---|
172 | for (i = 0; i < RTHT_LEVEL2_ENTRIES; i++)
|
---|
173 | if (!RTHT_IS_FREE(paTable[i].pvObj))
|
---|
174 | {
|
---|
175 | pfnDelete(hHandleTable, pThis->uBase + i + i1 * RTHT_LEVEL2_ENTRIES,
|
---|
176 | paTable[i].pvObj, paTable[i].pvCtx, pvUser);
|
---|
177 | Assert(cLeft > 0);
|
---|
178 | cLeft--;
|
---|
179 | }
|
---|
180 | }
|
---|
181 | }
|
---|
182 | else
|
---|
183 | {
|
---|
184 | for (i1 = 0; cLeft > 0 && i1 < pThis->cLevel1; i1++)
|
---|
185 | {
|
---|
186 | PRTHTENTRY paTable = (PRTHTENTRY)pThis->papvLevel1[i1];
|
---|
187 | if (paTable)
|
---|
188 | for (i = 0; i < RTHT_LEVEL2_ENTRIES; i++)
|
---|
189 | if (!RTHT_IS_FREE(paTable[i].pvObj))
|
---|
190 | {
|
---|
191 | pfnDelete(hHandleTable, pThis->uBase + i + i1 * RTHT_LEVEL2_ENTRIES,
|
---|
192 | paTable[i].pvObj, NULL, pvUser);
|
---|
193 | Assert(cLeft > 0);
|
---|
194 | cLeft--;
|
---|
195 | }
|
---|
196 | }
|
---|
197 | }
|
---|
198 | Assert(!cLeft);
|
---|
199 | }
|
---|
200 |
|
---|
201 | /*
|
---|
202 | * Free the memory.
|
---|
203 | */
|
---|
204 | for (i1 = 0; i1 < pThis->cLevel1; i1++)
|
---|
205 | if (pThis->papvLevel1[i1])
|
---|
206 | {
|
---|
207 | RTMemFree(pThis->papvLevel1[i1]);
|
---|
208 | pThis->papvLevel1[i1] = NULL;
|
---|
209 | }
|
---|
210 |
|
---|
211 | if (pThis->cMax / RTHT_LEVEL2_ENTRIES >= RTHT_LEVEL1_DYN_ALLOC_THRESHOLD)
|
---|
212 | RTMemFree(pThis->papvLevel1);
|
---|
213 |
|
---|
214 | RTMemFree(pThis);
|
---|
215 |
|
---|
216 | return VINF_SUCCESS;
|
---|
217 | }
|
---|
218 | RT_EXPORT_SYMBOL(RTHandleTableDestroy);
|
---|
219 |
|
---|