Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply a mask to a const void* address?

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]

  • I am using gcc v4.9 (with -std=gnu99 and a lot of -Wxxx flags) on Linux.
  • The excerpt is from a .h file, variables are "global".
like image 387
n0p Avatar asked Mar 18 '23 08:03

n0p


1 Answers

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 = ...
like image 124
2501 Avatar answered Mar 28 '23 09:03

2501