1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-list.c Generic linked list utility (internal to D-BUS implementation)
4 * Copyright (C) 2002 Red Hat, Inc.
6 * Licensed under the Academic Free License version 1.2
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "dbus-internals.h"
25 #include "dbus-list.h"
26 #include "dbus-mempool.h"
27 #include "dbus-threads.h"
30 * @defgroup DBusList Linked list
31 * @ingroup DBusInternals
32 * @brief DBusList data structure
34 * Types and functions related to DBusList.
37 static DBusMemPool *list_pool;
38 static DBusMutex *list_pool_lock = NULL;
40 DBusMutex *_dbus_list_init_lock (void);
42 _dbus_list_init_lock (void)
44 list_pool_lock = dbus_mutex_new ();
45 return list_pool_lock;
49 * @defgroup DBusListInternals Linked list implementation details
50 * @ingroup DBusInternals
51 * @brief DBusList implementation details
53 * The guts of DBusList.
58 /* the mem pool is probably a speed hit, with the thread
59 * lock, though it does still save memory - unknown.
62 alloc_link (void *data)
66 if (!dbus_mutex_lock (list_pool_lock))
71 list_pool = _dbus_mem_pool_new (sizeof (DBusList), TRUE);
73 if (list_pool == NULL)
75 dbus_mutex_unlock (list_pool_lock);
80 link = _dbus_mem_pool_alloc (list_pool);
84 dbus_mutex_unlock (list_pool_lock);
90 free_link (DBusList *link)
92 dbus_mutex_lock (list_pool_lock);
93 _dbus_mem_pool_dealloc (list_pool, link);
94 dbus_mutex_unlock (list_pool_lock);
98 link_before (DBusList **list,
99 DBusList *before_this_link,
110 link->next = before_this_link;
111 link->prev = before_this_link->prev;
112 before_this_link->prev = link;
113 link->prev->next = link;
115 if (before_this_link == *list)
121 link_after (DBusList **list,
122 DBusList *after_this_link,
133 link->prev = after_this_link;
134 link->next = after_this_link->next;
135 after_this_link->next = link;
136 link->next->prev = link;
143 * @addtogroup DBusList
150 * A node in a linked list.
152 * DBusList is a circular list; that is, the tail of the list
153 * points back to the head of the list. The empty list is
154 * represented by a #NULL pointer.
158 * @def _dbus_list_get_next_link
160 * Gets the next link in the list, or #NULL if
161 * there are no more links. Used for iteration.
165 * link = _dbus_list_get_first_link (&list);
166 * while (link != NULL)
168 * printf ("value is %p\n", link->data);
169 * link = _dbus_list_get_next_link (&link);
173 * @param list address of the list head.
174 * @param link current link.
175 * @returns the next link, or %NULL if none.
180 * @def _dbus_list_get_prev_link
182 * Gets the previous link in the list, or #NULL if
183 * there are no more links. Used for iteration.
187 * link = _dbus_list_get_last_link (&list);
188 * while (link != NULL)
190 * printf ("value is %p\n", link->data);
191 * link = _dbus_list_get_prev_link (&link);
195 * @param list address of the list head.
196 * @param link current link.
197 * @returns the previous link, or %NULL if none.
202 * Allocates a linked list node. Useful for preallocating
203 * nodes and using _dbus_list_append_link() to avoid
206 * @param data the value to store in the link.
207 * @returns a newly allocated link.
210 _dbus_list_alloc_link (void *data)
212 return alloc_link (data);
216 * Frees a linked list node allocated with _dbus_list_alloc_link.
217 * Does not free the data in the node.
219 * @param link the list node
222 _dbus_list_free_link (DBusList *link)
229 * Appends a value to the list. May return #FALSE
230 * if insufficient memory exists to add a list link.
231 * This is a constant-time operation.
233 * @param list address of the list head.
234 * @param data the value to append.
235 * @returns #TRUE on success.
238 _dbus_list_append (DBusList **list,
241 if (!_dbus_list_prepend (list, data))
244 /* Now cycle the list forward one so the prepended node is the tail */
245 *list = (*list)->next;
251 * Prepends a value to the list. May return #FALSE
252 * if insufficient memory exists to add a list link.
253 * This is a constant-time operation.
255 * @param list address of the list head.
256 * @param data the value to prepend.
257 * @returns #TRUE on success.
260 _dbus_list_prepend (DBusList **list,
265 link = alloc_link (data);
269 link_before (list, *list, link);
275 * Appends a link to the list.
276 * Cannot fail due to out of memory.
277 * This is a constant-time operation.
279 * @param list address of the list head.
280 * @param link the link to append.
283 _dbus_list_append_link (DBusList **list,
286 _dbus_list_prepend_link (list, link);
288 /* Now cycle the list forward one so the prepended node is the tail */
289 *list = (*list)->next;
295 * Prepends a link to the list.
296 * Cannot fail due to out of memory.
297 * This is a constant-time operation.
299 * @param list address of the list head.
300 * @param link the link to prepend.
303 _dbus_list_prepend_link (DBusList **list,
306 link_before (list, *list, link);
312 * Inserts data into the list before the given existing link.
314 * @param list the list to modify
315 * @param before_this_link existing link to insert before, or #NULL to append
316 * @param data the value to insert
317 * @returns #TRUE on success, #FALSE if memory allocation fails
320 _dbus_list_insert_before (DBusList **list,
321 DBusList *before_this_link,
326 if (before_this_link == NULL)
327 return _dbus_list_append (list, data);
330 link = alloc_link (data);
334 link_before (list, before_this_link, link);
341 * Inserts data into the list after the given existing link.
343 * @param list the list to modify
344 * @param after_this_link existing link to insert after, or #NULL to prepend
345 * @param data the value to insert
346 * @returns #TRUE on success, #FALSE if memory allocation fails
349 _dbus_list_insert_after (DBusList **list,
350 DBusList *after_this_link,
355 if (after_this_link == NULL)
356 return _dbus_list_prepend (list, data);
359 link = alloc_link (data);
363 link_after (list, after_this_link, link);
370 * Removes a value from the list. Only removes the
371 * first value equal to the given data pointer,
372 * even if multiple values exist which match.
373 * This is a linear-time operation.
375 * @param list address of the list head.
376 * @param data the value to remove.
377 * @returns #TRUE if a value was found to remove.
380 _dbus_list_remove (DBusList **list,
388 if (link->data == data)
390 _dbus_list_remove_link (list, link);
394 link = _dbus_list_get_next_link (list, link);
401 * Removes a value from the list. Only removes the
402 * last value equal to the given data pointer,
403 * even if multiple values exist which match.
404 * This is a linear-time operation.
406 * @param list address of the list head.
407 * @param data the value to remove.
408 * @returns #TRUE if a value was found to remove.
411 _dbus_list_remove_last (DBusList **list,
416 link = _dbus_list_get_last_link (list);
420 if (link->data == data)
422 _dbus_list_remove_link (list, link);
426 link = _dbus_list_get_prev_link (list, link);
433 * Removes a link from the list. This is a constant-time operation.
435 * @param list address of the list head.
436 * @param link the list link to remove.
439 _dbus_list_remove_link (DBusList **list,
442 if (link->next == link)
444 /* one-element list */
450 link->prev->next = link->next;
451 link->next->prev = link->prev;
461 * Frees all links in the list and sets the list head to #NULL. Does
462 * not free the data in each link, for obvious reasons. This is a
463 * linear-time operation.
465 * @param list address of the list head.
468 _dbus_list_clear (DBusList **list)
475 DBusList *next = _dbus_list_get_next_link (list, link);
486 * Gets the first link in the list.
487 * This is a constant-time operation.
489 * @param list address of the list head.
490 * @returns the first link, or #NULL for an empty list.
493 _dbus_list_get_first_link (DBusList **list)
499 * Gets the last link in the list.
500 * This is a constant-time operation.
502 * @param list address of the list head.
503 * @returns the last link, or #NULL for an empty list.
506 _dbus_list_get_last_link (DBusList **list)
511 return (*list)->prev;
515 * Gets the last data in the list.
516 * This is a constant-time operation.
518 * @param list address of the list head.
519 * @returns the last data in the list, or #NULL for an empty list.
522 _dbus_list_get_last (DBusList **list)
527 return (*list)->prev->data;
531 * Gets the first data in the list.
532 * This is a constant-time operation.
534 * @param list address of the list head.
535 * @returns the first data in the list, or #NULL for an empty list.
538 _dbus_list_get_first (DBusList **list)
543 return (*list)->data;
547 * Removes the first value in the list and returns it. This is a
548 * constant-time operation.
550 * @param list address of the list head.
551 * @returns the first data in the list, or #NULL for an empty list.
554 _dbus_list_pop_first (DBusList **list)
559 link = _dbus_list_get_first_link (list);
564 _dbus_list_remove_link (list, link);
570 * Removes the last value in the list and returns it. This is a
571 * constant-time operation.
573 * @param list address of the list head.
574 * @returns the last data in the list, or #NULL for an empty list.
577 _dbus_list_pop_last (DBusList **list)
582 link = _dbus_list_get_last_link (list);
587 _dbus_list_remove_link (list, link);
593 * Copies a list. This is a linear-time operation. If there isn't
594 * enough memory to copy the entire list, the destination list will be
597 * @param list address of the head of the list to copy.
598 * @param dest address where the copied list should be placed.
599 * @returns #TRUE on success, #FALSE if not enough memory.
602 _dbus_list_copy (DBusList **list,
607 _dbus_assert (list != dest);
614 if (!_dbus_list_append (dest, link->data))
616 /* free what we have so far */
617 _dbus_list_clear (dest);
621 link = _dbus_list_get_next_link (list, link);
628 * Gets the length of a list. This is a linear-time
631 * @param list address of the head of the list
632 * @returns number of elements in the list.
635 _dbus_list_get_length (DBusList **list)
647 link = _dbus_list_get_next_link (list, link);
654 * Calls the given function for each element in the list. The
655 * function is passed the list element as its first argument, and the
656 * given data as its second argument.
658 * @param list address of the head of the list.
659 * @param function function to call for each element.
660 * @param data extra data for the function.
664 _dbus_list_foreach (DBusList **list,
665 DBusForeachFunction function,
673 DBusList *next = _dbus_list_get_next_link (list, link);
675 (* function) (link->data, data);
683 #ifdef DBUS_BUILD_TESTS
684 #include "dbus-test.h"
688 verify_list (DBusList **list)
698 if (link->next == link)
700 _dbus_assert (link->prev == link);
701 _dbus_assert (*list == link);
709 _dbus_assert (link->prev->next == link);
710 _dbus_assert (link->next->prev == link);
713 while (link != *list);
715 _dbus_assert (length == _dbus_list_get_length (list));
719 is_ascending_sequence (DBusList **list)
724 prev = _DBUS_INT_MIN;
726 link = _dbus_list_get_first_link (list);
729 int v = _DBUS_POINTER_TO_INT (link->data);
736 link = _dbus_list_get_next_link (list, link);
743 is_descending_sequence (DBusList **list)
748 prev = _DBUS_INT_MAX;
750 link = _dbus_list_get_first_link (list);
753 int v = _DBUS_POINTER_TO_INT (link->data);
760 link = _dbus_list_get_next_link (list, link);
767 all_even_values (DBusList **list)
771 link = _dbus_list_get_first_link (list);
774 int v = _DBUS_POINTER_TO_INT (link->data);
779 link = _dbus_list_get_next_link (list, link);
786 all_odd_values (DBusList **list)
790 link = _dbus_list_get_first_link (list);
793 int v = _DBUS_POINTER_TO_INT (link->data);
798 link = _dbus_list_get_next_link (list, link);
805 lists_equal (DBusList **list1,
811 link1 = _dbus_list_get_first_link (list1);
812 link2 = _dbus_list_get_first_link (list2);
813 while (link1 && link2)
815 if (link1->data != link2->data)
818 link1 = _dbus_list_get_next_link (list1, link1);
819 link2 = _dbus_list_get_next_link (list2, link2);
829 * @ingroup DBusListInternals
830 * Unit test for DBusList
831 * @returns #TRUE on success.
834 _dbus_list_test (void)
847 /* Test append and prepend */
852 if (!_dbus_list_append (&list1, _DBUS_INT_TO_POINTER (i)))
853 _dbus_assert_not_reached ("could not allocate for append");
855 if (!_dbus_list_prepend (&list2, _DBUS_INT_TO_POINTER (i)))
856 _dbus_assert_not_reached ("count not allocate for prepend");
859 verify_list (&list1);
860 verify_list (&list2);
862 _dbus_assert (_dbus_list_get_length (&list1) == i);
863 _dbus_assert (_dbus_list_get_length (&list2) == i);
866 _dbus_assert (is_ascending_sequence (&list1));
867 _dbus_assert (is_descending_sequence (&list2));
869 /* Test list clear */
870 _dbus_list_clear (&list1);
871 _dbus_list_clear (&list2);
873 verify_list (&list1);
874 verify_list (&list2);
876 /* Test get_first, get_last, pop_first, pop_last */
881 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (i));
882 _dbus_list_prepend (&list2, _DBUS_INT_TO_POINTER (i));
895 got_data1 = _dbus_list_get_last (&list1);
896 got_data2 = _dbus_list_get_first (&list2);
898 data1 = _dbus_list_pop_last (&list1);
899 data2 = _dbus_list_pop_first (&list2);
901 _dbus_assert (got_data1 == data1);
902 _dbus_assert (got_data2 == data2);
904 _dbus_assert (_DBUS_POINTER_TO_INT (data1) == i);
905 _dbus_assert (_DBUS_POINTER_TO_INT (data2) == i);
907 verify_list (&list1);
908 verify_list (&list2);
910 _dbus_assert (is_ascending_sequence (&list1));
911 _dbus_assert (is_descending_sequence (&list2));
916 _dbus_assert (list1 == NULL);
917 _dbus_assert (list2 == NULL);
924 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (i));
925 _dbus_list_prepend (&list2, _DBUS_INT_TO_POINTER (i));
928 verify_list (&list1);
929 verify_list (&list2);
931 _dbus_assert (_dbus_list_get_length (&list1) == i);
932 _dbus_assert (_dbus_list_get_length (&list2) == i);
935 _dbus_assert (is_ascending_sequence (&list1));
936 _dbus_assert (is_descending_sequence (&list2));
939 link2 = _dbus_list_get_first_link (&list2);
940 while (link2 != NULL)
942 verify_list (&link2); /* pretend this link is the head */
944 _dbus_assert (_DBUS_POINTER_TO_INT (link2->data) == i);
946 link2 = _dbus_list_get_next_link (&list2, link2);
951 link1 = _dbus_list_get_first_link (&list1);
952 while (link1 != NULL)
954 verify_list (&link1); /* pretend this link is the head */
956 _dbus_assert (_DBUS_POINTER_TO_INT (link1->data) == i);
958 link1 = _dbus_list_get_next_link (&list1, link1);
963 link1 = _dbus_list_get_last_link (&list1);
964 while (link1 != NULL)
966 verify_list (&link1); /* pretend this link is the head */
968 _dbus_assert (_DBUS_POINTER_TO_INT (link1->data) == i);
970 link1 = _dbus_list_get_prev_link (&list1, link1);
974 _dbus_list_clear (&list1);
975 _dbus_list_clear (&list2);
982 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (i));
983 _dbus_list_prepend (&list2, _DBUS_INT_TO_POINTER (i));
992 if (!_dbus_list_remove (&list1, _DBUS_INT_TO_POINTER (i)))
993 _dbus_assert_not_reached ("element should have been in list");
994 if (!_dbus_list_remove (&list2, _DBUS_INT_TO_POINTER (i)))
995 _dbus_assert_not_reached ("element should have been in list");
997 verify_list (&list1);
998 verify_list (&list2);
1003 _dbus_assert (all_odd_values (&list1));
1004 _dbus_assert (all_odd_values (&list2));
1006 _dbus_list_clear (&list1);
1007 _dbus_list_clear (&list2);
1009 /* test removing the other half of the elements */
1014 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (i));
1015 _dbus_list_prepend (&list2, _DBUS_INT_TO_POINTER (i));
1024 if (!_dbus_list_remove (&list1, _DBUS_INT_TO_POINTER (i)))
1025 _dbus_assert_not_reached ("element should have been in list");
1026 if (!_dbus_list_remove (&list2, _DBUS_INT_TO_POINTER (i)))
1027 _dbus_assert_not_reached ("element should have been in list");
1029 verify_list (&list1);
1030 verify_list (&list2);
1035 _dbus_assert (all_even_values (&list1));
1036 _dbus_assert (all_even_values (&list2));
1038 /* clear list using remove_link */
1039 while (list1 != NULL)
1041 _dbus_list_remove_link (&list1, list1);
1042 verify_list (&list1);
1044 while (list2 != NULL)
1046 _dbus_list_remove_link (&list2, list2);
1047 verify_list (&list2);
1050 /* Test remove link more generally */
1054 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (i));
1055 _dbus_list_prepend (&list2, _DBUS_INT_TO_POINTER (i));
1060 link2 = _dbus_list_get_first_link (&list2);
1061 while (link2 != NULL)
1063 DBusList *next = _dbus_list_get_next_link (&list2, link2);
1065 _dbus_assert (_DBUS_POINTER_TO_INT (link2->data) == i);
1068 _dbus_list_remove_link (&list2, link2);
1070 verify_list (&list2);
1076 _dbus_assert (all_odd_values (&list2));
1077 _dbus_list_clear (&list2);
1080 link1 = _dbus_list_get_first_link (&list1);
1081 while (link1 != NULL)
1083 DBusList *next = _dbus_list_get_next_link (&list1, link1);
1085 _dbus_assert (_DBUS_POINTER_TO_INT (link1->data) == i);
1088 _dbus_list_remove_link (&list1, link1);
1090 verify_list (&list1);
1096 _dbus_assert (all_even_values (&list1));
1097 _dbus_list_clear (&list1);
1099 /* Test copying a list */
1103 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (i));
1104 _dbus_list_prepend (&list2, _DBUS_INT_TO_POINTER (i));
1108 /* bad pointers, because they are allowed in the copy dest */
1109 copy1 = _DBUS_INT_TO_POINTER (0x342234);
1110 copy2 = _DBUS_INT_TO_POINTER (23);
1112 _dbus_list_copy (&list1, ©1);
1113 verify_list (&list1);
1114 verify_list (©1);
1115 _dbus_assert (lists_equal (&list1, ©1));
1117 _dbus_list_copy (&list2, ©2);
1118 verify_list (&list2);
1119 verify_list (©2);
1120 _dbus_assert (lists_equal (&list2, ©2));
1122 /* Now test copying empty lists */
1123 _dbus_list_clear (&list1);
1124 _dbus_list_clear (&list2);
1125 _dbus_list_clear (©1);
1126 _dbus_list_clear (©2);
1128 /* bad pointers, because they are allowed in the copy dest */
1129 copy1 = _DBUS_INT_TO_POINTER (0x342234);
1130 copy2 = _DBUS_INT_TO_POINTER (23);
1132 _dbus_list_copy (&list1, ©1);
1133 verify_list (&list1);
1134 verify_list (©1);
1135 _dbus_assert (lists_equal (&list1, ©1));
1137 _dbus_list_copy (&list2, ©2);
1138 verify_list (&list2);
1139 verify_list (©2);
1140 _dbus_assert (lists_equal (&list2, ©2));
1142 _dbus_list_clear (&list1);
1143 _dbus_list_clear (&list2);
1145 /* insert_before on empty list */
1146 _dbus_list_insert_before (&list1, NULL,
1147 _DBUS_INT_TO_POINTER (0));
1148 verify_list (&list1);
1150 /* inserting before first element */
1151 _dbus_list_insert_before (&list1, list1,
1152 _DBUS_INT_TO_POINTER (2));
1153 verify_list (&list1);
1154 _dbus_assert (is_descending_sequence (&list1));
1156 /* inserting in the middle */
1157 _dbus_list_insert_before (&list1, list1->next,
1158 _DBUS_INT_TO_POINTER (1));
1159 verify_list (&list1);
1160 _dbus_assert (is_descending_sequence (&list1));
1162 /* using insert_before to append */
1163 _dbus_list_insert_before (&list1, NULL,
1164 _DBUS_INT_TO_POINTER (-1));
1165 verify_list (&list1);
1166 _dbus_assert (is_descending_sequence (&list1));
1168 _dbus_list_clear (&list1);
1170 /* insert_after on empty list */
1171 _dbus_list_insert_after (&list1, NULL,
1172 _DBUS_INT_TO_POINTER (0));
1173 verify_list (&list1);
1175 /* inserting after first element */
1176 _dbus_list_insert_after (&list1, list1,
1177 _DBUS_INT_TO_POINTER (1));
1178 verify_list (&list1);
1179 _dbus_assert (is_ascending_sequence (&list1));
1181 /* inserting at the end */
1182 _dbus_list_insert_after (&list1, list1->next,
1183 _DBUS_INT_TO_POINTER (2));
1184 verify_list (&list1);
1185 _dbus_assert (is_ascending_sequence (&list1));
1187 /* using insert_after to prepend */
1188 _dbus_list_insert_after (&list1, NULL,
1189 _DBUS_INT_TO_POINTER (-1));
1190 verify_list (&list1);
1191 _dbus_assert (is_ascending_sequence (&list1));
1193 _dbus_list_clear (&list1);
1195 /* using remove_last */
1196 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (2));
1197 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (1));
1198 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (3));
1200 _dbus_list_remove_last (&list1, _DBUS_INT_TO_POINTER (2));
1202 verify_list (&list1);
1203 _dbus_assert (is_ascending_sequence (&list1));
1205 _dbus_list_clear (&list1);