[原]C: A memory copy function

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;
}

posted on 2009-06-19 16:57  raof01  阅读(109)  评论(0编辑  收藏  举报