Cleaned up places where MM_Allocate was used without checks
[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                         {
64                                 if( !MM_Allocate( (Uint)gaUserHandles + addr ) )
65                                 {
66                                         Warning("OOM - VFS_AllocHandle");
67                                         Threads_Exit(0, 0xFF);  // Terminate user
68                                 }
69                         }
70                         memset( gaUserHandles, 0, size );
71                 }
72                 // Get a handle
73                 for(i=0;i<CFGINT(CFG_VFS_MAXFILES);i++)
74                 {
75                         if(gaUserHandles[i].Node)       continue;
76                         gaUserHandles[i].Node = Node;
77                         gaUserHandles[i].Position = 0;
78                         gaUserHandles[i].Mode = Mode;
79                         return i;
80                 }
81         }
82         else
83         {
84                 // Allocate space if not already
85                 if( MM_GetPhysAddr( (Uint)gaKernelHandles ) == 0 )
86                 {
87                         Uint    addr, size;
88                         size = MAX_KERNEL_FILES * sizeof(tVFS_Handle);
89                         for(addr = 0; addr < size; addr += 0x1000)
90                         {
91                                 if( !MM_Allocate( (Uint)gaKernelHandles + addr ) )
92                                 {
93                                         Panic("OOM - VFS_AllocHandle");
94                                         Threads_Exit(0, 0xFF);  // Terminate application (get some space back)
95                                 }
96                         }
97                         memset( gaKernelHandles, 0, size );
98                 }
99                 // Get a handle
100                 for(i=0;i<MAX_KERNEL_FILES;i++)
101                 {
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;
107                 }
108         }
109         
110         return -1;
111 }

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