The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Rolf Eike Beer <eike-kernel@sf-tec.de>
To: linux-kernel@vger.kernel.org
Subject: [RFC][PATCH] kmalloc + memset(foo, 0, bar) = kmalloc0
Date: Thu, 11 Sep 2003 15:40:58 +0200	[thread overview]
Message-ID: <200309111540.58729@bilbo.math.uni-mannheim.de> (raw)

Hi,

a (very) simple grep in drivers/ showed more than 300 matches of code like
this:

foo = kmalloc(bar, baz);
if (! foo)
	return -ENOMEM;
memset(foo, 0, sizeof(foo));

Why not add a small inlined function doing the memset for us
and reducing the code to

foo = kmalloc0(bar, baz);
if (! foo)
	return -ENOMEM;

Eike

--- linux-2.6.0-test5-bk1/include/linux/slab.h	2003-09-11 15:19:40.000000000 +0200
+++ linux-2.6.0-test5-bk1-caliban/include/linux/slab.h	2003-09-11 15:22:56.000000000 +0200
@@ -14,6 +14,7 @@
 #include	<linux/config.h>	/* kmalloc_sizes.h needs CONFIG_ options */
 #include	<linux/gfp.h>
 #include	<linux/types.h>
+#include	<linux/string.h>	/* for memset */
 #include	<asm/page.h>		/* kmalloc_sizes.h needs PAGE_SIZE */
 #include	<asm/cache.h>		/* kmalloc_sizes.h needs L1_CACHE_BYTES */
 
@@ -97,6 +98,16 @@
 	return __kmalloc(size, flags);
 }
 
+static inline void *kmalloc0(size_t size, int flags)
+{
+	void *res = kmalloc(size, flags);
+
+	if (res != NULL)
+		memset(res, 0, size);
+
+	return res;
+}
+
 extern void kfree(const void *);
 extern unsigned int ksize(const void *);
 

             reply	other threads:[~2003-09-11 13:39 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-09-11 13:40 Rolf Eike Beer [this message]
2003-09-11 13:45 ` [RFC][PATCH] kmalloc + memset(foo, 0, bar) = kmalloc0 viro
2003-09-11 14:11   ` Rolf Eike Beer
2003-09-11 17:09   ` Jamie Lokier
2003-09-11 21:58     ` J.A. Magallon
2003-09-11 22:12       ` Alan Cox
2003-09-11 22:25         ` J.A. Magallon
2003-09-11 13:58 ` Breno Silva

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=200309111540.58729@bilbo.math.uni-mannheim.de \
    --to=eike-kernel@sf-tec.de \
    --cc=linux-kernel@vger.kernel.org \
    /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