list.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (c) 2009 Openmoko Inc.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /*
  18. * Simple doubly linked list.
  19. *
  20. * This is a generic implementation.
  21. * No dynamic memeory allocation required.
  22. *
  23. */
  24. #ifndef LIST_H_
  25. #define LIST_H_
  26. /*
  27. * Use this struct in the beginning of your structure declaration.
  28. */
  29. struct wl_list {
  30. struct wl_list *prev, *next;
  31. };
  32. static inline void wl_list_init(struct wl_list *list)
  33. {
  34. list->prev = list;
  35. list->next = list;
  36. }
  37. static inline void
  38. wl_list_insert_after(struct wl_list *list, struct wl_list *node)
  39. {
  40. node->prev = list;
  41. node->next = list->next;
  42. list->next->prev = node;
  43. list->next = node;
  44. }
  45. static inline void
  46. wl_list_link_node(struct wl_list *prev, struct wl_list *next)
  47. {
  48. prev->next = next;
  49. next->prev = prev;
  50. }
  51. static inline void wl_list_del(struct wl_list *node)
  52. {
  53. wl_list_link_node(node->prev, node->next);
  54. wl_list_init(node);
  55. }
  56. static inline unsigned int wl_list_size(struct wl_list *list)
  57. {
  58. struct wl_list *node = list->next;
  59. unsigned int count = 0;
  60. while (node != list) {
  61. ++count;
  62. node = node->next;
  63. }
  64. return count;
  65. }
  66. static struct wl_list *
  67. wl_list_search(struct wl_list *head, const void *value, unsigned int offset,
  68. int (*comp)(const void *value, unsigned int offset, const struct wl_list *node))
  69. {
  70. struct wl_list *p = head->next;
  71. while (p != head) {
  72. if (!(comp)(value, offset, p))
  73. break;
  74. p = p->next;
  75. }
  76. return p == head ? NULL : p;
  77. }
  78. static struct wl_list * wl_list_find_nth_node(struct wl_list *list, unsigned int nth)
  79. {
  80. struct wl_list *node = list->next;
  81. while (nth) {
  82. --nth;
  83. node = node->next;
  84. }
  85. return node;
  86. }
  87. static struct wl_list *wl_list_remove_last(struct wl_list *list)
  88. {
  89. struct wl_list *node = list->prev;
  90. wl_list_del(node);
  91. return node;
  92. }
  93. static void wl_list_move2_first(struct wl_list *list, struct wl_list *node)
  94. {
  95. wl_list_link_node(node->prev, node->next);
  96. wl_list_insert_after(list, node);
  97. }
  98. #endif /* LIST_H_*/