add str/memcpy functions

This commit is contained in:
flysand7 2023-09-12 03:03:06 +11:00
parent a0a32bdb7c
commit 2b03bc5645
5 changed files with 83 additions and 11 deletions

3
cia.c
View File

@ -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"

View File

@ -1,3 +1,14 @@
void memset(void *dest, int ch, size_t length);
#pragma once
#include <cia/def.h>
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);

40
src/stdlib-string/mem.c Normal file
View File

@ -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;
}

26
src/stdlib-string/str.c Normal file
View File

@ -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;
}

View File

@ -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;