问题描述
tty_driver结构
struct tty_driver {
int magic; /* magic number for this structure */
struct kref kref; /* Reference management */
struct cdev cdev;
struct module *owner;
const char *driver_name;
....
....
我不明白为什么叫'magic'
I don't understand why is it called 'magic'
推荐答案
Magic数字通常指标识结构,文件类型或软件的特定常量。在这种情况下,tty_driver的魔术数显然总是这样定义:
Magic numbers often refer to specific constants that identify a structure, file type or software. In this case, the tty_driver's magic number is apparently always defined like this:
#define TTY_DRIVER_MAGIC 0x5402
在这种情况下,幻数的一种实际用法可能是检查第一个 sizeof(( int)
个字节,并确保它们 == 0x5402
,然后将其余接收的字节转换为 tty_driver结构
。在这方面,它还可以用于确定其余标头的适当字节顺序(小/大字节序)。
One practical use of the magic number in such a context might be to check the value of the first sizeof(int)
bytes and make sure they == 0x5402
before casting the rest of the received bytes into a tty_driver struct
. In this respect, it might also be used to determine the appropriate byte ordering (little/big endian) for the rest of the header.
这篇关于“神奇”价值的设备驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!