Chinaunix首页 | 论坛 | 博客
  • 博客访问: 641222
  • 博文数量: 75
  • 博客积分: 7001
  • 博客等级: 少将
  • 技术积分: 1465
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-11 17:39
文章分类

全部博文(75)

文章存档

2010年(1)

2009年(25)

2008年(49)

我的朋友

分类: 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.

阅读(1361) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~