public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* p = kmalloc(sizeof(*p), )
@ 2005-09-18 10:06 Russell King
  2005-09-18 11:04 ` Alan Cox
                   ` (2 more replies)
  0 siblings, 3 replies; 49+ messages in thread
From: Russell King @ 2005-09-18 10:06 UTC (permalink / raw)
  To: Linux Kernel List; +Cc: Linus Torvalds, Al Viro

Hi,

In todays git update, the following text has been added to the coding
style document:

+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.

I completely disagree with the above assertion for the following
reasons:

1. The above implies that the common case is that we are changing the
   names of structures more frequently than we change the contents of
   structures.  Reality is that we change the contents of structures
   more often than the names of those structures.

   Why is this relevant?  If you change the contents of structures,
   they need checking for initialisation.  How do you find all the
   locations that need initialisation checked?  Via grep.  The problem
   is that:

	p = kmalloc(sizeof(*p), ...)

   is not grep-friendly, and can not be used to identify potential
   initialisation sites.  However:

	p = kmalloc(sizeof(struct foo), ...)

   is grep-friendly, and will lead you to inspect each place where
   such a structure is allocated for correct initialisation.

2. in the rare case that you're changing the name of a structure, you're
   grepping the source for all instances for struct old_name, or doing
   a search and replace for struct old_name.  You will find all instances
   of struct old_name by this method and the bug alluded to will not
   happen.

3. if you are changing the name of a structure, in order to ensure that
   everyone gets fixed up correctly, you do not want to keep an old
   declaration of the structure around, unless you have a very very good
   reason to do so.  This will ensure that any missed old structure
   names (eg, because of merging of independent concurrent threads of
   development) get caught.  As a result, any sizeof(struct) also gets
   caught.

So the assertion above that kmalloc(sizeof(*p) is somehow superiour is
rather flawed, and as such should not be in the Coding Style document.

-- 
Russell King
 Linux kernel    2.6 ARM Linux   - http://www.arm.linux.org.uk/
 maintainer of:  2.6 Serial core

^ permalink raw reply	[flat|nested] 49+ messages in thread

end of thread, other threads:[~2005-09-21  2:19 UTC | newest]

Thread overview: 49+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox