Re: Newbie idiotic questions.

Geert Uytterhoeven (geert@linux-m68k.org)
Sun, 17 Jun 2001 21:18:23 +0200 (CEST)


On Sun, 17 Jun 2001, Jeff Garzik wrote:
> David Flynn wrote:
> > > Daniel Phillips wrote:
> > > > Yep, the only thing left to resolve is whether Jeff had coffee or not.
> > ;-)
> > > >
> > > > - if ((card->mpuout = kmalloc(sizeof(struct emu10k1_mpuout),
> > GFP_KERNEL))
> > > > + if ((card->mpuout = kmalloc(sizeof(*card->mpuout), GFP_KERNEL))
> > >
> > > Yeah, this is fine. The original posted omitted the '*' which was not
> > > fine :)
> >
> > The only other thing left to ask, is which is easier to read when glancing
> > through the code, and which is easier to read when maintaining the code.
> > imho, ist the former for reading the code, i dont know about maintaing the
> > code since i dont do that, however in my own projects i prefere the former
> > when maintaing the code.
>
> It's the preference of the maintainer. It's a tossup: using the type
> in the kmalloc makes the type being allocated obvious. But using
> sizeof(*var) is a tiny bit more resistant to change.
>
> Neither one sufficiently affects long term maintenance AFAICS, so it's
> personal preference, not any sort of kernel standard one way or the
> other...

The first one can be made a bit safer against changes by creating a `knew'
macro that behaves like `new' in C++:

| #define knew(type, flags) (type *)kmalloc(sizeof(type), (flags))

If the types in the assignment don't match, gcc will tell you.

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/