I work on an embedded target and want to define memory pools.
Memory addresses are represented as void*
. However in a particular case, those addresses are cached and I want to uncache them to get directly the "real" hardware address.
I want to define the address of the beginning of memory_area
(which is just a marker):
#define UNCACHE_MASK 0xABCDEF12UL // Value of the mask to apply
extern uint32_t memory_area; // Global, defined somewhere else
const void * virtual_address = &memory_area; // OK
const void * real_address =
(void*)(virtual_address | UNCACHE_MASK); // guilty line
Unfortunately, GCC
won't let me do this:
error: invalid operands to binary | (have 'const void *' and 'long unsigned int')
In desperation I tried :
const void * real_address =
(void*)(((uint32_t)virtual_address) | UNCACHE_MASK); // guilty line
In vain:
error: initializer element is not constant
I really want to keep the const
for safety : is it achievable ?
[EDIT]
gcc
v4.9 (with -std=gnu99
and a lot of -Wxxx
flags) on Linux..h
file, variables are "global".Simply make a define. It cannot be accidentally changed.
#define real_address ( (void*)( (uint32_t)virtual_address | UNCACHE_MASK ) )
The extra instruction or two per usage aren't very expensive.
Also add a const keyword to the virtual_address
variable, so it cannot be changed.
const void* const virtual_address = ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With