Added some very pedantic warning flags
[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 #if 0
17 tVFS_Handle     *VFS_GetHandle(int FD);
18 #endif
19  int    VFS_AllocHandle(int FD, tVFS_Node *Node, int Mode);
20
21 // === GLOBALS ===
22 tVFS_Handle     *gaUserHandles = (void*)MM_PPD_VFS;
23 tVFS_Handle     *gaKernelHandles = (void*)MM_KERNEL_VFS;
24
25 // === CODE ===
26 /**
27  * \fn tVFS_Handle *VFS_GetHandle(int FD)
28  * \brief Gets a pointer to the handle information structure
29  */
30 tVFS_Handle *VFS_GetHandle(int FD)
31 {
32         tVFS_Handle     *h;
33         
34         //Log_Debug("VFS", "VFS_GetHandle: (FD=0x%x)", FD);
35         
36         if(FD < 0)      return NULL;
37         
38         if(FD & VFS_KERNEL_FLAG) {
39                 FD &= (VFS_KERNEL_FLAG - 1);
40                 if(FD >= MAX_KERNEL_FILES)      return NULL;
41                 h = &gaKernelHandles[ FD ];
42         } else {
43                 if(FD >= CFGINT(CFG_VFS_MAXFILES))      return NULL;
44                 h = &gaUserHandles[ FD ];
45         }
46         
47         if(h->Node == NULL)     return NULL;
48         //Log_Debug("VFS", "VFS_GetHandle: RETURN %p", h);
49         return h;
50 }
51
52 int VFS_AllocHandle(int bIsUser, tVFS_Node *Node, int Mode)
53 {
54          int    i;
55         
56         // Check for a user open
57         if(bIsUser)
58         {
59                 // Allocate Buffer
60                 if( MM_GetPhysAddr( (Uint)gaUserHandles ) == 0 )
61                 {
62                         Uint    addr, size;
63                         size = CFGINT(CFG_VFS_MAXFILES) * sizeof(tVFS_Handle);
64                         for(addr = 0; addr < size; addr += 0x1000)
65                         {
66                                 if( !MM_Allocate( (Uint)gaUserHandles + addr ) )
67                                 {
68                                         Warning("OOM - VFS_AllocHandle");
69                                         Threads_Exit(0, 0xFF);  // Terminate user
70                                 }
71                         }
72                         memset( gaUserHandles, 0, size );
73                 }
74                 // Get a handle
75                 for(i=0;i<CFGINT(CFG_VFS_MAXFILES);i++)
76                 {
77                         if(gaUserHandles[i].Node)       continue;
78                         gaUserHandles[i].Node = Node;
79                         gaUserHandles[i].Position = 0;
80                         gaUserHandles[i].Mode = Mode;
81                         return i;
82                 }
83         }
84         else
85         {
86                 // Allocate space if not already
87                 if( MM_GetPhysAddr( (Uint)gaKernelHandles ) == 0 )
88                 {
89                         Uint    addr, size;
90                         size = MAX_KERNEL_FILES * sizeof(tVFS_Handle);
91                         for(addr = 0; addr < size; addr += 0x1000)
92                         {
93                                 if( !MM_Allocate( (Uint)gaKernelHandles + addr ) )
94                                 {
95                                         Panic("OOM - VFS_AllocHandle");
96                                         Threads_Exit(0, 0xFF);  // Terminate application (get some space back)
97                                 }
98                         }
99                         memset( gaKernelHandles, 0, size );
100                 }
101                 // Get a handle
102                 for(i=0;i<MAX_KERNEL_FILES;i++)
103                 {
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;
109                 }
110         }
111         
112         return -1;
113 }

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