lists.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright Institute of Communication and Computer Systems (ICCS)
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. /**
  18. * @file lists.h
  19. * @author Ioannis Koutras (joko@microlab.ntua.gr)
  20. * @date October 2012
  21. * @brief Macros for singly-linked lists
  22. *
  23. * These macros were copied from /usr/include/sys/queue.h
  24. *
  25. */
  26. #ifndef _DMMLIB_LISTS_H_
  27. #define _DMMLIB_LISTS_H_
  28. /*
  29. * Singly-linked List definitions.
  30. */
  31. /** The head of a singly-linked list */
  32. #define SLIST_HEAD(name, type) \
  33. struct name { \
  34. struct type *slh_first; /* first element */ \
  35. }
  36. /** Initial value of the head of a singly-linked list */
  37. #define SLIST_HEAD_INITIALIZER(head) \
  38. { NULL }
  39. /** Entry of a singly linked list */
  40. #define SLIST_ENTRY(type) \
  41. struct { \
  42. struct type *sle_next; /* next element */ \
  43. }
  44. /*
  45. * Singly-linked List functions.
  46. */
  47. /** Initializes the head of a singly-linked list */
  48. #define SLIST_INIT(head) do { \
  49. (head)->slh_first = NULL; \
  50. } while (/*CONSTCOND*/0)
  51. /** Inserts an element after a specific one in a singly-linked list */
  52. #define SLIST_INSERT_AFTER(slistelm, elm, field) do { \
  53. (elm)->field.sle_next = (slistelm)->field.sle_next; \
  54. (slistelm)->field.sle_next = (elm); \
  55. } while (/*CONSTCOND*/0)
  56. /** Inserts an element as the head of a singly-linked list */
  57. #define SLIST_INSERT_HEAD(head, elm, field) do { \
  58. (elm)->field.sle_next = (head)->slh_first; \
  59. (head)->slh_first = (elm); \
  60. } while (/*CONSTCOND*/0)
  61. /** Removes the head element of a singly-linked list */
  62. #define SLIST_REMOVE_HEAD(head, field) do { \
  63. (head)->slh_first = (head)->slh_first->field.sle_next; \
  64. } while (/*CONSTCOND*/0)
  65. /** Removes an element from a singly-linked list */
  66. #define SLIST_REMOVE(head, elm, type, field) do { \
  67. if ((head)->slh_first == (elm)) { \
  68. SLIST_REMOVE_HEAD((head), field); \
  69. } \
  70. else { \
  71. struct type *curelm = (head)->slh_first; \
  72. while(curelm->field.sle_next != (elm)) \
  73. curelm = curelm->field.sle_next; \
  74. curelm->field.sle_next = \
  75. curelm->field.sle_next->field.sle_next; \
  76. } \
  77. } while (/*CONSTCOND*/0)
  78. /** Iterates through a singly-linked list */
  79. #define SLIST_FOREACH(var, head, field) \
  80. for((var) = (head)->slh_first; (var); (var) = (var)->field.sle_next)
  81. /*
  82. * Singly-linked List access methods.
  83. */
  84. /** Empties a singly-linked list */
  85. #define SLIST_EMPTY(head) ((head)->slh_first == NULL)
  86. /** Gets the head of a singly-linked list */
  87. #define SLIST_FIRST(head) ((head)->slh_first)
  88. /** Gets the next element of a specific element from a singly-linked list */
  89. #define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
  90. #endif /* _DMMLIB_LISTS_H_ */