开发者

Is this a 64bit write operation in C

开发者 https://www.devze.com 2023-02-15 13:51 出处:网络
I have written a function that needs to do a 64bit initialisation of memory for an embedded device. Can anyone tell me if this function will carry out this task?

I have written a function that needs to do a 64bit initialisation of memory for an embedded device. Can anyone tell me if this function will carry out this task?

void write64BitValueTo(void* address, U_INT64 pattern)
{

    int i;

    U_INT64 size = (0x20开发者_高级运维000000 / sizeof(U_INT64));

    //printf("Size = 0x%8X \n",size);
    U_INT64 *ptr = (U_INT64 *) address;

    for(i = 0; i< size; i++)
    {
        *ptr = pattern;
        ptr++;
    }
}


You should declare ptr as volatile U_INT64 *ptr as the compiler could otherwise optimise away the assignment *ptr = pattern.

If it's important that the pattern is actually written to memory and not just the data cache (assuming there is one), then you should also flush the cache afterwards. Otherwise your code looks just fine.


It should work as long as address is properly aligned to allow storing 64-bit words on your architecture.

I'm curious: Why do you write

for(i = 0; i< size; i++)
{
    *ptr = pattern;
    ptr++;
}

when

for(i = 0; i < size; i++)
    *ptr++ = pattern;

is simpler and easier to write and read?

0

精彩评论

暂无评论...
验证码 换一张
取 消