LPVOID AplFillMemory()

in src/native/windows/src/mclib.c [56:123]


LPVOID AplFillMemory(PVOID Destination, SIZE_T Length, BYTE Fill)
{

    SIZE_T t;
#ifdef WIN64
    UINT64 c;
#else
    UINT   c;
#endif
    LPBYTE dst;

    dst = Destination;
    /*
     * If not enough words, just fill bytes.  A length >= 2 words
     * guarantees that at least one of them is `complete' after
     * any necessary alignment.  For instance:
     *
     *  |-----------|-----------|-----------|
     *  |00|01|02|03|04|05|06|07|08|09|0A|00|
     *            ^---------------------^
     *       dst         dst+length-1
     *
     * but we use a minimum of 3 here since the overhead of the code
     * to do word writes is substantial.
     */
    if (Length < 3 * wsize) {
        while (Length != 0) {
            *dst++ = Fill;
            --Length;
        }
        return (Destination);
    }

    if ((c = Fill) != 0) {  /* Fill the word. */
        c = (c << 8) | c;   /* u_int is 16 bits. */
        c = (c << 16) | c;  /* u_int is 32 bits. */
#ifdef WIN64
        c = (c << 32) | c;  /* u_int is 64 bits. */
#endif
    }
    /* Align destination by filling in bytes. */
    if ((t = (SIZE_T)dst & wmask) != 0) {
        t = wsize - t;
        Length -= t;
        do {
            *dst++ = Fill;
        } while (--t != 0);
    }

    /* Fill words.  Length was >= 2*words so we know t >= 1 here. */
    t = Length / wsize;
    do {
#ifdef WIN64
        *(UINT64 *)dst = c;
#else
        *(UINT   *)dst = c;
#endif
        dst += wsize;
    } while (--t != 0);

    /* Mop up trailing bytes, if any. */
    t = Length & wmask;
    if (t != 0)
        do {
            *dst++ = Fill;
        } while (--t != 0);
    return (Destination);
}