list.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #ifndef _LIST_HEAD_H
  2. #define _LIST_HEAD_H
  3. // 双向链表节点
  4. struct list_head {
  5. struct list_head *next, *prev;
  6. };
  7. // 初始化节点:设置name节点的前继节点和后继节点都是指向name本身。
  8. #define LIST_HEAD_INIT(name) { &(name), &(name) }
  9. // 定义表头(节点):新建双向链表表头name,并设置name的前继节点和后继节点都是指向name本身。
  10. #define LIST_HEAD(name) \
  11. struct list_head name = LIST_HEAD_INIT(name)
  12. // 初始化节点:将list节点的前继节点和后继节点都是指向list本身。
  13. static inline void INIT_LIST_HEAD(struct list_head *list)
  14. {
  15. list->next = list;
  16. list->prev = list;
  17. }
  18. // 添加节点:将new插入到prev和next之间。
  19. static inline void __list_add(struct list_head *new,
  20. struct list_head *prev,
  21. struct list_head *next)
  22. {
  23. next->prev = new;
  24. new->next = next;
  25. new->prev = prev;
  26. prev->next = new;
  27. }
  28. // 添加new节点:将new添加到head之后,是new称为head的后继节点。
  29. static inline void list_add(struct list_head *new, struct list_head *head)
  30. {
  31. __list_add(new, head, head->next);
  32. }
  33. // 添加new节点:将new添加到head之前,即将new添加到双链表的末尾。
  34. static inline void list_add_tail(struct list_head *new, struct list_head *head)
  35. {
  36. __list_add(new, head->prev, head);
  37. }
  38. // 从双链表中删除entry节点。
  39. static inline void __list_del(struct list_head * prev, struct list_head * next)
  40. {
  41. next->prev = prev;
  42. prev->next = next;
  43. }
  44. // 从双链表中删除entry节点。
  45. static inline void list_del(struct list_head *entry)
  46. {
  47. __list_del(entry->prev, entry->next);
  48. }
  49. // 从双链表中删除entry节点。
  50. static inline void __list_del_entry(struct list_head *entry)
  51. {
  52. __list_del(entry->prev, entry->next);
  53. }
  54. // 从双链表中删除entry节点,并将entry节点的前继节点和后继节点都指向entry本身。
  55. static inline void list_del_init(struct list_head *entry)
  56. {
  57. __list_del_entry(entry);
  58. INIT_LIST_HEAD(entry);
  59. }
  60. // 用new节点取代old节点
  61. static inline void list_replace(struct list_head *old,
  62. struct list_head *new)
  63. {
  64. new->next = old->next;
  65. new->next->prev = new;
  66. new->prev = old->prev;
  67. new->prev->next = new;
  68. }
  69. // 双链表是否为空
  70. static inline int list_empty(const struct list_head *head)
  71. {
  72. return head->next == head;
  73. }
  74. // 获取"MEMBER成员"在"结构体TYPE"中的位置偏移
  75. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  76. // 根据"结构体(type)变量"中的"域成员变量(member)的指针(ptr)"来获取指向整个结构体变量的指针
  77. #define container_of(ptr, type, member) ({ \
  78. const typeof( ((type *)0)->member ) *__mptr = (ptr); \
  79. (type *)( (char *)__mptr - offsetof(type,member) );})
  80. // 遍历双向链表
  81. #define list_for_each(pos, head) \
  82. for (pos = (head)->next; pos != (head); pos = pos->next)
  83. #define list_for_each_safe(pos, n, head) \
  84. for (pos = (head)->next, n = pos->next; pos != (head); \
  85. pos = n, n = pos->next)
  86. #define list_entry(ptr, type, member) \
  87. container_of(ptr, type, member)
  88. #endif