From: Robert P. J. Day Date: Fri, 22 Dec 2006 09:09:11 +0000 (-0800) Subject: [PATCH] Add a new section to CodingStyle, promoting include/linux/kernel.h X-Git-Url: http://git.cdn.openwrt.org/?a=commitdiff_plain;h=58637ec90b7ceed5909e726ac90118852f79d2b1;p=openwrt%2Fstaging%2Fblogic.git [PATCH] Add a new section to CodingStyle, promoting include/linux/kernel.h Add a new section to the CodingStyle file, encouraging people not to re-invent available kernel macros such as ARRAY_SIZE(), FIELD_SIZEOF(), min() and max(), among others. Signed-off-by: Robert P. J. Day Acked-by: Randy Dunlap Acked-by: Jan Engelhardt Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds --- diff --git a/Documentation/CodingStyle b/Documentation/CodingStyle index 0ad6dcb5d45f..9069189e78ef 100644 --- a/Documentation/CodingStyle +++ b/Documentation/CodingStyle @@ -682,6 +682,24 @@ result. Typical examples would be functions that return pointers; they use NULL or the ERR_PTR mechanism to report failure. + Chapter 17: Don't re-invent the kernel macros + +The header file include/linux/kernel.h contains a number of macros that +you should use, rather than explicitly coding some variant of them yourself. +For example, if you need to calculate the length of an array, take advantage +of the macro + + #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) + +Similarly, if you need to calculate the size of some structure member, use + + #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f)) + +There are also min() and max() macros that do strict type checking if you +need them. Feel free to peruse that header file to see what else is already +defined that you shouldn't reproduce in your code. + + Appendix I: References