Skip to content

Commit 58637ec

Browse files
Robert P. J. DayLinus Torvalds
authored andcommitted
[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 <rpjday@mindspring.com> Acked-by: Randy Dunlap <randy.dunlap@oracle.com> Acked-by: Jan Engelhardt <jengelh@gmx.de> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent 163ca88 commit 58637ec

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

‎Documentation/CodingStyle‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,24 @@ result. Typical examples would be functions that return pointers; they use
682682
NULL or the ERR_PTR mechanism to report failure.
683683

684684

685+
Chapter 17: Don't re-invent the kernel macros
686+
687+
The header file include/linux/kernel.h contains a number of macros that
688+
you should use, rather than explicitly coding some variant of them yourself.
689+
For example, if you need to calculate the length of an array, take advantage
690+
of the macro
691+
692+
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
693+
694+
Similarly, if you need to calculate the size of some structure member, use
695+
696+
#define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
697+
698+
There are also min() and max() macros that do strict type checking if you
699+
need them. Feel free to peruse that header file to see what else is already
700+
defined that you shouldn't reproduce in your code.
701+
702+
685703

686704
Appendix I: References
687705

0 commit comments

Comments
 (0)