kernel-list.h 2.4 KB

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