From 2b03bc5645304d9160f0778582151a406f79d62d Mon Sep 17 00:00:00 2001 From: flysand7 Date: Tue, 12 Sep 2023 03:03:06 +1100 Subject: [PATCH] add str/memcpy functions --- cia.c | 3 +++ include/string.h | 13 ++++++++++++- src/stdlib-string/mem.c | 40 ++++++++++++++++++++++++++++++++++++++++ src/stdlib-string/str.c | 26 ++++++++++++++++++++++++++ tests/mm_seq_cst.c | 12 ++---------- 5 files changed, 83 insertions(+), 11 deletions(-) create mode 100644 src/stdlib-string/mem.c create mode 100644 src/stdlib-string/str.c diff --git a/cia.c b/cia.c index bff0250..4f99121 100644 --- a/cia.c +++ b/cia.c @@ -32,3 +32,6 @@ // Module stdlib_file #include "src/stdlib-file/file.c" +// Module stdlib_string +#include "src/stdlib-string/mem.c" +#include "src/stdlib-string/str.c" diff --git a/include/string.h b/include/string.h index 55103d5..11edb3e 100644 --- a/include/string.h +++ b/include/string.h @@ -1,3 +1,14 @@ -void memset(void *dest, int ch, size_t length); +#pragma once + +#include + +void *memcpy(void *restrict dst, void const *restrict src, size_t count); +void *memccpy(void *restrict dst, void const *restrict src, int c, size_t count); +void *memmove(void *dst, const void *src, size_t count) + +char *strcpy(char *restrict dst, char const *restrict src); +char *strncpy(char *restrict dst, char const *restrict src, size_t count); + +void memset(void *dst, int ch, size_t length); int _wcsicmp(uint16_t *s1, uint16_t *s2); diff --git a/src/stdlib-string/mem.c b/src/stdlib-string/mem.c new file mode 100644 index 0000000..776e80b --- /dev/null +++ b/src/stdlib-string/mem.c @@ -0,0 +1,40 @@ + +void *memcpy(void *restrict dst, void const *restrict src, size_t count) { + u8 *restrict dst_bytes = dst; + u8 const *restrict src_bytes = src; + for(size_t i = 0; i < count; ++i) { + dst_bytes[i] = src_bytes[i]; + } + return dst_bytes; +} + +void *memccpy(void *restrict dst, void const *restrict src, int c, size_t count) { + u8 *restrict dst_bytes = dst; + u8 const *restrict src_bytes = src; + for(size_t i = 0; i < count; ++i) { + dst_bytes[i] = src_bytes[i]; + if(src_bytes[i] == c) { + return (void *)&src[i+1]; + } + } + return NULL; +} + +void *memmove(void *dst, const void *src, size_t count) { + if(dst == src) { + return dst; + } + u8 *dst_bytes = dst; + u8 const *src_bytes = src; + if(dst < src) { + for(size_t i = 0; i < count; ++i) { + dst_bytes[i] = src_bytes[i]; + } + } + else { + for(size_t i = count-1; i-- != 0; ) { + dst_bytes[i] = src_bytes[i]; + } + } + return dst_bytes; +} diff --git a/src/stdlib-string/str.c b/src/stdlib-string/str.c new file mode 100644 index 0000000..99f7dfe --- /dev/null +++ b/src/stdlib-string/str.c @@ -0,0 +1,26 @@ + +char *strcpy(char *restrict dst, char const *restrict src) { + i64 str_len = 0; + for(i64 i = 0; src[i] != 0; ++i) { + dst[i] = src[i]; + str_len += 1; + } + dst[str_len] = 0; + return dst; +} + +char *strncpy(char *restrict dst, char const *restrict src, size_t count) { + i64 str_len = 0; + for(i64 i = 0; i < count; ++i) { + if(src[i] == 0) { + break; + } + dst[i] = src[i]; + str_len += 1; + } + for(i64 i = str_len; i < count; ++i) { + dst[i] = 0; + } + return dst; +} + diff --git a/tests/mm_seq_cst.c b/tests/mm_seq_cst.c index 0d0260e..c1c2130 100644 --- a/tests/mm_seq_cst.c +++ b/tests/mm_seq_cst.c @@ -12,7 +12,7 @@ #endif // Scheduler on linux has too coarse granularity -// to allow a thread to a very tiny bit of work +// to allow a thread to kind of a lot of work // before switching to another thread. If we // introduce an arbitrary delay, the threads will // be able to be run in a random order better. @@ -100,15 +100,7 @@ int main() { // Check to see the cnt variable int result = atomic_load_explicit(&g_shared_state.cnt, memory_order_relaxed); if(result == 0) { - char msg[] = "Result was 0\n"; - fwrite(msg, 1, sizeof msg, stdout); - } - else if(result == 1) { - char msg[] = "Result was 1\n"; - fwrite(msg, 1, sizeof msg, stdout); - } - else { - char msg[] = "Result was 2\n"; + char msg[] = "This shouldn't happen in sequentially-consistent model!\n"; fwrite(msg, 1, sizeof msg, stdout); } return 0;