Presumably your smiley it to make pretending that I'm not the one
writing that macro, or function easier?
In general, I would expect that either:
1. You are working within an existing body of code, such as a kernel,
that has almost certainly provided a way to perform that calculation
so that you don't need to know how to do it.
2. You are the one writing that, and you can make suitable decisions
for your targets.
In general, that's the kind of thing where the answer is "the portable
thing to do is include a header which is architecture-specific."
'This' is singular. I mentioned 2 things (use of pointers, or
use of integers). 2 is plural.
Sorry, was referring to the "this" as in "the use of ints rather
than some other type as the thing used to stash a pointer value".
Alas that's wrong. The instructions the CPU uses in all cases that I
can think of do not distinguish between memory mapped memory, and
memory mapped peripherals (and yes, they are peripherals, your concept
that they are 'part of the CPU' is a pretty wacky one; clue - the 'C'
in 'SoC' does not stand for 'CPU'), and therefore there must still be
a correctly-arranged logical-address to physical-address mapping.
Oh, certainly.
My point is, if you know exactly what CPU your code will execute on, you
don't have to worry about whether or not it's portable to assume that an
int can contain a pointer -- you know for sure whether it can or can't.
Just setting a pointer up to the numerical value of the physical
address is quite the opposite of harmless, it's probably fatal.
This gets close to one of the reasons people sometimes stash values
in non-pointer objects. There have existed systems on which "pointers"
were loaded into registers such that loading an invalid value caused
a trap. On such a system, if you had magic cookies which might
represent a valid pointer some of the time, and not at other times (say,
depending on what you last programmed the MMU for), the natural idiom
would be to stash those cookies in a suitably-sized int, then, when you
are ready to use them (and unbeknownst to the compiler, you know that
the MMU is in a state where they work), you cast them over to pointers
and they work.
So it is *conceivable* that this code is not stupid. I'm not willing to
say anything more complimentary about it without a lot more information
than I have. I'm not going to argue that it could ever be good style;
even if it's technically correct for all systems it can possibly be
run on, it's still ugly.
-s