Life is Good.

Enhance Tech and English
随笔 - 65, 文章 - 20, 评论 - 21, 引用 - 0
数据加载中……

13 - memmove

/***
*memmove - Copy source buffer to destination buffer
*
*Purpose:
*       memmove() copies a source memory buffer to a destination memory buffer.
*       This routine recognize overlapping buffers to avoid propogation.
*       For cases where propogation is not a problem, memcpy() can be used.
*
*Entry:
*       void *dst = pointer to destination buffer
*       const void *src = pointer to source buffer
*       size_t count = number of bytes to copy
*
*Exit:
*       Returns a pointer to the destination buffer
*
*Exceptions:
******************************************************************************
*/

void * __cdecl memmove (
  
void * dst,
  
const void * src,
  size_t count
  )
{
  
void * ret = dst;

  
if (dst <= src || (char *)dst >= ((char *)src + count)) {
    
/*
    * Non-Overlapping Buffers
    * copy from lower addresses to higher addresses
    
*/
    
while (count--) {
      
*(char *)dst = *(char *)src;
      dst 
= (char *)dst + 1;
      src 
= (char *)src + 1;
    }
  }
  
else {
    
/*
    * Overlapping Buffers
    * copy from higher addresses to lower addresses
    
*/
    dst 
= (char *)dst + count - 1;
    src 
= (char *)src + count - 1;

    
while (count--) {
      
*(char *)dst = *(char *)src;
      dst 
= (char *)dst - 1;
      src 
= (char *)src - 1;
    }
  }

  
return(ret);
}

posted on 2011-06-11 21:52 Mike Song 阅读(222) 评论(0)  编辑 收藏 引用 所属分类: C字符串函数源码分析


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理