CodingStyle: add typedefs chapter
Subsystems (2)
PATCH 1/1 CodingStyle: add typedefs chapter
From: Randy Dunlap <[email protected]>
Add a chapter on typedefs, copied from an email from Linus to lkml on Feb. 3, 2006.
(Subject: Re: [RFC][PATCH 1/5] Virtualization/containers: startup)
--- Documentation/CodingStyle | 77 ++++++++++++++++++++++++++++++++++++++-------- 1 files changed, 65 insertions(+), 12 deletions(-)
Documentation/CodingStyle +65 -12
@@ -155,7 +155,60 @@ problem, which is called the function-gr See next chapter. - Chapter 5: Functions + Chapter 5: Typedefs + +Please don't use things like "vps_t". + +It's a _mistake_ to use typedef for structures and pointers. When you see a + + vps_t a; + +in the source, what does it mean? + +In contrast, if it says + + struct virtual_container *a; + +you can actually tell what "a" is. + +Lots of people think that typedefs "help readability". Not so. They are +useful only for: + + (a) totally opaque objects (where the typedef is actively used to _hide_ + what the object is). + + Example: "pte_t" etc. opaque objects that you can only access using + the proper accessor functions. + + NOTE! Opaqueness and "accessor functions" are not good in themselves. + The reason we have them for things like pte_t etc. is that there + really is absolutely _zero_ portably accessible information there. + + (b) Clear integer types, where the abstraction _helps_ avoid confusion + whether it is "int" or "long". + + u8/u16/u32 are perfectly fine typedefs. + + NOTE! Again - there needs to be a _reason_ for this. If something is + "unsigned long", then there's no reason to do + + typedef long myflags_t; + + but if there is a clear reason for why it under certain circumstances + might be an "unsigned int" and under other configurations might be + "unsigned long", then by all means go ahead and use a typedef. + + (c) when you use sparse to literally create a _new_ type for + type-checking. + +Maybe there are other cases too, but the rule should basically be to NEVER +EVER use a typedef unless you can clearly match one of those rules. + +In general, a pointer, or a struct that has elements that can reasonably +be directly accessed should _never_ be a typedef. + + + Chapter 6: Functions Functions should be short and sweet, and do just one thing. They should fit on one or two screenfuls of text (the ISO/ANSI screen size is 80x24,
@@ -183,7 +236,7 @@ and it gets confused. You know you're b to understand what you did 2 weeks from now. - Chapter 6: Centralized exiting of functions + Chapter 7: Centralized exiting of functions Albeit deprecated by some people, the equivalent of the goto statement is used frequently by compilers in form of the unconditional jump instruction.
@@ -220,7 +273,7 @@ out: return result; } - Chapter 7: Commenting + Chapter 8: Commenting Comments are good, but there is also a danger of over-commenting. NEVER try to explain HOW your code works in a comment: it's much better to
@@ -240,7 +293,7 @@ When commenting the kernel API functions See the files Documentation/kernel-doc-nano-HOWTO.txt and scripts/kernel-doc for details. - Chapter 8: You've made a mess of it + Chapter 9: You've made a mess of it That's OK, we all do. You've probably been told by your long-time Unix user helper that "GNU emacs" automatically formats the C sources for
@@ -288,7 +341,7 @@ re-formatting you may want to take a loo remember: "indent" is not a fix for bad programming. - Chapter 9: Configuration-files + Chapter 10: Configuration-files For configuration options (arch/xxx/Kconfig, and all the Kconfig files), somewhat different indentation is used.
@@ -313,7 +366,7 @@ support for file-systems, for instance) experimental options should be denoted (EXPERIMENTAL). - Chapter 10: Data structures + Chapter 11: Data structures Data structures that have visibility outside the single-threaded environment they are created and destroyed in should always have
@@ -344,7 +397,7 @@ Remember: if another thread can find you have a reference count on it, you almost certainly have a bug. - Chapter 11: Macros, Enums and RTL + Chapter 12: Macros, Enums and RTL Names of macros defining constants and labels in enums are capitalized.
@@ -399,7 +452,7 @@ The cpp manual deals with macros exhaust covers RTL which is used frequently with assembly language in the kernel. - Chapter 12: Printing kernel messages + Chapter 13: Printing kernel messages Kernel developers like to be seen as literate. Do mind the spelling of kernel messages to make a good impression. Do not use crippled
@@ -410,7 +463,7 @@ Kernel messages do not have to be termin Printing numbers in parentheses (%d) adds no value and should be avoided. - Chapter 13: Allocating memory + Chapter 14: Allocating memory The kernel provides the following general purpose memory allocators: kmalloc(), kzalloc(), kcalloc(), and vmalloc(). Please refer to the API
@@ -429,7 +482,7 @@ from void pointer to any other pointer t language. - Chapter 14: The inline disease + Chapter 15: The inline disease There appears to be a common misperception that gcc has a magic "make me faster" speedup option called "inline". While the use of inlines can be
@@ -457,7 +510,7 @@ something it would have done anyway. - Chapter 15: References + Appendix I: References The C Programming Language, Second Edition by Brian W. Kernighan and Dennis M. Ritchie.
@@ -481,4 +534,4 @@ Kernel CodingStyle, by [email protected] at http://www.kroah.com/linux/talks/ols_2002_kernel_codingstyle_talk/html/ -- -Last updated on 30 December 2005 by a community effort on LKML. +Last updated on 30 April 2006.
---
Documentation/CodingStyle problem, which is called the function-gr · line 195
Quoted 4 lines · you
Documentation/CodingStyle +4
-0+ NOTE! Again - there needs to be a _reason_ for this. If something is + "unsigned long", then there's no reason to do + + typedef long myflags_t;
typedef unsigned long myflags_t;
Signature · 1 line
Greetings Michael.
Quoted 7 lines · On Mon, 1 May 2006 09:28:33 +0200 Michael Buesch wrote:
Review
Quoted 4 lines · you
(quoted diff) +4
-0+ NOTE! Again - there needs to be a _reason_ for this. If something is + "unsigned long", then there's no reason to do + + typedef long myflags_t;typedef unsigned long
myflags_t;
You mean that Linus's original email contained a thinko?
Fixed, thanks.
---
From: Randy Dunlap <[email protected]>
Add a chapter on typedefs, copied from an email from Linus to lkml on Feb. 3, 2006.
(Subject: Re: [RFC][PATCH 1/5] Virtualization/containers: startup)
--- Documentation/CodingStyle | 77 ++++++++++++++++++++++++++++++++++++++-------- 1 files changed, 65 insertions(+), 12 deletions(-)
Documentation/CodingStyle +65 -12
@@ -155,7 +155,60 @@ problem, which is called the function-gr See next chapter. - Chapter 5: Functions + Chapter 5: Typedefs + +Please don't use things like "vps_t". + +It's a _mistake_ to use typedef for structures and pointers. When you see a + + vps_t a; + +in the source, what does it mean? + +In contrast, if it says + + struct virtual_container *a; + +you can actually tell what "a" is. + +Lots of people think that typedefs "help readability". Not so. They are +useful only for: + + (a) totally opaque objects (where the typedef is actively used to _hide_ + what the object is). + + Example: "pte_t" etc. opaque objects that you can only access using + the proper accessor functions. + + NOTE! Opaqueness and "accessor functions" are not good in themselves. + The reason we have them for things like pte_t etc. is that there + really is absolutely _zero_ portably accessible information there. + + (b) Clear integer types, where the abstraction _helps_ avoid confusion + whether it is "int" or "long". + + u8/u16/u32 are perfectly fine typedefs. + + NOTE! Again - there needs to be a _reason_ for this. If something is + "unsigned long", then there's no reason to do + + typedef unsigned long myflags_t; + + but if there is a clear reason for why it under certain circumstances + might be an "unsigned int" and under other configurations might be + "unsigned long", then by all means go ahead and use a typedef. + + (c) when you use sparse to literally create a _new_ type for + type-checking. + +Maybe there are other cases too, but the rule should basically be to NEVER +EVER use a typedef unless you can clearly match one of those rules. + +In general, a pointer, or a struct that has elements that can reasonably +be directly accessed should _never_ be a typedef. + + + Chapter 6: Functions Functions should be short and sweet, and do just one thing. They should fit on one or two screenfuls of text (the ISO/ANSI screen size is 80x24,
@@ -183,7 +236,7 @@ and it gets confused. You know you're b to understand what you did 2 weeks from now. - Chapter 6: Centralized exiting of functions + Chapter 7: Centralized exiting of functions Albeit deprecated by some people, the equivalent of the goto statement is used frequently by compilers in form of the unconditional jump instruction.
@@ -220,7 +273,7 @@ out: return result; } - Chapter 7: Commenting + Chapter 8: Commenting Comments are good, but there is also a danger of over-commenting. NEVER try to explain HOW your code works in a comment: it's much better to
@@ -240,7 +293,7 @@ When commenting the kernel API functions See the files Documentation/kernel-doc-nano-HOWTO.txt and scripts/kernel-doc for details. - Chapter 8: You've made a mess of it + Chapter 9: You've made a mess of it That's OK, we all do. You've probably been told by your long-time Unix user helper that "GNU emacs" automatically formats the C sources for
@@ -288,7 +341,7 @@ re-formatting you may want to take a loo remember: "indent" is not a fix for bad programming. - Chapter 9: Configuration-files + Chapter 10: Configuration-files For configuration options (arch/xxx/Kconfig, and all the Kconfig files), somewhat different indentation is used.
@@ -313,7 +366,7 @@ support for file-systems, for instance) experimental options should be denoted (EXPERIMENTAL). - Chapter 10: Data structures + Chapter 11: Data structures Data structures that have visibility outside the single-threaded environment they are created and destroyed in should always have
@@ -344,7 +397,7 @@ Remember: if another thread can find you have a reference count on it, you almost certainly have a bug. - Chapter 11: Macros, Enums and RTL + Chapter 12: Macros, Enums and RTL Names of macros defining constants and labels in enums are capitalized.
@@ -399,7 +452,7 @@ The cpp manual deals with macros exhaust covers RTL which is used frequently with assembly language in the kernel. - Chapter 12: Printing kernel messages + Chapter 13: Printing kernel messages Kernel developers like to be seen as literate. Do mind the spelling of kernel messages to make a good impression. Do not use crippled
@@ -410,7 +463,7 @@ Kernel messages do not have to be termin Printing numbers in parentheses (%d) adds no value and should be avoided. - Chapter 13: Allocating memory + Chapter 14: Allocating memory The kernel provides the following general purpose memory allocators: kmalloc(), kzalloc(), kcalloc(), and vmalloc(). Please refer to the API
@@ -429,7 +482,7 @@ from void pointer to any other pointer t language. - Chapter 14: The inline disease + Chapter 15: The inline disease There appears to be a common misperception that gcc has a magic "make me faster" speedup option called "inline". While the use of inlines can be
@@ -457,7 +510,7 @@ something it would have done anyway. - Chapter 15: References + Appendix I: References The C Programming Language, Second Edition by Brian W. Kernighan and Dennis M. Ritchie.
@@ -481,4 +534,4 @@ Kernel CodingStyle, by [email protected] at http://www.kroah.com/linux/talks/ols_2002_kernel_codingstyle_talk/html/ -- -Last updated on 30 December 2005 by a community effort on LKML. +Last updated on 30 April 2006.
Quoted 4 lines
Documentation/CodingStyle +2
-1- Chapter 5: Functions + Chapter 5: Typedefs +
Documentation/CodingStyle problem, which is called the function-gr · line 175
Quoted 10 lines
Documentation/CodingStyle +10
-0+Please don't use things like "vps_t". +It's a _mistake_ to use typedef for structures and pointers. When you see a + vps_t a; +in the source, what does it mean? +In contrast, if it says + struct virtual_container *a; +you can actually tell what "a" is. + +Lots of people think that typedefs "help readability". Not so. They are +useful only for:
[...]
What about task_t vs struct task_struct? Both are used in the kernel.
Jan Engelhardt
Quoted 12 lines · Jan Engelhardt
Quoted 10 lines
Documentation/CodingStyle +10
-0+Please don't use things like "vps_t". +It's a _mistake_ to use typedef for structures and pointers. When you see a + vps_t a; +in the source, what does it mean? +In contrast, if it says + struct virtual_container *a; +you can actually tell what "a" is. + +Lots of people think that typedefs "help readability". Not so. They are +useful only for:[...]
Quoted 1 line
What about
task_tvs structtask_struct? Both are used in the kernel.
task_t => struct task
struct task_struct => struct task
Roughly 2765 hits :-\
Quoted 13 lines · Alexey Dobriyan
Quoted 12 lines · Jan Engelhardt
Quoted 10 lines
(quoted diff) +10
-0+Please don't use things like "vps_t". +It's a _mistake_ to use typedef for structures and pointers. When you see a + vps_t a; +in the source, what does it mean? +In contrast, if it says + struct virtual_container *a; +you can actually tell what "a" is. + +Lots of people think that typedefs "help readability". Not so. They are +useful only for:[...]
Quoted 6 lines
Quoted 1 line
What about
task_tvs structtask_struct? Both are used in the kernel.
task_t=> struct task
structtask_struct=> struct taskRoughly 2765 hits :-\
Yes, also 'current' is probably the most used. Any, since this has become a FAQ, maybe it's about time to put something in the Documentation?
usr/src/linux-2.6.16.4/Documentation/CodingStyle renamed from usr/src/linux-2.6.16.4/Documentation/CodingStyle.orig +27 -0
@@ -343,6 +343,33 @@ Remember: if another thread can find your data structure, and you don't have a reference count on it, you almost certainly have a bug. + typedefs and and structs + +Typedefs should never be used for information hiding. In other words, +if a typedef defines an aggregate type, and the individual components +are accessed anywhere in the code, a typedef should not be used. + +An example of proper usage: +typedef struct opaque_type FILE; // In a header + + FILE *fp; // In a program block + +The type 'FILE' in this example is something that was defined in +a 'C' runtime library. The code uses pointers to this opaque type, +but never even knows, and doesn't care, what's inside that structure. +Therefore, FILE can have been defined using a typedef. + +An example of incorrect usage: + +typedef struct file_operations FO; // In a header + + FO fo; // In a program block + memset(&foo, 0x00, sizeof(foo)); + +In this case, object FO contains structure members that will be +accessed by the code. It should not have been defined. Instead, +the structure name should have been used directly. + Chapter 11: Macros, Enums and RTL
In case the M$ server mangles the patch, it's included as an attachment.
Cheers,
Dick Johnson
Penguin : Linux version 2.6.16.4 on an i686 machine (5592.89 BogoMips). New book: http://www.lymanschool.com
_
****************************************************************
The information transmitted in this message is confidential and may be privileged. Any review, retransmission, dissemination, or other use of this information by persons or entities other than the intended recipient is prohibited. If you are not the intended recipient, please notify Analogic Corporation immediately - by replying to this message or by sending an email to [email protected] - and destroy all copies of this information, including any attachments, without reading or disclosing them.
Thank you.
usr/src/linux-2.6.16.4/Documentation/CodingStyle renamed from usr/src/linux-2.6.16.4/Documentation/CodingStyle.orig +27 -0
@@ -343,6 +343,33 @@ Remember: if another thread can find your data structure, and you don't have a reference count on it, you almost certainly have a bug. + typedefs and and structs + +Typedefs should never be used for information hiding. In other words, +if a typedef defines an aggregate type, and the individual components +are accessed anywhere in the code, a typedef should not be used. + +An example of proper usage: +typedef struct opaque_type FILE; // In a header + + FILE *fp; // In a program block + +The type 'FILE' in this example is something that was defined in +a 'C' runtime library. The code uses pointers to this opaque type, +but never even knows, and doesn't care, what's inside that structure. +Therefore, FILE can have been defined using a typedef. + +An example of incorrect usage: + +typedef struct file_operations FO; // In a header + + FO fo; // In a program block + memset(&foo, 0x00, sizeof(foo)); + +In this case, object FO contains structure members that will be +accessed by the code. It should not have been defined. Instead, +the structure name should have been used directly. + Chapter 11: Macros, Enums and RTL
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
linux-os (Dick Johnson) napsal(a):
Quoted 99 lines · Alexey Dobriyan
Quoted 14 lines · Jan Engelhardt
Quoted 12 lines
Quoted 10 lines
(quoted diff) +10
-0+Please don't use things like "vps_t". +It's a _mistake_ to use typedef for structures and pointers. When you see a + vps_t a; +in the source, what does it mean? +In contrast, if it says + struct virtual_container *a; +you can actually tell what "a" is. + +Lots of people think that typedefs "help readability". Not so. They are +useful only for:[...]
Quoted 5 lines
Quoted 1 line
What about
task_tvs structtask_struct? Both are used in the kernel.
task_t=> struct task
structtask_struct=> struct taskRoughly 2765 hits :-\
Yes, also 'current' is probably the most used. Any, since this has become a FAQ, maybe it's about time to put something in the Documentation?
usr/src/linux-2.6.16.4/Documentation/CodingStyle renamed from usr/src/linux-2.6.16.4/Documentation/CodingStyle.orig +27
-0@@ -343,6 +343,33 @@ Remember: if another thread can find your data structure, and you don't have a reference count on it, you almost certainly have a bug. + typedefs and and structs + +Typedefs should never be used for information hiding. In other words, +if a typedef defines an aggregate type, and the individual components +are accessed anywhere in the code, a typedef should not be used. + +An example of proper usage: +typedef struct opaque_type FILE; // In a header + + FILE *fp; // In a program block + +The type 'FILE' in this example is something that was defined in +a 'C' runtime library. The code uses pointers to this opaque type, +but never even knows, and doesn't care, what's inside that structure. +Therefore, FILE can have been defined using a typedef. + +An example of incorrect usage: + +typedef struct file_operations FO; // In a header + + FO fo; // In a program block + memset(&foo, 0x00, sizeof(foo)); + +In this case, object FO contains structure members that will be +accessed by the code. It should not have been defined. Instead, +the structure name should have been used directly. + Chapter 11: Macros, Enums and RTLIn case the M$ server mangles the patch, it's included as an attachment.
Cheers,
Dick Johnson
Penguin : Linux version 2.6.16.4 on an i686 machine (5592.89 BogoMips). New book: http://www.lymanschool.com
_****************************************************************
The information transmitted in this message is confidential and may be privileged. Any review, retransmission, dissemination, or other use of this information by persons or entities other than the intended recipient is prohibited. If you are not the intended recipient, please notify Analogic Corporation immediately - by replying to this message or by sending an email to [email protected] - and destroy all copies of this information, including any attachments, without reading or disclosing them.Thank you.
------------------------------------------------------------------------
usr/src/linux-2.6.16.4/Documentation/CodingStyle renamed from usr/src/linux-2.6.16.4/Documentation/CodingStyle.orig +11
-0@@ -343,6 +343,33 @@ Remember: if another thread can find your data structure, and you don't have a reference count on it, you almost certainly have a bug. + typedefs and and structs + +Typedefs should never be used for information hiding. In other words, +if a typedef defines an aggregate type, and the individual components +are accessed anywhere in the code, a typedef should not be used. + +An example of proper usage: +typedef struct opaque_type FILE; // In a header + + FILE *fp; // In a program block +
usr/src/linux-2.6.16.4/Documentation/CodingStyle line 366
Quoted 10 lines
usr/src/linux-2.6.16.4/Documentation/CodingStyle renamed from usr/src/linux-2.6.16.4/Documentation/CodingStyle.orig +10
-0+The type 'FILE' in this example is something that was defined in +a 'C' runtime library. The code uses pointers to this opaque type, +but never even knows, and doesn't care, what's inside that structure. +Therefore, FILE can have been defined using a typedef. + +An example of incorrect usage: + +typedef struct file_operations FO; // In a header + + FO fo; // In a program block
You meant 'FO foo', didn't you?
usr/src/linux-2.6.16.4/Documentation/CodingStyle line 375
Quoted 9 lines
usr/src/linux-2.6.16.4/Documentation/CodingStyle renamed from usr/src/linux-2.6.16.4/Documentation/CodingStyle.orig +6
-0+ memset(&foo, 0x00, sizeof(foo)); + +In this case, object FO contains structure members that will be +accessed by the code. It should not have been defined. Instead, +the structure name should have been used directly. + Chapter 11: Macros, Enums and RTL
regards,
- --
Jiri Slaby www.fi.muni.cz/~xslaby
\_.-^-._ [email protected] _.-^-._/
B67499670407CE62ACC8 22A032CC55C339D47A7E -----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org
iD8DBQFEViK1MsxVwznUen4RAi8wAJ9km1HybAI8qRUR5IY9y52+LDHdogCfSy4O ENhfqjTKF/+zdyjmuV7wXws=
=yZTA
-----END PGP SIGNATURE-----
Quoted 1 line · Jan Engelhardt
What about
task_tvs structtask_struct? Both are used in the kernel.
If you use task_struct's fields, pick struct task_struct, otherwise pick task_t.
Signature · 4 lines
Best regards, Artem B. Bityutskiy Oktet Labs (St. Petersburg), Software Engineer. +7 812 4286709 (office) +7 911 2449030 (mobile) E-mail: [email protected], Web: www.oktetlabs.ru
Quoted 1 line · Jan Engelhardt
What about
task_tvs structtask_struct? Both are used in the kernel.
task_t should probably die.
Signature · 1 line
dwmw2
Quoted 4 lines
Quoted 2 lines
What about
task_tvs structtask_struct? Both are used in the kernel.
task_tshould probably die.
Acked.
Jan Engelhardt
Jan Engelhardt napsal(a):
Quoted 4 lines
Quoted 2 lines
Quoted 1 line
What about
task_tvs structtask_struct? Both are used in the kernel.
task_tshould probably die.Acked.
Wouldn't this be a janitors' task?
regards,
Signature · 3 lines
Jiri Slaby www.fi.muni.cz/~xslaby \_.-^-._ [email protected] _.-^-._/ B67499670407CE62ACC8 22A032CC55C339D47A7E
Quoted 5 lines
Quoted 4 lines
Quoted 2 lines
Quoted 1 line
What about
task_tvs structtask_struct? Both are used in the kernel.
task_tshould probably die.Acked.
Wouldn't this be a janitors' task?
Too simple :)
find rc3 -type f -print0 | xargs -0 perl -i -pe
's/\btask_t\b/struct task_struct'
+ a compile test afterwards. Something I missed? (Besides that lines may get longer and violate the 80-column CodingStyle rule.)
Jan Engelhardt
Quoted 5 lines · Jan Engelhardt
find rc3 -type f -print0 | xargs -0 perl -i -pe
's/\btask_t\b/structtask_struct'+ a compile test afterwards. Something I missed? (Besides that lines may get longer and violate the 80-column CodingStyle rule.)
If we're going to do that, we might as well make it 'struct task'. The additional '_struct' is redundant.
Signature · 1 line
dwmw2
Quoted 9 lines · David Woodhouse
Quoted 5 lines · Jan Engelhardt
find rc3 -type f -print0 | xargs -0 perl -i -pe
's/\btask_t\b/structtask_struct'+ a compile test afterwards. Something I missed? (Besides that lines may get longer and violate the 80-column CodingStyle rule.)
If we're going to do that, we might as well make it 'struct task'. The additional '
_struct' is redundant.
struct sighand_struct
signal_struct files_struct fs_struct sel_arg_struct mmap_arg_struct vm_area_struct tty_struct fasync_struct rose_facilities_struct poll_table_struct ...
What is the best day for Grand Renaming?
Quoted 2 lines · Alexey Dobriyan
What is the best day for Grand Renaming?
Usually when you're in the area changing something anyway.
Jörn
Signature · 2 lines
Good warriors cause others to come to them and do not go to others. -- Sun Tzu
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
"David" == David Woodhouse <[email protected]> writes:
Quoted 5 lines · Jan Engelhardt
Quoted 5 lines
find rc3 -type f -print0 | xargs -0 perl -i -pe
's/\btask_t\b/structtask_struct'+ a compile test afterwards. Something I missed? (Besides that lines may get longer and violate the 80-column CodingStyle rule.)
David> If we're going to do that, we might as well make it 'struct David> task'. The additional '_struct' is redundant.
There were some long discussions about that a couple of years ago. I believe Linus stated that he preferred _struct for those things.
It is also less likely to hit name clashes.
Jes
Documentation/CodingStyle problem, which is called the function-gr · line 190
Quoted 4 lines · Randy.Dunlap
+ (b) Clear integer types, where the abstraction _helps_ avoid confusion
Documentation/CodingStyle +3
-0+ whether it is "int" or "long". + + u8/u16/u32 are perfectly fine typedefs.
No, u8/u16/u32 are fall into category (d):
(d) New types which are identical to standard C99 types, in certain exceptional circumstances.
Although it would only take a short amount of time for the eyes and brain to become accustomed to the standard types like 'uint32_t', some people object to their use anyway.
Therefore, the gratuitous 'u8/u16/u32/u64' types and their signed equivalents which are identical to standard types are permitted -- although they are not mandatory.
(e) Types safe for use in userspace.
In certain structures which are visible to userspace, we cannot require C99 types and cannot use the 'u32' form above. Thus, we use __u32 and similar types in all structures which are shared with userspace.
Signature · 1 line
dwmw2
Quoted 7 lines · On Mon, 01 May 2006 18:06:06 +0100 David Woodhouse wrote:
Review
Quoted 4 lines · Randy.Dunlap
+ (b) Clear integer types, where the abstraction _helps_ avoid confusion
(quoted diff) +3
-0+ whether it is "int" or "long". + + u8/u16/u32 are perfectly fine typedefs.No, u8/u16/u32 are fall into category (d):
Thanks. Here's an updated version.
---
From: Randy Dunlap <[email protected]>
Add a chapter on typedefs, copied from an email from Linus to lkml on Feb. 3, 2006:
(Subject: Re: [RFC][PATCH 1/5] Virtualization/containers: startup) with added lkml feedback, esp. David Woodhouse.
--- Documentation/CodingStyle | 96 ++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 84 insertions(+), 12 deletions(-)
Documentation/CodingStyle +84 -12
@@ -155,7 +155,79 @@ problem, which is called the function-gr See next chapter. - Chapter 5: Functions + Chapter 5: Typedefs + +Please don't use things like "vps_t". + +It's a _mistake_ to use typedef for structures and pointers. When you see a + + vps_t a; + +in the source, what does it mean? + +In contrast, if it says + + struct virtual_container *a; + +you can actually tell what "a" is. + +Lots of people think that typedefs "help readability". Not so. They are +useful only for: + + (a) totally opaque objects (where the typedef is actively used to _hide_ + what the object is). + + Example: "pte_t" etc. opaque objects that you can only access using + the proper accessor functions. + + NOTE! Opaqueness and "accessor functions" are not good in themselves. + The reason we have them for things like pte_t etc. is that there + really is absolutely _zero_ portably accessible information there. + + (b) Clear integer types, where the abstraction _helps_ avoid confusion + whether it is "int" or "long". + + u8/u16/u32 are perfectly fine typedefs, although they fit into + category (d) better than here. + + NOTE! Again - there needs to be a _reason_ for this. If something is + "unsigned long", then there's no reason to do + + typedef unsigned long myflags_t; + + but if there is a clear reason for why it under certain circumstances + might be an "unsigned int" and under other configurations might be + "unsigned long", then by all means go ahead and use a typedef. + + (c) when you use sparse to literally create a _new_ type for + type-checking. + + (d) New types which are identical to standard C99 types, in certain + exceptional circumstances. + + Although it would only take a short amount of time for the eyes and + brain to become accustomed to the standard types like 'uint32_t', + some people object to their use anyway. + + Therefore, the gratuitous 'u8/u16/u32/u64' types and their signed + equivalents which are identical to standard types are permitted -- + although they are not mandatory. + + (e) Types safe for use in userspace. + + In certain structures which are visible to userspace, we cannot + require C99 types and cannot use the 'u32' form above. Thus, we + use __u32 and similar types in all structures which are shared + with userspace. + +Maybe there are other cases too, but the rule should basically be to NEVER +EVER use a typedef unless you can clearly match one of those rules. + +In general, a pointer, or a struct that has elements that can reasonably +be directly accessed should _never_ be a typedef. + + + Chapter 6: Functions Functions should be short and sweet, and do just one thing. They should fit on one or two screenfuls of text (the ISO/ANSI screen size is 80x24,
@@ -183,7 +255,7 @@ and it gets confused. You know you're b to understand what you did 2 weeks from now. - Chapter 6: Centralized exiting of functions + Chapter 7: Centralized exiting of functions Albeit deprecated by some people, the equivalent of the goto statement is used frequently by compilers in form of the unconditional jump instruction.
@@ -220,7 +292,7 @@ out: return result; } - Chapter 7: Commenting + Chapter 8: Commenting Comments are good, but there is also a danger of over-commenting. NEVER try to explain HOW your code works in a comment: it's much better to
@@ -240,7 +312,7 @@ When commenting the kernel API functions See the files Documentation/kernel-doc-nano-HOWTO.txt and scripts/kernel-doc for details. - Chapter 8: You've made a mess of it + Chapter 9: You've made a mess of it That's OK, we all do. You've probably been told by your long-time Unix user helper that "GNU emacs" automatically formats the C sources for
@@ -288,7 +360,7 @@ re-formatting you may want to take a loo remember: "indent" is not a fix for bad programming. - Chapter 9: Configuration-files + Chapter 10: Configuration-files For configuration options (arch/xxx/Kconfig, and all the Kconfig files), somewhat different indentation is used.
@@ -313,7 +385,7 @@ support for file-systems, for instance) experimental options should be denoted (EXPERIMENTAL). - Chapter 10: Data structures + Chapter 11: Data structures Data structures that have visibility outside the single-threaded environment they are created and destroyed in should always have
@@ -344,7 +416,7 @@ Remember: if another thread can find you have a reference count on it, you almost certainly have a bug. - Chapter 11: Macros, Enums and RTL + Chapter 12: Macros, Enums and RTL Names of macros defining constants and labels in enums are capitalized.
@@ -399,7 +471,7 @@ The cpp manual deals with macros exhaust covers RTL which is used frequently with assembly language in the kernel. - Chapter 12: Printing kernel messages + Chapter 13: Printing kernel messages Kernel developers like to be seen as literate. Do mind the spelling of kernel messages to make a good impression. Do not use crippled
@@ -410,7 +482,7 @@ Kernel messages do not have to be termin Printing numbers in parentheses (%d) adds no value and should be avoided. - Chapter 13: Allocating memory + Chapter 14: Allocating memory The kernel provides the following general purpose memory allocators: kmalloc(), kzalloc(), kcalloc(), and vmalloc(). Please refer to the API
@@ -429,7 +501,7 @@ from void pointer to any other pointer t language. - Chapter 14: The inline disease + Chapter 15: The inline disease There appears to be a common misperception that gcc has a magic "make me faster" speedup option called "inline". While the use of inlines can be
@@ -457,7 +529,7 @@ something it would have done anyway. - Chapter 15: References + Appendix I: References The C Programming Language, Second Edition by Brian W. Kernighan and Dennis M. Ritchie.
@@ -481,4 +553,4 @@ Kernel CodingStyle, by [email protected] at http://www.kroah.com/linux/talks/ols_2002_kernel_codingstyle_talk/html/ -- -Last updated on 30 December 2005 by a community effort on LKML. +Last updated on 30 April 2006.
Quoted 11 lines · David Woodhouse
Documentation/CodingStyle problem, which is called the function-gr · line 190
Quoted 4 lines · Randy.Dunlap
+ (b) Clear integer types, where the abstraction _helps_ avoid confusion
Documentation/CodingStyle +3
-0+ whether it is "int" or "long". + + u8/u16/u32 are perfectly fine typedefs.No, u8/u16/u32 are fall into category (d):
(d) New types which are identical to standard C99 types, in certain exceptional circumstances.
Quoted 7 lines
Although it would only take a short amount of time for the eyes and brain to become accustomed to the standard types like '
uint32_t', some people object to their use anyway.Therefore, the gratuitous 'u8/u16/u32/u64' types and their signed equivalents which are identical to standard types are permitted -- although they are not mandatory.
IMHO u32 etc. are the well established data types used everywhere in kernel source. Your wording suggests that the use of C99 types would be better, and while I respect your personal opinion, I think it is wrong to put that in the kernel CodingStyle document.
c.f. http://lkml.org/lkml/2004/12/14/127
Johannes
Quoted 5 lines · Johannes Stezenbach
IMHO u32 etc. are the well established data types used everywhere in kernel source. Your wording suggests that the use of C99 types would be better, and while I respect your personal opinion, I think it is wrong to put that in the kernel CodingStyle document.
Perhaps the word 'gratuitous' should be removed, then, if you object to being able to infer my opinion.
The point remains that the peculiarity should definitely be documented, along with an explanation of the reasoning (or lack of such) behind it.
Quoted 1 line
That's about types used for _export_. It's accepted that __uXX types are necessary in stuff which is visible by userspace. That was point (e).¹
The only bit in that mail which is relevant to my point (d) is the penultimate (and final) paragraphs. And those are a complete non sequitur and make just as much sense if you swap over 'u32' and 'uint32_t' and also 'kernel' and 'C language'...
"In other words, uint8_t/uint16_t/uint32_t/uint64_t (and the signed
versions: int8_t and friends) _are_ the standard names in the C language, and the __u8/__u16/etc versions have always existed alongside them for things like header files that have namespace issues.
"So forget about that stupid abortion called "u32" already. It buys you absolutely nothing."
Signature · 5 lines
dwmw2 ¹ Actually I had a conversation with Uli the other day in which he seemed to suggest that proper C99 types _would_ be better, but let's not go there for now.
Quoted 23 lines · David Woodhouse
Quoted 5 lines · Johannes Stezenbach
IMHO u32 etc. are the well established data types used everywhere in kernel source. Your wording suggests that the use of C99 types would be better, and while I respect your personal opinion, I think it is wrong to put that in the kernel CodingStyle document.
Perhaps the word 'gratuitous' should be removed, then, if you object to being able to infer my opinion.
The point remains that the peculiarity should definitely be documented, along with an explanation of the reasoning (or lack of such) behind it.
Quoted 1 line
That's about types used for _export_. It's accepted that
__uXXtypes are necessary in stuff which is visible by userspace. That was point (e).¹The only bit in that mail which is relevant to my point (d) is the penultimate (and final) paragraphs. And those are a complete non sequitur and make just as much sense if you swap over 'u32' and '
uint32_t' and also 'kernel' and 'C language'...
Quoted 7 lines
"In other words, uint8_t/uint16_t/uint32_t/uint64_t (and the signed
versions:int8_tand friends) _are_ the standard names in the C language, and the __u8/__u16/etc versions have always existed alongside them for things like header files that have namespace issues."So forget about that stupid abortion called "u32" already. It buys you absolutely nothing."
;-)
Maybe I got it wrong, but my impression so far was that u8 etc. are preferred for kernel code, and C99 types are merely tolerated. (Mostly for consistency reasons, I guess, since most old code uses u8 etc.)
However, personally I don't care either way, I just want to make sure that code written acording to Documentation/CodingStyle also meets Linus' expectations.
Johannes
Quoted 4 lines · Johannes Stezenbach
Maybe I got it wrong, but my impression so far was that u8 etc. are preferred for kernel code, and C99 types are merely tolerated. (Mostly for consistency reasons, I guess, since most old code uses u8 etc.)
It depends. In existing code, you should follow the precedent which is set already. In new code of your own, you do as you see fit. Perhaps that should be made clearer...
(d) New types which are identical to standard C99 types, in certain exceptional circumstances.
Although it would only take a short amount of time for the eyes and brain to become accustomed to the standard types like 'uint32_t', some people object to their use anyway.
Therefore, the Linux-specific 'u8/u16/u32/u64' types and their signed equivalents which are identical to standard types are permitted -- although they are not mandatory in new code of your own.
When editing existing code which already uses one or the other set of types, you should conform to the existing choices in that code.
Signature · 1 line
dwmw2
Quoted 9 lines · On Tue, 02 May 2006 15:31:48 +0100 David Woodhouse wrote:
Quoted 4 lines · Johannes Stezenbach
Maybe I got it wrong, but my impression so far was that u8 etc. are preferred for kernel code, and C99 types are merely tolerated. (Mostly for consistency reasons, I guess, since most old code uses u8 etc.)
It depends. In existing code, you should follow the precedent which is set already. In new code of your own, you do as you see fit. Perhaps that should be made clearer...
patch updated, thanks.
---
From: Randy Dunlap <[email protected]>
Add a chapter on typedefs, based on an email from Linus to lkml on Feb. 3, 2006:
(Subject: Re: [RFC][PATCH 1/5] Virtualization/containers: startup) with added lkml feedback, esp. David Woodhouse.
--- Documentation/CodingStyle | 100 ++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 88 insertions(+), 12 deletions(-)
Documentation/CodingStyle +88 -12
@@ -155,7 +155,83 @@ problem, which is called the function-gr See next chapter. - Chapter 5: Functions + Chapter 5: Typedefs + +Please don't use things like "vps_t". + +It's a _mistake_ to use typedef for structures and pointers. When you see a + + vps_t a; + +in the source, what does it mean? + +In contrast, if it says + + struct virtual_container *a; + +you can actually tell what "a" is. + +Lots of people think that typedefs "help readability". Not so. They are +useful only for: + + (a) totally opaque objects (where the typedef is actively used to _hide_ + what the object is). + + Example: "pte_t" etc. opaque objects that you can only access using + the proper accessor functions. + + NOTE! Opaqueness and "accessor functions" are not good in themselves. + The reason we have them for things like pte_t etc. is that there + really is absolutely _zero_ portably accessible information there. + + (b) Clear integer types, where the abstraction _helps_ avoid confusion + whether it is "int" or "long". + + u8/u16/u32 are perfectly fine typedefs, although they fit into + category (d) better than here. + + NOTE! Again - there needs to be a _reason_ for this. If something is + "unsigned long", then there's no reason to do + + typedef unsigned long myflags_t; + + but if there is a clear reason for why it under certain circumstances + might be an "unsigned int" and under other configurations might be + "unsigned long", then by all means go ahead and use a typedef. + + (c) when you use sparse to literally create a _new_ type for + type-checking. + + (d) New types which are identical to standard C99 types, in certain + exceptional circumstances. + + Although it would only take a short amount of time for the eyes and + brain to become accustomed to the standard types like 'uint32_t', + some people object to their use anyway. + + Therefore, the Linux-specific 'u8/u16/u32/u64' types and their + signed equivalents which are identical to standard types are + permitted -- although they are not mandatory in new code of your + own. + + When editing existing code which already uses one or the other set + of types, you should conform to the existing choices in that code. + + (e) Types safe for use in userspace. + + In certain structures which are visible to userspace, we cannot + require C99 types and cannot use the 'u32' form above. Thus, we + use __u32 and similar types in all structures which are shared + with userspace. + +Maybe there are other cases too, but the rule should basically be to NEVER +EVER use a typedef unless you can clearly match one of those rules. + +In general, a pointer, or a struct that has elements that can reasonably +be directly accessed should _never_ be a typedef. + + + Chapter 6: Functions Functions should be short and sweet, and do just one thing. They should fit on one or two screenfuls of text (the ISO/ANSI screen size is 80x24,
@@ -183,7 +259,7 @@ and it gets confused. You know you're b to understand what you did 2 weeks from now. - Chapter 6: Centralized exiting of functions + Chapter 7: Centralized exiting of functions Albeit deprecated by some people, the equivalent of the goto statement is used frequently by compilers in form of the unconditional jump instruction.
@@ -220,7 +296,7 @@ out: return result; } - Chapter 7: Commenting + Chapter 8: Commenting Comments are good, but there is also a danger of over-commenting. NEVER try to explain HOW your code works in a comment: it's much better to
@@ -240,7 +316,7 @@ When commenting the kernel API functions See the files Documentation/kernel-doc-nano-HOWTO.txt and scripts/kernel-doc for details. - Chapter 8: You've made a mess of it + Chapter 9: You've made a mess of it That's OK, we all do. You've probably been told by your long-time Unix user helper that "GNU emacs" automatically formats the C sources for
@@ -288,7 +364,7 @@ re-formatting you may want to take a loo remember: "indent" is not a fix for bad programming. - Chapter 9: Configuration-files + Chapter 10: Configuration-files For configuration options (arch/xxx/Kconfig, and all the Kconfig files), somewhat different indentation is used.
@@ -313,7 +389,7 @@ support for file-systems, for instance) experimental options should be denoted (EXPERIMENTAL). - Chapter 10: Data structures + Chapter 11: Data structures Data structures that have visibility outside the single-threaded environment they are created and destroyed in should always have
@@ -344,7 +420,7 @@ Remember: if another thread can find you have a reference count on it, you almost certainly have a bug. - Chapter 11: Macros, Enums and RTL + Chapter 12: Macros, Enums and RTL Names of macros defining constants and labels in enums are capitalized.
@@ -399,7 +475,7 @@ The cpp manual deals with macros exhaust covers RTL which is used frequently with assembly language in the kernel. - Chapter 12: Printing kernel messages + Chapter 13: Printing kernel messages Kernel developers like to be seen as literate. Do mind the spelling of kernel messages to make a good impression. Do not use crippled
@@ -410,7 +486,7 @@ Kernel messages do not have to be termin Printing numbers in parentheses (%d) adds no value and should be avoided. - Chapter 13: Allocating memory + Chapter 14: Allocating memory The kernel provides the following general purpose memory allocators: kmalloc(), kzalloc(), kcalloc(), and vmalloc(). Please refer to the API
@@ -429,7 +505,7 @@ from void pointer to any other pointer t language. - Chapter 14: The inline disease + Chapter 15: The inline disease There appears to be a common misperception that gcc has a magic "make me faster" speedup option called "inline". While the use of inlines can be
@@ -457,7 +533,7 @@ something it would have done anyway. - Chapter 15: References + Appendix I: References The C Programming Language, Second Edition by Brian W. Kernighan and Dennis M. Ritchie.
@@ -481,4 +557,4 @@ Kernel CodingStyle, by [email protected] at http://www.kroah.com/linux/talks/ols_2002_kernel_codingstyle_talk/html/ -- -Last updated on 30 December 2005 by a community effort on LKML. +Last updated on 30 April 2006.
Documentation/CodingStyle problem, which is called the function-gr · line 210
Quoted 4 lines · Randy.Dunlap
Documentation/CodingStyle +4
-0+ + Although it would only take a short amount of time for the eyes and + brain to become accustomed to the standard types like 'uint32_t', + some people object to their use anyway.
The problem with uint32_t is that it's ugly, it used to be unportable, and you can't use it in header files _anyway_.
In other words, there's no _point_ to the "standard type".
I really object to this whole thing. The fact is, "u8" and friends _are_ the standard types as far as the kernel is concerned. Claiming that they aren't is just silly.
It's the "uint32_t" kind of thing that isn't standard within the kernel. You can't use that thing in public header files anyway due to name scoping rules, so they have basically no redeeming features.
Linus
Quoted 2 lines · Linus Torvalds
The problem with
uint32_tis that it's ugly, it used to be unportable, and you can't use it in header files _anyway_.
Unportable? It's at least as portable as u32 is, surely? We probably wouldn't have used <stdint.h> in the kernel anyway -- we define them ourselves.
The header files are completely irrelevant too -- we're talking about 'u32' not '__u32'.
The important thing is your belief that it's ugly, which is what was documented.
Quoted 3 lines
I really object to this whole thing. The fact is, "u8" and friends _are_ the standard types as far as the kernel is concerned. Claiming that they aren't is just silly.
When describing the CodingStyle rules "thou shalt not use typedefs" we do need to list the exceptions, and that includes 'u32' et al.
Yes, those _are_ acceptable in the kernel. That's what the document _says_. It _doesn't_ say that they're not.
Signature · 1 line
dwmw2
Quoted 8 lines · David Woodhouse
Quoted 2 lines · Linus Torvalds
The problem with
uint32_tis that it's ugly, it used to be unportable, and you can't use it in header files _anyway_.Unportable? It's at least as portable as u32 is, surely? We probably wouldn't have used <
stdint.h> in the kernel anyway -- we define them ourselves.
When the u<n> things were done, uint<n>_t wasn't at all common.
Quoted 2 lines
The header files are completely irrelevant too -- we're talking about 'u32' not '
__u32'.
That's not irrelevant. Usually you want to have stuff in header files that you use in source code. You want the two to visually look similar. It's a hell of a lot less confusing to use "u32" (in source) and "__u32" (in the header file), than it is to mix "uint32_t" (in source) and some random other thing (in header file).
Quoted 2 lines
The important thing is your belief that it's ugly, which is what was documented.
And that wasn't what I objected to.
What I objected to was that other part, which said that "uint32_t" was somehow more standard.
IN THE KERNEL IT IS _LESS_ STANDARD.
And outside the kernel, that documentation is not exactly relevant.
Linus
Quoted 12 lines · Linus Torvalds
Quoted 7 lines · David Woodhouse
Quoted 1 line · Linus Torvalds
The problem with
uint32_tis that it's ugly, it used to be unportable, and you can't use it in header files _anyway_.Unportable? It's at least as portable as u32 is, surely? We probably wouldn't have used <
stdint.h> in the kernel anyway -- we define them ourselves.
Quoted 8 lines
When the u<n> things were done, uint<n>_t wasn't at all common.
Quoted 2 lines
The header files are completely irrelevant too -- we're talking about 'u32' not '
__u32'.That's not irrelevant. Usually you want to have stuff in header files that you use in source code. You want the two to visually look similar. It's a hell of a lot less confusing to use "u32" (in source) and "
__u32" (in the header file), than it is to mix "uint32_t" (in source) and some random other thing (in header file).
isn't it possible to mix up u32 and some random other thing?
Quoted 5 lines
Quoted 3 lines
The important thing is your belief that it's ugly, which is what was documented.
Quoted 7 lines
And that wasn't what I objected to.
What I objected to was that other part, which said that "
uint32_t" was somehow more standard.IN THE KERNEL IT IS _LESS_ STANDARD.
And outside the kernel, that documentation is not exactly relevant.
nack. something is a standard or something is not. black or white. grey isn't there. of course, you are free to create your own kernel standard or whatever. what about __uint32_t? *scnr*
marcel
Quoted 6 lines
Linus
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Quoted 3 lines · Marcel Siegert
nack. something is a standard or something is not. black or white. grey isn't there.
Here's a saying for you:
"Only stupid people are black or white. The real world isn't".
Anyway, look at the particular document we're talking about. We're talking about the kernel standard coding-style.
Within that one, 8-char tabs are standard. Within that one, "u8" is standard. Within that one, typedefs are frowned upon.
In other contexts, other things are standard. Live with it.
Linus
Quoted 4 lines · Linus Torvalds
And that wasn't what I objected to.
What I objected to was that other part, which said that "
uint32_t" was somehow more standard.
It didn't say "more standard". It referred to "the standard C99 types".
It's heading off the question "why object to ifdefs but permit _these_ gratuitous ones?" which would otherwise be asked.
It's a document which is _describing_ the Linux coding style. To refer to u32 et al as 'standard' would be self-referential. Describe them as 'the Linux standard types' in other documents by all means, but it doesn't make much sense to do so in Documentation/CodingStyle.
Signature · 1 line
dwmw2
Quoted 8 lines · On Wed, 03 May 2006 00:22:16 +0100 David Woodhouse wrote:
Quoted 4 lines · Linus Torvalds
And that wasn't what I objected to.
What I objected to was that other part, which said that "
uint32_t" was somehow more standard.It didn't say "more standard". It referred to "the standard C99 types".
Quoted 7 lines
It's heading off the question "why object to ifdefs but permit _these_ gratuitous ones?" which would otherwise be asked.
It's a document which is _describing_ the Linux coding style. To refer to u32 et al as 'standard' would be self-referential. Describe them as 'the Linux standard types' in other documents by all means, but it doesn't make much sense to do so in
Documentation/CodingStyle.
All references to "standard types" now say "standard C99 types". However, Linus still objects to the C99 integer typedefs AFAICT. Are we at an impasse?
It would be a really Good Idea to have something about typedefs in Doc/CodingStyle.
---
From: Randy Dunlap <[email protected]>
Add a chapter on typedefs, based on an email from Linus to lkml on Feb. 3, 2006:
(Subject: Re: [RFC][PATCH 1/5] Virtualization/containers: startup) with added lkml feedback, esp. David Woodhouse.
--- Documentation/CodingStyle | 100 ++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 88 insertions(+), 12 deletions(-)
Documentation/CodingStyle +88 -12
@@ -155,7 +155,83 @@ problem, which is called the function-gr See next chapter. - Chapter 5: Functions + Chapter 5: Typedefs + +Please don't use things like "vps_t". + +It's a _mistake_ to use typedef for structures and pointers. When you see a + + vps_t a; + +in the source, what does it mean? + +In contrast, if it says + + struct virtual_container *a; + +you can actually tell what "a" is. + +Lots of people think that typedefs "help readability". Not so. They are +useful only for: + + (a) totally opaque objects (where the typedef is actively used to _hide_ + what the object is). + + Example: "pte_t" etc. opaque objects that you can only access using + the proper accessor functions. + + NOTE! Opaqueness and "accessor functions" are not good in themselves. + The reason we have them for things like pte_t etc. is that there + really is absolutely _zero_ portably accessible information there. + + (b) Clear integer types, where the abstraction _helps_ avoid confusion + whether it is "int" or "long". + + u8/u16/u32 are perfectly fine typedefs, although they fit into + category (d) better than here. + + NOTE! Again - there needs to be a _reason_ for this. If something is + "unsigned long", then there's no reason to do + + typedef unsigned long myflags_t; + + but if there is a clear reason for why it under certain circumstances + might be an "unsigned int" and under other configurations might be + "unsigned long", then by all means go ahead and use a typedef. + + (c) when you use sparse to literally create a _new_ type for + type-checking. + + (d) New types which are identical to standard C99 types, in certain + exceptional circumstances. + + Although it would only take a short amount of time for the eyes and + brain to become accustomed to the standard C99 types like 'uint32_t', + some people object to their use anyway. + + Therefore, the Linux-specific 'u8/u16/u32/u64' types and their + signed equivalents which are identical to standard C99 types are + permitted -- although they are not mandatory in new code of your + own. + + When editing existing code which already uses one or the other set + of types, you should conform to the existing choices in that code. + + (e) Types safe for use in userspace. + + In certain structures which are visible to userspace, we cannot + require C99 types and cannot use the 'u32' form above. Thus, we + use __u32 and similar types in all structures which are shared + with userspace. + +Maybe there are other cases too, but the rule should basically be to NEVER +EVER use a typedef unless you can clearly match one of those rules. + +In general, a pointer, or a struct that has elements that can reasonably +be directly accessed should _never_ be a typedef. + + + Chapter 6: Functions Functions should be short and sweet, and do just one thing. They should fit on one or two screenfuls of text (the ISO/ANSI screen size is 80x24,
@@ -183,7 +259,7 @@ and it gets confused. You know you're b to understand what you did 2 weeks from now. - Chapter 6: Centralized exiting of functions + Chapter 7: Centralized exiting of functions Albeit deprecated by some people, the equivalent of the goto statement is used frequently by compilers in form of the unconditional jump instruction.
@@ -220,7 +296,7 @@ out: return result; } - Chapter 7: Commenting + Chapter 8: Commenting Comments are good, but there is also a danger of over-commenting. NEVER try to explain HOW your code works in a comment: it's much better to
@@ -240,7 +316,7 @@ When commenting the kernel API functions See the files Documentation/kernel-doc-nano-HOWTO.txt and scripts/kernel-doc for details. - Chapter 8: You've made a mess of it + Chapter 9: You've made a mess of it That's OK, we all do. You've probably been told by your long-time Unix user helper that "GNU emacs" automatically formats the C sources for
@@ -288,7 +364,7 @@ re-formatting you may want to take a loo remember: "indent" is not a fix for bad programming. - Chapter 9: Configuration-files + Chapter 10: Configuration-files For configuration options (arch/xxx/Kconfig, and all the Kconfig files), somewhat different indentation is used.
@@ -313,7 +389,7 @@ support for file-systems, for instance) experimental options should be denoted (EXPERIMENTAL). - Chapter 10: Data structures + Chapter 11: Data structures Data structures that have visibility outside the single-threaded environment they are created and destroyed in should always have
@@ -344,7 +420,7 @@ Remember: if another thread can find you have a reference count on it, you almost certainly have a bug. - Chapter 11: Macros, Enums and RTL + Chapter 12: Macros, Enums and RTL Names of macros defining constants and labels in enums are capitalized.
@@ -399,7 +475,7 @@ The cpp manual deals with macros exhaust covers RTL which is used frequently with assembly language in the kernel. - Chapter 12: Printing kernel messages + Chapter 13: Printing kernel messages Kernel developers like to be seen as literate. Do mind the spelling of kernel messages to make a good impression. Do not use crippled
@@ -410,7 +486,7 @@ Kernel messages do not have to be termin Printing numbers in parentheses (%d) adds no value and should be avoided. - Chapter 13: Allocating memory + Chapter 14: Allocating memory The kernel provides the following general purpose memory allocators: kmalloc(), kzalloc(), kcalloc(), and vmalloc(). Please refer to the API
@@ -429,7 +505,7 @@ from void pointer to any other pointer t language. - Chapter 14: The inline disease + Chapter 15: The inline disease There appears to be a common misperception that gcc has a magic "make me faster" speedup option called "inline". While the use of inlines can be
@@ -457,7 +533,7 @@ something it would have done anyway. - Chapter 15: References + Appendix I: References The C Programming Language, Second Edition by Brian W. Kernighan and Dennis M. Ritchie.
@@ -481,4 +557,4 @@ Kernel CodingStyle, by [email protected] at http://www.kroah.com/linux/talks/ols_2002_kernel_codingstyle_talk/html/ -- -Last updated on 30 December 2005 by a community effort on LKML. +Last updated on 30 April 2006.
Quoted 5 lines · Randy.Dunlap
All references to "standard types" now say "standard C99 types". However, Linus still objects to the C99 integer typedefs AFAICT. Are we at an impasse?
It would be a really Good Idea to have something about typedefs in Doc/CodingStyle.
Absolutely, and you need to document the exceptions. That includes saying that 'u32' et al are OK.
Signature · 1 line
dwmw2
Quoted 9 lines · On Wed, 03 May 2006 22:52:44 +0100 David Woodhouse wrote:
Quoted 5 lines · Randy.Dunlap
All references to "standard types" now say "standard C99 types". However, Linus still objects to the C99 integer typedefs AFAICT. Are we at an impasse?
It would be a really Good Idea to have something about typedefs in Doc/CodingStyle.Absolutely, and you need to document the exceptions. That includes saying that 'u32' et al are OK.
Uh, I think that it currently says that...
---
~Randy
Quoted 1 line · Randy.Dunlap
Uh, I think that it currently says that...
Yes, it does. So that should be fine then...
Signature · 1 line
dwmw2
Quoted 6 lines · Randy.Dunlap
+ (b) Clear integer types, where the abstraction _helps_ avoid confusion
Documentation/CodingStyle +5
-0+ whether it is "int" or "long". + + u8/u16/u32 are perfectly fine typedefs. + + NOTE! Again - there needs to be a _reason_ for this. If something is
Documentation/CodingStyle problem, which is called the function-gr · line 202
Quoted 10 lines
Documentation/CodingStyle +10
-0+ "unsigned long", then there's no reason to do + + typedef long myflags_t; + + but if there is a clear reason for why it under certain circumstances + might be an "unsigned int" and under other configurations might be + "unsigned long", then by all means go ahead and use a typedef. + + (c) when you use sparse to literally create a _new_ type for + type-checking.
Is there an official position on "typedef unsigned __bitwise__ myflags_t"? That is, getting into case (c) with something not particularly useful for typechecking, just because you want to use a typedef.
I don't remember if the gfp_t thing was only done because it would catch a very common type of bug, or if it's generally suggested for new code.
-Daniel
*This .sig left intentionally blank*