PageRenderTime 55ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/usr/src/common/list/list.c

https://bitbucket.org/a3217055/illumos-gate
C | 251 lines | 185 code | 36 blank | 30 comment | 24 complexity | bc1b3fa9619895e43c2cafbae07bc1e3 MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, LGPL-2.0, 0BSD, AGPL-3.0, GPL-2.0, GPL-3.0, LGPL-2.1, LGPL-3.0, BSD-3-Clause-No-Nuclear-License-2014, MPL-2.0-no-copyleft-exception, AGPL-1.0
  1. /*
  2. * CDDL HEADER START
  3. *
  4. * The contents of this file are subject to the terms of the
  5. * Common Development and Distribution License (the "License").
  6. * You may not use this file except in compliance with the License.
  7. *
  8. * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  9. * or http://www.opensolaris.org/os/licensing.
  10. * See the License for the specific language governing permissions
  11. * and limitations under the License.
  12. *
  13. * When distributing Covered Code, include this CDDL HEADER in each
  14. * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15. * If applicable, add the following below this CDDL HEADER, with the
  16. * fields enclosed by brackets "[]" replaced with your own identifying
  17. * information: Portions Copyright [yyyy] [name of copyright owner]
  18. *
  19. * CDDL HEADER END
  20. */
  21. /*
  22. * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
  23. */
  24. /*
  25. * Generic doubly-linked list implementation
  26. */
  27. #include <sys/list.h>
  28. #include <sys/list_impl.h>
  29. #include <sys/types.h>
  30. #include <sys/sysmacros.h>
  31. #ifdef _KERNEL
  32. #include <sys/debug.h>
  33. #else
  34. #include <assert.h>
  35. #define ASSERT(a) assert(a)
  36. #endif
  37. #ifdef lint
  38. extern list_node_t *list_d2l(list_t *list, void *obj);
  39. #else
  40. #define list_d2l(a, obj) ((list_node_t *)(((char *)obj) + (a)->list_offset))
  41. #endif
  42. #define list_object(a, node) ((void *)(((char *)node) - (a)->list_offset))
  43. #define list_empty(a) ((a)->list_head.list_next == &(a)->list_head)
  44. #define list_insert_after_node(list, node, object) { \
  45. list_node_t *lnew = list_d2l(list, object); \
  46. lnew->list_prev = (node); \
  47. lnew->list_next = (node)->list_next; \
  48. (node)->list_next->list_prev = lnew; \
  49. (node)->list_next = lnew; \
  50. }
  51. #define list_insert_before_node(list, node, object) { \
  52. list_node_t *lnew = list_d2l(list, object); \
  53. lnew->list_next = (node); \
  54. lnew->list_prev = (node)->list_prev; \
  55. (node)->list_prev->list_next = lnew; \
  56. (node)->list_prev = lnew; \
  57. }
  58. #define list_remove_node(node) \
  59. (node)->list_prev->list_next = (node)->list_next; \
  60. (node)->list_next->list_prev = (node)->list_prev; \
  61. (node)->list_next = (node)->list_prev = NULL
  62. void
  63. list_create(list_t *list, size_t size, size_t offset)
  64. {
  65. ASSERT(list);
  66. ASSERT(size > 0);
  67. ASSERT(size >= offset + sizeof (list_node_t));
  68. list->list_size = size;
  69. list->list_offset = offset;
  70. list->list_head.list_next = list->list_head.list_prev =
  71. &list->list_head;
  72. }
  73. void
  74. list_destroy(list_t *list)
  75. {
  76. list_node_t *node = &list->list_head;
  77. ASSERT(list);
  78. ASSERT(list->list_head.list_next == node);
  79. ASSERT(list->list_head.list_prev == node);
  80. node->list_next = node->list_prev = NULL;
  81. }
  82. void
  83. list_insert_after(list_t *list, void *object, void *nobject)
  84. {
  85. if (object == NULL) {
  86. list_insert_head(list, nobject);
  87. } else {
  88. list_node_t *lold = list_d2l(list, object);
  89. list_insert_after_node(list, lold, nobject);
  90. }
  91. }
  92. void
  93. list_insert_before(list_t *list, void *object, void *nobject)
  94. {
  95. if (object == NULL) {
  96. list_insert_tail(list, nobject);
  97. } else {
  98. list_node_t *lold = list_d2l(list, object);
  99. list_insert_before_node(list, lold, nobject);
  100. }
  101. }
  102. void
  103. list_insert_head(list_t *list, void *object)
  104. {
  105. list_node_t *lold = &list->list_head;
  106. list_insert_after_node(list, lold, object);
  107. }
  108. void
  109. list_insert_tail(list_t *list, void *object)
  110. {
  111. list_node_t *lold = &list->list_head;
  112. list_insert_before_node(list, lold, object);
  113. }
  114. void
  115. list_remove(list_t *list, void *object)
  116. {
  117. list_node_t *lold = list_d2l(list, object);
  118. ASSERT(!list_empty(list));
  119. ASSERT(lold->list_next != NULL);
  120. list_remove_node(lold);
  121. }
  122. void *
  123. list_remove_head(list_t *list)
  124. {
  125. list_node_t *head = list->list_head.list_next;
  126. if (head == &list->list_head)
  127. return (NULL);
  128. list_remove_node(head);
  129. return (list_object(list, head));
  130. }
  131. void *
  132. list_remove_tail(list_t *list)
  133. {
  134. list_node_t *tail = list->list_head.list_prev;
  135. if (tail == &list->list_head)
  136. return (NULL);
  137. list_remove_node(tail);
  138. return (list_object(list, tail));
  139. }
  140. void *
  141. list_head(list_t *list)
  142. {
  143. if (list_empty(list))
  144. return (NULL);
  145. return (list_object(list, list->list_head.list_next));
  146. }
  147. void *
  148. list_tail(list_t *list)
  149. {
  150. if (list_empty(list))
  151. return (NULL);
  152. return (list_object(list, list->list_head.list_prev));
  153. }
  154. void *
  155. list_next(list_t *list, void *object)
  156. {
  157. list_node_t *node = list_d2l(list, object);
  158. if (node->list_next != &list->list_head)
  159. return (list_object(list, node->list_next));
  160. return (NULL);
  161. }
  162. void *
  163. list_prev(list_t *list, void *object)
  164. {
  165. list_node_t *node = list_d2l(list, object);
  166. if (node->list_prev != &list->list_head)
  167. return (list_object(list, node->list_prev));
  168. return (NULL);
  169. }
  170. /*
  171. * Insert src list after dst list. Empty src list thereafter.
  172. */
  173. void
  174. list_move_tail(list_t *dst, list_t *src)
  175. {
  176. list_node_t *dstnode = &dst->list_head;
  177. list_node_t *srcnode = &src->list_head;
  178. ASSERT(dst->list_size == src->list_size);
  179. ASSERT(dst->list_offset == src->list_offset);
  180. if (list_empty(src))
  181. return;
  182. dstnode->list_prev->list_next = srcnode->list_next;
  183. srcnode->list_next->list_prev = dstnode->list_prev;
  184. dstnode->list_prev = srcnode->list_prev;
  185. srcnode->list_prev->list_next = dstnode;
  186. /* empty src list */
  187. srcnode->list_next = srcnode->list_prev = srcnode;
  188. }
  189. void
  190. list_link_replace(list_node_t *lold, list_node_t *lnew)
  191. {
  192. ASSERT(list_link_active(lold));
  193. ASSERT(!list_link_active(lnew));
  194. lnew->list_next = lold->list_next;
  195. lnew->list_prev = lold->list_prev;
  196. lold->list_prev->list_next = lnew;
  197. lold->list_next->list_prev = lnew;
  198. lold->list_next = lold->list_prev = NULL;
  199. }
  200. void
  201. list_link_init(list_node_t *link)
  202. {
  203. link->list_next = NULL;
  204. link->list_prev = NULL;
  205. }
  206. int
  207. list_link_active(list_node_t *link)
  208. {
  209. return (link->list_next != NULL);
  210. }
  211. int
  212. list_is_empty(list_t *list)
  213. {
  214. return (list_empty(list));
  215. }