All of lore.kernel.org
 help / color / mirror / Atom feed
* [RFC][PATCH] kmalloc + memset(foo, 0, bar) = kmalloc0
@ 2003-09-11 13:40 Rolf Eike Beer
  2003-09-11 13:45 ` viro
  2003-09-11 13:58 ` Breno Silva
  0 siblings, 2 replies; 8+ messages in thread
From: Rolf Eike Beer @ 2003-09-11 13:40 UTC (permalink / raw)
  To: linux-kernel

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 *);
 

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

end of thread, other threads:[~2003-09-11 22:25 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-09-11 13:40 [RFC][PATCH] kmalloc + memset(foo, 0, bar) = kmalloc0 Rolf Eike Beer
2003-09-11 13:45 ` 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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.