Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why mark function argument as volatile

Tags:

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?

like image 292
dmeister Avatar asked Feb 20 '12 15:02

dmeister


People also ask

Why do we declare variable as volatile?

volatile is a keyword that must be used when declaring any variable that will reference a device register. If this is not done, the compile-time optimizer might optimize important accesses away. This is important; neglecting to use volatile can result in bugs that are difficult to track down.

What type of variables should be declared as volatile and why?

A variable should be declared volatile whenever its value could change unexpectedly. In practice, only three types of variables could change: Memory-mapped peripheral registers. Global variables modified by an interrupt service routine.

Why do we use volatile in C?

The volatile keyword is intended to prevent the compiler from applying any optimizations on objects that can change in ways that cannot be determined by the compiler. Objects declared as volatile are omitted from optimization because their values can be changed by code outside the scope of current code at any time.

What is the purpose of the keyword volatile which is used in declaring some variables in C #?

C's volatile keyword is a qualifier that is applied to a variable when it is declared. It tells the compiler that the value of the variable may change at any time--without any action being taken by the code the compiler finds nearby.


2 Answers

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.

like image 183
T.J. Crowder Avatar answered Sep 30 '22 05:09

T.J. Crowder


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...

like image 40
glglgl Avatar answered Sep 30 '22 05:09

glglgl