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分钟入门教程  

     
     
     
  • 相关阅读:
    DeepFM
    Wide & Deep Learning Model
    机器学习优化方法总结比较(SGD,Adagrad,Adadelta,Adam,Adamax,Nadam)
    svm、logistic regression对比
    梯度下降和EM算法,kmeans的em推导
    牛顿法、拟牛顿法以及与梯度下降法的对比
    【effective c++】构造/析构/赋值运算
    Tips/Tricks in Deep Neural Networks
    GoogLeNet系列解读
    电脑组装的配件及其功能
  • 原文地址:https://www.cnblogs.com/shaohef/p/3930380.html
Copyright © 2011-2022 走看看