Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1691926
  • 博文数量: 177
  • 博客积分: 9416
  • 博客等级: 中将
  • 技术积分: 2513
  • 用 户 组: 普通用户
  • 注册时间: 2006-01-06 16:08
文章分类

全部博文(177)

文章存档

2013年(4)

2012年(13)

2011年(9)

2010年(71)

2009年(12)

2008年(11)

2007年(32)

2006年(25)

分类: C/C++

2009-06-19 14:08:01

No comments provided because it's quite simple. This function will avoid overwritten of source data when source and destination have space overlap. Can only be used on 32-bit platform.

void* WordCopy(void* dest, const void* src, const size_t length)
{
    ASSERT(dest && src && ((uint32)dest) & 0x3 == 0 && ((uint32)src) & 0x3 == 0 && length & 0x3 ==0);
    uint32* pD = (uint32*)dest;
    size_t len = length >> 2;
    const uint32* pS = (const uint32*)src;
    const uint32* pEnd = pS + len;
    if (pS < pD)
    {
        pD += len;
        while (pEnd > pS)
            *--pD = *--pEnd;
    }
    else if (pS > pD)
    {
        while (pS < pEnd)
            *pD++ = *pS++;
    }
    return dest;
}

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

chinaunix网友2010-05-21 14:59:11

为什么不是unsigned char类型 而非要是uint32呢 这样你的len不就必须得是4字节对齐?

chinaunix网友2009-06-30 13:15:00

why not to refine it like this? that is better for no confusion. uint32* MemoryCopy_UINT32(uint32* dest, const uint32* src, const size_t len) { ASSERT(dest && src && ((uint32)dest) & 0x3 == 0 && ((uint32)src) & 0x3 == 0 ); uint32* pD = dest; const uint32* pS = src; const uint32* pEnd = pS + len; if (pS < pD) { pD += len; while (pEnd > pS) *--pD = *--pEnd; } else if (pS > pD) { while (pS < pEnd) *p

fera2009-06-23 17:58:13

memory alignment of 32-bit machine

chinaunix网友2009-06-22 12:07:29

内存大小必须是4个字节的整数倍?