2
0

list.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #include <stddef.h>
  14. struct nl_list_head
  15. {
  16. struct nl_list_head * next;
  17. struct nl_list_head * prev;
  18. };
  19. static inline void __nl_list_add(struct nl_list_head *obj,
  20. struct nl_list_head *prev,
  21. struct nl_list_head *next)
  22. {
  23. prev->next = obj;
  24. obj->prev = prev;
  25. next->prev = obj;
  26. obj->next = next;
  27. }
  28. static inline void nl_list_add_tail(struct nl_list_head *obj,
  29. struct nl_list_head *head)
  30. {
  31. __nl_list_add(obj, head->prev, head);
  32. }
  33. static inline void nl_list_add_head(struct nl_list_head *obj,
  34. struct nl_list_head *head)
  35. {
  36. __nl_list_add(obj, head, head->next);
  37. }
  38. static inline void nl_list_del(struct nl_list_head *obj)
  39. {
  40. obj->next->prev = obj->prev;
  41. obj->prev->next = obj->next;
  42. }
  43. static inline int nl_list_empty(struct nl_list_head *head)
  44. {
  45. return head->next == head;
  46. }
  47. #define nl_container_of(ptr, type, member) ({ \
  48. const typeof( ((type *)0)->member ) *__mptr = (ptr); \
  49. (type *) ((char *) __mptr - (offsetof(type, member)));})
  50. #define nl_list_entry(ptr, type, member) \
  51. nl_container_of(ptr, type, member)
  52. #define nl_list_at_tail(pos, head, member) \
  53. ((pos)->member.next == (head))
  54. #define nl_list_at_head(pos, head, member) \
  55. ((pos)->member.prev == (head))
  56. #define NL_LIST_HEAD(name) \
  57. struct nl_list_head name = { &(name), &(name) }
  58. #define nl_list_first_entry(head, type, member) \
  59. nl_list_entry((head)->next, type, member)
  60. #define nl_list_for_each_entry(pos, head, member) \
  61. for (pos = nl_list_entry((head)->next, typeof(*pos), member); \
  62. &(pos)->member != (head); \
  63. (pos) = nl_list_entry((pos)->member.next, typeof(*(pos)), member))
  64. #define nl_list_for_each_entry_safe(pos, n, head, member) \
  65. for (pos = nl_list_entry((head)->next, typeof(*pos), member), \
  66. n = nl_list_entry(pos->member.next, typeof(*pos), member); \
  67. &(pos)->member != (head); \
  68. pos = n, n = nl_list_entry(n->member.next, typeof(*n), member))
  69. #define nl_init_list_head(head) \
  70. do { (head)->next = (head); (head)->prev = (head); } while (0)
  71. #endif