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

 

输出结果:

1 $ gcc -o list list.c
2 $ ./list 
3 the device list is empty!
4 the device list is not empty!
5 the device name is device1
6 the device name is device2

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。