list.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* vi: set sw=4 ts=4: */
  2. #include "list.h"
  3. /*
  4. * Insert a new entry between two known consecutive entries.
  5. *
  6. * This is only for internal list manipulation where we know
  7. * the prev/next entries already!
  8. */
  9. void __list_add(struct list_head * add,
  10. struct list_head * prev,
  11. struct list_head * next)
  12. {
  13. next->prev = add;
  14. add->next = next;
  15. add->prev = prev;
  16. prev->next = add;
  17. }
  18. /*
  19. * list_add - add a new entry
  20. * @add: new entry to be added
  21. * @head: list head to add it after
  22. *
  23. * Insert a new entry after the specified head.
  24. * This is good for implementing stacks.
  25. */
  26. void list_add(struct list_head *add, struct list_head *head)
  27. {
  28. __list_add(add, head, head->next);
  29. }
  30. /*
  31. * list_add_tail - add a new entry
  32. * @add: new entry to be added
  33. * @head: list head to add it before
  34. *
  35. * Insert a new entry before the specified head.
  36. * This is useful for implementing queues.
  37. */
  38. void list_add_tail(struct list_head *add, struct list_head *head)
  39. {
  40. __list_add(add, head->prev, head);
  41. }
  42. /*
  43. * Delete a list entry by making the prev/next entries
  44. * point to each other.
  45. *
  46. * This is only for internal list manipulation where we know
  47. * the prev/next entries already!
  48. */
  49. void __list_del(struct list_head * prev, struct list_head * next)
  50. {
  51. next->prev = prev;
  52. prev->next = next;
  53. }
  54. /*
  55. * list_del - deletes entry from list.
  56. * @entry: the element to delete from the list.
  57. *
  58. * list_empty() on @entry does not return true after this, @entry is
  59. * in an undefined state.
  60. */
  61. void list_del(struct list_head *entry)
  62. {
  63. __list_del(entry->prev, entry->next);
  64. }
  65. /*
  66. * list_del_init - deletes entry from list and reinitialize it.
  67. * @entry: the element to delete from the list.
  68. */
  69. void list_del_init(struct list_head *entry)
  70. {
  71. __list_del(entry->prev, entry->next);
  72. INIT_LIST_HEAD(entry);
  73. }
  74. /*
  75. * list_empty - tests whether a list is empty
  76. * @head: the list to test.
  77. */
  78. int list_empty(struct list_head *head)
  79. {
  80. return head->next == head;
  81. }
  82. /*
  83. * list_splice - join two lists
  84. * @list: the new list to add.
  85. * @head: the place to add it in the first list.
  86. */
  87. void list_splice(struct list_head *list, struct list_head *head)
  88. {
  89. struct list_head *first = list->next;
  90. if (first != list) {
  91. struct list_head *last = list->prev;
  92. struct list_head *at = head->next;
  93. first->prev = head;
  94. head->next = first;
  95. last->next = at;
  96. at->prev = last;
  97. }
  98. }