3 * - AllocHandle, GetHandle
13 #define MAX_KERNEL_FILES 128
16 tVFS_Handle *VFS_GetHandle(int FD);
17 int VFS_AllocHandle(int FD, tVFS_Node *Node, int Mode);
20 tVFS_Handle *gaUserHandles = (void*)MM_PPD_VFS;
21 tVFS_Handle *gaKernelHandles = (void*)MM_KERNEL_VFS;
25 * \fn tVFS_Handle *VFS_GetHandle(int FD)
26 * \brief Gets a pointer to the handle information structure
28 tVFS_Handle *VFS_GetHandle(int FD)
32 //Log_Debug("VFS", "VFS_GetHandle: (FD=0x%x)", FD);
34 if(FD < 0) return NULL;
36 if(FD & VFS_KERNEL_FLAG) {
37 FD &= (VFS_KERNEL_FLAG - 1);
38 if(FD >= MAX_KERNEL_FILES) return NULL;
39 h = &gaKernelHandles[ FD ];
41 if(FD >= CFGINT(CFG_VFS_MAXFILES)) return NULL;
42 h = &gaUserHandles[ FD ];
45 if(h->Node == NULL) return NULL;
46 //Log_Debug("VFS", "VFS_GetHandle: RETURN %p", h);
50 int VFS_AllocHandle(int bIsUser, tVFS_Node *Node, int Mode)
54 // Check for a user open
58 if( MM_GetPhysAddr( (Uint)gaUserHandles ) == 0 )
61 size = CFGINT(CFG_VFS_MAXFILES) * sizeof(tVFS_Handle);
62 for(addr = 0; addr < size; addr += 0x1000)
64 if( !MM_Allocate( (Uint)gaUserHandles + addr ) )
66 Warning("OOM - VFS_AllocHandle");
67 Threads_Exit(0, 0xFF); // Terminate user
70 memset( gaUserHandles, 0, size );
73 for(i=0;i<CFGINT(CFG_VFS_MAXFILES);i++)
75 if(gaUserHandles[i].Node) continue;
76 gaUserHandles[i].Node = Node;
77 gaUserHandles[i].Position = 0;
78 gaUserHandles[i].Mode = Mode;
84 // Allocate space if not already
85 if( MM_GetPhysAddr( (Uint)gaKernelHandles ) == 0 )
88 size = MAX_KERNEL_FILES * sizeof(tVFS_Handle);
89 for(addr = 0; addr < size; addr += 0x1000)
91 if( !MM_Allocate( (Uint)gaKernelHandles + addr ) )
93 Panic("OOM - VFS_AllocHandle");
94 Threads_Exit(0, 0xFF); // Terminate application (get some space back)
97 memset( gaKernelHandles, 0, size );
100 for(i=0;i<MAX_KERNEL_FILES;i++)
102 if(gaKernelHandles[i].Node) continue;
103 gaKernelHandles[i].Node = Node;
104 gaKernelHandles[i].Position = 0;
105 gaKernelHandles[i].Mode = Mode;
106 return i|VFS_KERNEL_FLAG;