Is there a way to tell the C++ compiler that an object that's reachable through a pointer has changed, even if it has every reason to believe it hasn't?
My use-case is something like this:
// Three argument system call.
inline unsigned long do_syscall(unsigned short callnum,
unsigned long p1,
unsigned long p2,
unsigned long p3) noexcept
{
val_t retval;
asm volatile (
"syscall\n\t"
:"=a"(retval)
:"a"(static_cast<::std::uint64_t>(callnum)), "D"(p1), "S"(p2), "d"(p3)
:"%rcx", "%r11"
);
return retval;
}
inline void read(int fd, char *buf, int size) noexcept {
do_syscall(0, fd, reinterpret_cast<unsigned long>(buf), size);
}
char buf[1024] = {'\0'};
read(0, buf, 1024);
// Here, the compiler needs to believe it knows nothing about what's in buf.
The compiler has full access to the code for read, and it has every reason to believe that read doesn't modify the contents of buf
. Is there anything I can put in read
that would doesn't result in any code by itself, but does tell the compiler it must read the contents of buf
from memory again if it's accessed?
An answer specific to gcc and/or clang would be acceptable, but I would prefer a method that was standard C++.