40,6 → 40,8 |
#include <string.h> |
#include <assert.h> |
#include <bool.h> |
#include <fibril.h> |
#include <fibril_sync.h> |
#include "vfs.h" |
|
/** |
55,9 → 57,9 |
* first VFS_OPEN operation. |
* |
* This resource being per-connection and, in the first place, per-fibril, we |
* don't need to protect it by a futex. |
* don't need to protect it by a mutex. |
*/ |
__thread vfs_file_t **files = NULL; |
fibril_local vfs_file_t **files = NULL; |
|
/** Initialize the table of open files. */ |
bool vfs_files_init(void) |
78,19 → 80,23 |
*/ |
int vfs_fd_alloc(void) |
{ |
int i; |
|
if (!vfs_files_init()) |
return ENOMEM; |
|
unsigned int i; |
for (i = 0; i < MAX_OPEN_FILES; i++) { |
if (!files[i]) { |
files[i] = (vfs_file_t *) malloc(sizeof(vfs_file_t)); |
if (!files[i]) |
return ENOMEM; |
|
memset(files[i], 0, sizeof(vfs_file_t)); |
futex_initialize(&files[i]->lock, 1); |
fibril_mutex_initialize(&files[i]->lock); |
vfs_file_addref(files[i]); |
return i; |
return (int) i; |
} |
} |
|
return EMFILE; |
} |
|
103,10 → 109,15 |
*/ |
int vfs_fd_free(int fd) |
{ |
if ((fd >= MAX_OPEN_FILES) || (files[fd] == NULL)) |
if (!vfs_files_init()) |
return ENOMEM; |
|
if ((fd < 0) || (fd >= MAX_OPEN_FILES) || (files[fd] == NULL)) |
return EBADF; |
|
vfs_file_delref(files[fd]); |
files[fd] = NULL; |
|
return EOK; |
} |
|
150,11 → 161,15 |
*/ |
vfs_file_t *vfs_file_get(int fd) |
{ |
if (fd < MAX_OPEN_FILES) |
if (!vfs_files_init()) |
return NULL; |
|
if ((fd >= 0) && (fd < MAX_OPEN_FILES)) |
return files[fd]; |
|
return NULL; |
} |
|
/** |
* @} |
*/ |
*/ |