C库函数之memcpy的实现
memcpy的实现方式是当满足四字节对齐时,进行四字节的拷贝,不满足时进行单字节的拷贝。例如拷贝10个字节,循环两次拷贝四字节,在循环两次拷贝一字节。
void mem_memcpy(void* dst, const void* src, unsigned int cnt)
{
char *d = (char*)(void *)dst;
const char *s = src;
while (cnt >= sizeof (int)) {
*(int*)d = *(int*)s;
d += sizeof (int);
s += sizeof (int);
cnt -= sizeof (int);
}
while ((cnt) > 0U){
*d = *s;
d += 1U;
s += 1U;
cnt -= 1U;
}
}
end