kernel-list.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* vi: set sw=4 ts=4: */
  2. #ifndef LINUX_LIST_H
  3. #define LINUX_LIST_H 1
  4. /*
  5. * Simple doubly linked list implementation.
  6. *
  7. * Some of the internal functions ("__xxx") are useful when
  8. * manipulating whole lists rather than single entries, as
  9. * sometimes we already know the next/prev entries and we can
  10. * generate better code by using them directly rather than
  11. * using the generic single-entry routines.
  12. */
  13. struct list_head {
  14. struct list_head *next, *prev;
  15. };
  16. #define LIST_HEAD_INIT(name) { &(name), &(name) }
  17. #define LIST_HEAD(name) \
  18. struct list_head name = { &name, &name }
  19. #define INIT_LIST_HEAD(ptr) do { \
  20. (ptr)->next = (ptr); (ptr)->prev = (ptr); \
  21. } while (0)
  22. #if (!defined(__GNUC__) && !defined(__WATCOMC__))
  23. #define __inline__
  24. #endif
  25. /*
  26. * Insert a new entry between two known consecutive entries.
  27. *
  28. * This is only for internal list manipulation where we know
  29. * the prev/next entries already!
  30. */
  31. static __inline__ void __list_add(struct list_head * new,
  32. struct list_head * prev,
  33. struct list_head * next)
  34. {
  35. next->prev = new;
  36. new->next = next;
  37. new->prev = prev;
  38. prev->next = new;
  39. }
  40. /*
  41. * Insert a new entry after the specified head..
  42. */
  43. static __inline__ void list_add(struct list_head *new, struct list_head *head)
  44. {
  45. __list_add(new, head, head->next);
  46. }
  47. /*
  48. * Insert a new entry at the tail
  49. */
  50. static __inline__ void list_add_tail(struct list_head *new, struct list_head *head)
  51. {
  52. __list_add(new, head->prev, head);
  53. }
  54. /*
  55. * Delete a list entry by making the prev/next entries
  56. * point to each other.
  57. *
  58. * This is only for internal list manipulation where we know
  59. * the prev/next entries already!
  60. */
  61. static __inline__ void __list_del(struct list_head * prev,
  62. struct list_head * next)
  63. {
  64. next->prev = prev;
  65. prev->next = next;
  66. }
  67. static __inline__ void list_del(struct list_head *entry)
  68. {
  69. __list_del(entry->prev, entry->next);
  70. }
  71. static __inline__ int list_empty(struct list_head *head)
  72. {
  73. return head->next == head;
  74. }
  75. /*
  76. * Splice in "list" into "head"
  77. */
  78. static __inline__ void list_splice(struct list_head *list, struct list_head *head)
  79. {
  80. struct list_head *first = list->next;
  81. if (first != list) {
  82. struct list_head *last = list->prev;
  83. struct list_head *at = head->next;
  84. first->prev = head;
  85. head->next = first;
  86. last->next = at;
  87. at->prev = last;
  88. }
  89. }
  90. #define list_entry(ptr, type, member) \
  91. ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
  92. #define list_for_each(pos, head) \
  93. for (pos = (head)->next; pos != (head); pos = pos->next)
  94. #endif