Skip to contentSkip to conversation

include/linux/slab.h: new KFREE() macro.

PATCH Superseded Amit Choudhary 4 participants 4 messages Jan 1, 2007 – Jan 2, 2007 Slab allocator

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 include/linux/slab.h: new KFREE() macro.

Description: new KFREE() macro to set the variable NULL after freeing it.

include/linux/slab.h +6 -0
index 1ef822e..28da74c 100644
@@ -75,6 +75,12 @@ void *__kzalloc(size_t, gfp_t);
 void kfree(const void *);
 unsigned int ksize(const void *);
 
+#define KFREE(x)		\
+	do {			\
+		kfree(x);	\
+		x = NULL;	\
+	} while(0)
+
 /**
  * kcalloc - allocate memory for an array. The memory is set to zero.
  * @n: number of elements.
#1 lore raw

include/linux/slab.h void *__kzalloc(size_t, gfp_t); · line 82

Quoted 5 lines
include/linux/slab.h +5 -0
+#define KFREE(x)		\
+	do {			\
+		kfree(x);	\
+		x = NULL;	\
+	} while(0)

This doesn't work correctly if "x" has side effects -- double evaluation. Use a temporary variable instead, or better, an inline function. A better name wouldn't hurt either (kfree_and_zero()?)

Segher

#2 lore raw
In reply to Amit Choudhary

include/linux/slab.h void *__kzalloc(size_t, gfp_t); · line 82

Quoted 5 lines · Amit Choudhary
include/linux/slab.h +5 -0
+#define KFREE(x)               \
+       do {                    \
+               kfree(x);       \
+               x = NULL;       \
+       } while(0)

NAK until you have actual callers for it. CONFIG_SLAB_DEBUG already catches use after free and double-free so I don't see the point of this.

#3 lore raw
Quoted 3 lines · Pekka Enberg

NAK until you have actual callers for it. CONFIG_SLAB_DEBUG already catches use after free and double-free so I don't see the point of this.

And CONFIG_SLAB_DEBUG actually finds them for real using poisoning, unlike setting the pointer to NULL which is too magic to catch most bugs but rather papers over them.