list.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Linux klist like system
  3. *
  4. * Copyright (c) 2016-2017 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #ifndef LIST_H
  25. #define LIST_H
  26. #ifndef NULL
  27. #include <stddef.h>
  28. #endif
  29. struct list_head {
  30. struct list_head *prev;
  31. struct list_head *next;
  32. };
  33. #define LIST_HEAD_INIT(el) { &(el), &(el) }
  34. /* return the pointer of type 'type *' containing 'el' as field 'member' */
  35. #define list_entry(el, type, member) container_of(el, type, member)
  36. static inline void init_list_head(struct list_head *head)
  37. {
  38. head->prev = head;
  39. head->next = head;
  40. }
  41. /* insert 'el' between 'prev' and 'next' */
  42. static inline void __list_add(struct list_head *el,
  43. struct list_head *prev, struct list_head *next)
  44. {
  45. prev->next = el;
  46. el->prev = prev;
  47. el->next = next;
  48. next->prev = el;
  49. }
  50. /* add 'el' at the head of the list 'head' (= after element head) */
  51. static inline void list_add(struct list_head *el, struct list_head *head)
  52. {
  53. __list_add(el, head, head->next);
  54. }
  55. /* add 'el' at the end of the list 'head' (= before element head) */
  56. static inline void list_add_tail(struct list_head *el, struct list_head *head)
  57. {
  58. __list_add(el, head->prev, head);
  59. }
  60. static inline void list_del(struct list_head *el)
  61. {
  62. struct list_head *prev, *next;
  63. prev = el->prev;
  64. next = el->next;
  65. prev->next = next;
  66. next->prev = prev;
  67. el->prev = NULL; /* fail safe */
  68. el->next = NULL; /* fail safe */
  69. }
  70. static inline int list_empty(struct list_head *el)
  71. {
  72. return el->next == el;
  73. }
  74. #define list_for_each(el, head) \
  75. for(el = (head)->next; el != (head); el = el->next)
  76. #define list_for_each_safe(el, el1, head) \
  77. for(el = (head)->next, el1 = el->next; el != (head); \
  78. el = el1, el1 = el->next)
  79. #define list_for_each_prev(el, head) \
  80. for(el = (head)->prev; el != (head); el = el->prev)
  81. #define list_for_each_prev_safe(el, el1, head) \
  82. for(el = (head)->prev, el1 = el->prev; el != (head); \
  83. el = el1, el1 = el->prev)
  84. #endif /* LIST_H */