mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] Add a new section to CodingStyle, promoting include/linux/kernel.h.
@ 2006-12-18 17:43 Robert P. J. Day
  2006-12-18 17:57 ` Randy Dunlap
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Robert P. J. Day @ 2006-12-18 17:43 UTC (permalink / raw)
  To: Linux kernel mailing list; +Cc: Andrew Morton


  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>

---

  NOTE:  at the moment, there is not a single invocation of the
FIELD_SIZEOF() macro anywhere in the entire source tree, so if someone
had a hankering to rename it to something more catchy, now would be a
good time and i can always resubmit the patch i sent in yesterday.



diff --git a/Documentation/CodingStyle b/Documentation/CodingStyle
index 0ad6dcb..a736333 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




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] Add a new section to CodingStyle, promoting include/linux/kernel.h.
  2006-12-18 17:43 [PATCH] Add a new section to CodingStyle, promoting include/linux/kernel.h Robert P. J. Day
@ 2006-12-18 17:57 ` Randy Dunlap
  2006-12-18 20:18 ` Jan Engelhardt
  2006-12-18 21:50 ` Erik Mouw
  2 siblings, 0 replies; 5+ messages in thread
From: Randy Dunlap @ 2006-12-18 17:57 UTC (permalink / raw)
  To: Robert P. J. Day; +Cc: Linux kernel mailing list, Andrew Morton

On Mon, 18 Dec 2006 12:43:35 -0500 (EST) Robert P. J. Day wrote:

> 
>   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>


> ---
> 
>   NOTE:  at the moment, there is not a single invocation of the
> FIELD_SIZEOF() macro anywhere in the entire source tree, so if someone
> had a hankering to rename it to something more catchy, now would be a
> good time and i can always resubmit the patch i sent in yesterday.
> 
> 
> 
> diff --git a/Documentation/CodingStyle b/Documentation/CodingStyle
> index 0ad6dcb..a736333 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

---
~Randy

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] Add a new section to CodingStyle, promoting include/linux/kernel.h.
  2006-12-18 17:43 [PATCH] Add a new section to CodingStyle, promoting include/linux/kernel.h Robert P. J. Day
  2006-12-18 17:57 ` Randy Dunlap
@ 2006-12-18 20:18 ` Jan Engelhardt
  2006-12-18 21:50 ` Erik Mouw
  2 siblings, 0 replies; 5+ messages in thread
From: Jan Engelhardt @ 2006-12-18 20:18 UTC (permalink / raw)
  To: Robert P. J. Day; +Cc: Linux kernel mailing list, Andrew Morton


>  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: Jan Engelhardt <jengelh@gmx.de>


	-`J'
-- 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] Add a new section to CodingStyle, promoting include/linux/kernel.h.
  2006-12-18 17:43 [PATCH] Add a new section to CodingStyle, promoting include/linux/kernel.h Robert P. J. Day
  2006-12-18 17:57 ` Randy Dunlap
  2006-12-18 20:18 ` Jan Engelhardt
@ 2006-12-18 21:50 ` Erik Mouw
  2006-12-18 21:54   ` Robert P. J. Day
  2 siblings, 1 reply; 5+ messages in thread
From: Erik Mouw @ 2006-12-18 21:50 UTC (permalink / raw)
  To: Robert P. J. Day; +Cc: Linux kernel mailing list, Andrew Morton

On Mon, Dec 18, 2006 at 12:43:35PM -0500, Robert P. J. Day wrote:
>   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.

Good stuff. Could you also mention the printk() KERN_ALERT etc. levels?
I've seen quite some people using "<1>" on the kernelnewbies list.


Erik

-- 
+-- Erik Mouw -- www.harddisk-recovery.com -- +31 70 370 12 90 --
| Lab address: Delftechpark 26, 2628 XH, Delft, The Netherlands

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] Add a new section to CodingStyle, promoting include/linux/kernel.h.
  2006-12-18 21:50 ` Erik Mouw
@ 2006-12-18 21:54   ` Robert P. J. Day
  0 siblings, 0 replies; 5+ messages in thread
From: Robert P. J. Day @ 2006-12-18 21:54 UTC (permalink / raw)
  To: Erik Mouw; +Cc: Linux kernel mailing list, Andrew Morton

On Mon, 18 Dec 2006, Erik Mouw wrote:

> On Mon, Dec 18, 2006 at 12:43:35PM -0500, Robert P. J. Day wrote:
> >   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.
>
> Good stuff. Could you also mention the printk() KERN_ALERT etc.
> levels? I've seen quite some people using "<1>" on the kernelnewbies
> list.

they may be using it on that list, but there's very little of that in
the kernel source itself.  this is the only example i found:

$ grep -r '<[0-7]>' * | grep printk
arch/ppc/syslib/m8260_pci_erratum9.c:   printk("<4>Using IDMA%d for MPC8260 device erratum PCI 9 workaround\n",

maybe i'll submit a patch to clean that one up.

rday

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2006-12-18 21:58 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-12-18 17:43 [PATCH] Add a new section to CodingStyle, promoting include/linux/kernel.h Robert P. J. Day
2006-12-18 17:57 ` Randy Dunlap
2006-12-18 20:18 ` Jan Engelhardt
2006-12-18 21:50 ` Erik Mouw
2006-12-18 21:54   ` Robert P. J. Day

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®