From 135bc33b570940a7e949aa9927e8f33d195ca2b0 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Fri, 15 May 2020 14:54:39 +0100 Subject: [PATCH] Implemented memset() --- photon/libc/include/string.h | 1 + photon/libc/string/memset.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 photon/libc/string/memset.c diff --git a/photon/libc/include/string.h b/photon/libc/include/string.h index a0b904f..7e816e3 100644 --- a/photon/libc/include/string.h +++ b/photon/libc/include/string.h @@ -10,6 +10,7 @@ extern "C" { extern void *memcpy(void *dest, const void *src, size_t sz); extern void *memcmp(const void *a, const void *b, size_t sz); extern void *memmove(void *dest, const void *src, size_t sz); +extern void *memset(void *ptr, int value, size_t sz); extern size_t strlen(const char *str); extern int strcmp(const char *a, const char *b); diff --git a/photon/libc/string/memset.c b/photon/libc/string/memset.c new file mode 100644 index 0000000..35b5cc7 --- /dev/null +++ b/photon/libc/string/memset.c @@ -0,0 +1,28 @@ +#include + +void *memset(void *ptr, int value, size_t sz) +{ + value = (unsigned char)value; + unsigned int realval = + value | + (value << 8) | + (value << 16) | + (value << 24); + + unsigned char *buf = ptr; + size_t i = 0; + for (i = 0; i < sz; i += sizeof(unsigned int)) { + unsigned int *t = (unsigned int *)&buf[i]; + *t = realval; + } + + if (sz % sizeof(unsigned int)) { + i -= sizeof(unsigned int); + + while (i < sz) { + buf[i++] = value; + } + } + + return ptr; +}