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


Groups > comp.lang.c > #18681

Re: partial struct usage

From blp@cs.stanford.edu (Ben Pfaff)
Newsgroups comp.lang.c
Subject Re: partial struct usage
Date 2012-03-13 09:55 -0700
Message-ID <874ntsv3th.fsf@blp.benpfaff.org> (permalink)
References (1 earlier) <87vcshy8wu.fsf@bazspaz.fatphil.org> <kfnty3kejt6.fsf@x-alumni2.alumni.caltech.edu> <8739agvrmk.fsf@bazspaz.fatphil.org> <kfnboo7t2m7.fsf@x-alumni2.alumni.caltech.edu> <871uowcvyn.fsf@bazspaz.fatphil.org>

Show all headers | View raw


Phil Carmody <thefatphil_demunged@yahoo.co.uk> writes:

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

({ }) and "typeof" are of course non-portable, but one can write
a more portable variant without them, as I have in GNU PSPP:

/* Given expressions A and B, both of which have pointer type,
   expands to a void expression that causes a compiler warning if
   A and B are not pointers to qualified or unqualified versions
   of compatible types.

   Examples similar to those given for CHECK_POINTER_HAS_TYPE,
   above, can easily be devised. */
#define CHECK_POINTER_COMPATIBILITY(A, B) ((void) sizeof ((A) == (B)))

/* Given POINTER, a pointer to the given MEMBER within structure
   STRUCT, returns the address of the STRUCT. */
#define UP_CAST(POINTER, STRUCT, MEMBER)                                \
        (CHECK_POINTER_COMPATIBILITY (&((STRUCT *) 0)->MEMBER, POINTER), \
         (STRUCT *) ((char *) (POINTER) - offsetof (STRUCT, MEMBER)))

-- 
"Debugging is twice as hard as writing the code in the first place.
 Therefore, if you write the code as cleverly as possible, you are,
 by definition, not smart enough to debug it."
--Brian Kernighan

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