diff options
Diffstat (limited to 'kernel')
-rw-r--r-- | kernel/string.c | 42 | ||||
-rw-r--r-- | kernel/string.h | 3 |
2 files changed, 45 insertions, 0 deletions
diff --git a/kernel/string.c b/kernel/string.c index d038c27..006b907 100644 --- a/kernel/string.c +++ b/kernel/string.c @@ -7,3 +7,45 @@ size_t strlen(const char *str) len++; return len; } + +/* + Copy bytes in memory. If copying takes place between objects + that overlap, the behavior is undefined. +*/ +void *memcpy(void *dest, const void *src, size_t n) +{ + size_t i; + const char *s = src; + char *d = dest; + + for(i = 0; i < n; i++) + d[i] = s[i]; + return dest; +} + +void *memset(void *dest, int c, size_t n) +{ + char *d = dest; + while(n--) + *d++ = c; + return dest; +} + +/* + Copy bytes in memory with overlapping areas. +*/ +void *memmove(void *dest, const void *src, size_t n) +{ + const char *s = src; + char *d = dest; + + if(dest == src) + return dest; + + if(dest < src) + return memcpy(dest, src, n); + + while(n--) + d[n] = s[n]; + return dest; +} diff --git a/kernel/string.h b/kernel/string.h index dc760a5..3c628b3 100644 --- a/kernel/string.h +++ b/kernel/string.h @@ -5,5 +5,8 @@ #include <stdint.h> size_t strlen(const char *str); +void *memcpy(void *dest, const void *src, size_t n); +void *memset(void *dest, int c, size_t n); +void *memmove(void *dest, const void *src, size_t n); #endif /* KERNEL_STRING */ |