From 7acf810e81491b08410df1b7bb886bd11b44cd98 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Sun, 14 Nov 2021 17:46:20 +0100 Subject: shared: use a single implementation of mem* functions everywhere --- src/init/stdlib.c | 26 -------------------------- src/init/stdlib.h | 4 +--- src/kernel/arch/i386/gdt/gdt.c | 2 +- src/kernel/arch/i386/pagedir.c | 2 +- src/kernel/mem/virt.c | 1 + src/kernel/proc.c | 2 +- src/kernel/syscalls.c | 1 + src/kernel/tests/util.c | 2 +- src/kernel/tests/vfs.c | 2 +- src/kernel/util.c | 28 ---------------------------- src/kernel/util.h | 5 ----- src/kernel/vfs/mount.c | 2 +- src/kernel/vfs/path.c | 1 + src/kernel/vfs/root.c | 2 +- src/shared/mem.c | 28 ++++++++++++++++++++++++++++ src/shared/mem.h | 7 +++++++ 16 files changed, 46 insertions(+), 69 deletions(-) delete mode 100644 src/kernel/util.c create mode 100644 src/shared/mem.c create mode 100644 src/shared/mem.h (limited to 'src') diff --git a/src/init/stdlib.c b/src/init/stdlib.c index 3d9d8c3..c83440f 100644 --- a/src/init/stdlib.c +++ b/src/init/stdlib.c @@ -5,32 +5,6 @@ int __tty_fd; -void *memcpy(void *dest, const void *src, size_t n) { - char *d = dest; - const char *s = src; - for (size_t i = 0; i < n; i++) - d[i] = s[i]; - return dest; -} - -void *memset(void *s, int c, size_t n) { - uint8_t *s2 = s; - for (size_t i = 0; i < n; n++) - s2[i] = c; - return s; -} - -int memcmp(const void *s1, const void *s2, size_t n) { - const unsigned char *c1 = s1, *c2 = s2; - for (size_t i = 0; i < n; i++) { - if (c1[i] != c2[i]) { - if (c1[i] < c2[i]) return -1; - else return 1; - } - } - return 0; -} - int strcmp(const char *s1, const char *s2) { while (*s1) { if (*s1 != *s2) { diff --git a/src/init/stdlib.h b/src/init/stdlib.h index 5049a23..af0788b 100644 --- a/src/init/stdlib.h +++ b/src/init/stdlib.h @@ -1,11 +1,9 @@ #pragma once +#include #include extern int __tty_fd; -void *memcpy(void *dest, const void *src, size_t n); -void *memset(void *s, int c, size_t n); -int memcmp(const void *s1, const void *s2, size_t n); int strcmp(const char *s1, const char *s2); size_t strlen(const char *s); int printf(const char *fmt, ...); diff --git a/src/kernel/arch/i386/gdt/gdt.c b/src/kernel/arch/i386/gdt/gdt.c index 6958bb5..56df995 100644 --- a/src/kernel/arch/i386/gdt/gdt.c +++ b/src/kernel/arch/i386/gdt/gdt.c @@ -1,6 +1,6 @@ #include #include -#include +#include #include #include diff --git a/src/kernel/arch/i386/pagedir.c b/src/kernel/arch/i386/pagedir.c index 5dd36ec..88f6ebb 100644 --- a/src/kernel/arch/i386/pagedir.c +++ b/src/kernel/arch/i386/pagedir.c @@ -1,6 +1,6 @@ #include #include -#include +#include #include /* nitpick: I highly recommend you dont use bitfields for paging diff --git a/src/kernel/mem/virt.c b/src/kernel/mem/virt.c index b15297e..f0dca06 100644 --- a/src/kernel/mem/virt.c +++ b/src/kernel/mem/virt.c @@ -1,6 +1,7 @@ #include #include #include +#include void virt_iter_new( struct virt_iter *iter, void __user *virt, size_t length, diff --git a/src/kernel/proc.c b/src/kernel/proc.c index aebd509..e67af19 100644 --- a/src/kernel/proc.c +++ b/src/kernel/proc.c @@ -3,8 +3,8 @@ #include #include #include -#include #include +#include #include struct process *process_first; diff --git a/src/kernel/syscalls.c b/src/kernel/syscalls.c index 9a9b5d6..9948506 100644 --- a/src/kernel/syscalls.c +++ b/src/kernel/syscalls.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include diff --git a/src/kernel/tests/util.c b/src/kernel/tests/util.c index 3efbee8..05bba24 100644 --- a/src/kernel/tests/util.c +++ b/src/kernel/tests/util.c @@ -1,6 +1,6 @@ #include #include -#include +#include TEST(memcmp) { // basic equality checks diff --git a/src/kernel/tests/vfs.c b/src/kernel/tests/vfs.c index 339c6bd..8b749b4 100644 --- a/src/kernel/tests/vfs.c +++ b/src/kernel/tests/vfs.c @@ -1,9 +1,9 @@ #include #include #include -#include #include #include +#include TEST(path_simplify) { #define TEST_WRAPPER(argument, result) do { \ diff --git a/src/kernel/util.c b/src/kernel/util.c deleted file mode 100644 index 0977745..0000000 --- a/src/kernel/util.c +++ /dev/null @@ -1,28 +0,0 @@ -#include -#include - -void *memcpy(void *dest, const void *src, size_t n) { - char *d = dest; - const char *s = src; - for (size_t i = 0; i < n; i++) - d[i] = s[i]; - return dest; -} - -void *memset(void *s, int c, size_t n) { - uint8_t *s2 = s; - for (size_t i = 0; i < n; n++) - s2[i] = c; - return s; -} - -int memcmp(const void *s1, const void *s2, size_t n) { - const unsigned char *c1 = s1, *c2 = s2; - for (size_t i = 0; i < n; i++) { - if (c1[i] != c2[i]) { - if (c1[i] < c2[i]) return -1; - else return 1; - } - } - return 0; -} diff --git a/src/kernel/util.h b/src/kernel/util.h index 839aa49..9b8cb3d 100644 --- a/src/kernel/util.h +++ b/src/kernel/util.h @@ -4,11 +4,6 @@ #define __NUM2STR(x) #x #define NUM2STR(x) __NUM2STR(x) -void *memcpy(void *dest, const void *src, size_t n); -void *memset(void *s, int c, size_t n); - -int memcmp(const void *s1, const void *s2, size_t n); - // see https://gcc.gnu.org/onlinedocs/gcc/Typeof.html #define min(a,b) ({ \ typeof (a) _a = (a); \ diff --git a/src/kernel/vfs/mount.c b/src/kernel/vfs/mount.c index b6b3e8d..c962ba5 100644 --- a/src/kernel/vfs/mount.c +++ b/src/kernel/vfs/mount.c @@ -1,6 +1,6 @@ #include -#include #include +#include struct vfs_mount *vfs_mount_seed(void) { struct vfs_mount *mount = kmalloc(sizeof *mount); diff --git a/src/kernel/vfs/path.c b/src/kernel/vfs/path.c index 096d3fe..df5f00f 100644 --- a/src/kernel/vfs/path.c +++ b/src/kernel/vfs/path.c @@ -1,5 +1,6 @@ #include #include +#include int path_simplify(const char *in, char *out, size_t len) { if (len == 0) return -1; // empty paths are invalid diff --git a/src/kernel/vfs/root.c b/src/kernel/vfs/root.c index 834bed7..adf1bfe 100644 --- a/src/kernel/vfs/root.c +++ b/src/kernel/vfs/root.c @@ -1,8 +1,8 @@ #include #include #include -#include #include +#include enum { HANDLE_ROOT, diff --git a/src/shared/mem.c b/src/shared/mem.c new file mode 100644 index 0000000..1b483fe --- /dev/null +++ b/src/shared/mem.c @@ -0,0 +1,28 @@ +#include +#include + +int memcmp(const void *s1, const void *s2, size_t n) { + const unsigned char *c1 = s1, *c2 = s2; + for (size_t i = 0; i < n; i++) { + if (c1[i] != c2[i]) { + if (c1[i] < c2[i]) return -1; + else return 1; + } + } + return 0; +} + +void *memcpy(void *dest, const void *src, size_t n) { + char *d = dest; + const char *s = src; + for (size_t i = 0; i < n; i++) + d[i] = s[i]; + return dest; +} + +void *memset(void *s, int c, size_t n) { + uint8_t *s2 = s; + for (size_t i = 0; i < n; n++) + s2[i] = c; + return s; +} diff --git a/src/shared/mem.h b/src/shared/mem.h new file mode 100644 index 0000000..682c184 --- /dev/null +++ b/src/shared/mem.h @@ -0,0 +1,7 @@ +#pragma once +#include + +int memcmp(const void *s1, const void *s2, size_t n); + +void *memcpy(void *dest, const void *src, size_t n); +void *memset(void *s, int c, size_t n); -- cgit v1.2.3