mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Kevin O'Connor" <kevin@koconnor.net>
To: Neil Brown <neilb@cse.unsw.edu.au>
Cc: Linus Torvalds <torvalds@transmeta.com>, linux-kernel@vger.kernel.org
Subject: Re: PATCH: type safe(r) list_entry repacement: generic_out_cast
Date: Tue, 23 Jul 2002 18:58:52 -0400	[thread overview]
Message-ID: <20020723185852.A12295@arizona.localdomain> (raw)
In-Reply-To: <15677.15834.295020.89244@notabene.cse.unsw.edu.au>; from neilb@cse.unsw.edu.au on Tue, Jul 23, 2002 at 09:28:26PM +1000

On Tue, Jul 23, 2002 at 09:28:26PM +1000, Neil Brown wrote:
> However it doesn't do any type checking on the pointer, and you can 
> give it a pointer to something else... just as I did in line 850 of
> md/md.c :-(

Hi Neil,

I've experienced the same problem with some of my own code.  I came up with
the following macro to help solve the problem:

/* Given a pointer to a member of a struct, return a pointer to the struct
   itself. */
#define BackPtr(ptr, type, member) ({                                         \
        typeof( ((type *)0)->member ) *__mptr = (ptr);                        \
        ((type *)( (char *)__mptr - (unsigned long)(&((type *)0)->member) ));})

> So I thought I would add some type checking to list_entry so that
> you have to pass it a "struct list_head *", but I then discovered that
> lots of places are using list_entry to do creative casting on
> all sorts of other things like inodes embedded in bigger structures
> and so on.
> 
> So... I have created "generic_out_cast" which is like the old
> list_entry but with an extra type arguement.

I don't think this is necessary.  The compiler can get this information
from the type/member parameters and the typeof() call.

> Why "out_cast"???

Ugh..  I had a similar naming dilemma - I choose BackPtr (because the
macro causes a negative pointer offset).

-Kevin

-- 
 ------------------------------------------------------------------------
 | Kevin O'Connor                     "BTW, IMHO we need a FAQ for      |
 | kevin@koconnor.net                  'IMHO', 'FAQ', 'BTW', etc. !"    |
 ------------------------------------------------------------------------

  parent reply	other threads:[~2002-07-23 22:55 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-07-23 11:28 Neil Brown
2002-07-23 11:47 ` Jakob Oestergaard
2002-07-23 22:07   ` type safe lists (was Re: PATCH: type safe(r) list_entry repacement: generic_out_cast) Joshua MacDonald
2002-07-24  7:39     ` Martin Brulisauer
2002-07-24  8:24       ` Anton Altaparmakov
2002-07-24  9:56       ` Joshua MacDonald
2002-07-24 10:50         ` Martin Brulisauer
2002-07-24 11:58           ` Jakob Oestergaard
2002-07-24 16:49             ` John Alvord
2002-07-25  5:29       ` Greg KH
2002-07-25  6:20         ` Roland Dreier
2002-09-04 13:48         ` Alexander Kellett
2002-09-04 14:27           ` DervishD
2002-07-24 12:22     ` Jakob Oestergaard
2002-07-24 12:40       ` Joshua MacDonald
2002-07-24 15:15         ` Hans Reiser
2002-07-23 12:42 ` PATCH: type safe(r) list_entry repacement: generic_out_cast Brian Gerst
2002-07-23 16:29 ` Linus Torvalds
2002-07-24  5:25   ` Thunder from the hill
2002-07-23 16:36 ` Christoph Hellwig
2002-07-23 22:58 ` Kevin O'Connor [this message]
2002-07-24 12:38 ` PATCH - mark 2: type safe(r) list_entry repacement: container_of Neil Brown
2002-07-24  0:51 PATCH: type safe(r) list_entry repacement: generic_out_cast Mikael Pettersson
2002-07-24  1:04 ` Linus Torvalds

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20020723185852.A12295@arizona.localdomain \
    --to=kevin@koconnor.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=neilb@cse.unsw.edu.au \
    --cc=torvalds@transmeta.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®