diff options
author | johannst <johannes.stoelp@gmail.com> | 2021-04-20 22:05:13 +0200 |
---|---|---|
committer | johannst <johannes.stoelp@gmail.com> | 2021-04-20 22:05:13 +0200 |
commit | 1df4dfdcdbbaef7e5b32c5c0bfadec02b3ccd6f0 (patch) | |
tree | 467a166384174cf4877aa9492f31e194f3ab82e8 /lib | |
parent | 3155439c1a96f1964aaee799b008331d0b362db3 (diff) | |
download | dynld-1df4dfdcdbbaef7e5b32c5c0bfadec02b3ccd6f0.tar.gz dynld-1df4dfdcdbbaef7e5b32c5c0bfadec02b3ccd6f0.zip |
added memcpy + tests for memcpy/memset
Diffstat (limited to 'lib')
-rw-r--r-- | lib/include/common.h | 2 | ||||
-rw-r--r-- | lib/src/common.c | 20 |
2 files changed, 22 insertions, 0 deletions
diff --git a/lib/include/common.h b/lib/include/common.h index 1e48097..d006d71 100644 --- a/lib/include/common.h +++ b/lib/include/common.h @@ -15,3 +15,5 @@ void* memset(void* s, int c, size_t n); +void* memcpy(void* d, const void* s, size_t n); + diff --git a/lib/src/common.c b/lib/src/common.c index 2d122f5..dd806bf 100644 --- a/lib/src/common.c +++ b/lib/src/common.c @@ -16,3 +16,23 @@ void* memset(void* s, int c, size_t n) { : "memory"); return s; } + +void* memcpy(void* d, const void* s, size_t n) { + // When `d` points into `[s, s+n[` we would override `s` while copying into `d`. + // |------------|--------| + // s d s+n + // -> We don't support. + // + // When `d` points into `]s-n, s[` it is destructive for `s` but all data + // from `s` are copied into `d`. The user gets what he asks for. + // -> Supported. + ERROR_ON(s <= d && d < (void*)((unsigned char*)s + n), "memcpy: Unsupported overlap!"); + asm volatile( + "cld" + "\n" + "rep movsb" + : "+D"(d), "+S"(s), "+c"(n) + : + : "memory"); + return d; +} |