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