
Hi Alessandro,
Alessandro Rubini a écrit :
- unsigned long *dl = (unsigned long *)dest, *sl = (unsigned long *)src;
Nitpick: Are you sure the casts are necessary here ?
Without the one on src it complains because of "const". So I write both for symetry.
Yes, of course, you and the compiler are absolutely right. Silly me, I completely overlooked the const :( To me casting away const is a cardinal sin (it rather defeats the object) and I find that the ease with which it can be done in C is frightening. So I feel obliged to (hopefully) correct my submission:
void *memcpy(void *dest, const void *src, size_t count) { char *d8; const char *s8; unsigned long *dl = dest; const unsigned long *sl = src;
/* while all data is aligned (common case), copy multiple bytes at a time */ if ( (((int)(long)dest | (int)(long)src) & (sizeof(*dl) - 1)) == 0) { while (count >= sizeof(*dl)) { *dl++ = *sl++; count -= sizeof(*dl); } }
d8 = (char *)dl; s8 = (const char *)sl;
/* copy any remaining data byte by byte */ while (count--) *d8++ = *s8++;
return dest; }
But this is still not even compile tested :( I never learn :(
Cheers, Chris