Subversion Repositories HelenOS

Rev

Rev 4362 | Rev 4518 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4362 Rev 4432
Line 76... Line 76...
76
 * @return      First available file descriptor or a negative error
76
 * @return      First available file descriptor or a negative error
77
 *          code.
77
 *          code.
78
 */
78
 */
79
int vfs_fd_alloc(void)
79
int vfs_fd_alloc(void)
80
{
80
{
-
 
81
    if (!vfs_files_init())
81
    int i;
82
        return ENOMEM;
82
 
83
   
-
 
84
    unsigned int i;
83
    for (i = 0; i < MAX_OPEN_FILES; i++) {
85
    for (i = 0; i < MAX_OPEN_FILES; i++) {
84
        if (!files[i]) {
86
        if (!files[i]) {
85
            files[i] = (vfs_file_t *) malloc(sizeof(vfs_file_t));
87
            files[i] = (vfs_file_t *) malloc(sizeof(vfs_file_t));
86
            if (!files[i])
88
            if (!files[i])
87
                return ENOMEM;
89
                return ENOMEM;
-
 
90
           
88
            memset(files[i], 0, sizeof(vfs_file_t));
91
            memset(files[i], 0, sizeof(vfs_file_t));
89
            futex_initialize(&files[i]->lock, 1);
92
            futex_initialize(&files[i]->lock, 1);
90
            vfs_file_addref(files[i]);
93
            vfs_file_addref(files[i]);
91
            return i;
94
            return (int) i;
92
        }
95
        }
93
    }
96
    }
-
 
97
   
94
    return EMFILE;
98
    return EMFILE;
95
}
99
}
96
 
100
 
97
/** Release file descriptor.
101
/** Release file descriptor.
98
 *
102
 *
Line 101... Line 105...
101
 * @return      EOK on success or EBADF if fd is an invalid file
105
 * @return      EOK on success or EBADF if fd is an invalid file
102
 *          descriptor.
106
 *          descriptor.
103
 */
107
 */
104
int vfs_fd_free(int fd)
108
int vfs_fd_free(int fd)
105
{
109
{
-
 
110
    if (!vfs_files_init())
-
 
111
        return ENOMEM;
-
 
112
   
106
    if ((fd < 0) || (fd >= MAX_OPEN_FILES) || (files[fd] == NULL))
113
    if ((fd < 0) || (fd >= MAX_OPEN_FILES) || (files[fd] == NULL))
107
        return EBADF;
114
        return EBADF;
-
 
115
   
108
    vfs_file_delref(files[fd]);
116
    vfs_file_delref(files[fd]);
109
    files[fd] = NULL;
117
    files[fd] = NULL;
-
 
118
   
110
    return EOK;
119
    return EOK;
111
}
120
}
112
 
121
 
113
/** Increment reference count of VFS file structure.
122
/** Increment reference count of VFS file structure.
114
 *
123
 *
Line 148... Line 157...
148
 *
157
 *
149
 * @return      VFS file structure corresponding to fd.
158
 * @return      VFS file structure corresponding to fd.
150
 */
159
 */
151
vfs_file_t *vfs_file_get(int fd)
160
vfs_file_t *vfs_file_get(int fd)
152
{
161
{
-
 
162
    if (!vfs_files_init())
-
 
163
        return NULL;
-
 
164
   
153
    if ((fd >= 0) && (fd < MAX_OPEN_FILES))
165
    if ((fd >= 0) && (fd < MAX_OPEN_FILES))
154
        return files[fd];
166
        return files[fd];
-
 
167
   
155
    return NULL;
168
    return NULL;
156
}
169
}
157
 
170
 
158
/**
171
/**
159
 * @}
172
 * @}
160
 */
173
 */