VirtualBox

source: vbox/trunk/src/VBox/HostServices/SharedFolders/shflhandle.cpp@ 17822

Last change on this file since 17822 was 8155, checked in by vboxsync, 17 years ago

The Big Sun Rebranding Header Change

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.5 KB
Line 
1/** @file
2 *
3 * Shared Folders:
4 * Handles helper functions.
5 */
6
7/*
8 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 *
18 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
19 * Clara, CA 95054 USA or visit http://www.sun.com if you need
20 * additional information or have any questions.
21 */
22
23#include "shflhandle.h"
24#include <iprt/alloc.h>
25#include <iprt/assert.h>
26#include <iprt/critsect.h>
27
28
29/*
30 * Very basic and primitive handle management. Should be sufficient for our needs.
31 * Handle allocation can be rather slow, but at least lookup is fast.
32 *
33 */
34typedef struct
35{
36 uint32_t uFlags;
37 uintptr_t pvUserData;
38} SHFLINTHANDLE, *PSHFLINTHANDLE;
39
40static SHFLINTHANDLE *pHandles = NULL;
41static int32_t lastHandleIndex = 0;
42static RTCRITSECT lock = {0};
43
44int vbsfInitHandleTable()
45{
46 pHandles = (SHFLINTHANDLE *)RTMemAllocZ (sizeof (SHFLINTHANDLE) * SHFLHANDLE_MAX);
47 if (pHandles == NULL)
48 {
49 AssertFailed();
50 return VERR_NO_MEMORY;
51 }
52
53 /* Never return handle 0 */
54 pHandles[0].uFlags = SHFL_HF_TYPE_DONTUSE;
55 lastHandleIndex = 1;
56
57 return RTCritSectInit(&lock);
58}
59
60int vbsfFreeHandleTable()
61{
62 if (pHandles)
63 RTMemFree(pHandles);
64
65 pHandles = NULL;
66
67 if (RTCritSectIsInitialized(&lock))
68 RTCritSectDelete(&lock);
69
70 return VINF_SUCCESS;
71}
72
73SHFLHANDLE vbsfAllocHandle(uint32_t uType, uintptr_t pvUserData)
74{
75 SHFLHANDLE handle;
76
77 Assert((uType & SHFL_HF_TYPE_MASK) != 0 && pvUserData);
78
79 RTCritSectEnter(&lock);
80
81 /* Find next free handle */
82 if(lastHandleIndex >= SHFLHANDLE_MAX-1)
83 {
84 lastHandleIndex = 1;
85 }
86
87 /* Nice linear search */
88 for(handle=lastHandleIndex;handle<SHFLHANDLE_MAX;handle++)
89 {
90 if(pHandles[handle].pvUserData == 0)
91 {
92 lastHandleIndex = handle;
93 break;
94 }
95 }
96
97 if(handle == SHFLHANDLE_MAX)
98 {
99 /* Try once more from the start */
100 for(handle=1;handle<SHFLHANDLE_MAX;handle++)
101 {
102 if(pHandles[handle].pvUserData == 0)
103 {
104 lastHandleIndex = handle;
105 break;
106 }
107 }
108 if(handle == SHFLHANDLE_MAX)
109 { /* Out of handles */
110 RTCritSectLeave(&lock);
111 AssertFailed();
112 return SHFL_HANDLE_NIL;
113 }
114 }
115 pHandles[handle].uFlags = (uType & SHFL_HF_TYPE_MASK) | SHFL_HF_VALID;
116 pHandles[handle].pvUserData = pvUserData;
117
118 lastHandleIndex++;
119
120 RTCritSectLeave(&lock);
121
122 return handle;
123}
124
125int vbsfFreeHandle(SHFLHANDLE handle)
126{
127 if (handle < SHFLHANDLE_MAX && (pHandles[handle].uFlags & SHFL_HF_VALID))
128 {
129 pHandles[handle].uFlags = 0;
130 pHandles[handle].pvUserData = 0;
131 return VINF_SUCCESS;
132 }
133 return VERR_INVALID_HANDLE;
134}
135
136uintptr_t vbsfQueryHandle(SHFLHANDLE handle, uint32_t uType)
137{
138 if (handle < SHFLHANDLE_MAX && (pHandles[handle].uFlags & SHFL_HF_VALID))
139 {
140 Assert((uType & SHFL_HF_TYPE_MASK) != 0);
141
142 if (pHandles[handle].uFlags & uType)
143 return pHandles[handle].pvUserData;
144 }
145 return 0;
146}
147
148SHFLHANDLE vbsfAllocDirHandle (void)
149{
150 SHFLFILEHANDLE *pHandle = (SHFLFILEHANDLE *)RTMemAllocZ (sizeof (SHFLFILEHANDLE));
151
152 if (pHandle)
153 {
154 pHandle->Header.u32Flags = SHFL_HF_TYPE_DIR;
155 return vbsfAllocHandle(pHandle->Header.u32Flags, (uintptr_t)pHandle);
156 }
157
158 return SHFL_HANDLE_NIL;
159}
160
161SHFLHANDLE vbsfAllocFileHandle (void)
162{
163 SHFLFILEHANDLE *pHandle = (SHFLFILEHANDLE *)RTMemAllocZ (sizeof (SHFLFILEHANDLE));
164
165 if (pHandle)
166 {
167 pHandle->Header.u32Flags = SHFL_HF_TYPE_FILE;
168 return vbsfAllocHandle(pHandle->Header.u32Flags, (uintptr_t)pHandle);
169 }
170
171 return SHFL_HANDLE_NIL;
172}
173
174void vbsfFreeFileHandle (SHFLHANDLE hHandle)
175{
176 SHFLFILEHANDLE *pHandle = (SHFLFILEHANDLE *)vbsfQueryHandle(hHandle, SHFL_HF_TYPE_DIR|SHFL_HF_TYPE_FILE);
177
178 if (pHandle)
179 {
180 vbsfFreeHandle(hHandle);
181 RTMemFree (pHandle);
182 }
183 else
184 AssertFailed();
185
186 return;
187}
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