All of lore.kernel.org
 help / color / mirror / Atom feed
* [RFC PATCH] type safe allocator
@ 2007-08-01  9:06 ` Miklos Szeredi
  0 siblings, 0 replies; 60+ messages in thread
From: Miklos Szeredi @ 2007-08-01  9:06 UTC (permalink / raw)
  To: linux-kernel; +Cc: linux-mm, akpm, torvalds

I wonder why we don't have type safe object allocators a-la new() in
C++ or g_new() in glib?

  fooptr = k_new(struct foo, GFP_KERNEL);

is nicer and more descriptive than

  fooptr = kmalloc(sizeof(*fooptr), GFP_KERNEL);

and more safe than

  fooptr = kmalloc(sizeof(struct foo), GFP_KERNEL);

And we have zillions of both variants.

Note, I'm not advocating mass replacement, but using this in new code,
and gradually converting old ones whenever they need touching anyway.

Signed-off-by: Miklos Szeredi <mszeredi@suse.cz>
---

Index: linux-2.6.22/include/linux/slab.h
===================================================================
--- linux-2.6.22.orig/include/linux/slab.h	2007-07-09 01:32:17.000000000 +0200
+++ linux-2.6.22/include/linux/slab.h	2007-08-01 10:42:45.000000000 +0200
@@ -110,6 +110,38 @@ static inline void *kcalloc(size_t n, si
 	return __kzalloc(n * size, flags);
 }
 
+/**
+ * k_new - allocate given type object
+ * @type: the type of the object to allocate
+ * @flags: the type of memory to allocate.
+ */
+#define k_new(type, flags) ((type *) kmalloc(sizeof(type), flags))
+
+/**
+ * k_new0 - allocate given type object, zero out allocated space
+ * @type: the type of the object to allocate
+ * @flags: the type of memory to allocate.
+ */
+#define k_new0(type, flags) ((type *) kzalloc(sizeof(type), flags))
+
+/**
+ * k_new_array - allocate array of given type object
+ * @type: the type of the object to allocate
+ * @len: the length of the array
+ * @flags: the type of memory to allocate.
+ */
+#define k_new_array(type, len, flags) \
+	((type *) kmalloc(sizeof(type) * (len), flags))
+
+/**
+ * k_new0_array - allocate array of given type object, zero out allocated space
+ * @type: the type of the object to allocate
+ * @len: the length of the array
+ * @flags: the type of memory to allocate.
+ */
+#define k_new0_array(type, len, flags) \
+	((type *) kzalloc(sizeof(type) * (len), flags))
+
 /*
  * Allocator specific definitions. These are mainly used to establish optimized
  * ways to convert kmalloc() calls to kmem_cache_alloc() invocations by selecting

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

end of thread, other threads:[~2007-08-02 19:16 UTC | newest]

Thread overview: 60+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-08-01  9:06 [RFC PATCH] type safe allocator Miklos Szeredi
2007-08-01  9:06 ` Miklos Szeredi
2007-08-01  9:29 ` Adrian Bunk
2007-08-01  9:29   ` Adrian Bunk
2007-08-01  9:41   ` Miklos Szeredi
2007-08-01  9:41     ` Miklos Szeredi
2007-08-01 10:44 ` Andi Kleen
2007-08-01 10:44   ` Andi Kleen
2007-08-01  9:57   ` Miklos Szeredi
2007-08-01  9:57     ` Miklos Szeredi
2007-08-01 11:34     ` Andi Kleen
2007-08-01 11:34       ` Andi Kleen
2007-08-01 10:45       ` Miklos Szeredi
2007-08-01 10:45         ` Miklos Szeredi
2007-08-01 11:44         ` Jan Engelhardt
2007-08-01 11:44           ` Jan Engelhardt
2007-08-01 11:56           ` Miklos Szeredi
2007-08-01 11:56             ` Miklos Szeredi
2007-08-02  3:56 ` Linus Torvalds
2007-08-02  3:56   ` Linus Torvalds
2007-08-02  7:27   ` Miklos Szeredi
2007-08-02  7:27     ` Miklos Szeredi
2007-08-02 12:05     ` Al Viro
2007-08-02 12:05       ` Al Viro
2007-08-02 13:05       ` Miklos Szeredi
2007-08-02 13:05         ` Miklos Szeredi
2007-08-02 17:23     ` Linus Torvalds
2007-08-02 17:23       ` Linus Torvalds
2007-08-02  5:33 ` Christoph Lameter
2007-08-02  5:33   ` Christoph Lameter
2007-08-02  7:38   ` Miklos Szeredi
2007-08-02  7:38     ` Miklos Szeredi
2007-08-02 19:16     ` Christoph Lameter
2007-08-02 19:16       ` Christoph Lameter
2007-08-02  7:37 ` Satyam Sharma
2007-08-02  7:37   ` Satyam Sharma
2007-08-02  7:40   ` Miklos Szeredi
2007-08-02  7:40     ` Miklos Szeredi
2007-08-02 11:31 ` [PATCH] " Miklos Szeredi
2007-08-02 11:31   ` Miklos Szeredi, Miklos Szeredi
2007-08-02 12:04   ` Alexey Dobriyan
2007-08-02 12:04     ` Alexey Dobriyan
2007-08-02 12:24     ` Jan Engelhardt
2007-08-02 12:24       ` Jan Engelhardt
2007-08-02 13:06       ` Miklos Szeredi
2007-08-02 13:06         ` Miklos Szeredi
2007-08-02 13:35         ` Jan Engelhardt
2007-08-02 13:35           ` Jan Engelhardt
2007-08-02 13:49           ` Miklos Szeredi
2007-08-02 13:49             ` Miklos Szeredi
2007-08-02 13:47     ` Peter Zijlstra
2007-08-02 13:47       ` Peter Zijlstra
2007-08-02 14:06       ` Bernd Petrovitsch
2007-08-02 14:06         ` Bernd Petrovitsch
2007-08-02 14:08         ` Jan Engelhardt
2007-08-02 14:08           ` Jan Engelhardt
2007-08-02 18:36   ` Andrew Morton
2007-08-02 18:36     ` Andrew Morton
2007-08-02 18:48     ` Miklos Szeredi
2007-08-02 18:48       ` Miklos Szeredi

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.