“memory“ clobber

The "memory" clobber tells the compiler that the assembly code performs memory reads or writes to items other than those listed in the input and output operands. GCC may need to flush specific register values to memory before executing the asm to ensure the memory contains the correct values. Furthermore, the compiler does not assume any values read from memory before an asm remain unchanged after that asm. Instead, it reloads them as needed. Using the "memory" clobber effectively forms a read/write memory barrier for the compiler.

The "memory" clobber tells the compiler that the instruction may read or write to any memory address. Thus, the compiler will hold back the actions, so the memory access operation before this instruction will not be moved after this instruction and vice versa. This prevents instruction reordering due to compiler optimization and ensures the relative order of the code blocks before and after barrier().

There is a side effect of "memory" clobber. The compiler will flush the cached values from all registers. Then, it will reread the values from the memory and caches them into the registers. Therefore, the compiler reordering optimization is suppressed.

The volatile keyword is used to inform the compiler that the value of the variable it modifies is likely to be changed by factors outside the program. For example, if the variable is stored in the mapped memory of hardware register IO, the value may be changed. As a result, the compiler is prevented from caching the variable. For a variable modified by volatile, the compiler cannot cache the variable. Instead, when the value of the variable is used each time, the compiler must reread the value of the variable from memory.

猜你喜欢

转载自blog.csdn.net/world_hello_100/article/details/132714797