On Fri, 11 Jun 2004, Alexander Valyalkin wrote:
Read it:
void *memmove(void *s1, const void *s2, size_t n);
If s1 and s2 overlap, all bytes are copied in a preserving manner (unlike
memcpy())
And anwer the question: are s1 and s2 overlap in the str_repeat() ? Why don't use memcpy() instead of memmove() ?
Because it's slower and we're copying to newly allocated memory anyway so things CAN not overlap. Also, your patch is the wrong way around.
Mybe I'm mistaken, but I think, that memmove() looks like that (without any error checks):
void *memmove(void *dst, void *src, size_t len)
{
/* check overlapping of src & dst */
size_t distance = (char *)dst > (char *)src ? (char *)dst - (char *)src : (char *)src - (char *)dst;
if (distance < len) {
/* overlap. Use temporary buffer & memcpy(). Here could be used
byte-to-byte copying or any oher tricks
*/
char *tmp = malloc(len);
memcpy(tmp, src, len);
memcpy(dst, tmp, len);
free(tmp);
} else {
/* not overlap. Use memcpy() */
memcpy(dst, src, len);
}
return dst;
}
Are you still think that memcpy() is slower than memmove() ? I would tell that memcpy() at least not more slowly than memmove().
-- Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
-- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php