Re: Q: volatile struct elements or entire struct?

From: Chris Torek (nospam_at_torek.net)
Date: 09/23/04


Date: 23 Sep 2004 00:07:17 GMT

In article <news:Xns956C9FD7F9153CopyrightMarkOdell@130.133.1.4>
Mark A. Odell <odellmark@hotmail.com> wrote:

[which of these should one use:]

>a) volatile on struct objects
>
>struct Foo {
> unsigned char command;
> unsigned char status;
> unsigned long baseAddr;
> unsigned long addrRange;
>}
>
>volatile struct Foo *pDevFoo = (some dev. address);
> struct Foo hostFoo;

This is the version I prefer, for several reasons, the most valid :-)
being the one shown right here in your example: you can make a
non-volatile version of it.

>b) volatile on struct members
>
>struct Foo
>{
> volatile unsigned char command;
> volatile unsigned char status;
> volatile unsigned long baseAddr;
> volatile unsigned long addrRange;
>}
>
>struct Foo *pDevFoo = (some dev. address);
>struct Foo hostFoo; /* live with extra accesses */
>
>Does my use of volatile in a) even do what I intend? That is, make
>accesses to the device volatile?

Yes. Both methods work (provided, of course, that your implementation's
definition of "volatile" does what you need in the first place --
if not, neither method works anyway!).

-- 
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W)  +1 801 277 2603
email: forget about it   http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.


Relevant Pages

  • Re: [releng_6 tinderbox] failure on sparc64/sparc64
    ... Write it out 1 unsigned char at a time for the simple version. ... nbytes = sizeof; ...
    (freebsd-arch)
  • Re: Coding style survey
    ... rather than let it sit on your disk misleading future programmers. ... typedef struct foo foo; ... seriously concerned that 'unsigned char' may change in future versions, ...
    (comp.lang.c)
  • Re: pointer addition and structs
    ... > int i; ... unsigned char *p = pfoo + offsetof ... to char or unsigned char. ...
    (microsoft.public.vc.language)
  • Re: pointer addition and structs
    ... >>Is there a way using pointer addition to access struct members? ... > struct foo x; ... > to char or unsigned char. ...
    (microsoft.public.vc.language)
  • Re: Returning results through the function parameter list?
    ... If you wish to have control of the order of the parts, ... to put out a series of 8-bit values in big-endian order ... The masking with 0xff is not necessary if "unsigned char" happens ... Reading email is like searching for food in the garbage, ...
    (comp.lang.c)