tech-kern archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: proposal: some pointer and arithmetic utilities

On Thu 21 Mar 2013 at 18:12:03 +0000, Taylor R Campbell wrote:
> We have various structs of the form
>       struct foo {
>               struct bar f_bar;
>               ...
>       };
> and routines that are called like mumble(&foo->f_bar) and look like
>       int
>       mumble(struct bar *bar)
>       {
>               struct foo *foo = (void *)bar;
>               ...
>       }
> which assume that nobody has rearranged the fields of struct foo, an
> assumption which is neither semantically significant nor checked by
> the compiler; nor does it work with multiple different members from
> whose pointers we may want to find the containing object.

I would argue that all such mumble() should really be changed to

        mumble(struct foo *foo)
                struct bar *bar = &foo->f_bar;

since they are making that assumption already anyway.
That also works if f_bar isn't the first member in struct foo.

In C++ terms, mumble() is declared with a base class (struct bar) and
assumes it is actually called with a (specific) derived class (struct foo).
That is bad practice there, too.
The only advantage in C++ is that you can have a run-time type check for
it (at a price).

On Thu 21 Mar 2013 at 18:42:16 +0000, Taylor R Campbell wrote:
> /* multiple-evaluation-free roundup2/rounddown2 */
> #define       roundup2(x, m)  ((((x) - 1) | ((m) - 1)) + 1)

That seems to be typeof-free too, so the best of both worlds.

___ Olaf 'Rhialto' Seibert  -- The Doctor: No, 'eureka' is Greek for
\X/ rhialto/at/    -- 'this bath is too hot.'

Home | Main Index | Thread Index | Old Index