list.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * netlink/list.h Netlink List Utilities
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation version 2.1
  7. * of the License.
  8. *
  9. * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
  10. */
  11. #ifndef NETLINK_LIST_H_
  12. #define NETLINK_LIST_H_
  13. struct nl_list_head
  14. {
  15. struct nl_list_head * next;
  16. struct nl_list_head * prev;
  17. };
  18. static inline void NL_INIT_LIST_HEAD(struct nl_list_head *list)
  19. {
  20. list->next = list;
  21. list->prev = list;
  22. }
  23. static inline void __nl_list_add(struct nl_list_head *obj,
  24. struct nl_list_head *prev,
  25. struct nl_list_head *next)
  26. {
  27. prev->next = obj;
  28. obj->prev = prev;
  29. next->prev = obj;
  30. obj->next = next;
  31. }
  32. static inline void nl_list_add_tail(struct nl_list_head *obj,
  33. struct nl_list_head *head)
  34. {
  35. __nl_list_add(obj, head->prev, head);
  36. }
  37. static inline void nl_list_add_head(struct nl_list_head *obj,
  38. struct nl_list_head *head)
  39. {
  40. __nl_list_add(obj, head, head->next);
  41. }
  42. static inline void nl_list_del(struct nl_list_head *obj)
  43. {
  44. obj->next->prev = obj->prev;
  45. obj->prev->next = obj->next;
  46. }
  47. static inline int nl_list_empty(struct nl_list_head *head)
  48. {
  49. return head->next == head;
  50. }
  51. #define nl_container_of(ptr, type, member) ({ \
  52. const typeof( ((type *)0)->member ) *__mptr = (ptr); \
  53. (type *)( (char *)__mptr - ((size_t) &((type *)0)->member));})
  54. #define nl_list_entry(ptr, type, member) \
  55. nl_container_of(ptr, type, member)
  56. #define nl_list_at_tail(pos, head, member) \
  57. ((pos)->member.next == (head))
  58. #define nl_list_at_head(pos, head, member) \
  59. ((pos)->member.prev == (head))
  60. #define NL_LIST_HEAD(name) \
  61. struct nl_list_head name = { &(name), &(name) }
  62. #define nl_list_first_entry(head, type, member) \
  63. nl_list_entry((head)->next, type, member)
  64. #define nl_list_for_each_entry(pos, head, member) \
  65. for (pos = nl_list_entry((head)->next, typeof(*pos), member); \
  66. &(pos)->member != (head); \
  67. (pos) = nl_list_entry((pos)->member.next, typeof(*(pos)), member))
  68. #define nl_list_for_each_entry_safe(pos, n, head, member) \
  69. for (pos = nl_list_entry((head)->next, typeof(*pos), member), \
  70. n = nl_list_entry(pos->member.next, typeof(*pos), member); \
  71. &(pos)->member != (head); \
  72. pos = n, n = nl_list_entry(n->member.next, typeof(*n), member))
  73. #define nl_init_list_head(head) \
  74. do { (head)->next = (head); (head)->prev = (head); } while (0)
  75. #endif