Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #18680

Re: partial struct usage

From Phil Carmody <thefatphil_demunged@yahoo.co.uk>
Newsgroups comp.lang.c
Subject Re: partial struct usage
Date 2012-03-13 18:22 +0200
Organization A noiseless patient Spider
Message-ID <871uowcvyn.fsf@bazspaz.fatphil.org> (permalink)
References <2011092410234736215-madler@alumnicaltechedu> <87vcshy8wu.fsf@bazspaz.fatphil.org> <kfnty3kejt6.fsf@x-alumni2.alumni.caltech.edu> <8739agvrmk.fsf@bazspaz.fatphil.org> <kfnboo7t2m7.fsf@x-alumni2.alumni.caltech.edu>

Show all headers | View raw


Tim Rentsch <txr@alumni.caltech.edu> writes:
> Phil Carmody <thefatphil_demunged@yahoo.co.uk> writes:
> > Tim Rentsch <txr@alumni.caltech.edu> writes:
> >> Phil Carmody <thefatphil_demunged@yahoo.co.uk> writes:
> >> > Mark Adler <madler@alumni.caltech.edu> writes:
...
> >> > struct private {
> >> >         struct public {
> >> >                 int have;
> >> >                 char *next;
> >> >         } pub;
> >> >         other stuff;
> >> > }
> >> >
> >> > let the clients see and pass around pointers to pub, and then either
> >> > use something like container_of() (which is not strictly portable), or
> >> 
> >> Actually container_of() should be strictly portable provided the
> >> outer structure type being casted to matches the actual object
> >> being referred to.  (This assuming container_of() is defined
> >> properly.)
> >
> > All the implementations I've seen (which isn't a huge number) have
> > implemented container_of as an almost identical macro in a way that I
> > would consider non-portable. Looking more closely, I'm fairly sure
> > that this nonportability is historical accident being propagated
> > rather than actually being necessary. Nothing more than casting to
> > character pointer, subtracting offset_of, and casting again should
> > work portably (given the provision you mention).
> 
> I see, that's interesting.  Do you know what the original
> motivation was for defining it that way, rather than (what
> seems to me to be) the more straightfoward portable one?

I had presumed Linux's definition:

#define container_of(ptr, type, member) ({                      \
        const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
        (type *)( (char *)__mptr - offsetof(type,member) );})

was lifted from an early gcc definition, as a lot of things like that were.

It seems that someone else has questioned the seemingly necessary
additional line:
http://psomas.wordpress.com/2009/07/01/weird-kernel-macros-container_of/
"""
EDIT: Apparently, the first line is there for `type checking'. It
ensures that type has a member called member(howerver this is done by
offsetof macro too, I think), and if ptr isn't a pointer to the
correct type(the type of the member), the compiler will print a
warning, which can be useful for debuging.
"""

Which implies that it wasn't a gcc-ism after all.

The gccs I have access to seem to have now pulled it out of the header
files and absorbed it into the compiler itself.

So the linux weirdness seems to be confined. (And that's all I've 
surrounded myself in for the last few years.)

Phil
-- 
> I'd argue that there is much evidence for the existence of a God.
Pics or it didn't happen.
-- Tom (/. uid 822)

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

partial struct usage Mark Adler <madler@alumni.caltech.edu> - 2011-09-24 10:23 -0700
  Re: partial struct usage Richard Damon <news.x.richarddamon@xoxy.net> - 2011-09-24 13:44 -0400
    Re: partial struct usage Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-24 14:08 -0800
  Re: partial struct usage BGB <cr88192@hotmail.com> - 2011-09-24 11:08 -0700
  Re: partial struct usage Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-09-24 23:36 +0300
    Re: partial struct usage Mark Adler <madler@alumni.caltech.edu> - 2011-09-25 13:23 -0700
      Re: partial struct usage Gene <gene.ressler@gmail.com> - 2011-09-26 01:14 -0700
        Re: partial struct usage Richard Damon <news.x.richarddamon@xoxy.net> - 2011-09-26 12:37 -0400
          Re: partial struct usage Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-24 16:04 -0800
        Re: partial struct usage Mark Adler <madler@alumni.caltech.edu> - 2011-09-26 18:52 -0700
          Re: partial struct usage Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-27 12:14 +0100
    Re: partial struct usage Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-24 15:56 -0800
      Re: partial struct usage Shao Miller <sha0.miller@gmail.com> - 2012-01-24 23:30 -0500
        Re: partial struct usage Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-31 16:11 -0800
          Re: partial struct usage Shao Miller <sha0.miller@gmail.com> - 2012-02-01 00:42 -0500
            Re: partial struct usage Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-08 09:33 -0800
      Re: partial struct usage Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-02-12 02:10 +0200
        Re: partial struct usage Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-08 09:38 -0800
          Re: partial struct usage Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-03-13 18:22 +0200
            Re: partial struct usage blp@cs.stanford.edu (Ben Pfaff) - 2012-03-13 09:55 -0700
  Re: partial struct usage Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-09-24 17:56 -0400

csiph-web