base: fix memmove implementation

Also make 'memcpy' to behave like memmove on region overlap.

Issue #604
This commit is contained in:
Sebastian Sumpf 2013-01-10 15:43:55 +01:00 committed by Norman Feske
parent e8c063a8b4
commit 1a30bac3ea
1 changed files with 27 additions and 7 deletions

View File

@ -49,6 +49,29 @@ namespace Genode {
}
/**
* Simple memmove
*
* \param dst destination memory block
* \param src source memory block
* \param size number of bytes to move
*
* \return pointer to destination memory block
*/
inline void *memmove(void *dst, const void *src, size_t size)
{
char *d = (char *)dst, *s = (char *)src;
size_t i;
if (s > d)
for (i = 0; i < size; i++, *d++ = *s++);
else
for (s += size - 1, d += size - 1, i = size; i-- > 0; *d-- = *s--);
return dst;
}
/**
* Copy memory block
*
@ -63,6 +86,10 @@ namespace Genode {
char *d = (char *)dst, *s = (char *)src;
size_t i;
/* check for overlap */
if ((d + size > s) && (s + size > d))
return memmove(dst, src, size);
/* try cpu specific version first */
if ((i = size - memcpy_cpu(dst, src, size)) == size)
return dst;
@ -86,13 +113,6 @@ namespace Genode {
}
/**
* Memmove wrapper for sophisticated overlapping-aware memcpy
*/
inline void *memmove(void *dst, const void *src, size_t size) {
return memcpy(dst, src, size); }
/**
* Copy string
*