0

I have some trouble to understand the usage of optimization barrier code in the following sequence:

   struct globals;
/* '*const' ptr makes gcc optimize code much better.
 * Magic prevents ptr_to_globals from going into rodata.
 * If you want to assign a value, use SET_PTR_TO_GLOBALS(x) */
extern struct globals *const ptr_to_globals;
/* At least gcc 3.4.6 on mipsel system needs optimization barrier */
#define barrier() __asm__ __volatile__("":::"memory")
#define SET_PTR_TO_GLOBALS(x) do { \
    (*(struct globals**)&ptr_to_globals) = (void*)(x); \
    barrier(); \
} while (0)

Is this something necessary becaause of the "while (0) condition?

1 Answers1

0

The while(0) is just syntactic sugar and has no impact on optimization. See answers to this question for why it is there. The barrier is likely there to prevent the compiler from moving loads/stores upwards over SET_PTR_TO_GLOBALS, which it might otherwise be tempted to do since under normal rules for C, the assignment to a const variable can't have any effect.

Community
  • 1
  • 1
Arch D. Robison
  • 3,829
  • 2
  • 16
  • 26