1 | /** @file
|
---|
2 | *
|
---|
3 | * HGCM (Host-Guest Communication Manager):
|
---|
4 | * HGCMObjects - Host-Guest Communication Manager objects
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
9 | *
|
---|
10 | * innotek GmbH confidential
|
---|
11 | * All rights reserved
|
---|
12 | */
|
---|
13 |
|
---|
14 | #define LOG_GROUP_MAIN_OVERRIDE LOG_GROUP_HGCM
|
---|
15 | #include "Logging.h"
|
---|
16 |
|
---|
17 | #include "HGCMObjects.h"
|
---|
18 |
|
---|
19 | #include <string.h>
|
---|
20 |
|
---|
21 | #include <VBox/err.h>
|
---|
22 |
|
---|
23 |
|
---|
24 | static RTCRITSECT g_critsect;
|
---|
25 |
|
---|
26 | /* There are internal handles, which are not saved,
|
---|
27 | * and client handles, which are saved.
|
---|
28 | * They use different range of values:
|
---|
29 | * 1..7FFFFFFF for clients,
|
---|
30 | * 0x80000001..0xFFFFFFFF for other handles.
|
---|
31 | */
|
---|
32 | static uint32_t volatile g_u32InternalHandleCount;
|
---|
33 | static uint32_t volatile g_u32ClientHandleCount;
|
---|
34 |
|
---|
35 | static PAVLULNODECORE g_pTree;
|
---|
36 |
|
---|
37 |
|
---|
38 | DECLINLINE(int) hgcmObjEnter (void)
|
---|
39 | {
|
---|
40 | return RTCritSectEnter (&g_critsect);
|
---|
41 | }
|
---|
42 |
|
---|
43 | DECLINLINE(void) hgcmObjLeave (void)
|
---|
44 | {
|
---|
45 | RTCritSectLeave (&g_critsect);
|
---|
46 | }
|
---|
47 |
|
---|
48 | int hgcmObjInit (void)
|
---|
49 | {
|
---|
50 | int rc = VINF_SUCCESS;
|
---|
51 |
|
---|
52 | LogFlow(("MAIN::hgcmObjInit\n"));
|
---|
53 |
|
---|
54 | g_u32InternalHandleCount = 0x80000000;
|
---|
55 | g_u32ClientHandleCount = 0;
|
---|
56 | g_pTree = NULL;
|
---|
57 |
|
---|
58 | rc = RTCritSectInit (&g_critsect);
|
---|
59 |
|
---|
60 | LogFlow(("MAIN::hgcmObjInit: rc = %Vrc\n", rc));
|
---|
61 |
|
---|
62 | return rc;
|
---|
63 | }
|
---|
64 |
|
---|
65 | void hgcmObjUninit (void)
|
---|
66 | {
|
---|
67 | if (RTCritSectIsInitialized (&g_critsect))
|
---|
68 | {
|
---|
69 | RTCritSectDelete (&g_critsect);
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | uint32_t hgcmObjMake (HGCMObject *pObject, uint32_t u32HandleIn)
|
---|
74 | {
|
---|
75 | int handle = 0;
|
---|
76 |
|
---|
77 | LogFlow(("MAIN::hgcmObjGenerateHandle: pObject %p\n", pObject));
|
---|
78 |
|
---|
79 | int rc = hgcmObjEnter ();
|
---|
80 |
|
---|
81 | if (VBOX_SUCCESS(rc))
|
---|
82 | {
|
---|
83 | ObjectAVLCore *pCore = &pObject->Core;
|
---|
84 |
|
---|
85 | /* Generate a new handle value. */
|
---|
86 |
|
---|
87 | uint32_t volatile *pu32HandleCountSource = pObject->Type () == HGCMOBJ_CLIENT?
|
---|
88 | &g_u32ClientHandleCount:
|
---|
89 | &g_u32InternalHandleCount;
|
---|
90 |
|
---|
91 | uint32_t u32Start = *pu32HandleCountSource;
|
---|
92 |
|
---|
93 | for (;;)
|
---|
94 | {
|
---|
95 | uint32_t Key;
|
---|
96 |
|
---|
97 | if (u32HandleIn == 0)
|
---|
98 | {
|
---|
99 | Key = ASMAtomicIncU32 (pu32HandleCountSource);
|
---|
100 |
|
---|
101 | if (Key == u32Start)
|
---|
102 | {
|
---|
103 | /* Rollover. Something is wrong. */
|
---|
104 | AssertReleaseFailed ();
|
---|
105 | break;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /* 0 and 0x80000000 are not valid handles. */
|
---|
109 | if ((Key & 0x7FFFFFFF) == 0)
|
---|
110 | {
|
---|
111 | /* Over the invalid value, reinitialize the source. */
|
---|
112 | *pu32HandleCountSource = pObject->Type () == HGCMOBJ_CLIENT?
|
---|
113 | 0:
|
---|
114 | 0x80000000;
|
---|
115 | continue;
|
---|
116 | }
|
---|
117 | }
|
---|
118 | else
|
---|
119 | {
|
---|
120 | Key = u32HandleIn;
|
---|
121 | }
|
---|
122 |
|
---|
123 | /* Insert object to AVL tree. */
|
---|
124 | pCore->AvlCore.Key = Key;
|
---|
125 |
|
---|
126 | bool bRC = RTAvlULInsert(&g_pTree, &pCore->AvlCore);
|
---|
127 |
|
---|
128 | /* Could not insert a handle. */
|
---|
129 | if (!bRC)
|
---|
130 | {
|
---|
131 | if (u32HandleIn == 0)
|
---|
132 | {
|
---|
133 | /* Try another generated handle. */
|
---|
134 | continue;
|
---|
135 | }
|
---|
136 | else
|
---|
137 | {
|
---|
138 | /* Could not use the specified handle. */
|
---|
139 | break;
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | /* Initialize backlink. */
|
---|
144 | pCore->pSelf = pObject;
|
---|
145 |
|
---|
146 | /* Reference the object for time while it resides in the tree. */
|
---|
147 | pObject->Reference ();
|
---|
148 |
|
---|
149 | /* Store returned handle. */
|
---|
150 | handle = Key;
|
---|
151 |
|
---|
152 | Log(("Object key inserted 0x%08X\n", Key));
|
---|
153 |
|
---|
154 | break;
|
---|
155 | }
|
---|
156 |
|
---|
157 | hgcmObjLeave ();
|
---|
158 | }
|
---|
159 | else
|
---|
160 | {
|
---|
161 | AssertReleaseMsgFailed (("MAIN::hgcmObjGenerateHandle: Failed to acquire object pool semaphore"));
|
---|
162 | }
|
---|
163 |
|
---|
164 | LogFlow(("MAIN::hgcmObjGenerateHandle: handle = 0x%08X, rc = %Vrc, return void\n", handle, rc));
|
---|
165 |
|
---|
166 | return handle;
|
---|
167 | }
|
---|
168 |
|
---|
169 | uint32_t hgcmObjGenerateHandle (HGCMObject *pObject)
|
---|
170 | {
|
---|
171 | return hgcmObjMake (pObject, 0);
|
---|
172 | }
|
---|
173 |
|
---|
174 | uint32_t hgcmObjAssignHandle (HGCMObject *pObject, uint32_t u32Handle)
|
---|
175 | {
|
---|
176 | return hgcmObjMake (pObject, u32Handle);
|
---|
177 | }
|
---|
178 |
|
---|
179 | void hgcmObjDeleteHandle (uint32_t handle)
|
---|
180 | {
|
---|
181 | int rc = VINF_SUCCESS;
|
---|
182 |
|
---|
183 | LogFlow(("MAIN::hgcmObjDeleteHandle: handle 0x%08X\n", handle));
|
---|
184 |
|
---|
185 | if (handle)
|
---|
186 | {
|
---|
187 | rc = hgcmObjEnter ();
|
---|
188 |
|
---|
189 | if (VBOX_SUCCESS(rc))
|
---|
190 | {
|
---|
191 | ObjectAVLCore *pCore = (ObjectAVLCore *)RTAvlULRemove (&g_pTree, handle);
|
---|
192 |
|
---|
193 | if (pCore)
|
---|
194 | {
|
---|
195 | AssertRelease(pCore->pSelf);
|
---|
196 |
|
---|
197 | pCore->pSelf->Dereference ();
|
---|
198 | }
|
---|
199 |
|
---|
200 | hgcmObjLeave ();
|
---|
201 | }
|
---|
202 | else
|
---|
203 | {
|
---|
204 | AssertReleaseMsgFailed (("Failed to acquire object pool semaphore, rc = %Vrc", rc));
|
---|
205 | }
|
---|
206 | }
|
---|
207 |
|
---|
208 | LogFlow(("MAIN::hgcmObjDeleteHandle: rc = %Vrc, return void\n", rc));
|
---|
209 |
|
---|
210 | return;
|
---|
211 | }
|
---|
212 |
|
---|
213 | HGCMObject *hgcmObjReference (uint32_t handle, HGCMOBJ_TYPE enmObjType)
|
---|
214 | {
|
---|
215 | LogFlow(("MAIN::hgcmObjReference: handle 0x%08X\n", handle));
|
---|
216 |
|
---|
217 | HGCMObject *pObject = NULL;
|
---|
218 |
|
---|
219 | if ((handle & 0x7FFFFFFF) == 0)
|
---|
220 | {
|
---|
221 | return pObject;
|
---|
222 | }
|
---|
223 |
|
---|
224 | int rc = hgcmObjEnter ();
|
---|
225 |
|
---|
226 | if (VBOX_SUCCESS(rc))
|
---|
227 | {
|
---|
228 | ObjectAVLCore *pCore = (ObjectAVLCore *)RTAvlULGet (&g_pTree, handle);
|
---|
229 |
|
---|
230 | Assert(!pCore || (pCore->pSelf && pCore->pSelf->Type() == enmObjType));
|
---|
231 | if ( pCore
|
---|
232 | && pCore->pSelf
|
---|
233 | && pCore->pSelf->Type() == enmObjType)
|
---|
234 | {
|
---|
235 | pObject = pCore->pSelf;
|
---|
236 |
|
---|
237 | AssertRelease(pObject);
|
---|
238 |
|
---|
239 | pObject->Reference ();
|
---|
240 | }
|
---|
241 |
|
---|
242 | hgcmObjLeave ();
|
---|
243 | }
|
---|
244 | else
|
---|
245 | {
|
---|
246 | AssertReleaseMsgFailed (("Failed to acquire object pool semaphore, rc = %Vrc", rc));
|
---|
247 | }
|
---|
248 |
|
---|
249 | LogFlow(("MAIN::hgcmObjReference: return pObject %p\n", pObject));
|
---|
250 |
|
---|
251 | return pObject;
|
---|
252 | }
|
---|
253 |
|
---|
254 | void hgcmObjDereference (HGCMObject *pObject)
|
---|
255 | {
|
---|
256 | LogFlow(("MAIN::hgcmObjDereference: pObject %p\n", pObject));
|
---|
257 |
|
---|
258 | AssertRelease(pObject);
|
---|
259 |
|
---|
260 | pObject->Dereference ();
|
---|
261 |
|
---|
262 | LogFlow(("MAIN::hgcmObjDereference: return\n"));
|
---|
263 | }
|
---|
264 |
|
---|
265 | uint32_t hgcmObjQueryHandleCount ()
|
---|
266 | {
|
---|
267 | return g_u32ClientHandleCount;
|
---|
268 | }
|
---|
269 |
|
---|
270 | void hgcmObjSetHandleCount (uint32_t u32ClientHandleCount)
|
---|
271 | {
|
---|
272 | Assert(g_u32ClientHandleCount <= u32ClientHandleCount);
|
---|
273 |
|
---|
274 | int rc = hgcmObjEnter ();
|
---|
275 |
|
---|
276 | if (VBOX_SUCCESS(rc))
|
---|
277 | {
|
---|
278 | if (g_u32ClientHandleCount <= u32ClientHandleCount)
|
---|
279 | g_u32ClientHandleCount = u32ClientHandleCount;
|
---|
280 | hgcmObjLeave ();
|
---|
281 | }
|
---|
282 | }
|
---|