mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Paul Jackson <pj@engr.sgi.com>
To: Paulo Marques <pmarques@grupopie.com>
Cc: bunk@stusta.de, linux-kernel@vger.kernel.org
Subject: Re: RFC: turn kmalloc+memset(,0,) into kcalloc
Date: Sat, 9 Apr 2005 07:19:13 -0700	[thread overview]
Message-ID: <20050409071913.7f2aab65.pj@engr.sgi.com> (raw)
In-Reply-To: <42567B3E.8010403@grupopie.com>

Paulo wrote:
> In the first case you have to read carefully to make sure that the size 
> argument in both the kmalloc and the memset are the same.

If that were the only concern (which it isn't, and I don't pretend to be
addressing the other concerns on this thread) then pulling out the
common sub-expression would help readability, and reduce the chance of a
coding bug should the size change in the future.

Anytime you find yourself coding the same complex expression twice, an
alarm should go off in your mind, and you should ask yourself if there
is a reasonable way to get by with only one instance of the coding in
the source.

The following patch shows what I mean here.

It also replaces the less conventional form:

	if (!(ptr = some_function(args)) do-something;

with the more conventional form:

	if ((ptr = some_function(args)) == NULL)
		do-something;


diff -Naurp 2.6.12-rc2.orig/drivers/usb/input/hid-core.c 2.6.12-rc2/drivers/usb/input/hid-core.c
--- 2.6.12-rc2.orig/drivers/usb/input/hid-core.c	2005-04-09 06:57:47.000000000 -0700
+++ 2.6.12-rc2/drivers/usb/input/hid-core.c	2005-04-09 07:05:14.000000000 -0700
@@ -90,17 +90,18 @@ static struct hid_report *hid_register_r
 static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
 {
 	struct hid_field *field;
+	int sz;
 
 	if (report->maxfield == HID_MAX_FIELDS) {
 		dbg("too many fields in report");
 		return NULL;
 	}
 
-	if (!(field = kmalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
-		+ values * sizeof(unsigned), GFP_KERNEL))) return NULL;
-
-	memset(field, 0, sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
-		+ values * sizeof(unsigned));
+	sz = sizeof(struct hid_field) + usages * sizeof(struct hid_usage) +
+			values * sizeof(unsigned);
+	if ((field = kmalloc(sz, GFP_KERNEL)) == NULL)
+		return NULL;
+	memset(field, 0, sz);
 
 	field->index = report->maxfield++;
 	report->field[field->index] = field;


-- 
                  I won't rest till it's the best ...
                  Programmer, Linux Scalability
                  Paul Jackson <pj@engr.sgi.com> 1.650.933.1373, 1.925.600.0401

      parent reply	other threads:[~2005-04-09 14:21 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-04-05 16:26 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
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     ` Paul Jackson [this message]

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=20050409071913.7f2aab65.pj@engr.sgi.com \
    --to=pj@engr.sgi.com \
    --cc=bunk@stusta.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pmarques@grupopie.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

all inboxes | Powered by JetHome®