Re: str_repeat() patch
| From: | Alexander Valyalkin | Date: | Sat, 12 Jun 2004 14:27:45 +0000 |
| Subject: | Re: str_repeat() patch | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-10408@lists.php.net to get a copy of this message | ||
On Fri, 11 Jun 2004 17:00:03 +0200 (CEST), Derick Rethans <derick@php.net> wrote:
On Fri, 11 Jun 2004, Alexander Valyalkin wrote: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 tricksRead 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.
*/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/