内核compiler.h的学习

简介: 版权声明:您好,转载请留下本人博客的地址,谢谢 https://blog.csdn.net/hongbochen1223/article/details/44962633 直接上代码...
版权声明:您好,转载请留下本人博客的地址,谢谢 https://blog.csdn.net/hongbochen1223/article/details/44962633

直接上代码就可以了,所以的学习都在注释当中呢!

#ifndef __LINUX_COMPILER_H
#define __LINUX_COMPILER_H

#ifndef __ASSEMBLY__

//如果宏定义了__CHECKER__
//详细学习一个Sparse
#ifdef __CHECKER__

/**
 * 这个使用来修饰一个变量的,这个变量必须是非解除参考的,no 
 * dereference的,即这个变量必须是有效的,而且变量所在的
 * 地址空间必须是1,即用户程序所使用的。
 * 程序空间分成3部分,0表示normal space,即普通地址空间,
 * 对于内核代码讲,就是内核空间,1表示用户地址空间
 * 2表示设备地址映射空间,如硬件设备的寄存器在内核
 * 里所映射的地址空间
 *
 */
# define __user     __attribute__((noderef, address_space(1)))

//默认的是内核地址空间
# define __kernel   /* default address space */

//变量可以为空
# define __safe     __attribute__((safe))

//变量可以进行强制转换
# define __force    __attribute__((force))

//参数类型必须与实际参数类型一致
# define __nocast   __attribute__((nocast))

//指针地址在设备地址空间
# define __iomem    __attribute__((noderef, address_space(2)))

//参数x,在执行前引用计数必须为0,执行后,引用计数必须为1
# define __acquires(x)  __attribute__((context(0,1)))

//参数x,在执行前引用计数必须为1,执行后,必须为0
# define __releases(x)  __attribute__((context(1,0)))

//参数x的引用计数在执行后+1
# define __acquire(x)   __context__(1)

//参数x的引用计数在执行后-1
# define __release(x)   __context__(-1)

//参数x为0,则返回0;如果参数x不为0,的引用计数+1,并且返回1
# define __cond_lock(x) ((x) ? ({ __context__(1); 1; }) : 0)

//检查用户地址空间指针
extern void __chk_user_ptr(void __user *);

//检查设备地址空间指针
extern void __chk_io_ptr(void __iomem *);

#else

# define __user
# define __kernel
# define __safe
# define __force
# define __nocast
# define __iomem
# define __chk_user_ptr(x) (void)0
# define __chk_io_ptr(x) (void)0
# define __builtin_warning(x, y...) (1)
# define __acquires(x)
# define __releases(x)
# define __acquire(x) (void)0
# define __release(x) (void)0
# define __cond_lock(x) (x)
#endif

#ifdef __KERNEL__

//__GNUC__ 该宏表示GCC的版本
//可以使用该宏针对不同版本的gcc进行条件编译
#if __GNUC__ > 4
//错误,没有对应版本的compiler-gcc.h文件 
#error no compiler-gcc.h file for this gcc version
#elif __GNUC__ == 4
# include <linux/compiler-gcc4.h>
#elif __GNUC__ == 3
# include <linux/compiler-gcc3.h>
#else
//对不起,错误。你的编译器版本太老了或者是不被识别。
# error Sorry, your compiler is too old/not recognized.
#endif

/* Intel compiler defines __GNUC__. So weabove header files here will overwrite implementations
 * coming from above header files here
 * Intel编译器定义了__GNUC__.所以在这里也包含compiler-intel.h头文件。
 */
#ifdef __INTEL_COMPILER
# include <linux/compiler-intel.h>
#endif

/*
 * Generic compiler-dependent macros required for kernel
 * build go below this comment. Actual compiler/compiler version
 * specific implementations come from the above header files
 * 定义likely和unlikely宏
 *
 * 在内核编译的时候,所需要的宏定义在该注释下面。真正的编译器或者是
 * 编译器版本实现在上面的头文件中。
 */

/*
 * __builtin_expect()是有GCC或者是intel编译器提供的实现
 * 提供该实现的目的是为了优化代码,避免过度跳转带来的性能
 * 上的下降。
 * __builtin(!!(x),1)表示x的值为1的可能性比较大
 * __builtin(!!(x),0)表示x的值为0的可能性比较大
 * 
 * 例如:
 * int x;
 * if(unlikely(x))
 * {
 *   x += 1;
 * }else{
 *  x -= 1;
 * }
 * 这段代码表示,x为0的可能性较大,也就是说
 * 程序执行else中的代码的可能性比较大,所以在
 * 编译的时候else里面的内容紧跟着上面的代码
 */

#define likely(x)   __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)

//优化屏障
//详细了解请看我的博客[《内核Memory Barrier学习》](http://blog.csdn.net/hongbochen1223/article/details/44961487)
/* Optimization barrier */
#ifndef barrier
# define barrier() __memory_barrier()
#endif

#ifndef RELOC_HIDE
# define RELOC_HIDE(ptr, off)                   \
  ({ unsigned long __ptr;                   \
     __ptr = (unsigned long) (ptr);             \
    (typeof(ptr)) (__ptr + (off)); })
#endif

#endif /* __KERNEL__ */

#endif /* __ASSEMBLY__ */

#ifdef __KERNEL__
/*
 * Allow us to mark functions as 'deprecated' and have gcc emit a nice
 * warning for each use, in hopes of speeding the functions removal.
 * Usage is:
 *      int __deprecated foo(void)
 * 
 *
 * 允许我们标志一个函数为'过时的',并且会使gcc对没一个使用发出合理的警告,
 * 希望能够加快函数的移除。
 * 使用方式:
 *      int __deprecated foo(void)
 */
#ifndef __deprecated
# define __deprecated       /* unimplemented */ //未实现
#endif

#ifdef MODULE
#define __deprecated_for_modules __deprecated
#else
#define __deprecated_for_modules
#endif

#ifndef __must_check
#define __must_check
#endif

/*
 * Allow us to avoid 'defined but not used' warnings on functions and data,
 * as well as force them to be emitted to the assembly file.
 *
 * As of gcc 3.3, static functions that are not marked with attribute((used))
 * may be elided from the assembly file.  As of gcc 3.3, static data not so
 * marked will not be elided, but this may change in a future gcc version.
 *
 * In prior versions of gcc, such functions and data would be emitted, but
 * would be warned about except with attribute((unused)).
 *
 * 允许我们避免在函数和数据中‘定义了但是没有使用’的警告,同时强制他们发送给
 * 汇编文件。
 * 对于编译器gcc 3.3,未被标记上已用属性的静态函数将会被汇编文件忽略。但是对于
 * 编译器gcc 3.3,没有这样标记静态数据将不会被忽略,但是在后续的gcc版本中将会
 * 发生改变。
 * 在更高级的gcc版本中,这样的函数和数据虽然会被传送,但是将会用‘未使用的属性’
 * 来发出异常警告。
 */
#ifndef __attribute_used__
# define __attribute_used__ /* unimplemented */  //未实现
#endif

/*
 * From the GCC manual:
 * 来自GCC手册
 *
 * Many functions have no effects except the return value and their
 * return value depends only on the parameters and/or global
 * variables.  Such a function can be subject to common subexpression
 * elimination and loop optimization just as an arithmetic operator
 * would be.
 * [...]
 * 很多函数除了他们的返回值都是没有影响的,并且他们的返回值仅仅依赖于参数
 * 或者是全局变量。这样的函数将受公共子表达式消除和循环优化支配作为一个算数
 * 运算符。
 */
#ifndef __attribute_pure__
# define __attribute_pure__ /* unimplemented */
#endif

#ifndef noinline
#define noinline
#endif

#ifndef __always_inline
#define __always_inline inline
#endif

#endif /* __KERNEL__ */

/*
 * From the GCC manual:
 * 定义const函数
 * Many functions do not examine any values except their arguments,
 * and have no effects except the return value.  Basically this is
 * just slightly more strict class than the `pure' attribute above,
 * since function is not allowed to read global memory.
 *
 * Note that a function that has pointer arguments and examines the
 * data pointed to must _not_ be declared `const'.  Likewise, a
 * function that calls a non-`const' function usually must not be
 * `const'.  It does not make sense for a `const' function to return
 * `void'.
 */
#ifndef __attribute_const__
# define __attribute_const__    /* unimplemented */
#endif

#endif /* __LINUX_COMPILER_H */
目录
相关文章
|
7月前
|
Ubuntu Linux 开发工具
嵌入式Linux系列第4篇:Kernel编译下载
嵌入式Linux系列第4篇:Kernel编译下载
|
3月前
|
Linux 编译器
一起来认识Linux中的 BUILD_BUG_ON 宏
一起来认识Linux中的 BUILD_BUG_ON 宏
|
编解码 自然语言处理 JavaScript
编译程序(compiler)的简单分析
在现今前端项目中,模块化是一个避不开的话题。所以就会出现AMD,CMD等模块加载方式。同时由于JS不停的在更新迭代。出现很多实用的新语法。但是由于有些语法有些超前,JS的宿主环境(浏览器/Node没有跟上JS更新步骤),但是为了在项目中使用这些好用到令人发指的新特性,来提高开发效率等。就出现了各种前端编译插件(Babel)。
|
存储 编译器 Linux
MDK编译过程及ARM编译工具链
MDK编译过程及ARM编译工具链
336 0
MDK编译过程及ARM编译工具链
|
Linux 编译器
Linux下make -j加快编译速度
Linux下make -j加快编译速度
111 0