Skip to content

Commit

Permalink
io: refactor for ref counting
Browse files Browse the repository at this point in the history
  • Loading branch information
jewelcodes committed Nov 28, 2024
1 parent c29f5b4 commit 2b97d6f
Show file tree
Hide file tree
Showing 5 changed files with 2 additions and 19 deletions.
4 changes: 0 additions & 4 deletions src/dirent.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,10 @@ int closedir(Thread *t, DIR *dir) {
if(!descriptor) return -EBADF;

// destroy the descriptor
if(!p->io[dd].clone)
free(p->io[dd].data);

p->io[dd].valid = false;
p->io[dd].type = 0;
p->io[dd].flags = 0;
p->io[dd].data = NULL;
p->io[dd].clone = false;
p->iodCount--;

return 0;
Expand Down
1 change: 1 addition & 0 deletions src/include/kernel/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ typedef struct {
char path[MAX_FILE_PATH]; // path relative to device mountpount
off_t position;
uint64_t id; // unique ID, this is for device files
int refCount;
} FileDescriptor;

/* file system syscalls */
Expand Down
2 changes: 1 addition & 1 deletion src/include/kernel/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
// TODO: decide whether to implement named pipes as files or an independent type

typedef struct IODescriptor {
bool valid, clone;
bool valid;
int type;
uint16_t flags;
void *data; // file or socket-specific data
Expand Down
11 changes: 0 additions & 11 deletions src/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ int openIO(void *pv, void **iodv) {
if(desc >= MAX_IO_DESCRIPTORS) return -EMFILE;

p->io[desc].valid = true;
p->io[desc].clone = false;
p->io[desc].type = IO_WAITING;
p->io[desc].data = NULL;

Expand Down Expand Up @@ -134,16 +133,6 @@ int close(Thread *t, int fd) {

if(!p->io[fd].valid || !p->io[fd].data) return -EBADF;

if(p->io[fd].clone) {
p->io[fd].valid = false;
p->io[fd].clone = false;
p->io[fd].type = 0;
p->io[fd].flags = 0;
p->io[fd].data = NULL;

return 0;
}

if(p->io[fd].type == IO_SOCKET) return closeSocket(t, fd);
else if(p->io[fd].type == IO_FILE) return closeFile(t, fd);
else return -EBADF;
Expand Down
3 changes: 0 additions & 3 deletions src/sched/fork.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,6 @@ pid_t fork(Thread *t) {
memcpy(p->io, parent->io, sizeof(IODescriptor) * MAX_IO_DESCRIPTORS);
p->iodCount = parent->iodCount;

for(int i = 0; i < MAX_IO_DESCRIPTORS; i++)
if(p->io[i].valid) p->io[i].clone = true;

// clone working directory
strcpy(p->cwd, parent->cwd);

Expand Down

0 comments on commit 2b97d6f

Please sign in to comment.