static char __setup_str_##unique_id[] __initdata __aligned(1) = str
来源:互联网 发布:品质软件管理系统 编辑:程序博客网 时间:2024/05/02 01:09
__setup这条宏在Linux Kernel中使用最多的地方就是定义处理Kernel的启动参数的函数及数据结构,宏定义如下:
#define __setup(str, fn) \
__setup_param(str, fn, fn, 0)
#define __setup_param(str, unique_id, fn, early) \
static char __setup_str_##unique_id[] __initdata __aligned(1) = str; \
static struct obs_kernel_param __setup_##unique_id \
__used __section(.init.setup) \
__attribute__((aligned((sizeof(long))))) \
= { __setup_str_##unique_id, fn, early }
使用Kernel中的例子分析一下这两条定义:
__setup("root=",root_dev_setup);
这条语句出现在init/do_mounts.c中,其作用是处理Kernel启动时的像root=/dev/mtdblock3之类的参数的。
分解一下这条语句,首先变为:
__setup_param("root=",root_dev_setup,root_dev_setup,0);
继续分解,将得到下面这段代吗:
static char __setup_str_root_dev_setup_id[] __initdata __aligned(1) = "root=";
static struct obs_kernel_param __setup_root_dev_setup_id
__used __section(.init.setup)
__attribute__((aligned((sizeof(long)))))
= { __setup_str_root_dev_setup_id, root_dev_setup, 0 };
这段代码定义了两个变量:字符数组变量__setup_str_root_dev_setup_id,其初始化内容为"root=",由于该变量用 __initdata修饰,它将被放入.init.data输入段;另一变量是结构变量__setup_root_dev_setup_id,其类型为 struct obs_kernel_param, 该变理被放入输入段.init.setup中。结构struct struct obs_kernel_param也在该文件中定义如下:
struct obs_kernel_param {
const char *str;
int (*setup_func)(char *);
int early;
};
变量__setup_root_dev_setup_id的三个成员分别被初始化为:
__setup_str_root_dev_setup_id --> 前面定义的字符数组变量,初始内容为"root="。
root_dev_setup --> 通过宏传过来的处理函数。
0 -->常量0,该成员的作用以后分析。
现在不难想像内核启动时怎么处理启动参数的了:通过__setup宏定义obs_kernel_param结构变量都被放入.init.setup段中,这样一来实际是使.init.setup段变成一张表,Kernel在处理每一个启动参数时,都会来查找这张表,与每一个数据项中的成员str进行比较,如果完全相同,就会调用该数据项的函数指针成员setup_func所指向的函数(该函数是在使用__setup宏定义该变量时传入的函数参数),并将启动参数如root=后面的内容传给该处理函数。
- static char __setup_str_##unique_id[] __initdata __aligned(1) = str
- 对 static const char* const str[] = {“string”} 的理解
- char * str="hello"
- char str[]="hello"; sizeof(str)是多少?
- C++,为什么str=new char[strlen(s)+1];中要加一
- 在函数体内声明[1] char *str=”abc”;和[2] char str[]={‘a’,’b’,’c’}有什么区别
- char *str 和 char str[]
- char*str与char str[]
- char *Str;Str="abcdef";和char Str[]="abcdef";有什么区别??
- char *strdup(char *str)
- 字符串赋值问题 char *str="abc4Efg";
- char str[], const char str[], const char *str区别
- const char *str, char *str, char str[]讨论
- [C++ VC MFC] char* str="heelo"与char str[]="heelo"区别
- char str[]="hello" 与 char *str="hello" 不是等价的
- Difference between char *str=“STRING” and char str[] = “STRING”
- char str[] = "hello world";和char *str = "hello world";区别
- char str[] = "hello world";和char *str = "hello world";区别
- 【机房合作】——数风流人物,还看今朝
- Nginx系列(二十.)隐藏nginx版本号
- 天声人語 20150531
- 【HTTP】http 401Basic验证和WWW-Authenticate、Authorization
- oracle学习1-安装
- static char __setup_str_##unique_id[] __initdata __aligned(1) = str
- oracle调用存储过程和函数返回结果集
- 面试题21:包含min函数的栈
- 日经春秋 20150531
- 机房重构之七层登录
- Java基础知识之IO(2)
- javascript处理HTML的Encode(转码)和Decode(解码)总结
- 日经社説 20150531 各国で鉄鋼過剰への対応を
- Redhat6.2升级为6.3之后再降级为Redhat6.2 (linux内核降级)