[RFC][PATCH] kmalloc + memset(foo, 0, bar) = kmalloc0

From: Rolf Eike Beer
Date: Thu Sep 11 2003 - 08:40:34 EST


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

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/