linux-kernel - 为什么结构化类型通过结构标记而不是类型名来引用?

标签 linux-kernel

在Linux内核中,结构化类型的定义如下:

typedef struct _TAG_ { ... };

然后在这样的例程中使用:

struct _TAG_ structured_entity;   
struct _TAG_ *pointer_to_structured_entity;
void function(struct _TAG_ *arg, ...);

为什么不这样:

typedef struct _TAG_ { ... } _typename_; 

然后:

_typename_ structured_entity;   
_typename_ *pointer_to_structured_entity;
void function(_typename_ *arg, ...);

这样做的技术必要性是什么?或者这只是传统/风格/魔法?

最佳答案

Linux kernel coding style - 在 kernel.org 和内核附带的文档目录中 - 不鼓励对结构使用 typedef:

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.

该文档继续列出了作者认为 typedef 有用的情况 - 例如只能使用访问器函数访问的不透明对象 - 最后是:

In general, a pointer, or a struct that has elements that can reasonably
be directly accessed should _never_ be a typedef.

关于linux-kernel - 为什么结构化类型通过结构标记而不是类型名来引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27437861/

相关文章:

c - 按下键盘按钮时,在哪里可以找到 linux 内核调用的 ISR?

linux - 如果禁用 IOMMU(intel VT-D),PCI 设备能否直接寻址 CPU PA

io - 如何判断给定进程是否使用 O_DIRECT 打开文件?

assembly - Linux x86 64 中 MSR_GS_BASE 的详细信息

linux-kernel - strcmp 内核模块内部崩溃

linux - 内核空间有libc吗?

linux - Linux 中的 Numa 平衡器

更改内核函数指针的地址

linux - 编译内核出错

linux - 关闭内核中的功能会导致内核模块(使用功能)行为不端吗?