Why mark function argument as volatile
volatile BufferDesc *buf
means that the data that buf
points to is volatile, not that the pointer contained by buf
is volatile. (That would be BufferDesc * volatile buf
.)
From the page you linked to:
On the other hand, if you have a pointer variable where the address itself was volatile but the memory pointed to was not then we have:
int * volatile x;
Re this part of your question:
So why are certain function arguments declared as volatile?
Presumably because the data it points to can change in a way that the compiler wouldn't necessarily know about. The volatile
keyword is there to prevent the compiler applying optimizations that assume the data doesn't change in ways it doesn't know about.
I don't expect that DMA changes the pointer location.
Not the location, but maybe the content. And that's exactly what it is about...
Databases implementation does not rely on the OS buffers&caches when accessing files. They prefer to implement they own cache system and get direct access to the file on the physical disk, for reliability issues: data MUST to be flushed to the physical disk. (They use the O_DIRECT option to perform direct access to physical disk.)
The 3 functions you are showing us make me think of asynchronous handling of data coming from the disk. (StartBufferIO(), TerminatedBufferIO() and so on). Honestly I am not sure of what is their purpose, but based on what I know about databases implementation, and those function names, I would say that the buffer content might be modified by the "disk" itself with data from the file on the disk (or whatever device), and therefore needs to be flaged as volatile.
This hypothesis joins your usual interpretation of what the volatile keyword is usually used for: device drivers.