Skip to content
Snippets Groups Projects
Commit 7f399cca authored by rsc's avatar rsc
Browse files

use ftable like btable and itable

parent ce72cadb
No related branches found
No related tags found
No related merge requests found
...@@ -6,31 +6,32 @@ ...@@ -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) ...@@ -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) ...@@ -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.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment