3 * - AllocHandle, GetHandle
11 #include <threads.h> // GetMaxFD
12 #include <vfs_threads.h> // Handle maintainance
15 #define MAX_KERNEL_FILES 128
19 tVFS_Handle *VFS_GetHandle(int FD);
21 int VFS_AllocHandle(int FD, tVFS_Node *Node, int Mode);
24 tVFS_Handle *gaUserHandles = (void*)MM_PPD_HANDLES;
25 tVFS_Handle *gaKernelHandles = (void*)MM_KERNEL_VFS;
29 * \fn tVFS_Handle *VFS_GetHandle(int FD)
30 * \brief Gets a pointer to the handle information structure
32 tVFS_Handle *VFS_GetHandle(int FD)
36 //Log_Debug("VFS", "VFS_GetHandle: (FD=0x%x)", FD);
38 if(FD < 0) return NULL;
40 if(FD & VFS_KERNEL_FLAG) {
41 FD &= (VFS_KERNEL_FLAG - 1);
42 if(FD >= MAX_KERNEL_FILES) return NULL;
43 h = &gaKernelHandles[ FD ];
45 if(FD >= *Threads_GetMaxFD()) return NULL;
46 h = &gaUserHandles[ FD ];
49 if(h->Node == NULL) return NULL;
50 //Log_Debug("VFS", "VFS_GetHandle: RETURN %p", h);
54 int VFS_AllocHandle(int bIsUser, tVFS_Node *Node, int Mode)
58 // Check for a user open
61 int max_handles = *Threads_GetMaxFD();
63 if( MM_GetPhysAddr( (tVAddr)gaUserHandles ) == 0 )
66 size = max_handles * sizeof(tVFS_Handle);
67 for(addr = 0; addr < size; addr += 0x1000)
69 if( !MM_Allocate( (tVAddr)gaUserHandles + addr ) )
71 Warning("OOM - VFS_AllocHandle");
72 Threads_Exit(0, 0xFF); // Terminate user
75 memset( gaUserHandles, 0, size );
78 for( i = 0; i < max_handles; i ++ )
80 if(gaUserHandles[i].Node) continue;
81 gaUserHandles[i].Node = Node;
82 gaUserHandles[i].Position = 0;
83 gaUserHandles[i].Mode = Mode;
89 // Allocate space if not already
90 if( MM_GetPhysAddr( (tVAddr)gaKernelHandles ) == 0 )
93 size = MAX_KERNEL_FILES * sizeof(tVFS_Handle);
94 for(addr = 0; addr < size; addr += 0x1000)
96 if( !MM_Allocate( (tVAddr)gaKernelHandles + addr ) )
98 Panic("OOM - VFS_AllocHandle");
99 Threads_Exit(0, 0xFF); // Terminate application (get some space back)
102 memset( gaKernelHandles, 0, size );
105 for(i=0;i<MAX_KERNEL_FILES;i++)
107 if(gaKernelHandles[i].Node) continue;
108 gaKernelHandles[i].Node = Node;
109 gaKernelHandles[i].Position = 0;
110 gaKernelHandles[i].Mode = Mode;
111 return i|VFS_KERNEL_FLAG;
118 void VFS_ReferenceUserHandles(void)
121 int max_handles = *Threads_GetMaxFD();
123 // Check if this process has any handles
124 if( MM_GetPhysAddr( (tVAddr)gaUserHandles ) == 0 )
127 for( i = 0; i < max_handles; i ++ )
130 h = &gaUserHandles[i];
133 if( h->Node->Type && h->Node->Type->Reference )
134 h->Node->Type->Reference( h->Node );
138 void VFS_CloseAllUserHandles(void)
141 int max_handles = *Threads_GetMaxFD();
143 // Check if this process has any handles
144 if( MM_GetPhysAddr( (tVAddr)gaUserHandles ) == 0 )
147 for( i = 0; i < max_handles; i ++ )
150 h = &gaUserHandles[i];
153 if( h->Node->Type && h->Node->Type->Close )
154 h->Node->Type->Close( h->Node );
159 * \brief Take a backup of a set of file descriptors
161 void *VFS_SaveHandles(int NumFDs, int *FDs)
165 int max_handles = *Threads_GetMaxFD();
167 // Check if this process has any handles
168 if( MM_GetPhysAddr( (tVAddr)gaUserHandles ) == 0 )
172 ret = malloc( NumFDs * sizeof(tVFS_Handle) );
176 if( NumFDs > max_handles )
177 NumFDs = max_handles;
179 // Take copies of the handles
180 for( i = 0; i < NumFDs; i ++ )
184 h = &gaUserHandles[i];
186 h = VFS_GetHandle(FDs[i] & (VFS_KERNEL_FLAG - 1));
188 Log_Warning("VFS", "VFS_SaveHandles - Invalid FD %i",
189 FDs[i] & (VFS_KERNEL_FLAG - 1) );
194 memcpy( &ret[i], h, sizeof(tVFS_Handle) );
199 if( h->Node->Type && h->Node->Type->Reference )
200 h->Node->Type->Reference( h->Node );
206 void VFS_RestoreHandles(int NumFDs, void *Handles)
208 tVFS_Handle *handles = Handles;
211 // NULL = nothing to do
215 // Check if there is already a set of handles
216 if( MM_GetPhysAddr( (tVAddr)gaUserHandles ) != 0 )
220 // Allocate user handle area
223 int max_handles = *Threads_GetMaxFD();
224 size = max_handles * sizeof(tVFS_Handle);
225 for(addr = 0; addr < size; addr += 0x1000)
227 if( !MM_Allocate( (tVAddr)gaUserHandles + addr ) )
229 Warning("OOM - VFS_AllocHandle");
230 Threads_Exit(0, 0xFF); // Terminate user
233 memset( gaUserHandles, 0, size );
237 memcpy( gaUserHandles, handles, NumFDs * sizeof(tVFS_Handle) );
238 // Reference when copied
239 for( i = 0; i < NumFDs; i ++ )
241 tVFS_Handle *h = &handles[i];
245 if( h->Node->Type && h->Node->Type->Reference )
246 h->Node->Type->Reference( h->Node );
250 void VFS_FreeSavedHandles(int NumFDs, void *Handles)
252 tVFS_Handle *handles = Handles;
255 // NULL = nothing to do
259 // Dereference all saved nodes
260 for( i = 0; i < NumFDs; i ++ )
262 tVFS_Handle *h = &handles[i];
266 if( h->Node->Type && h->Node->Type->Close )
267 h->Node->Type->Close( h->Node );