From: brucedickey Date: 2001-08-17T06:45:20+09:00 Subject: [ruby-talk:19921] RE: Why the " * volatile " declarations in the C source? The K & R book says that "the purpose of volatile is for force an implementation to suppress optimization that could otherwise occur. For example, for a machine with memory-mapped input/output, a pointer to a device register might be declared as a pointer to volatile, in order to prevent the compiler from removing the apparently redundant references through the pointer." Not said is that if its a device register, it could change without the program knowing it, i.e. the program can't assume it knows the current value just because it has read it once. Volatile tells the compiler to insert code to re-read the register each time its contents are needed. Bruce > -----Original Message----- > From: Aaron Harsh [mailto:harsh@computer.org] > Sent: Thursday, August 16, 2001 2:57 PM > To: ruby-talk@ruby-lang.org > Subject: [ruby-talk:19918] Why the " * volatile " > declarations in the C > source? > > > Sorry in advance if this is a stupid question, but... > > I noticed a lot of pointers in the C source being declared as '* > volatile'. For example, this declaration at the top of > rb_eval in eval.c: > > NODE * volatile node = n; > > I would have just ignored them if they'd been 'volatile NODE * node', > but 'NODE * volatile' is pretty interesting. No one on #ruby-lang at > the time was quite sure, so I thought I'd bug c.l.r. > > Here are my guesses so far, and my reasons for rejecting them: > > 1) The garbage collector could conceivably move an object, and would > need to modify any pointers to the old location. This one seemed > promising, but I couldn't see any compaction in the GC code, and the > #ruby-lang'ers confirmed that Ruby's GC doesn't compact > > 2) The 'volatile' thing might just be a trick to make sure > the pointer > is stored on the stack (rather than a register), so that the > GC sees it > during marking. But it seems a lot more straightforward to just scan > through the registers during mark (rather than put a 'volatile' > definition in every place where a variable could be the only > pointer to > an object). > > So I'm stumped, and I'm losing sleep over this. What's the > real reason? >