You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
494<br />
495 /*<br />
496 * List <strong>of</strong> hosts per template.<br />
497 *<br />
498 * This is only for use by scsi_module.c for legacy templates.<br />
499 * For these access to it is synchronized implicitly by<br />
106<br />
500 * module_init/module_exit.<br />
501 */<br />
502 struct list_head sht_legacy_list;<br />
503<br />
504 /*<br />
505 * Points to the transport data (if any) which is allocated<br />
506 * separately<br />
507 */<br />
508 void *shost_data;<br />
509 struct class_device transport_classdev;<br />
510<br />
511 /*<br />
512 * We should ensure that this is aligned, both for better performance<br />
513 * and also because some compilers (m68k) don't automatically force<br />
514 * alignment to a long boundary.<br />
515 */<br />
516 unsigned long hostdata[0] /* Used for storage <strong>of</strong> host specific stuff */<br />
517 __attribute__ ((aligned (size<strong>of</strong>(unsigned long))));<br />
518 };<br />
需要提一下最后这个元素,hostdata[0],相信您感兴趣的是__attribute__,这是gcc 的关键字.她的<br />
作用是,描述函数,变量,类型的属性,很显然谭浩强大哥的C 程序设计中是不会介绍这玩艺儿的,事实上大多<br />
数人也用不着知道她.不过在gcc 里面,她出现的很频繁,因为她有利于代码优化和代码检查,特别是当咱们<br />
编写的是一个要在各种硬件平台上运行的操作系统的时候,这些属性是相当的有必要的.通常<br />
__attribute__(单词attribute 前后各两个underscore,即下划线.)出现在定义一个变量/函数/类型的时<br />
候,她紧跟在变量/函数/类型定义的后面,此处咱们看到的__attribute__是紧跟在unsigned long<br />
hostdata[0]这样一个数组(汗,数组居然只有一个元素)后面,数组的每一个元素是一个unsigned long 的<br />
类型变量,__attribute__后面的括号里则表明了属性,gcc 一共支持十几种属性,其中aligned 是一种,此<br />
外<br />
还有一些常用的,比如packed,noreturn,而packed 其实咱们前面见过,只是没有提起.现在讲一下<br />
aligned 属性和packed 属性.这两个都是和字节对齐有关的属性,前些年像intel 和micros<strong>of</strong>t 这些外企<br />
笔<br />
试面试题目中常考察字节对齐的冬冬,所以相信很多人对字节对齐并不陌生.简而言之,字节对齐是这么一回<br />
事,变量存放在内存中本来是无所谓怎么放的,因为怎么放都能访问到,但是不同的硬件访问内存的方式不一<br />
样,只有把变量按特定的规则存放在内存中,存取效率才会高,否则不按规则的存放变量将有可能降低变量存<br />
取效率.很多情况下,人们并不关心字节对齐,因为通常这些事情由编译器来处理,编译器很冰雪,她知道针对<br />
什么平台该如何对齐.但有时候,咱们需要显式的去设定对齐方式,因为有时候咱们对编译器的所作所为并不<br />
满意,或者咱们自己觉得自己指定的方式会更好,比如此处,在定义hostdata 之前的几行注释,已经很清楚