How to apply a mask to const void * address?

I am working on a built-in target and want to define memory pools.

Memory addresses are represented as void*. However, in the specific case, these addresses are cached, and I want to be able to link them to directly get the "real" hardware address.

I want to determine the start address 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, GCCwon'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 constit safe: is this achievable?

[EDIT]

  • I am using GCCv4.9 (with -std=gnu99and many flags -Wxxx) on Linux.
  • Excerpt from the file .h, the variables are "global."
+4
2

, bitwise-OR . , , (, ++, C). , .

:

const void * real_address

, "real_address - ". , ( , ), :

const void * const real_address

, . , , C.

+2

. .

#define real_address  ( (void*)( (uint32_t)virtual_address | UNCACHE_MASK ) )

.

const virtual_address, .

const void* const virtual_address = ...
+5

All Articles