VirtualBox

source: vbox/trunk/src/VBox/Main/hgcm/HGCMObjects.cpp@ 794

Last change on this file since 794 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.7 KB
Line 
1/** @file
2 *
3 * HGCM (Host-Guest Communication Manager):
4 * HGCMObjects - Host-Guest Communication Manager objects
5 */
6
7/*
8 * Copyright (C) 2006 InnoTek Systemberatung GmbH
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License as published by the Free Software Foundation,
14 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
15 * distribution. VirtualBox OSE is distributed in the hope that it will
16 * be useful, but WITHOUT ANY WARRANTY of any kind.
17 *
18 * If you received this file as part of a commercial VirtualBox
19 * distribution, then only the terms of your commercial VirtualBox
20 * license agreement apply instead of the previous paragraph.
21 */
22
23#define LOG_GROUP_MAIN_OVERRIDE LOG_GROUP_HGCM
24#include "Logging.h"
25
26#include "hgcm/HGCMObjects.h"
27
28#include <string.h>
29
30#include <VBox/err.h>
31
32
33static RTCRITSECT g_critsect;
34
35static uint32_t volatile g_u32HandleCount;
36
37static PAVLULNODECORE g_pTree;
38
39
40DECLINLINE(int) hgcmObjEnter (void)
41{
42 return RTCritSectEnter (&g_critsect);
43}
44
45DECLINLINE(void) hgcmObjLeave (void)
46{
47 RTCritSectLeave (&g_critsect);
48}
49
50int hgcmObjInit (void)
51{
52 int rc = VINF_SUCCESS;
53
54 LogFlow(("MAIN::hgcmObjInit\n"));
55
56 g_u32HandleCount = 0;
57 g_pTree = NULL;
58
59 rc = RTCritSectInit (&g_critsect);
60
61 LogFlow(("MAIN::hgcmObjInit: rc = %Vrc\n", rc));
62
63 return rc;
64}
65
66void hgcmObjUninit (void)
67{
68 if (RTCritSectIsInitialized (&g_critsect))
69 {
70 RTCritSectDelete (&g_critsect);
71 }
72}
73
74uint32_t hgcmObjGenerateHandle (HGCMObject *pObject)
75{
76 int handle = 0;
77
78 LogFlow(("MAIN::hgcmObjGenerateHandle: pObject %p\n", pObject));
79
80 int rc = hgcmObjEnter ();
81
82 if (VBOX_SUCCESS(rc))
83 {
84 ObjectAVLCore *pCore = &pObject->Core;
85
86 /* Generate a new handle value. */
87
88 uint32_t u32Start = g_u32HandleCount;
89
90 for (;;)
91 {
92 uint32_t Key = ASMAtomicIncU32 (&g_u32HandleCount);
93
94 if (Key == u32Start)
95 {
96 /* Rollover. Something is wrong. */
97 break;
98 }
99
100 /* 0 is not a valid handle. */
101 if (Key == 0)
102 {
103 continue;
104 }
105
106 /* Insert object to AVL tree. */
107 pCore->AvlCore.Key = Key;
108
109 bool bRC = RTAvlULInsert(&g_pTree, &pCore->AvlCore);
110
111 /* Could not insert a handle. */
112 if (!bRC)
113 {
114 continue;
115 }
116
117 /* Initialize backlink. */
118 pCore->pSelf = pObject;
119
120 /* Reference the object for time while it resides in the tree. */
121 pObject->Reference ();
122
123 /* Store returned handle. */
124 handle = Key;
125
126 break;
127 }
128
129 hgcmObjLeave ();
130 }
131 else
132 {
133 AssertReleaseMsgFailed (("MAIN::hgcmObjGenerateHandle: Failed to acquire object pool semaphore"));
134 }
135
136 LogFlow(("MAIN::hgcmObjGenerateHandle: handle = %d, rc = %Vrc, return void\n", handle, rc));
137
138 return handle;
139}
140
141void hgcmObjDeleteHandle (uint32_t handle)
142{
143 int rc = VINF_SUCCESS;
144
145 LogFlow(("MAIN::hgcmObjDeleteHandle: handle %d\n", handle));
146
147 if (handle)
148 {
149 rc = hgcmObjEnter ();
150
151 if (VBOX_SUCCESS(rc))
152 {
153 ObjectAVLCore *pCore = (ObjectAVLCore *)RTAvlULRemove (&g_pTree, handle);
154
155 if (pCore)
156 {
157 AssertRelease(pCore->pSelf);
158
159 pCore->pSelf->Dereference ();
160 }
161
162 hgcmObjLeave ();
163 }
164 else
165 {
166 AssertReleaseMsgFailed (("Failed to acquire object pool semaphore, rc = %Vrc", rc));
167 }
168 }
169
170 LogFlow(("MAIN::hgcmObjDeleteHandle: rc = %Vrc, return void\n", rc));
171
172 return;
173}
174
175HGCMObject *hgcmObjReference (uint32_t handle)
176{
177 LogFlow(("MAIN::hgcmObjReference: handle %d\n", handle));
178
179 HGCMObject *pObject = NULL;
180
181 int rc = hgcmObjEnter ();
182
183 if (VBOX_SUCCESS(rc))
184 {
185 ObjectAVLCore *pCore = (ObjectAVLCore *)RTAvlULGet (&g_pTree, handle);
186
187 if (pCore)
188 {
189 pObject = pCore->pSelf;
190
191 AssertRelease(pObject);
192
193 pObject->Reference ();
194 }
195
196 hgcmObjLeave ();
197 }
198 else
199 {
200 AssertReleaseMsgFailed (("Failed to acquire object pool semaphore, rc = %Vrc", rc));
201 }
202
203 LogFlow(("MAIN::hgcmObjReference: return pObject %p\n", pObject));
204
205 return pObject;
206}
207
208void hgcmObjDereference (HGCMObject *pObject)
209{
210 LogFlow(("MAIN::hgcmObjDereference: pObject %p\n", pObject));
211
212 AssertRelease(pObject);
213
214 pObject->Dereference ();
215
216 LogFlow(("MAIN::hgcmObjDereference: return\n"));
217}
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