From 86d2e0776812650bdeef4c8d9d70aaae6746ad95 Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Thu, 3 Apr 2025 16:47:01 -0400 Subject: volatile string fns --- include/string.h | 32 ++++++++++++++++++++++++++++++++ lib/memcpyv.c | 11 +++++++++++ lib/memmovev.c | 20 ++++++++++++++++++++ lib/memsetv.c | 10 ++++++++++ 4 files changed, 73 insertions(+) create mode 100644 lib/memcpyv.c create mode 100644 lib/memmovev.c create mode 100644 lib/memsetv.c diff --git a/include/string.h b/include/string.h index 4d32f57..e376918 100644 --- a/include/string.h +++ b/include/string.h @@ -51,6 +51,38 @@ extern void *memmove(void *restrict dest, const void *restrict src, size_t n); */ extern void *memset(void *restrict dest, int c, size_t n); +/** + * Copy the first n bytes from memory area src to memory area dest. The memory + * areas must not overlap. + * @param dest - the destination + * @param src - the source + * @param n - the byte count + * @returns a pointer to dest + */ +extern void *memcpyv(volatile void *restrict dest, + const volatile void *restrict src, size_t n); + +/** + * Copy the first n bytes from memory area src to memory area dest. The memory + * areas may overlap; memmove behaves as though the bytes are first copied to a + * temporary array. + * @param dest - the destination + * @param src - the source + * @param n - the byte count + * @returns a pointer to dest + */ +extern void *memmovev(volatile void *restrict dest, + const volatile void *restrict src, size_t n); + +/** + * Fill the first n bytes of the memory region dest with the constant byte c. + * @param dest - the destination + * @param c - the byte to write + * @param n - the byte count + * @returns a pointer to dest + */ +extern void *memsetv(volatile void *restrict dest, int c, size_t n); + /** * Calculates the length of the string pointed to by str, excluding * the terminating null byte diff --git a/lib/memcpyv.c b/lib/memcpyv.c new file mode 100644 index 0000000..205d9c1 --- /dev/null +++ b/lib/memcpyv.c @@ -0,0 +1,11 @@ +#include + +void *memcpyv(volatile void *restrict dest, const volatile void *restrict src, + size_t n) +{ + char *d = dest; + const char *s = src; + for (; n; n--) + *d++ = *s++; + return dest; +} diff --git a/lib/memmovev.c b/lib/memmovev.c new file mode 100644 index 0000000..d2fc683 --- /dev/null +++ b/lib/memmovev.c @@ -0,0 +1,20 @@ +#include + +void *memmovev(volatile void *dest, const volatile void *src, size_t n) +{ + char *d = dest; + const char *s = src; + + if (d == s) + return d; + + if (d < s) { + for (; n; n--) + *d++ = *s++; + } else { + while (n) + n--, d[n] = s[n]; + } + + return dest; +} diff --git a/lib/memsetv.c b/lib/memsetv.c new file mode 100644 index 0000000..9039cb7 --- /dev/null +++ b/lib/memsetv.c @@ -0,0 +1,10 @@ +#include + +void *memsetv(volatile void *dest, int c, size_t n) +{ + unsigned char *d = dest; + for (; n; n--) { + *d++ = c; + }; + return dest; +} -- cgit v1.2.3-freya