LibC: Relax memmove() to memcpy() in more cases

`memcpy()` and `memmove()` are functionally equivalent if the source and
destination memory regions do not overlap. If this is the case, we
should prefer `memcpy()` as it's implemented in a simpler and faster
way. As our `memcpy()` starts copying from the lowest address first,
relaxing a `memmove()` call to `memcpy()` is safe if the destination:
- starts at a lower address than the source
- starts at a higher address than the source's end
This commit is contained in:
Daniel Bertalan 2023-07-06 23:43:00 +02:00 committed by Jelle Raaijmakers
parent 176baf7cdb
commit 210448b6ed
Notes: sideshowbarker 2024-07-17 02:55:44 +09:00

View file

@ -167,6 +167,8 @@ void* memset(void* dest_ptr, int c, size_t n)
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/memmove.html
void* memmove(void* dest, void const* src, size_t n)
{
if (dest < src)
return memcpy(dest, src, n);
if (((FlatPtr)dest - (FlatPtr)src) >= n)
return memcpy(dest, src, n);