diff options
author | Alex Auvolat <alex.auvolat@ens.fr> | 2014-11-30 20:31:03 +0100 |
---|---|---|
committer | Alex Auvolat <alex.auvolat@ens.fr> | 2014-11-30 20:31:03 +0100 |
commit | c5cc8713b8541cd39c4bf0de4b364ce3908d9548 (patch) | |
tree | 255657380da939325853e7ed750f0a842343df5c | |
parent | a375561ca15a99dd7024e5b8170a3c8fcf25b892 (diff) | |
download | macroscope-c5cc8713b8541cd39c4bf0de4b364ce3908d9548.tar.gz macroscope-c5cc8713b8541cd39c4bf0de4b364ce3908d9548.zip |
Conformity of memset/memcpy ; implement memmove/memcmp.
-rw-r--r-- | kernel/include/string.h | 6 | ||||
-rw-r--r-- | kernel/lib/string.c | 33 |
2 files changed, 31 insertions, 8 deletions
diff --git a/kernel/include/string.h b/kernel/include/string.h index 1eb3eef..3cb0db3 100644 --- a/kernel/include/string.h +++ b/kernel/include/string.h @@ -3,8 +3,10 @@ #include <stddef.h> #include <stdint.h> -void *memcpy(void *dest, const void *src, int count); -void *memset(void *dest, int val, int count); +void *memcpy(void *dest, const void *src, size_t count); +void *memset(void *dest, int val, size_t count); +int memcmp(const void *s1, const void *s2, size_t n); +void *memmove(void *dest, const void *src, size_t count); size_t strlen(const char *str); char *strchr(const char *str, char c); diff --git a/kernel/lib/string.c b/kernel/lib/string.c index da8f60e..6d26478 100644 --- a/kernel/lib/string.c +++ b/kernel/lib/string.c @@ -38,12 +38,12 @@ int strcmp(const char *s1, const char *s2) { s1++; s2++; } - return (* (unsigned char*)s1 - *(unsigned char*)s2); + return (*(unsigned char*)s1 - *(unsigned char*)s2); } -void *memcpy(void *vd, const void *vs, int count) { +void *memcpy(void *vd, const void *vs, size_t count) { uint8_t *dest = (uint8_t*)vd, *src = (uint8_t*)vs; - int f = count % 4, n = count / 4, i; + size_t f = count % 4, n = count / 4, i; const uint32_t* s = (uint32_t*)src; uint32_t* d = (uint32_t*)dest; for (i = 0; i < n; i++) { @@ -57,10 +57,31 @@ void *memcpy(void *vd, const void *vs, int count) { return vd; } -void *memset(void *dest, int val, int count) { +void *memmove(void *vd, const void *vs, size_t count) { + uint8_t *dest = (uint8_t*)vd, *src = (uint8_t*)vs; + + if (vd < vs) { + for (size_t i = 0; i < count; i++) + dest[i] = src[i]; + } else { + for (size_t i = 0; i < count; i++) + dest[count - i] = src[count - i]; + } + return vd; +} + +int memcmp(const void *va, const void *vb, size_t count) { + uint8_t *a = (uint8_t*)va; + uint8_t *b = (uint8_t*)vb; + for (size_t i = 0; i < count; i++) { + if (a[i] != b[i]) return (int)a[i] - (int)b[i]; + } + return 0; +} + +void *memset(void *dest, int val, size_t count) { uint8_t *dest_c = (uint8_t*)dest; - int i; - for (i = 0; i < count; i++) { + for (size_t i = 0; i < count; i++) { dest_c[i] = val; } return dest; |