lkml.org 
[lkml]   [2003]   [Sep]   [11]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
From
Subject[RFC][PATCH] kmalloc + memset(foo, 0, bar) = kmalloc0
Date
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@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
\
 
 \ /
  Last update: 2005-03-22 13:48    [W:0.061 / U:0.844 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site