全部博文(75)
分类: LINUX
2008-06-17 14:52:16
The difference between mem_cpy() and mem_move()
The prototype of the two functions:
void * memcpy(void * dest,const void *src,size_t count)
void * memmove(void * dest,const void *src,size_t count)
The implementation of the two funtions:
/**
* memcpy - Copy one area of memory to another
* @dest: Where to copy to
* @src: Where to copy from
* @count: The size of the area.
*
* You should not use this function to access IO space, use memcpy_toio()
* or memcpy_fromio() instead.
*/
void * memcpy(void * dest,const void *src,size_t count)
{
char *tmp = (char *) dest, *s = (char *) src;
while (count--)
*tmp++ = *s++;
return dest;
}
#endif
/**
* memmove - Copy one area of memory to another
* @dest: Where to copy to
* @src: Where to copy from
* @count: The size of the area.
*
* Unlike memcpy(), memmove() copes with overlapping areas.
*/
void * memmove(void * dest,const void *src,size_t count)
{
char *tmp, *s;
if (dest <= src) {
tmp = (char *) dest;
s = (char *) src;
while (count--)
*tmp++ = *s++;
}
else {
tmp = (char *) dest + count;
s = (char *) src + count;
while (count--)
*--tmp = *--s;
}
return dest;
}
Conclusion:
The two functions mem_cpy() and mem_move() accomplish the same goal “copy” operation. But unlike mem_cpy(), mem_move copys with overlapping areas by means of copy data byte by byte from tail to head, while mem_cpy() in the opposite direction.
Note:
In do_bootm() function of u-boot source code, mem_move() is called to move kernel image to specified address.