In order to make a page dirty (switching on the dirty bit in the page table entry), I touch the first bytes of the page like this:
pageptr[0] = pageptr[0];
But in practice, GCC will ignore the statement by dead store elimination. In order to prevent GCC optimizing it, I rewrite the statement as follows:
volatile int tmp;
tmp = pageptr[0];
pageptr[0] = tmp;
It seems the trick works, but somewhat ugly. I would like to know is there any directives or syntax which has the same effect? And I don't want to use a -O0 flag, since it will bring great performance penalty as well.
-O0
did not prevent dead code "optimization", e.g., when GCC detects some code has no effect, it simply removes it. AFAIK this is a stage even before-O0
... But that's just my experience – Hodden