diff options
author | dzwdz | 2021-08-22 16:08:17 +0200 |
---|---|---|
committer | dzwdz | 2021-08-22 16:08:17 +0200 |
commit | 6d0a0443fecef518e4a01af5d6dad5a098999560 (patch) | |
tree | 1541d8fd7020804551345cec85f5164e3bb16efd | |
parent | e2075e56f875e077cd5c3d6e3c85368c1087654f (diff) |
virt_iter: count bytes that were already iterated over
-rw-r--r-- | src/kernel/mem.c | 3 | ||||
-rw-r--r-- | src/kernel/mem.h | 1 | ||||
-rw-r--r-- | src/kernel/syscalls.c | 24 |
3 files changed, 11 insertions, 17 deletions
diff --git a/src/kernel/mem.c b/src/kernel/mem.c index 7c1c640..aed2ff9 100644 --- a/src/kernel/mem.c +++ b/src/kernel/mem.c @@ -44,6 +44,8 @@ void virt_iter_new( struct pagedir *pages, bool user, bool writeable) { iter->frag = 0; + iter->frag_len = 0; + iter->prior = 0; iter->error = false; iter->_virt = virt; iter->_remaining = length; @@ -60,6 +62,7 @@ bool virt_iter_next(struct virt_iter *iter) { uintptr_t virt = (uintptr_t) iter->_virt; size_t partial = iter->_remaining; + iter->prior += iter->frag_len; if (partial <= 0) return false; // don't read past the page diff --git a/src/kernel/mem.h b/src/kernel/mem.h index d8496fd..c8192fd 100644 --- a/src/kernel/mem.h +++ b/src/kernel/mem.h @@ -19,6 +19,7 @@ void kfree(void *ptr); struct virt_iter { void *frag; size_t frag_len; + size_t prior; // sum of all prior frag_lens bool error; void *_virt; diff --git a/src/kernel/syscalls.c b/src/kernel/syscalls.c index ccf0a52..8ee90eb 100644 --- a/src/kernel/syscalls.c +++ b/src/kernel/syscalls.c @@ -69,16 +69,13 @@ fd_t _syscall_fs_open(const char *path, int len) { struct virt_iter iter; struct vfs_mount *mount = process_current->mount; static char buffer[PATH_MAX]; // holds the path - size_t pos = 0; if (len > PATH_MAX) return -1; // copy the path to the kernel virt_iter_new(&iter, (void*)path, len, process_current->pages, true, false); - while (virt_iter_next(&iter)) { - memcpy(buffer + pos, iter.frag, iter.frag_len); - pos += iter.frag_len; - } + while (virt_iter_next(&iter)) + memcpy(buffer + iter.prior, iter.frag, iter.frag_len); if (iter.error) return -1; len = path_simplify(buffer, buffer, len); @@ -106,17 +103,14 @@ int _syscall_mount(const char *path, int len, fd_t fd) { struct virt_iter iter; struct vfs_mount *mount; char *path_buf; - size_t pos = 0; if (len > PATH_MAX) return -1; // copy the path to the kernel - virt_iter_new(&iter, (void*)path, len, process_current->pages, true, false); path_buf = kmalloc(len); - while (virt_iter_next(&iter)) { // TODO abstract away - memcpy(path_buf + pos, iter.frag, iter.frag_len); - pos += iter.frag_len; - } + virt_iter_new(&iter, (void*)path, len, process_current->pages, true, false); + while (virt_iter_next(&iter)) + memcpy(path_buf + iter.prior, iter.frag, iter.frag_len); if (iter.error) goto fail; // simplify it @@ -138,14 +132,10 @@ fail: int _syscall_debuglog(const char *msg, size_t len) { struct virt_iter iter; - size_t written = 0; - virt_iter_new(&iter, (void*)msg, len, process_current->pages, true, false); - while (virt_iter_next(&iter)) { + while (virt_iter_next(&iter)) tty_write(iter.frag, iter.frag_len); - written += iter.frag_len; - } - return written; + return iter.prior; } int syscall_handler(int num, int a, int b, int c) { |