码迷,mamicode.com
首页 > 编程语言 > 详细

内核代码中一些c语言用法

时间:2020-06-25 09:56:37      阅读:62      评论:0      收藏:0      [点我收藏+]

标签:compiler   执行   virt   ptr   翻译   comm   drive   语言   csdn   

container_of,(C 语言 up cast) 

内核中的定义:

Cscope tag: container_of
   #   line  filename / context / line
   1     27  drivers/gpu/drm/radeon/mkregtable.c <<container_of>>
             #define container_of(ptr, type, member) ({ \
   2     63  drivers/staging/lustre/include/linux/libcfs/libcfs.h <<container_of>>
             #define container_of(ptr, type, member) \
   3     78  drivers/staging/rtl8192e/rtllib.h <<container_of>>
             #define container_of(ptr, type, member) ({ \
   4     61  drivers/staging/rtl8192u/ieee80211/ieee80211.h <<container_of>>
             #define container_of(ptr, type, member) ({ \
   5    791  include/linux/kernel.h <<container_of>>
             #define container_of(ptr, type, member) ({ \
   6     18  scripts/kconfig/list.h <<container_of>>
             #define container_of(ptr, type, member) ({ \
   7     26  tools/perf/util/include/linux/kernel.h <<container_of>>
             #define container_of(ptr, type, member) ({ \
   8     84  tools/virtio/linux/kernel.h <<container_of>>
             #define container_of(ptr, type, member) ({ \

scripts/kconfig/list.h

 1 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
 2 
 3 /**
 4  * container_of - cast a member of a structure out to the containing structure
 5  * @ptr:        the pointer to the member.
 6  * @type:       the type of the container struct this is embedded in.
 7  * @member:     the name of the member within the struct.
 8  *
 9  */
10 #define container_of(ptr, type, member) ({                      11         const typeof( ((type *)0)->member ) *__mptr = (ptr);    12         (type *)( (char *)__mptr - offsetof(type,member) );})

 

分析详见:

http://www.cnblogs.com/cute/archive/2011/04/01/2002474.html

结构体和数据 

谈结构体struct 初始化多出的点号“.”,数组[]初始化多出的逗号“,”

cat test.c

内容如下:

 1 #include <stdio.h>
 2 
 3 #define MAXTITL 30
 4 #define MAXAUTL 20
 5 
 6 struct book {
 7   float off;
 8   char title[MAXTITL];
 9   char author[MAXAUTL];
10   float value;
11 
12 };
13 
14 int main()
15 {
16     puts("C lange test!");
17     struct book b1 = { .value =  19.9, .author = "author", .title="C test tutorial" };
18     printf("Book value: %f\n", b1.value);
19     printf("Book author: %s\n", b1.author);
20     printf("Book title: %s\n", b1.title);
21     printf("Book off: %f\n", b1.off);
22     int m,n;
23     int a2[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
24     for(n=0;n<11;n++)
25         printf("%d\n",a2[n]);
26     int a1[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0, };
27     for(m=0;m<11;m++)
28         printf("%d\n",a1[m]);
29     return 0;
30 }

 

执行:

gcc test.c -o test
./test

  

 

 

 

REF:

gcc 编译  

GCC 参数详解 

[翻译]15个最常用的GCC编译器参数  

GCC编译优化指南  

GCC编译C语言程序完整演示   

GCC编译器30分钟入门教程  

 
 

 

 

内核代码中一些c语言用法

标签:compiler   执行   virt   ptr   翻译   comm   drive   语言   csdn   

原文地址:https://www.cnblogs.com/shaohef/p/3930380.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!