Cleaning up code, and separating a little to allow native code to share
[tpg/acess2.git] / Kernel / vfs / handle.c
1 /*
2  * Acess2 VFS
3  * - AllocHandle, GetHandle
4  */
5 #define DEBUG   0
6 #include <acess.h>
7 #include <mm_virt.h>
8 #include "vfs.h"
9 #include "vfs_int.h"
10 #include "vfs_ext.h"
11
12 // === CONSTANTS ===
13 #define MAX_KERNEL_FILES        128
14
15 // === PROTOTYPES ===
16 tVFS_Handle     *VFS_GetHandle(int FD);
17  int    VFS_AllocHandle(int FD, tVFS_Node *Node, int Mode);
18
19 // === GLOBALS ===
20 tVFS_Handle     *gaUserHandles = (void*)MM_PPD_VFS;
21 tVFS_Handle     *gaKernelHandles = (void*)MM_KERNEL_VFS;
22
23 // === CODE ===
24 /**
25  * \fn tVFS_Handle *VFS_GetHandle(int FD)
26  * \brief Gets a pointer to the handle information structure
27  */
28 tVFS_Handle *VFS_GetHandle(int FD)
29 {
30         tVFS_Handle     *h;
31         
32         //Log_Debug("VFS", "VFS_GetHandle: (FD=0x%x)", FD);
33         
34         if(FD < 0)      return NULL;
35         
36         if(FD & VFS_KERNEL_FLAG) {
37                 FD &= (VFS_KERNEL_FLAG - 1);
38                 if(FD >= MAX_KERNEL_FILES)      return NULL;
39                 h = &gaKernelHandles[ FD ];
40         } else {
41                 if(FD >= CFGINT(CFG_VFS_MAXFILES))      return NULL;
42                 h = &gaUserHandles[ FD ];
43         }
44         
45         if(h->Node == NULL)     return NULL;
46         //Log_Debug("VFS", "VFS_GetHandle: RETURN %p", h);
47         return h;
48 }
49
50 int VFS_AllocHandle(int bIsUser, tVFS_Node *Node, int Mode)
51 {
52          int    i;
53         
54         // Check for a user open
55         if(bIsUser)
56         {
57                 // Allocate Buffer
58                 if( MM_GetPhysAddr( (Uint)gaUserHandles ) == 0 )
59                 {
60                         Uint    addr, size;
61                         size = CFGINT(CFG_VFS_MAXFILES) * sizeof(tVFS_Handle);
62                         for(addr = 0; addr < size; addr += 0x1000)
63                                 MM_Allocate( (Uint)gaUserHandles + addr );
64                         memset( gaUserHandles, 0, size );
65                 }
66                 // Get a handle
67                 for(i=0;i<CFGINT(CFG_VFS_MAXFILES);i++)
68                 {
69                         if(gaUserHandles[i].Node)       continue;
70                         gaUserHandles[i].Node = node;
71                         gaUserHandles[i].Position = 0;
72                         gaUserHandles[i].Mode = Mode;
73                         return i;
74                 }
75         }
76         else
77         {
78                 // Allocate space if not already
79                 if( MM_GetPhysAddr( (Uint)gaKernelHandles ) == 0 )
80                 {
81                         Uint    addr, size;
82                         size = MAX_KERNEL_FILES * sizeof(tVFS_Handle);
83                         for(addr = 0; addr < size; addr += 0x1000)
84                                 MM_Allocate( (Uint)gaKernelHandles + addr );
85                         memset( gaKernelHandles, 0, size );
86                 }
87                 // Get a handle
88                 for(i=0;i<MAX_KERNEL_FILES;i++)
89                 {
90                         if(gaKernelHandles[i].Node)     continue;
91                         gaKernelHandles[i].Node = node;
92                         gaKernelHandles[i].Position = 0;
93                         gaKernelHandles[i].Mode = Mode;
94                         return i|VFS_KERNEL_FLAG;
95                 }
96         }
97         
98         return -1;
99 }

UCC git Repository :: git.ucc.asn.au