safe_list.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * safe_list - linked list protected against recursive iteration with deletes
  3. *
  4. * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include "safe_list.h"
  19. struct safe_list_iterator {
  20. struct safe_list_iterator **head;
  21. struct safe_list_iterator *next_i;
  22. struct safe_list *next;
  23. };
  24. static void
  25. __safe_list_set_iterator(struct safe_list *list,
  26. struct safe_list_iterator *i)
  27. {
  28. struct safe_list_iterator *next_i;
  29. struct safe_list *next;
  30. next = list_entry(list->list.next, struct safe_list, list);
  31. next_i = next->i;
  32. next->i = i;
  33. i->next = next;
  34. i->head = &next->i;
  35. i->next_i = next_i;
  36. if (next_i)
  37. next_i->head = &i->next_i;
  38. }
  39. static void
  40. __safe_list_del_iterator(struct safe_list_iterator *i)
  41. {
  42. *i->head = i->next_i;
  43. if (i->next_i)
  44. i->next_i->head = i->head;
  45. }
  46. static void
  47. __safe_list_move_iterator(struct safe_list *list,
  48. struct safe_list_iterator *i)
  49. {
  50. __safe_list_del_iterator(i);
  51. __safe_list_set_iterator(list, i);
  52. }
  53. int safe_list_for_each(struct safe_list *head,
  54. int (*cb)(void *ctx, struct safe_list *list),
  55. void *ctx)
  56. {
  57. struct safe_list_iterator i;
  58. struct safe_list *cur;
  59. int ret = 0;
  60. for (cur = list_entry(head->list.next, struct safe_list, list),
  61. __safe_list_set_iterator(cur, &i);
  62. cur != head;
  63. cur = i.next, __safe_list_move_iterator(cur, &i)) {
  64. ret = cb(ctx, cur);
  65. if (ret)
  66. break;
  67. }
  68. __safe_list_del_iterator(&i);
  69. return ret;
  70. }
  71. void safe_list_add(struct safe_list *list, struct safe_list *head)
  72. {
  73. list->i = NULL;
  74. list_add_tail(&list->list, &head->list);
  75. }
  76. void safe_list_add_first(struct safe_list *list, struct safe_list *head)
  77. {
  78. list->i = NULL;
  79. list_add(&list->list, &head->list);
  80. }
  81. void safe_list_del(struct safe_list *list)
  82. {
  83. struct safe_list_iterator *i, *next_i, **tail;
  84. struct safe_list *next;
  85. next = list_entry(list->list.next, struct safe_list, list);
  86. list_del(&list->list);
  87. if (!list->i)
  88. return;
  89. next_i = next->i;
  90. tail = &next->i;
  91. for (i = list->i; i; i = i->next_i) {
  92. tail = &i->next_i;
  93. i->next = next;
  94. }
  95. next->i = list->i;
  96. list->i->head = &next->i;
  97. *tail = next_i;
  98. if (next_i)
  99. next_i->head = tail;
  100. list->i = NULL;
  101. }