3 * - AllocHandle, GetHandle
13 #define MAX_KERNEL_FILES 128
17 tVFS_Handle *VFS_GetHandle(int FD);
19 int VFS_AllocHandle(int FD, tVFS_Node *Node, int Mode);
22 tVFS_Handle *gaUserHandles = (void*)MM_PPD_VFS;
23 tVFS_Handle *gaKernelHandles = (void*)MM_KERNEL_VFS;
27 * \fn tVFS_Handle *VFS_GetHandle(int FD)
28 * \brief Gets a pointer to the handle information structure
30 tVFS_Handle *VFS_GetHandle(int FD)
34 //Log_Debug("VFS", "VFS_GetHandle: (FD=0x%x)", FD);
36 if(FD < 0) return NULL;
38 if(FD & VFS_KERNEL_FLAG) {
39 FD &= (VFS_KERNEL_FLAG - 1);
40 if(FD >= MAX_KERNEL_FILES) return NULL;
41 h = &gaKernelHandles[ FD ];
43 if(FD >= CFGINT(CFG_VFS_MAXFILES)) return NULL;
44 h = &gaUserHandles[ FD ];
47 if(h->Node == NULL) return NULL;
48 //Log_Debug("VFS", "VFS_GetHandle: RETURN %p", h);
52 int VFS_AllocHandle(int bIsUser, tVFS_Node *Node, int Mode)
56 // Check for a user open
60 if( MM_GetPhysAddr( (Uint)gaUserHandles ) == 0 )
63 size = CFGINT(CFG_VFS_MAXFILES) * sizeof(tVFS_Handle);
64 for(addr = 0; addr < size; addr += 0x1000)
66 if( !MM_Allocate( (Uint)gaUserHandles + addr ) )
68 Warning("OOM - VFS_AllocHandle");
69 Threads_Exit(0, 0xFF); // Terminate user
72 memset( gaUserHandles, 0, size );
75 for(i=0;i<CFGINT(CFG_VFS_MAXFILES);i++)
77 if(gaUserHandles[i].Node) continue;
78 gaUserHandles[i].Node = Node;
79 gaUserHandles[i].Position = 0;
80 gaUserHandles[i].Mode = Mode;
86 // Allocate space if not already
87 if( MM_GetPhysAddr( (Uint)gaKernelHandles ) == 0 )
90 size = MAX_KERNEL_FILES * sizeof(tVFS_Handle);
91 for(addr = 0; addr < size; addr += 0x1000)
93 if( !MM_Allocate( (Uint)gaKernelHandles + addr ) )
95 Panic("OOM - VFS_AllocHandle");
96 Threads_Exit(0, 0xFF); // Terminate application (get some space back)
99 memset( gaKernelHandles, 0, size );
102 for(i=0;i<MAX_KERNEL_FILES;i++)
104 if(gaKernelHandles[i].Node) continue;
105 gaKernelHandles[i].Node = Node;
106 gaKernelHandles[i].Position = 0;
107 gaKernelHandles[i].Mode = Mode;
108 return i|VFS_KERNEL_FLAG;