use ftable like btable and itable

This commit is contained in:
rsc 2009-05-31 02:07:26 +00:00
parent ce72cadbe0
commit 7f399ccaa4

43
file.c
View file

@ -6,31 +6,32 @@
#include "dev.h" #include "dev.h"
struct devsw devsw[NDEV]; struct devsw devsw[NDEV];
struct spinlock file_table_lock; struct {
struct file file[NFILE]; struct spinlock lock;
struct file file[NFILE];
} ftable;
void void
fileinit(void) fileinit(void)
{ {
initlock(&file_table_lock, "file_table"); initlock(&ftable.lock, "file_table");
} }
// Allocate a file structure. // Allocate a file structure.
struct file* struct file*
filealloc(void) filealloc(void)
{ {
int i; struct file *f;
acquire(&file_table_lock); acquire(&ftable.lock);
for(i = 0; i < NFILE; i++){ for(f = ftable.file; f < ftable.file + NFILE; f++){
if(file[i].type == FD_CLOSED){ if(f->ref == 0){
file[i].type = FD_NONE; f->ref = 1;
file[i].ref = 1; release(&ftable.lock);
release(&file_table_lock); return f;
return file + i;
} }
} }
release(&file_table_lock); release(&ftable.lock);
return 0; return 0;
} }
@ -38,11 +39,11 @@ filealloc(void)
struct file* struct file*
filedup(struct file *f) filedup(struct file *f)
{ {
acquire(&file_table_lock); acquire(&ftable.lock);
if(f->ref < 1 || f->type == FD_CLOSED) if(f->ref < 1)
panic("filedup"); panic("filedup");
f->ref++; f->ref++;
release(&file_table_lock); release(&ftable.lock);
return f; return f;
} }
@ -52,24 +53,22 @@ fileclose(struct file *f)
{ {
struct file ff; struct file ff;
acquire(&file_table_lock); acquire(&ftable.lock);
if(f->ref < 1 || f->type == FD_CLOSED) if(f->ref < 1)
panic("fileclose"); panic("fileclose");
if(--f->ref > 0){ if(--f->ref > 0){
release(&file_table_lock); release(&ftable.lock);
return; return;
} }
ff = *f; ff = *f;
f->ref = 0; f->ref = 0;
f->type = FD_CLOSED; f->type = FD_NONE;
release(&file_table_lock); release(&ftable.lock);
if(ff.type == FD_PIPE) if(ff.type == FD_PIPE)
pipeclose(ff.pipe, ff.writable); pipeclose(ff.pipe, ff.writable);
else if(ff.type == FD_INODE) else if(ff.type == FD_INODE)
iput(ff.ip); iput(ff.ip);
else
panic("fileclose");
} }
// Get metadata about file f. // Get metadata about file f.