linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Willy Tarreau <willy@w.ods.org>
To: Robert Love <rml@novell.com>
Cc: Russell King <rmk+lkml@arm.linux.org.uk>,
	Linux Kernel List <linux-kernel@vger.kernel.org>,
	Linus Torvalds <torvalds@osdl.org>,
	Al Viro <viro@ftp.linux.org.uk>
Subject: Re: p = kmalloc(sizeof(*p), )
Date: Sun, 18 Sep 2005 18:52:19 +0200	[thread overview]
Message-ID: <20050918165219.GA595@alpha.home.local> (raw)
In-Reply-To: <1127061146.6939.6.camel@phantasy>

On Sun, Sep 18, 2005 at 12:32:26PM -0400, Robert Love wrote:
> On Sun, 2005-09-18 at 11:06 +0100, Russell King wrote:
> 
> > +The preferred form for passing a size of a struct is the following:
> > +
> > +       p = kmalloc(sizeof(*p), ...);
> > +
> > +The alternative form where struct name is spelled out hurts readability and
> > +introduces an opportunity for a bug when the pointer variable type is changed
> > +but the corresponding sizeof that is passed to a memory allocator is not.
> 
> Agreed.
> 
> Also, after Alan's #4:
> 
> 5.  Contrary to the above statement, such coding style does not help,
>     but in fact hurts, readability.  How on Earth is sizeof(*p) more
>     readable and information-rich than sizeof(struct foo)?  It looks
>     like the remains of a 5,000 year old wolverine's spleen and
>     conveys no information about the type of the object that is being
>     created.
> 
> 	Robert Love

To be honnest, before reading this thread, I would have voted for the
sizeof(*p). However, I completely agree that there is a high risk of
messing up the initialization, and that structures don't change often.
The situations where I think that sizeof(*p) is better than
sizeof(struct foo) is more on functions such as memset() than {,k}malloc() :
forgetting to initialize a struct member is always a high risk, but if the
object is not a struct (eg, a scalar), then it could be tolerated. I don't
know anybody who does kmalloc(sizeof(int)) nor kmalloc(sizeof(char)), but
with memset, it's different. Doing memset(p, 0, sizeof(*p)) seems better
to me than memset(p, 0, sizeof(short)), and represents a smaller risk
when 'p' will silently evolve to a long int.

Last, there's little probability that a scalar will evolve into a struct
without code modifications, while it has happened often that a __u8 or
__u16 was changed to __u32. So perhaps we could accept use of sizeof(*p)
when (*p) is a scalar to protect against silent type changes, and reject
it when (*p) is a structure to avoid incomplete initialization ?

Alan, I like your proposal BTW ;-)

Regards,
Willy


  reply	other threads:[~2005-09-18 16:52 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-09-18 10:06 p = kmalloc(sizeof(*p), ) Russell King
2005-09-18 11:04 ` Alan Cox
2005-09-18 14:39   ` Al Viro
2005-09-18 16:25     ` Denis Vlasenko
2005-09-18 17:30       ` Al Viro
2005-09-18 18:00         ` Willy Tarreau
2005-09-18 17:47       ` Alan Cox
2005-09-18 16:32 ` Robert Love
2005-09-18 16:52   ` Willy Tarreau [this message]
2005-09-18 17:18     ` Al Viro
2005-09-18 17:31       ` Linus Torvalds
2005-09-18 17:45         ` Al Viro
2005-09-18 20:34           ` Roman Zippel
2005-09-18 21:12             ` Al Viro
2005-09-18 21:52               ` Al Viro
2005-09-18 22:25                 ` Linus Torvalds
2005-09-18 23:07                   ` Al Viro
2005-09-20  6:31                     ` Richard Henderson
2005-09-19 21:20                   ` Matthias Urlichs
2005-09-19 21:28                     ` Matthias Urlichs
2005-09-18 19:07         ` Al Viro
2005-09-18 21:30           ` Alan Cox
2005-09-18 21:14             ` Al Viro
2005-09-19  6:09             ` Coywolf Qi Hunt
2005-09-21  2:18         ` Miles Bader
2005-09-18 17:32   ` Randy.Dunlap
2005-09-19  6:47   ` Coywolf Qi Hunt
2005-09-20  8:53   ` Pekka Enberg
2005-09-20  9:39     ` Al Viro
2005-09-20  9:47       ` Pekka J Enberg
2005-09-20  9:53         ` Al Viro
2005-09-20 10:07           ` Pekka J Enberg
2005-09-20 15:14         ` Randy.Dunlap
2005-09-20 11:18 ` Pekka Enberg
2005-09-20 11:40   ` Russell King
2005-09-20 11:56     ` Denis Vlasenko
2005-09-20 12:20     ` Pekka J Enberg
2005-09-20 12:31       ` Russell King
2005-09-20 12:35         ` Pekka J Enberg
2005-09-20 15:21           ` Randy.Dunlap
2005-09-20 12:53         ` Pekka J Enberg
2005-09-20 17:11         ` Andrew Morton
2005-09-20 17:17           ` Russell King
2005-09-20 18:02           ` Alan Cox
2005-09-20 17:59             ` Andrew Morton
2005-09-20 18:11               ` Russell King
2005-09-20 18:41                 ` Jeff Garzik
2005-09-20 20:41               ` Alan Cox
2005-09-20 19:41             ` Horst von Brand

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=20050918165219.GA595@alpha.home.local \
    --to=willy@w.ods.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rmk+lkml@arm.linux.org.uk \
    --cc=rml@novell.com \
    --cc=torvalds@osdl.org \
    --cc=viro@ftp.linux.org.uk \
    /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;
as well as URLs for NNTP newsgroup(s).