zoukankan      html  css  js  c++  java
  • 内核代码中一些c语言用法

    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
    ", b1.value);
    19     printf("Book author: %s
    ", b1.author);
    20     printf("Book title: %s
    ", b1.title);
    21     printf("Book off: %f
    ", 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
    ",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
    ",a1[m]);
    29     return 0;
    30 }

    执行:

    gcc test.c -o test
    ./test
    

      

     

    结构体的空数组

    example 1, 求大小

     1 #include <string.h>
     2 #include <stdio.h>
     3 
     4 struct tag1
     5 {
     6       int a;
     7       int b;
     8 }__attribute ((packed));   //size = 8
     9 
    10 struct tag2
    11 {
    12       int a;
    13       int b;
    14       char *c;
    15 }__attribute ((packed));  //size = 12 on 32bit platform, 16 size on 64bit
    16 
    17 struct tag3
    18 {
    19     int a;
    20     int b;
    21     char c[0];
    22 }__attribute ((packed));  //size = 8
    23 
    24 struct tag4
    25 {
    26     int a;
    27     int b;
    28     char c[1];
    29 }__attribute ((packed));  //size = 9
    30 
    31 int main()
    32 {
    33       struct tag2 l_tag2;
    34       struct tag3 l_tag3;
    35       struct tag4 l_tag4;
    36 
    37       memset(&l_tag2,0,sizeof(struct tag2));
    38       memset(&l_tag3,0,sizeof(struct tag3));
    39       memset(&l_tag4,0,sizeof(struct tag4));
    40 
    41       printf("size of tag1 = %ld
    ",sizeof(struct tag1));
    42       printf("size of tag2 = %ld
    ",sizeof(struct tag2));
    43       printf("size of tag3 = %ld
    ",sizeof(struct tag3));
    44       printf("size of tag4 = %ld
    ",sizeof(struct tag4));
    45 
    46       printf("l_tag2 = %p,&l_tag2.c = %p,l_tag2.c = %p
    ",&l_tag2,&l_tag2.c,l_tag2.c);
    47       printf("l_tag3 = %p,l_tag3.c = %p
    ",&l_tag3,l_tag3.c);
    48       printf("l_tag4 = %p,l_tag4.c = %p
    ",&l_tag4,l_tag4.c);
    49       return 0;
    50 }

    example 2,柔性数组,动态内存

    C语言0长度数组使用技巧, 又解决了小内存碎片问题提高了性能。

    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    
    typedef struct{
        int stuID;
        int age;
        char address[];  // same with address[0]
    }ST_STU_INFO,*pStuInfo; // size is 8
    
    pStuInfo ComposeStuInfo( int stuID,int age, const char *paddress)
    {
        pStuInfo ptmpInfo = malloc(sizeof(*ptmpInfo) + sizeof(char) * strlen(paddress) + 1);
        if(ptmpInfo != NULL){
            ptmpInfo->stuID = stuID;
            ptmpInfo->age = age;
            strcpy(ptmpInfo->address, paddress);
        }
        return ptmpInfo;
    }
    
    void printStuInfo(pStuInfo ptmpInfo)
    {
        printf("stuID : %d, age : %d, 
    Address: %s
    "
               "Size of Struct:%ld
    
    ",
               ptmpInfo->stuID, ptmpInfo->age,
               ptmpInfo->address, sizeof(*ptmpInfo));
    }
    
    int main()
    {
        pStuInfo CodeLab = ComposeStuInfo(
            100013, 20, "Tencent Building, Central District, High-tech Park, "
            "Nanshan District, Shenzhen");
        if(CodeLab != NULL){
            printStuInfo(CodeLab);
            free(CodeLab);
        }
        pStuInfo subCodeLab = ComposeStuInfo(200013, 23, "Tencent Building");
        if(subCodeLab != NULL){
            printStuInfo(subCodeLab);
            free(subCodeLab);
        }
        return 0;
    }

    c语言中__attribute__的意义

    REF:

    gcc 编译  

    GCC 参数详解 

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

    GCC编译优化指南  

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

    GCC编译器30分钟入门教程  

     
     
     
  • 相关阅读:
    in_array()和explode()的使用笔记
    写sql语句连接的时候注意的一个小细节
    在thinkphp框架模板中引用session
    查询数据库所有(某个)表中字段名,数据类型,说明等,导出数据字典
    (委托事件处理)关于多线程执行显示进度条的实例(转)&&线程间操作无效: 从不是创建控件“rtxtEntryNO”的线程访问它。
    判断当前线程所处的状态 (转)以及终止当前线程
    string 字符串的分隔处理与list的相互转换
    C# 动态调用webservice
    C#中的List<string>泛型类示例
    命名空间"system.web"中不存在类型或命名空间名称security"
  • 原文地址:https://www.cnblogs.com/shaohef/p/3930380.html
Copyright © 2011-2022 走看看