llist.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /* llist.c - single link list implementation */
  13. #include <string.h>
  14. #include "slapi-plugin.h"
  15. #include "slapi-private.h"
  16. #include "llist.h"
  17. #include "repl_shared.h"
  18. /* data structures */
  19. /* link list node */
  20. typedef struct lnode
  21. {
  22. char *key;
  23. void *data;
  24. struct lnode *next;
  25. } LNode;
  26. /* This structure defines a one-way linked list with head and tail pointers.
  27. The list contains a "dummy" head node which makes sure that every node
  28. has a previous node. This allows to remove a node during iteration without
  29. breaking the list */
  30. struct llist
  31. {
  32. LNode *head;
  33. LNode *tail;
  34. };
  35. /* forward declarations */
  36. static LNode* _llistNewNode (const char *key, void *data);
  37. static void _llistDestroyNode (LNode **node, FNFree fnFree);
  38. LList* llistNew ()
  39. {
  40. LList *list = (LList*) slapi_ch_calloc (1, sizeof (LList));
  41. /* allocate a special head node - it contains no data but just
  42. fulfills the requirement that every node has a previous one.
  43. This is used during iteration with removal */
  44. if (list)
  45. {
  46. list->head = (LNode*)slapi_ch_calloc (1, sizeof (LNode));
  47. if (list->head == NULL)
  48. {
  49. slapi_ch_free ((void**)&list);
  50. }
  51. }
  52. return list;
  53. }
  54. void llistDestroy (LList **list, FNFree fnFree)
  55. {
  56. LNode *node = NULL, *prev_node;
  57. if (list == NULL || *list == NULL)
  58. return;
  59. if ((*list)->head)
  60. node = (*list)->head->next;
  61. while (node)
  62. {
  63. prev_node = node;
  64. node = node->next;
  65. _llistDestroyNode (&prev_node, fnFree);
  66. }
  67. slapi_ch_free ((void**)&((*list)->head));
  68. slapi_ch_free ((void**)list);
  69. }
  70. void* llistGetFirst(LList *list, void **iterator)
  71. {
  72. if (list == NULL || iterator == NULL || list->head == NULL || list->head->next == NULL)
  73. {
  74. /* empty list or error */
  75. return NULL;
  76. }
  77. /* Iterator points to the previous element (so that we can remove current element
  78. and still keep the list in tact. In case of the first element, iterator points
  79. to the dummy head element */
  80. (*iterator) = list->head;
  81. return list->head->next->data;
  82. }
  83. void* llistGetNext (LList *list, void **iterator)
  84. {
  85. LNode *node;
  86. if (list == NULL || list->head == NULL || iterator == NULL || *iterator == NULL)
  87. {
  88. /* end of the list or error */
  89. return NULL;
  90. }
  91. /* Iterator points to the previous element (so that we can
  92. remove current element and still keep list in tact. */
  93. node = *(LNode **)iterator;
  94. node = node->next;
  95. (*iterator) = node;
  96. if (node && node->next)
  97. return node->next->data;
  98. else
  99. return NULL;
  100. }
  101. void* llistRemoveCurrentAndGetNext (LList *list, void **iterator)
  102. {
  103. LNode *prevNode, *node;
  104. /* end of the list is reached or error occured */
  105. if (list == NULL || iterator == NULL || *iterator == NULL)
  106. return NULL;
  107. /* Iterator points to the previous element (so that we can
  108. remove current element and still keep list in tact. */
  109. prevNode = *(LNode **)iterator;
  110. node = prevNode->next;
  111. if (node)
  112. {
  113. prevNode->next = node->next;
  114. if (list->tail == node) {
  115. list->tail = prevNode;
  116. }
  117. _llistDestroyNode (&node, NULL);
  118. node = prevNode->next;
  119. if (node) {
  120. return node->data;
  121. } else {
  122. return NULL;
  123. }
  124. }
  125. else
  126. return NULL;
  127. }
  128. void* llistGetHead (LList *list)
  129. {
  130. if (list == NULL || list->head == NULL || list->head->next == NULL)
  131. {
  132. /* empty list or error */
  133. return NULL;
  134. }
  135. return list->head->next->data;
  136. }
  137. void* llistGetTail (LList *list)
  138. {
  139. if (list == NULL || list->tail == NULL)
  140. {
  141. /* empty list or error */
  142. return NULL;
  143. }
  144. return list->tail->data;
  145. }
  146. void* llistGet (LList *list, const char* key)
  147. {
  148. LNode *node;
  149. /* empty list or invalid input */
  150. if (list == NULL || list->head == NULL || list->head->next == NULL || key == NULL)
  151. return NULL;
  152. node = list->head->next;
  153. while (node)
  154. {
  155. if (node->key && strcmp (key, node->key) == 0)
  156. {
  157. return node->data;
  158. }
  159. node = node->next;
  160. }
  161. /* node with specified key is not found */
  162. return NULL;
  163. }
  164. int llistInsertHead (LList *list, const char *key, void *data)
  165. {
  166. LNode *node;
  167. if (list == NULL || list->head == NULL || data == NULL)
  168. {
  169. slapi_log_error(SLAPI_LOG_PLUGIN, repl_plugin_name, "llistInsertHead: invalid argument\n");
  170. return -1;
  171. }
  172. node = _llistNewNode (key, data);
  173. if (node == NULL)
  174. {
  175. slapi_log_error(SLAPI_LOG_PLUGIN, repl_plugin_name, "llistInsertHead: failed to allocate list node\n");
  176. return -1;
  177. }
  178. if (list->head->next == NULL) /* empty list */
  179. {
  180. list->head->next = node;
  181. list->tail = node;
  182. }
  183. else
  184. {
  185. node->next = list->head->next;
  186. list->head->next = node;
  187. }
  188. return 0;
  189. }
  190. int llistInsertTail (LList *list, const char *key, void *data)
  191. {
  192. LNode *node;
  193. if (list == NULL || list->head == NULL || data == NULL)
  194. {
  195. slapi_log_error(SLAPI_LOG_PLUGIN, repl_plugin_name, "llistInsertHead: invalid argument\n");
  196. return -1;
  197. }
  198. node = _llistNewNode (key, data);
  199. if (node == NULL)
  200. {
  201. slapi_log_error(SLAPI_LOG_PLUGIN, repl_plugin_name, "llistInsertHead: failed to allocate list node\n");
  202. return -1;
  203. }
  204. if (list->head->next == NULL) /* empty list */
  205. {
  206. list->head->next = node;
  207. list->tail = node;
  208. }
  209. else
  210. {
  211. list->tail->next = node;
  212. list->tail = node;
  213. }
  214. return 0;
  215. }
  216. void* llistRemoveHead (LList *list)
  217. {
  218. LNode *node;
  219. void *data;
  220. if (list == NULL || list->head == NULL || list->head->next == NULL)
  221. return NULL;
  222. node = list->head->next;
  223. data = node->data;
  224. list->head->next = node->next;
  225. /* last element removed */
  226. if (list->head->next == NULL)
  227. list->tail = NULL;
  228. _llistDestroyNode (&node, NULL);
  229. return data;
  230. }
  231. void* llistRemove (LList *list, const char *key)
  232. {
  233. LNode *node, *prev_node;
  234. void *data;
  235. if (list == NULL || list->head == NULL || list->head->next == NULL || key == NULL)
  236. return NULL;
  237. node = list->head->next;
  238. prev_node = list->head;
  239. while (node)
  240. {
  241. if (node->key && strcmp (key, node->key) == 0)
  242. {
  243. prev_node->next = node->next;
  244. /* last element removed */
  245. if (node->next == NULL)
  246. {
  247. /* no more elements in the list */
  248. if (list->head->next == NULL)
  249. {
  250. list->tail = NULL;
  251. }
  252. else
  253. {
  254. list->tail = prev_node;
  255. }
  256. }
  257. data = node->data;
  258. _llistDestroyNode (&node, NULL);
  259. return data;
  260. }
  261. prev_node = node;
  262. node = node->next;
  263. }
  264. /* node with specified key is not found */
  265. return NULL;
  266. }
  267. static LNode* _llistNewNode (const char *key, void *data)
  268. {
  269. LNode *node = (LNode*) slapi_ch_malloc (sizeof (LNode));
  270. if (node == NULL)
  271. return NULL;
  272. if (key)
  273. node->key = slapi_ch_strdup (key);
  274. else
  275. node->key = NULL;
  276. node->data = data;
  277. node->next = NULL;
  278. return node;
  279. }
  280. static void _llistDestroyNode (LNode **node, FNFree fnFree)
  281. {
  282. if ((*node)->data && fnFree)
  283. fnFree (&(*node)->data);
  284. if ((*node)->key)
  285. slapi_ch_free ((void**)&((*node)->key));
  286. slapi_ch_free ((void**)node);
  287. }