zoukankan      html  css  js  c++  java
  • linux中的部分宏


    链表

    struct list_head{
        struct list_head *next,*prev;}
    

    链表声明

    #define LIST_HEAD_INIT(name) {&(name),&(name)}
    ================================================
    1.静态初始化
    #define LIST_HEAD(name)
        struct list_head name = LIST_HEAD_INIT(name)
    ================================================= 
    LIST_HEAD(list_name); 
    ==>
        struct list_head list_name = LIST_HEAD_INIT(name);
    ==>
        struct list_head list_name = {&(list_name), &(list_name)};
        //相当于给一个结构声明加初始化,让list_name的成员变量
        //【next,prev】 = {&(list_name), &(list_name)}
    
    2.运行时初始化
    stattic inline void INIT_LIST_HEAD(struct list_head *list)
    {
        list->next = list;
        list->prev = list;
    }
    

    链表是否为空

    static inline int list_empty(const struct list_head* head)
    {
        return head->next == head;
    }
    

    链表插入

    1.插入head后
    static inline void list_add(struct list_head* new, struct list_head* head)
    {
        __list_add(new, head, head->next);
    }
    2.插入head->prev后
    static inline void list_add_tail(struct list_head* new, struct list_head* head)
    {
        __list_add(new, head->prev, head);
    }
    

    删除

    static inline void list_replace_init(struct list_head *old, struct list_head *new)
    {
        list_replace(old,new);
        INIT_LIST_HEAD(old);
    }
    

    遍历

    #define list_entry(ptr, type, member) 
        container_of(ptr, type, member)
    
    #define container_of(ptr, type, member) 
    {
        const typeof( ((type *)0)->member ) *__mptr = (ptr);
        (type *)( (char *)__mptr - offsetof(type,member) ); 
    }
    
    #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
    

    注意:为什么能这么实现,跟C语言特性很有关系,在C中,给定的结构中,成员的偏移在编译时被固定下来了。

    遍历链表:
    宏:list_for_each()
    #define list_for_each(pos, head) 
    for (pos = (head)->next, prefetch(pos->next); 
            pos != (head); 
                pos = pos->next, prefetch(pos->next))
    
    pos是迭代指针,指向当前访问的链表头。
    head是要访问的链表的链表头。
    
    结合list_entry使用,就可以访问链表头所在元素了。
    list_for_each(p,&elem_list){
        f = list_entry(p, struct elem, list);
    }
    
    更简单使用:
    宏:list_for_each_entry(pos,head,member)
    
  • 相关阅读:
    Android 四大组件 (二) Service 使用
    使用fiddler抓手机端http数据包
    解决问题:保存图片到本地文件夹后,在图库里看不到保存的图片问题。
    Android 四大组件 (一) Activity 生命周期
    第二次裸辞_潜伏期_一些感想
    最近的一些感想(关于移动客户端开发android,ios)
    错误:类型 'System.Object' 未定义或者不能引入项目
    easyui换主题,并记录在cookie.以及cookie作用域介绍
    VS发布报错 "未能将文件……复制到……"
    VS2013修改模板,增加类文件的头注释
  • 原文地址:https://www.cnblogs.com/jsgnadsj/p/5173562.html
Copyright © 2011-2022 走看看