summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordzwdz2021-09-04 18:17:35 +0200
committerdzwdz2021-09-04 18:17:35 +0200
commitc4531fa5e1aa16b7ba74de2975eadf06b3052b78 (patch)
tree7538d728e7828bc3661052c6a181d8f365fc3df7
parent400f218cdad6bceaed9843d4d44ab66593da591a (diff)
refactor finding empty handles to a function
-rw-r--r--src/kernel/proc.c10
-rw-r--r--src/kernel/proc.h1
-rw-r--r--src/kernel/syscalls.c6
3 files changed, 12 insertions, 5 deletions
diff --git a/src/kernel/proc.c b/src/kernel/proc.c
index 19b09a8..e504a5d 100644
--- a/src/kernel/proc.c
+++ b/src/kernel/proc.c
@@ -90,3 +90,13 @@ struct process *_process_find_recursive(
struct process *process_find(enum process_state target) {
return _process_find_recursive(target, process_first);
}
+
+handle_t process_find_handle(struct process *proc) {
+ handle_t handle;
+ for (handle = 0; handle < HANDLE_MAX; handle++) {
+ if (proc->handles[handle].type == HANDLE_EMPTY)
+ break;
+ }
+ if (handle == HANDLE_MAX) handle = -1;
+ return handle;
+}
diff --git a/src/kernel/proc.h b/src/kernel/proc.h
index ed7c656..447dac8 100644
--- a/src/kernel/proc.h
+++ b/src/kernel/proc.h
@@ -43,3 +43,4 @@ _Noreturn void process_switch(struct process *proc);
_Noreturn void process_switch_any(void); // switches to any running process
struct process *process_find(enum process_state);
+handle_t process_find_handle(struct process *proc); // finds the first free handle
diff --git a/src/kernel/syscalls.c b/src/kernel/syscalls.c
index 98df4f3..002f3fd 100644
--- a/src/kernel/syscalls.c
+++ b/src/kernel/syscalls.c
@@ -75,11 +75,7 @@ handle_t _syscall_fs_open(const user_ptr path, int len) {
if (len > PATH_MAX) return -1;
// find the first free handle
- for (handle = 0; handle < HANDLE_MAX; handle++) {
- if (process_current->handles[handle].type == HANDLE_EMPTY)
- break;
- }
- if (handle == HANDLE_MAX) return -1;
+ handle = process_find_handle(process_current);
// copy the path to the kernel
virt_iter_new(&iter, path, len, process_current->pages, true, false);