提交 13fb63d6 创建 作者: Nickolai Zeldovich's avatar Nickolai Zeldovich

get rid of ftable

上级 f1d37b91
...@@ -8,44 +8,28 @@ ...@@ -8,44 +8,28 @@
#include "stat.h" #include "stat.h"
struct devsw __attribute__ ((aligned (CACHELINE))) devsw[NDEV]; struct devsw __attribute__ ((aligned (CACHELINE))) devsw[NDEV];
struct {
struct spinlock lock;
struct file file[NFILE];
} __attribute__ ((aligned (CACHELINE))) ftable;
void void
fileinit(void) fileinit(void)
{ {
initlock(&ftable.lock, "ftable");
} }
// Allocate a file structure. // Allocate a file structure.
struct file* struct file*
filealloc(void) filealloc(void)
{ {
struct file *f; struct file *f = kmalloc(sizeof(struct file));
f->ref = 1;
acquire(&ftable.lock); return f;
for(f = ftable.file; f < ftable.file + NFILE; f++){
if(f->ref == 0){
f->ref = 1;
release(&ftable.lock);
return f;
}
}
release(&ftable.lock);
return 0;
} }
// Increment ref count for file f. // Increment ref count for file f.
struct file* struct file*
filedup(struct file *f) filedup(struct file *f)
{ {
acquire(&ftable.lock);
if(f->ref < 1) if(f->ref < 1)
panic("filedup"); panic("filedup");
f->ref++; __sync_fetch_and_add(&f->ref, 1);
release(&ftable.lock);
return f; return f;
} }
...@@ -53,24 +37,14 @@ filedup(struct file *f) ...@@ -53,24 +37,14 @@ filedup(struct file *f)
void void
fileclose(struct file *f) fileclose(struct file *f)
{ {
struct file ff; if (__sync_sub_and_fetch(&f->ref, 1) > 0)
acquire(&ftable.lock);
if(f->ref < 1)
panic("fileclose");
if(--f->ref > 0){
release(&ftable.lock);
return; return;
}
ff = *f; if(f->type == FD_PIPE)
f->ref = 0; pipeclose(f->pipe, f->writable);
f->type = FD_NONE; else if(f->type == FD_INODE)
release(&ftable.lock); iput(f->ip);
kmfree(f);
if(ff.type == FD_PIPE)
pipeclose(ff.pipe, ff.writable);
else if(ff.type == FD_INODE)
iput(ff.ip);
} }
// Get metadata about file f. // Get metadata about file f.
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论