Why Typedef is Rarely Used in the Linux Kernel

Follow+Star Public Account Number, don’t miss out on exciting contentSource | Embedded Art

Have you ever read the source code of the Linux kernel? During the process of<span>Linux</span> driver development, have you encountered such a warning?

WARNING: do not add new typedefs

Using<span>typedef</span> is not allowed!

Although it’s just a warning, if you want to submit code to the open-source repository, this is a must-optimize item.

So, why does the<span>Linux</span> kernel discourage the use of<span>typedef</span>?

1. Linus Torvalds’ Attitude

Why Typedef is Rarely Used in the Linux Kernel

> On Mon, 10 Jun 2002, Linus Torvalds wrote:

>> –snip/snip

> > But in the end, maintainership matters. I personally don’t want the

> > typedef culture to get the upper hand, but I don’t mind a few of them, and

> > people who maintain their own code usually get the last word.

>> to sum it up:

>> using the “struct mystruct” is recommended, but not a must.

<span>Torvalds </span> himself does not want to see the<span>typedef</span> culture dominate, but those who maintain their own code usually have the final say.

  • <span>Torvalds </span> still recommends using the structure of<span>struct mystruct</span>.
  • Hard to Understand: Using<span>typedef</span> types makes it difficult to understand what the actual type of the variable is.
  • Poor Maintainability: Due to the vast architecture of the<span>Linux</span> kernel, the<span>typedef</span> types defined across different architectures may not be universally applicable.

<span>Torvalds </span> original text can be found at: https://lkml.indiana.edu/hypermail/linux/kernel/0206.1/0402.html

2. Kernel Coding Standards

Why Typedef is Rarely Used in the Linux Kernel

From the perspective of kernel coding standards, let’s look at<span>typedef</span>

The kernel coding standards provide some scenarios for using<span>typedef</span>:

  • Completely opaque objects: hiding internal objects
  • Explicit integer types: abstraction helps avoid confusion about whether it is an int or long type, such as<span>u8/u16/u32</span>
  • In certain special cases, new types that are the same as standard<span>C99</span> types.
  • Types that can be used in user space

For more on kernel coding standards, see: https://www.kernel.org/doc/html/v4.10/process/coding-style.html

3. Personal Opinion

Personally, I feel that from the perspective of maintaining large projects,<span>typedef</span> is not recommended to avoid type proliferation, which also makes it harder to understand.

For small personal projects,<span>typedef</span> can be used at one’s discretion, as<span>typedef</span> has mixed reviews.

Here are some community discussion threads:

  • Why we frequently use<span>typedef</span> in the<span>C</span> language: https://stackoverflow.com/questions/252780/why-should-we-typedef-a-struct-so-often-in-c
  • Why the<span>Linux</span> coding style guide discourages the use of<span>typedef</span>: https://www.reddit.com/r/C_Programming/comments/dan8vr/why_does_the_linux_kernel_coding_style_guide/?rdt=36702

———— END ————Why Typedef is Rarely Used in the Linux Kernel● Column “Embedded Tools”● Column “Embedded Development”● Column “Keil Tutorials”● Selected Tutorials from Embedded ColumnFollow the public accountReply “Add Group” to join the technical exchange group according to the rules, reply “1024” to see more content.Click “Read the original text” to see more shares.

Leave a Comment