void *memcpy(void *dst, const void *src, size_t count)
{
void * ret = dst;
while (count--) {
*(char *)dst = *(char *)src;
dst = (char *)dst + 1;
src = (char *)src + 1;
}
return(ret);
}
void *memcpy(void *dstpp,const void *srcpp,size_t len)
{
unsigned long int dstp = (long int) dstpp;
unsigned long int srcp = (long int) srcpp;
/* Copy from the beginning to the end. */
/* If there not too few bytes to copy, use word copy. */
if (len >= OP_T_THRES)
{
/* Copy just a few bytes to make DSTP aligned. */
len -= (-dstp) % OPSIZ;
BYTE_COPY_FWD (dstp, srcp, (-dstp) % OPSIZ);
/* Copy from SRCP to DSTP taking advantage of the known
alignment of DSTP. Number of bytes remaining is put
in the third argumnet, i.e. in LEN. This number may
vary from machine to machine. */
WORD_COPY_FWD (dstp, srcp, len, len);
/* Fall out and copy the tail. */
}
/* There are just a few bytes to copy. Use byte memory operations. */
BYTE_COPY_FWD (dstp, srcp, len);
return dstpp;
}
参考glibc的实现我们最终实现的版本如下:
memcpy函数有一个问题,如果|dest-src| < count,也就是他们有重叠区域时,该函数不能正常工作,此时要使用memmove
void *memcpy(void *dest,const void *src,size_t len)
{
void * ret = dest;
//dest步进char_len字节后内存对齐
unsigned char_len = sizeof(unsigned long) - dest % sizeof(unsigned long);
char *ch_start = src;//按字节复制,并使内存对齐
while((char_len>0) && (len >= char_len))
{
*(char *)dest = *(char *)src;
dest = (char *)dest + 1;
src = (char *)src + 1;
len--;
}
while(len >= sizeof(unsigned long))//按unsigned long大小复制
{
*(unsigned long *)dest = *(unsigned long *)src;
dest = (unsigned long *)dest + sizeof(unsigned long);
src = (unsigned long *)src + sizeof(unsigned long);
len -= sizeof(unsigned long);
}
while(len-- > 0) //剩余部分按字节复制
{
*(char *)dest = *(char *)src;
dest = (char *)dest + 1;
src = (char *)src + 1;
}
return ret;
}
//memmove函数如果区域没重叠,等效于memcpy函数,否则从src的尾部往头部的顺序执行复制。
void * memmove(void * dst, const void * src, size_t count)
{
void * ret = dst;
if (dst <= src || dst >= (src + count)) {/*Non-Overlapping Buffers*/
while (count--)
*dst++ = *src++;
}
else {
/*
* Overlapping Buffers
* copy from higher addresses to lower addresses
*/
dst += count - 1;
src += count - 1;
while (count--)
*dst-- = *src--;
}
return(ret);
}
void *memmove(void *dest,const void *src,size_t len)
{
void * ret = dest;
if (dest <= src || dest >= (src + count)) {
return memcpy(dest,src,len);
}
dest += len - 1;
src += len - 1;
unsigned char_len = dest % sizeof(unsigned long);
char *ch_start = src;//按字节复制,并使内存对齐
while((char_len > 0) && (len >= char_len) )
{
*(char *)dest = *(char *)src;
dest = (char *)dest - 1;
src = (char *)src - 1;
len--;
char_len--;
}
while(len >= sizeof(unsigned long))//按unsigned long大小复制
{
*(unsigned long *)dest = *(unsigned long *)src;
dest = (unsigned long *)dest - sizeof(unsigned long);
src = (unsigned long *)src - sizeof(unsigned long);
len -= sizeof(unsigned long);
}
while(len-- > 0) //剩余部分按字节复制
{
*(char *)dest = *(char *)src;
dest = (char *)dest - 1;
src = (char *)src - 1;
}
return ret;
}