I am currently reading the PostgreSql code. Here is an excerpt from the buffer manager:
static void WaitIO(volatile BufferDesc *buf);
static bool StartBufferIO(volatile BufferDesc *buf, bool forInput);
static void TerminateBufferIO(volatile BufferDesc *buf, bool clear_dirty,
I know the volatile keyword is usually used for device drivers and in embedded systems. There is an explanation of the keyword.
When the keyword volatile is used in the type definition it is giving an indication to the compiler on how it should handle the variable. Primarily it is telling the compiler that the value of the variable may change at any time as a result of actions external to the program or current line of execution.
(Source)
So why are certain function arguments declared as volatile? I don’t expect that DMA changes the pointer location. So what happens here?
volatile BufferDesc *bufmeans that the data thatbufpoints to is volatile, not that the pointer contained bybufis volatile. (That would beBufferDesc * volatile buf.)From the page you linked to:
Re this part of your question:
Presumably because the data it points to can change in a way that the compiler wouldn’t necessarily know about. The
volatilekeyword is there to prevent the compiler applying optimizations that assume the data doesn’t change in ways it doesn’t know about.