'magic' value Device Driver

和自甴很熟 提交于 2019-12-11 19:32:51

问题


what is the 'magic' value in tty_driver struct

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

I don't understand why is it called 'magic'


回答1:


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

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.



来源:https://stackoverflow.com/questions/21296469/magic-value-device-driver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!