In order to make a page dirty, I touch the first bytes of the page like this:
pageptr[0] = pageptr[0];
But it seems gcc will eliminate the statement when optimizing. In order to prevent gcc optimizing it, I write the statement like this:
volatile int tmp;
tmp = pageptr[0];
pageptr[0] = tmp;
It seems the trick works, I'd like to know is there any directives or syntax which has the same effect?
=========
Summary
Dietrich Epp's solution:
((unsigned char volatile *)pageptr)[0] = pageptr[0];
Plow's solution: using function specific option pragmas introduced since gcc 4.4
#pragma GCC push_options
#pragma GCC optimize ("O0")
your code
#pragma GCC pop_options