• 练习一下linux中的list函数。


    所有的list函数见 include/linux/list.h

    自己从 include/linux/list.h 拷贝了一些函数到自己的list.c中, 然后练习了一下。

    没有别的目的,就是想熟练一下。毕竟linux内核代码中试用了大量的list函数。

    list的函数太方便使用了。

    文件:list.c

      1 #include <stdio.h>
      2 // #include <linux/list.h>
      3 
      4 struct list_head {
      5     struct list_head *next, *prev;
      6 };
      7 
      8 #define LIST_HEAD_INIT(name) { &(name), &(name) }
      9 #define LIST_HEAD(name) 
     10         struct list_head name = LIST_HEAD_INIT(name)
     11 
     12 static inline void INIT_LIST_HEAD(struct list_head *list)
     13 {
     14     list->next = list;
     15     list->prev = list;
     16 }
     17 
     18 static inline void __list_add(struct list_head *new,
     19          struct list_head *prev,
     20          struct list_head *next) //ËüÖ»Êǽ«newÌí¼Óµ½prevºÍnextÖ®¼ä
     21 {
     22     next->prev = new;
     23     new->next = next;
     24     new->prev = prev;
     25     prev->next = new;
     26 }
     27 
     28 static inline void list_add_tail(struct list_head *new, struct list_head *head)
     29 {
     30     __list_add(new, head->prev, head);
     31 }
     32 
     33 static inline int list_empty(const struct list_head *head)
     34 {
     35     return head->next == head;
     36 }
     37 
     38 
     39 #undef offsetof
     40 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
     41 
     42 /**
     43  *  * container_of - cast a member of a structure out to the containing structure
     44  *   * @ptr:        the pointer to the member.
     45  *    * @type:       the type of the container struct this is embedded in.
     46  *     * @member:     the name of the member within the struct.
     47  *      *
     48  *       */
     49 #define container_of(ptr, type, member) ({                      
     50         const typeof( ((type *)0)->member ) *__mptr = (ptr);    
     51         (type *)( (char *)__mptr - offsetof(type,member) );})
     52 
     53 
     54 #define list_entry(ptr, type, member) 
     55         container_of(ptr, type, member)
     56 
     57 
     58 /**
     59  *  * list_for_each_entry  -       iterate over list of given type
     60  *   * @pos:        the type * to use as a loop cursor.
     61  *    * @head:       the head for your list.
     62  *     * @member:     the name of the list_struct within the struct.
     63  *      */
     64 #define list_for_each_entry(pos, head, member)                          
     65         for (pos = list_entry((head)->next, typeof(*pos), member);      
     66              &pos->member != (head);    
     67              pos = list_entry(pos->member.next, typeof(*pos), member))
     68 
     69 #define list_for_each(pos, head) 
     70         for (pos = (head)->next; pos != (head); pos = pos->next)
     71 
     72 
     73 struct devlist {
     74         struct list_head        list;
     75         char * name;
     76 };
     77 
     78 struct devlist dev0 = {
     79         .list = LIST_HEAD_INIT(dev0.list),
     80         .name = NULL,
     81 };
     82 
     83 /* static initiate head list*/
     84 // LIST_HEAD(hl);
     85 struct list_head hl = LIST_HEAD_INIT(hl);
     86 
     87 void main(void) {
     88     /* dynamic initiate head list. */
     89     INIT_LIST_HEAD(&hl);
     90     struct list_head *l;
     91     struct devlist * pdev = NULL; 
     92     int empty = list_empty(&hl);
     93     if(empty)
     94         printf("the device list is empty!
    "); 
     95     struct devlist dev1; 
     96     dev1.name = "device1";
     97     list_add_tail(&dev1.list, &hl);
     98     struct devlist edev1 = {{}, "entry device1"};
     99     list_add_tail(&edev1.list, &dev0.list);
    100     struct devlist dev2 = {{}, "device2"};
    101     list_add_tail(&dev2.list, &hl);
    102     struct devlist edev2 = {{}, "entry device2"};
    103     list_add_tail(&edev2.list, &dev0.list);
    104 
    105     empty = list_empty(&hl);
    106     if(!empty)
    107         printf("the device list is not empty!
    "); 
    108      
    109     list_for_each_entry(pdev, &dev0.list, list){
    110             printf("the device name is %s
    ", pdev->name);
    111     }
    112 
    113     list_for_each(l, &hl) {
    114     // for (l = (&hl)->next; l != (&hl); l = l->next){
    115     //         printf("l is: %p, head is: %p 
    ", l, &hl);
    116             pdev = list_entry(l, typeof(dev0), list);
    117             printf("the device name is %s
    ", pdev->name); 
    118     }
    119 }

    输出结果:

    1 $ ./list 
    2 the device list is empty!
    3 the device list is not empty!
    4 the device name is entry device1
    5 the device name is entry device2
    6 the device name is device1
    7 the device name is device2
  • 相关阅读:
    五月八日冲刺
    五月七号冲刺
    五月六日站立会议
    prufer序列学习笔记
    批量数据导入优化
    索引失效
    慢查询定位与分析
    redis主从同步
    redis RBD机制
    redis AOF机制
  • 原文地址:https://www.cnblogs.com/shaohef/p/3932079.html
Copyright © 2020-2023  润新知