Re: kfree(NULL)

From: linux
Date: Sat Apr 22 2006 - 16:09:52 EST


> Well, we'd have to start by fixing up the janitors that run around
> taking out the if statements in the callers. :)

You just need to document those two as special. Probably the
simplest is to tell the programmer and the compiler in one
fell swoop:

if (unlikely(p))
kfree(p);

Or that could be wrapped up in a macro:

#define kfree_likely_null(p) if (unlikely(p)) kfree(p)

Or just mention it to the programmer. A few possible one-line comments:

/* Testing before calling is faster if often NULL, as here. */
/* It's worth the (redundant) test for NULL if it often succeeds */
/* This test saves the call often enough to be worth it. */
/* Test for NULL not necessary, but worth it here */
/* Don't delete NULL test; speed trumps code size here */
/* Very often NULL, so avoid call overhead if possible */
/* kfree(NULL) is legal, but probabilities favor testing here */
-
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/