llist.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /* llist.c - single link list implementation */
  42. #include <string.h>
  43. #include "slapi-plugin.h"
  44. #include "slapi-private.h"
  45. #include "llist.h"
  46. #include "repl_shared.h"
  47. /* data structures */
  48. /* link list node */
  49. typedef struct lnode
  50. {
  51. char *key;
  52. void *data;
  53. struct lnode *next;
  54. } LNode;
  55. /* This structure defines a one-way linked list with head and tail pointers.
  56. The list contains a "dummy" head node which makes sure that every node
  57. has a previous node. This allows to remove a node during iteration without
  58. breaking the list */
  59. struct llist
  60. {
  61. LNode *head;
  62. LNode *tail;
  63. };
  64. /* forward declarations */
  65. static LNode* _llistNewNode (const char *key, void *data);
  66. static void _llistDestroyNode (LNode **node, FNFree fnFree);
  67. LList* llistNew ()
  68. {
  69. LList *list = (LList*) slapi_ch_calloc (1, sizeof (LList));
  70. /* allocate a special head node - it contains no data but just
  71. fulfills the requirement that every node has a previous one.
  72. This is used during iteration with removal */
  73. if (list)
  74. {
  75. list->head = (LNode*)slapi_ch_calloc (1, sizeof (LNode));
  76. if (list->head == NULL)
  77. {
  78. slapi_ch_free ((void**)&list);
  79. }
  80. }
  81. return list;
  82. }
  83. void llistDestroy (LList **list, FNFree fnFree)
  84. {
  85. LNode *node = NULL, *prev_node;
  86. if (list == NULL || *list == NULL)
  87. return;
  88. if ((*list)->head)
  89. node = (*list)->head->next;
  90. while (node)
  91. {
  92. prev_node = node;
  93. node = node->next;
  94. _llistDestroyNode (&prev_node, fnFree);
  95. }
  96. slapi_ch_free ((void**)&((*list)->head));
  97. slapi_ch_free ((void**)list);
  98. }
  99. void* llistGetFirst(LList *list, void **iterator)
  100. {
  101. if (list == NULL || iterator == NULL || list->head == NULL || list->head->next == NULL)
  102. {
  103. /* empty list or error */
  104. return NULL;
  105. }
  106. /* Iterator points to the previous element (so that we can remove current element
  107. and still keep the list in tact. In case of the first element, iterator points
  108. to the dummy head element */
  109. (*iterator) = list->head;
  110. return list->head->next->data;
  111. }
  112. void* llistGetNext (LList *list, void **iterator)
  113. {
  114. LNode *node;
  115. if (list == NULL || list->head == NULL || iterator == NULL || *iterator == NULL)
  116. {
  117. /* end of the list or error */
  118. return NULL;
  119. }
  120. /* Iterator points to the previous element (so that we can
  121. remove current element and still keep list in tact. */
  122. node = *(LNode **)iterator;
  123. node = node->next;
  124. (*iterator) = node;
  125. if (node && node->next)
  126. return node->next->data;
  127. else
  128. return NULL;
  129. }
  130. void* llistRemoveCurrentAndGetNext (LList *list, void **iterator)
  131. {
  132. LNode *prevNode, *node;
  133. /* end of the list is reached or error occured */
  134. if (list == NULL || iterator == NULL || *iterator == NULL)
  135. return NULL;
  136. /* Iterator points to the previous element (so that we can
  137. remove current element and still keep list in tact. */
  138. prevNode = *(LNode **)iterator;
  139. node = prevNode->next;
  140. if (node)
  141. {
  142. prevNode->next = node->next;
  143. if (list->tail == node) {
  144. list->tail = prevNode;
  145. }
  146. _llistDestroyNode (&node, NULL);
  147. node = prevNode->next;
  148. if (node) {
  149. return node->data;
  150. } else {
  151. return NULL;
  152. }
  153. }
  154. else
  155. return NULL;
  156. }
  157. void* llistGetHead (LList *list)
  158. {
  159. if (list == NULL || list->head == NULL || list->head->next == NULL)
  160. {
  161. /* empty list or error */
  162. return NULL;
  163. }
  164. return list->head->next->data;
  165. }
  166. void* llistGetTail (LList *list)
  167. {
  168. if (list == NULL || list->tail == NULL)
  169. {
  170. /* empty list or error */
  171. return NULL;
  172. }
  173. return list->tail->data;
  174. }
  175. void* llistGet (LList *list, const char* key)
  176. {
  177. LNode *node;
  178. /* empty list or invalid input */
  179. if (list == NULL || list->head == NULL || list->head->next == NULL || key == NULL)
  180. return NULL;
  181. node = list->head->next;
  182. while (node)
  183. {
  184. if (node->key && strcmp (key, node->key) == 0)
  185. {
  186. return node->data;
  187. }
  188. node = node->next;
  189. }
  190. /* node with specified key is not found */
  191. return NULL;
  192. }
  193. int llistInsertHead (LList *list, const char *key, void *data)
  194. {
  195. LNode *node;
  196. if (list == NULL || list->head == NULL || data == NULL)
  197. {
  198. slapi_log_error(SLAPI_LOG_PLUGIN, repl_plugin_name, "llistInsertHead: invalid argument\n");
  199. return -1;
  200. }
  201. node = _llistNewNode (key, data);
  202. if (node == NULL)
  203. {
  204. slapi_log_error(SLAPI_LOG_PLUGIN, repl_plugin_name, "llistInsertHead: failed to allocate list node\n");
  205. return -1;
  206. }
  207. if (list->head->next == NULL) /* empty list */
  208. {
  209. list->head->next = node;
  210. list->tail = node;
  211. }
  212. else
  213. {
  214. node->next = list->head->next;
  215. list->head->next = node;
  216. }
  217. return 0;
  218. }
  219. int llistInsertTail (LList *list, const char *key, void *data)
  220. {
  221. LNode *node;
  222. if (list == NULL || list->head == NULL || data == NULL)
  223. {
  224. slapi_log_error(SLAPI_LOG_PLUGIN, repl_plugin_name, "llistInsertHead: invalid argument\n");
  225. return -1;
  226. }
  227. node = _llistNewNode (key, data);
  228. if (node == NULL)
  229. {
  230. slapi_log_error(SLAPI_LOG_PLUGIN, repl_plugin_name, "llistInsertHead: failed to allocate list node\n");
  231. return -1;
  232. }
  233. if (list->head->next == NULL) /* empty list */
  234. {
  235. list->head->next = node;
  236. list->tail = node;
  237. }
  238. else
  239. {
  240. list->tail->next = node;
  241. list->tail = node;
  242. }
  243. return 0;
  244. }
  245. void* llistRemoveHead (LList *list)
  246. {
  247. LNode *node;
  248. void *data;
  249. if (list == NULL || list->head == NULL || list->head->next == NULL)
  250. return NULL;
  251. node = list->head->next;
  252. data = node->data;
  253. list->head->next = node->next;
  254. /* last element removed */
  255. if (list->head->next == NULL)
  256. list->tail = NULL;
  257. _llistDestroyNode (&node, NULL);
  258. return data;
  259. }
  260. void* llistRemove (LList *list, const char *key)
  261. {
  262. LNode *node, *prev_node;
  263. void *data;
  264. if (list == NULL || list->head == NULL || list->head->next == NULL || key == NULL)
  265. return NULL;
  266. node = list->head->next;
  267. prev_node = list->head;
  268. while (node)
  269. {
  270. if (node->key && strcmp (key, node->key) == 0)
  271. {
  272. prev_node->next = node->next;
  273. /* last element removed */
  274. if (node->next == NULL)
  275. {
  276. /* no more elements in the list */
  277. if (list->head->next == NULL)
  278. {
  279. list->tail = NULL;
  280. }
  281. else
  282. {
  283. list->tail = prev_node;
  284. }
  285. }
  286. data = node->data;
  287. _llistDestroyNode (&node, NULL);
  288. return data;
  289. }
  290. prev_node = node;
  291. node = node->next;
  292. }
  293. /* node with specified key is not found */
  294. return NULL;
  295. }
  296. static LNode* _llistNewNode (const char *key, void *data)
  297. {
  298. LNode *node = (LNode*) slapi_ch_malloc (sizeof (LNode));
  299. if (node == NULL)
  300. return NULL;
  301. if (key)
  302. node->key = slapi_ch_strdup (key);
  303. else
  304. node->key = NULL;
  305. node->data = data;
  306. node->next = NULL;
  307. return node;
  308. }
  309. static void _llistDestroyNode (LNode **node, FNFree fnFree)
  310. {
  311. if ((*node)->data && fnFree)
  312. fnFree (&(*node)->data);
  313. if ((*node)->key)
  314. slapi_ch_free ((void**)&((*node)->key));
  315. slapi_ch_free ((void**)node);
  316. }