3 * - AllocHandle, GetHandle
11 #include <threads.h> // GetMaxFD
14 #define MAX_KERNEL_FILES 128
18 tVFS_Handle *VFS_GetHandle(int FD);
20 int VFS_AllocHandle(int FD, tVFS_Node *Node, int Mode);
23 tVFS_Handle *gaUserHandles = (void*)MM_PPD_HANDLES;
24 tVFS_Handle *gaKernelHandles = (void*)MM_KERNEL_VFS;
28 * \fn tVFS_Handle *VFS_GetHandle(int FD)
29 * \brief Gets a pointer to the handle information structure
31 tVFS_Handle *VFS_GetHandle(int FD)
35 //Log_Debug("VFS", "VFS_GetHandle: (FD=0x%x)", FD);
37 if(FD < 0) return NULL;
39 if(FD & VFS_KERNEL_FLAG) {
40 FD &= (VFS_KERNEL_FLAG - 1);
41 if(FD >= MAX_KERNEL_FILES) return NULL;
42 h = &gaKernelHandles[ FD ];
44 if(FD >= *Threads_GetMaxFD()) return NULL;
45 h = &gaUserHandles[ FD ];
48 if(h->Node == NULL) return NULL;
49 //Log_Debug("VFS", "VFS_GetHandle: RETURN %p", h);
53 int VFS_AllocHandle(int bIsUser, tVFS_Node *Node, int Mode)
57 // Check for a user open
60 int max_handles = *Threads_GetMaxFD();
62 if( MM_GetPhysAddr( (tVAddr)gaUserHandles ) == 0 )
65 size = max_handles * sizeof(tVFS_Handle);
66 for(addr = 0; addr < size; addr += 0x1000)
68 if( !MM_Allocate( (tVAddr)gaUserHandles + addr ) )
70 Warning("OOM - VFS_AllocHandle");
71 Threads_Exit(0, 0xFF); // Terminate user
74 memset( gaUserHandles, 0, size );
77 for( i = 0; i < max_handles; i ++ )
79 if(gaUserHandles[i].Node) continue;
80 gaUserHandles[i].Node = Node;
81 gaUserHandles[i].Position = 0;
82 gaUserHandles[i].Mode = Mode;
88 // Allocate space if not already
89 if( MM_GetPhysAddr( (tVAddr)gaKernelHandles ) == 0 )
92 size = MAX_KERNEL_FILES * sizeof(tVFS_Handle);
93 for(addr = 0; addr < size; addr += 0x1000)
95 if( !MM_Allocate( (tVAddr)gaKernelHandles + addr ) )
97 Panic("OOM - VFS_AllocHandle");
98 Threads_Exit(0, 0xFF); // Terminate application (get some space back)
101 memset( gaKernelHandles, 0, size );
104 for(i=0;i<MAX_KERNEL_FILES;i++)
106 if(gaKernelHandles[i].Node) continue;
107 gaKernelHandles[i].Node = Node;
108 gaKernelHandles[i].Position = 0;
109 gaKernelHandles[i].Mode = Mode;
110 return i|VFS_KERNEL_FLAG;