[lkml]   [2002]   [Jul]   [23]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
Messages in this thread
    SubjectRe: PATCH: type safe(r) list_entry repacement: generic_out_cast
    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 O'Connor "BTW, IMHO we need a FAQ for |
    | 'IMHO', 'FAQ', 'BTW', etc. !" |
    To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
    the body of a message to
    More majordomo info at
    Please read the FAQ at

     \ /
      Last update: 2005-03-22 13:22    [W:0.022 / U:2.580 seconds]
    ©2003-2016 Jasper Spaans. hosted at Digital OceanAdvertise on this site