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

/usr/src/cmd/rad/daemon/list.c

https://bitbucket.org/dilos/vpanels
C | 247 lines | 181 code | 36 blank | 30 comment | 24 complexity | ca9313af9d483ba2878b6f97861132d5 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause, LGPL-2.1, BSD-3-Clause-No-Nuclear-License-2014
  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, 2011, 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. #include <sys/debug.h>
  32. #ifdef lint
  33. #define list_d2l(a, obj) \
  34. ((list_node_t *)(((long *)obj) + (a)->list_offset / sizeof (long)))
  35. #else
  36. #define list_d2l(a, obj) ((list_node_t *)(((char *)obj) + (a)->list_offset))
  37. #endif
  38. #define list_object(a, node) ((void *)(((char *)node) - (a)->list_offset))
  39. #define list_empty(a) ((a)->list_head.list_next == &(a)->list_head)
  40. #define list_insert_after_node(list, node, object) { \
  41. list_node_t *lnew = list_d2l(list, object); \
  42. lnew->list_prev = (node); \
  43. lnew->list_next = (node)->list_next; \
  44. (node)->list_next->list_prev = lnew; \
  45. (node)->list_next = lnew; \
  46. }
  47. #define list_insert_before_node(list, node, object) { \
  48. list_node_t *lnew = list_d2l(list, object); \
  49. lnew->list_next = (node); \
  50. lnew->list_prev = (node)->list_prev; \
  51. (node)->list_prev->list_next = lnew; \
  52. (node)->list_prev = lnew; \
  53. }
  54. #define list_remove_node(node) \
  55. (node)->list_prev->list_next = (node)->list_next; \
  56. (node)->list_next->list_prev = (node)->list_prev; \
  57. (node)->list_next = (node)->list_prev = NULL
  58. void
  59. list_create(list_t *list, size_t size, size_t offset)
  60. {
  61. ASSERT(list);
  62. ASSERT(size > 0);
  63. ASSERT(size >= offset + sizeof (list_node_t));
  64. list->list_size = size;
  65. list->list_offset = offset;
  66. list->list_head.list_next = list->list_head.list_prev =
  67. &list->list_head;
  68. }
  69. void
  70. list_destroy(list_t *list)
  71. {
  72. list_node_t *node = &list->list_head;
  73. ASSERT(list);
  74. ASSERT(list->list_head.list_next == node);
  75. ASSERT(list->list_head.list_prev == node);
  76. node->list_next = node->list_prev = NULL;
  77. }
  78. void
  79. list_insert_after(list_t *list, void *object, void *nobject)
  80. {
  81. if (object == NULL) {
  82. list_insert_head(list, nobject);
  83. } else {
  84. list_node_t *lold = list_d2l(list, object);
  85. list_insert_after_node(list, lold, nobject);
  86. }
  87. }
  88. void
  89. list_insert_before(list_t *list, void *object, void *nobject)
  90. {
  91. if (object == NULL) {
  92. list_insert_tail(list, nobject);
  93. } else {
  94. list_node_t *lold = list_d2l(list, object);
  95. list_insert_before_node(list, lold, nobject);
  96. }
  97. }
  98. void
  99. list_insert_head(list_t *list, void *object)
  100. {
  101. list_node_t *lold = &list->list_head;
  102. list_insert_after_node(list, lold, object);
  103. }
  104. void
  105. list_insert_tail(list_t *list, void *object)
  106. {
  107. list_node_t *lold = &list->list_head;
  108. list_insert_before_node(list, lold, object);
  109. }
  110. void
  111. list_remove(list_t *list, void *object)
  112. {
  113. list_node_t *lold = list_d2l(list, object);
  114. ASSERT(!list_empty(list));
  115. ASSERT(lold->list_next != NULL);
  116. list_remove_node(lold);
  117. }
  118. void *
  119. list_remove_head(list_t *list)
  120. {
  121. list_node_t *head = list->list_head.list_next;
  122. if (head == &list->list_head)
  123. return (NULL);
  124. list_remove_node(head);
  125. return (list_object(list, head));
  126. }
  127. void *
  128. list_remove_tail(list_t *list)
  129. {
  130. list_node_t *tail = list->list_head.list_prev;
  131. if (tail == &list->list_head)
  132. return (NULL);
  133. list_remove_node(tail);
  134. return (list_object(list, tail));
  135. }
  136. void *
  137. list_head(list_t *list)
  138. {
  139. if (list_empty(list))
  140. return (NULL);
  141. return (list_object(list, list->list_head.list_next));
  142. }
  143. void *
  144. list_tail(list_t *list)
  145. {
  146. if (list_empty(list))
  147. return (NULL);
  148. return (list_object(list, list->list_head.list_prev));
  149. }
  150. void *
  151. list_next(list_t *list, void *object)
  152. {
  153. list_node_t *node = list_d2l(list, object);
  154. if (node->list_next != &list->list_head)
  155. return (list_object(list, node->list_next));
  156. return (NULL);
  157. }
  158. void *
  159. list_prev(list_t *list, void *object)
  160. {
  161. list_node_t *node = list_d2l(list, object);
  162. if (node->list_prev != &list->list_head)
  163. return (list_object(list, node->list_prev));
  164. return (NULL);
  165. }
  166. /*
  167. * Insert src list after dst list. Empty src list thereafter.
  168. */
  169. void
  170. list_move_tail(list_t *dst, list_t *src)
  171. {
  172. list_node_t *dstnode = &dst->list_head;
  173. list_node_t *srcnode = &src->list_head;
  174. ASSERT(dst->list_size == src->list_size);
  175. ASSERT(dst->list_offset == src->list_offset);
  176. if (list_empty(src))
  177. return;
  178. dstnode->list_prev->list_next = srcnode->list_next;
  179. srcnode->list_next->list_prev = dstnode->list_prev;
  180. dstnode->list_prev = srcnode->list_prev;
  181. srcnode->list_prev->list_next = dstnode;
  182. /* empty src list */
  183. srcnode->list_next = srcnode->list_prev = srcnode;
  184. }
  185. void
  186. list_link_replace(list_node_t *lold, list_node_t *lnew)
  187. {
  188. ASSERT(list_link_active(lold));
  189. ASSERT(!list_link_active(lnew));
  190. lnew->list_next = lold->list_next;
  191. lnew->list_prev = lold->list_prev;
  192. lold->list_prev->list_next = lnew;
  193. lold->list_next->list_prev = lnew;
  194. lold->list_next = lold->list_prev = NULL;
  195. }
  196. void
  197. list_link_init(list_node_t *link)
  198. {
  199. link->list_next = NULL;
  200. link->list_prev = NULL;
  201. }
  202. int
  203. list_link_active(list_node_t *link)
  204. {
  205. return (link->list_next != NULL);
  206. }
  207. int
  208. list_is_empty(list_t *list)
  209. {
  210. return (list_empty(list));
  211. }