Linux内核源码分析(3)——compiler-gcc[x].h分析

    这部分简要介绍一下不同版本gcc编译器特有的头文件。这里只介绍gcc3和gcc4的头文件。

1、GCC3


#if __GNUC_MINOR__ < 2
# error Sorry, your compiler is too old - please upgrade it.
#endif

#if __GNUC_MINOR__ >= 3
# define __used   __attribute__((__used__))
#else
# define __used   __attribute__((__unused__))
#endif

#if __GNUC_MINOR__ >= 4
#define __must_check  __attribute__((warn_unused_result))
#endif

/*
 * A trick to suppress uninitialized variable warning without generating any
 * code
 */
#define uninitialized_var(x) x = x

#define __always_inline  inline __attribute__((always_inline))


    第一个部分说明该linux源码只支持采用GCC3.2以上的编译器。

    当版本大于3.3时,__used属性定义为__used__,反之定义为__unused__。

    当版本大于3.4使,定义属性__must_check为warn_unused_result,对不使用返回值的情况进行编译告警。

    uninitialized_var(x) 是一个简易宏,用于去除编译器对为初始化变量的警告。

    最后还定义__always_inline属性的宏,这个宏的作用在之前已经介绍过了。

 

2、GCC4


/* GCC 4.1.[01] miscompiles __weak */
#ifdef __KERNEL__
# if __GNUC_MINOR__ == 1 && __GNUC_PATCHLEVEL__ <= 1
#  error Your version of gcc miscompiles the __weak directive
# endif
#endif

#define __used   __attribute__((__used__))
#define __must_check   __attribute__((warn_unused_result))
#define __compiler_offsetof(a,b) __builtin_offsetof(a,b)
#define __always_inline  inline __attribute__((always_inline))

/*
 * A trick to suppress uninitialized variable warning without generating any
 * code
 */
#define uninitialized_var(x) x = x

#if __GNUC_MINOR__ >= 3
/* Mark functions as cold. gcc will assume any path leading to a call
   to them will be unlikely.  This means a lot of manual unlikely()s
   are unnecessary now for any paths leading to the usual suspects
   like BUG(), printk(), panic() etc. [but let's keep them for now for
   older compilers]

   Early snapshots of gcc 4.3 don't support this and we can't detect this
   in the preprocessor, but we can live with this because they're unreleased.
   Maketime probing would be overkill here.

   gcc also has a __attribute__((__hot__)) to move hot functions into
   a special section, but I don't see any sense in this right now in
   the kernel context */
#define __cold   __attribute__((__cold__))


    这个部分的内容之前都已经介绍过了,这里只对特有的部分进行说明。

    第一个内容是对使用GCC 4.1.[01]版本的编译器编译内核,产生一个无法汇编__weak指令的错误。提示用户不能采用这个版本的编译器。

    __builtin_offsetof(a,b)与offsetof的作用一样,即 ((size_t) & ((TYPE *)0)->MEMBER )。

    GCC4.3版本以上还定义了cold属性,这个属性的作用之前已经介绍了。

 


转载时请注明出处和作者联系方式

文章出处:http://www.mythfish.com/

作者联系方式:张天才 mythfish@gmail.com

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值