VirtualBox

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

Last change on this file since 34562 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

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