Re: [PATCH] Document kfree and vfree NULL usage

From: Nick Piggin
Date: Sat Nov 27 2004 - 19:18:39 EST


Tomas Carnecky wrote:
Marcel Sebek wrote:

On Sat, Nov 27, 2004 at 04:26:00PM +0200, Pekka Enberg wrote:

Hi,

This patch adds comments for kfree() and vfree() stating that both accept
NULL pointers. I audited vfree() callers and there seems to be lots of
confusion over this in the kernel.

I've cleaned up sound/ directory from "if (x) {k/v}free(x);" and similar
constructions. I'm going to to this for most of the kernel if I found
some time.


Isn't 'if (x) { free(x); }' faster than the call to free() with a NULL
pointer?
What about a macro ?
#define fast_free(x) if (x) { free(x); }
Or even
#define kfree(x) if (x) { _kfree(x); }
Or maybe a inline function so it doesn't break existing code.
inline void kfree(x) { if (x) { _kfree(x); } }


Well if a NULL parameter is the exceptional case, then you don't want to
litter the L1 cache with the extra code that will only save a function
call in rare cases.

And I think it should be the exceptional case, because it shouldn't really
be used for much other than streamline error handling or cleanup functions
to cope with failed allocations without adding checks everywhere. If you're
doing lots of kfree(NULL) as part of normal operation, then that may
suggest you aren't tracking your memory very well.
-
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/