list.h 2.5 KB

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