diff options
author | Freya Murphy <freya@freyacat.org> | 2025-03-27 11:39:12 -0400 |
---|---|---|
committer | Freya Murphy <freya@freyacat.org> | 2025-03-27 11:39:12 -0400 |
commit | 0ff301cda68669c59351e5854ce98f2cf460543f (patch) | |
tree | cfe8f976261962420ada64b821559b9da0a56841 /lib/blkmov.c | |
parent | add compile_flags.txt for clangd lsp (diff) | |
download | comus-0ff301cda68669c59351e5854ce98f2cf460543f.tar.gz comus-0ff301cda68669c59351e5854ce98f2cf460543f.tar.bz2 comus-0ff301cda68669c59351e5854ce98f2cf460543f.zip |
pull upstream changes, add auto formatting
Diffstat (limited to 'lib/blkmov.c')
-rw-r--r-- | lib/blkmov.c | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/lib/blkmov.c b/lib/blkmov.c new file mode 100644 index 0000000..6423810 --- /dev/null +++ b/lib/blkmov.c @@ -0,0 +1,57 @@ +/** +** @file blkmov.c +** +** @author Numerous CSCI-452 classes +** +** @brief C implementations of common library functions +*/ + +#ifndef BLKMOV_SRC_INC +#define BLKMOV_SRC_INC + +#include <common.h> + +#include <lib.h> + +/** +** blkmov(dst,src,len) +** +** Copy a word-aligned block from src to dst. Deals with overlapping +** buffers. +** +** @param dst Destination buffer +** @param src Source buffer +** @param len Buffer size (in bytes) +*/ +void blkmov(void *dst, const void *src, register uint32_t len) +{ + // verify that the addresses are aligned and + // the length is a multiple of four bytes + if ((((uint32_t)dst) & 0x3) != 0 || (((uint32_t)src) & 0x3) != 0 || + (len & 0x3) != 0) { + // something isn't aligned, so just use memmove() + memmove(dst, src, len); + return; + } + + // everything is nicely aligned, so off we go + register uint32_t *dest = dst; + register const uint32_t *source = src; + + // now copying 32-bit values + len /= 4; + + if (source < dest && (source + len) > dest) { + source += len; + dest += len; + while (len-- > 0) { + *--dest = *--source; + } + } else { + while (len--) { + *dest++ = *source++; + } + } +} + +#endif |