public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Paulo Marques <pmarques@grupopie.com>
To: "Jörn Engel" <joern@wohnheim.fh-wedel.de>
Cc: Jesper Juhl <juhl-lkml@dif.dk>,
	Roland Dreier <roland@topspin.com>,
	LKML <linux-kernel@vger.kernel.org>
Subject: Re: RFC: turn kmalloc+memset(,0,) into kcalloc
Date: Wed, 06 Apr 2005 13:15:09 +0100	[thread overview]
Message-ID: <4253D2CD.2040600@grupopie.com> (raw)
In-Reply-To: <20050406112837.GC7031@wohnheim.fh-wedel.de>

Jörn Engel wrote:
> On Tue, 5 April 2005 22:01:49 +0200, Jesper Juhl wrote:
> 
>>On Tue, 5 Apr 2005, Roland Dreier wrote:
>>
>>
>>>    > or simply
>>>    > 	if (!(ptr = kcalloc(n, size, ...)))
>>>    > 		goto out;
>>>    > and save an additional line of screen realestate while you are at it...
>>>
>>>No, please don't do that.  The general kernel style is to avoid
>>>assignments within conditionals.

FWIW, I also agree that assignments within conditionals are evil and 
hurt readability a lot, for no actual benefit.

Since one of the advantages of this cleanup is improve readability, it 
would be counterproductive to do this.

To explain why this cleanup improves readability take the following 
sample from sound/usb/usx2y/usbusx2yaudio.c (a random sample):

> 		us = kmalloc(sizeof(*us) + sizeof(struct urb*) * NOOF_SETRATE_URBS, GFP_KERNEL);
> 		if (NULL == us) {
> 			err = -ENOMEM;
> 			goto cleanup;
> 		}
> 		memset(us, 0, sizeof(*us) + sizeof(struct urb*) * NOOF_SETRATE_URBS); 

In this case you have to read the size portion of the kmalloc and the 
memset carefully to make sure that they are exactly the same, whereas in 
code like this:

> 		us = kcalloc(1, sizeof(*us) + sizeof(struct urb*) * NOOF_SETRATE_URBS, GFP_KERNEL);
> 		if (NULL == us) {
> 			err = -ENOMEM;
> 			goto cleanup;
> 		}

it is self-evident that the whole area is being cleared. It also leaves 
a smaller room for mistakes.

I still don't like the kcalloc API with a "1" argument. Not only most of 
these allocations fall into that case, but also kmalloc_zero (or 
kmalloc_zeroed) is much more clear than kcalloc that changes just one 
letter from kmalloc. Someone reading fast through the code may kcalloc 
as if it were kmalloc.

More over, passing an extra parameter waste a few more bytes of code. I 
know is not much, but if the cleanup will address hundreds of these then 
it starts to be something to consider.

However "calloc" is the standard C interface for doing this, so it makes 
some sense to use it here as well... :(

-- 
Paulo Marques - www.grupopie.com

All that is necessary for the triumph of evil is that good men do nothing.
Edmund Burke (1729 - 1797)

  reply	other threads:[~2005-04-06 12:18 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-04-05 16:26 RFC: turn kmalloc+memset(,0,) into kcalloc Paulo Marques
2005-04-05 18:00 ` Jörn Engel
2005-04-06 12:09   ` Denis Vlasenko
2005-04-05 18:54 ` Jesper Juhl
2005-04-05 19:20   ` Roland Dreier
2005-04-05 20:01     ` Jesper Juhl
2005-04-06 11:28       ` Jörn Engel
2005-04-06 12:15         ` Paulo Marques [this message]
2005-04-06 13:10           ` Pekka Enberg
2005-04-06 15:50             ` Paulo Marques
2005-04-07 23:54               ` Kyle Moffett
2005-04-09  2:11         ` Jesper Juhl
2005-04-07 21:47 ` Adrian Bunk
2005-04-08 12:38   ` Paulo Marques
2005-04-08 13:00     ` Adrian Bunk
2005-04-08 13:20       ` Jörn Engel
2005-04-08 13:29         ` Adrian Bunk
2005-04-08 16:24       ` Paulo Marques
2005-04-08 19:43         ` Adrian Bunk
2005-04-08 19:49           ` Randy.Dunlap
2005-04-08 13:00     ` stack checking (was: Re: RFC: turn kmalloc+memset(,0,) into kcalloc) Jörn Engel
2005-04-09 14:19     ` RFC: turn kmalloc+memset(,0,) into kcalloc Paul Jackson

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=4253D2CD.2040600@grupopie.com \
    --to=pmarques@grupopie.com \
    --cc=joern@wohnheim.fh-wedel.de \
    --cc=juhl-lkml@dif.dk \
    --cc=linux-kernel@vger.kernel.org \
    --cc=roland@topspin.com \
    /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