This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Are arrays guaranteed to be affected by a "memory" clobber?


The gcc manual [1] only mentions "memory reads or writes to items other than those listed in the input and output operands", it doesn't mention anything about volatile qualifiers.

But according to an article in the avr-libc manual [2], "it does not ensure the compiler moving non-volatile-related statements across the barrier".
The example given there doesn't involve arrays/pointers. The critical variable is optimized into a register.

Now I wonder if array elements, too,  must be qualified "volatile" if I want to access them from an ISR (Interrupt Service Routine) and from "normal" code.

Assuming these declarations:

    typedef volatile char array_element;
    array_element buffer[ARRAY_SIZE];
    array_element * volatile read_pointer;
    array_element * volatile write_pointer;
    
and assuming all accesses to read_pointer and write_pointer are in atomic blocks [3], which disable interrupts and contain a memory clobber at enter and exit [4].

Is volatile needed in the definition of array_element, or is any array guaranteed to be in memory, and therefore affected by a memory clobber?
In current and future versions of gcc, with above variables "static" so the optimizer knows about all uses?

Are there better ways to force a variable into memory?
- Declare it "extern" - will this work after Link Time Optimization, too?
- __attribute__ ((section ("...")))

Or should I clobber the variable which I'm concerned about, by putting "buffer" or "*read_pointer" into a clobber list? How do I do this?

The reason why I would like to avoid the volatile qualifier:
As soon as I use it once, I must use it in all functions which get a pointer to the buffer. So I can't reuse these functions for other arrays.
With other variables, this is not so serious - you can assign the value of a volatile variable to a temporary variable, or to a parameter, without trouble. Only with pointers/arrays, this becomes a problem.


[1]: https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#Clobbers
[2]: http://www.nongnu.org/avr-libc/user-manual/optimization.html
[3]: http://www.nongnu.org/avr-libc/user-manual/group__util__atomic.html
[4]: http://www.nongnu.org/avr-libc/user-manual/atomic_8h_source.html
     http://www.nongnu.org/avr-libc/user-manual/interrupt_8h_source.html
     Line 99 # define cli() __asm__ __volatile__ ("cli" ::: "memory")


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]