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 DBusStaticMutex list_pool_lock = DBUS_STATIC_MUTEX_INIT;
41 * @defgroup DBusListInternals Linked list implementation details
42 * @ingroup DBusInternals
43 * @brief DBusList implementation details
45 * The guts of DBusList.
50 /* the mem pool is probably a speed hit, with the thread
51 * lock, though it does still save memory - unknown.
54 alloc_link (void *data)
58 if (!dbus_static_mutex_lock (&list_pool_lock))
63 list_pool = _dbus_mem_pool_new (sizeof (DBusList), TRUE);
65 if (list_pool == NULL)
67 dbus_static_mutex_unlock (&list_pool_lock);
72 link = _dbus_mem_pool_alloc (list_pool);
75 dbus_static_mutex_unlock (&list_pool_lock);
81 free_link (DBusList *link)
83 dbus_static_mutex_lock (&list_pool_lock);
84 _dbus_mem_pool_dealloc (list_pool, link);
85 dbus_static_mutex_unlock (&list_pool_lock);
89 link_before (DBusList **list,
90 DBusList *before_this_link,
101 link->next = before_this_link;
102 link->prev = before_this_link->prev;
103 before_this_link->prev = link;
104 link->prev->next = link;
106 if (before_this_link == *list)
112 link_after (DBusList **list,
113 DBusList *after_this_link,
124 link->prev = after_this_link;
125 link->next = after_this_link->next;
126 after_this_link->next = link;
127 link->next->prev = link;
134 * @addtogroup DBusList
141 * A node in a linked list.
143 * DBusList is a circular list; that is, the tail of the list
144 * points back to the head of the list. The empty list is
145 * represented by a #NULL pointer.
149 * @def _dbus_list_get_next_link
151 * Gets the next link in the list, or #NULL if
152 * there are no more links. Used for iteration.
156 * link = _dbus_list_get_first_link (&list);
157 * while (link != NULL)
159 * printf ("value is %p\n", link->data);
160 * link = _dbus_list_get_next_link (&link);
164 * @param list address of the list head.
165 * @param link current link.
166 * @returns the next link, or %NULL if none.
171 * @def _dbus_list_get_prev_link
173 * Gets the previous link in the list, or #NULL if
174 * there are no more links. Used for iteration.
178 * link = _dbus_list_get_last_link (&list);
179 * while (link != NULL)
181 * printf ("value is %p\n", link->data);
182 * link = _dbus_list_get_prev_link (&link);
186 * @param list address of the list head.
187 * @param link current link.
188 * @returns the previous link, or %NULL if none.
193 * Appends a value to the list. May return #FALSE
194 * if insufficient memory exists to add a list link.
195 * This is a constant-time operation.
197 * @param list address of the list head.
198 * @param data the value to append.
199 * @returns #TRUE on success.
202 _dbus_list_append (DBusList **list,
205 if (!_dbus_list_prepend (list, data))
208 /* Now cycle the list forward one so the prepended node is the tail */
209 *list = (*list)->next;
215 * Prepends a value to the list. May return #FALSE
216 * if insufficient memory exists to add a list link.
217 * This is a constant-time operation.
219 * @param list address of the list head.
220 * @param data the value to prepend.
221 * @returns #TRUE on success.
224 _dbus_list_prepend (DBusList **list,
229 link = alloc_link (data);
233 link_before (list, *list, link);
239 * Inserts data into the list before the given existing link.
241 * @param list the list to modify
242 * @param before_this_link existing link to insert before, or #NULL to append
243 * @param data the value to insert
244 * @returns #TRUE on success, #FALSE if memory allocation fails
247 _dbus_list_insert_before (DBusList **list,
248 DBusList *before_this_link,
253 if (before_this_link == NULL)
254 return _dbus_list_append (list, data);
257 link = alloc_link (data);
261 link_before (list, before_this_link, link);
268 * Inserts data into the list after the given existing link.
270 * @param list the list to modify
271 * @param after_this_link existing link to insert after, or #NULL to prepend
272 * @param data the value to insert
273 * @returns #TRUE on success, #FALSE if memory allocation fails
276 _dbus_list_insert_after (DBusList **list,
277 DBusList *after_this_link,
282 if (after_this_link == NULL)
283 return _dbus_list_prepend (list, data);
286 link = alloc_link (data);
290 link_after (list, after_this_link, link);
297 * Removes a value from the list. Only removes the
298 * first value equal to the given data pointer,
299 * even if multiple values exist which match.
300 * This is a linear-time operation.
302 * @param list address of the list head.
303 * @param data the value to remove.
304 * @returns #TRUE if a value was found to remove.
307 _dbus_list_remove (DBusList **list,
315 if (link->data == data)
317 _dbus_list_remove_link (list, link);
321 link = _dbus_list_get_next_link (list, link);
328 * Removes a value from the list. Only removes the
329 * last value equal to the given data pointer,
330 * even if multiple values exist which match.
331 * This is a linear-time operation.
333 * @param list address of the list head.
334 * @param data the value to remove.
335 * @returns #TRUE if a value was found to remove.
338 _dbus_list_remove_last (DBusList **list,
343 link = _dbus_list_get_last_link (list);
346 if (link->data == data)
348 _dbus_list_remove_link (list, link);
352 link = _dbus_list_get_prev_link (list, link);
359 * Removes a link from the list. This is a constant-time operation.
361 * @param list address of the list head.
362 * @param link the list link to remove.
365 _dbus_list_remove_link (DBusList **list,
368 if (link->next == link)
370 /* one-element list */
376 link->prev->next = link->next;
377 link->next->prev = link->prev;
387 * Frees all links in the list and sets the list head to #NULL. Does
388 * not free the data in each link, for obvious reasons. This is a
389 * linear-time operation.
391 * @param list address of the list head.
394 _dbus_list_clear (DBusList **list)
401 DBusList *next = _dbus_list_get_next_link (list, link);
412 * Gets the first link in the list.
413 * This is a constant-time operation.
415 * @param list address of the list head.
416 * @returns the first link, or #NULL for an empty list.
419 _dbus_list_get_first_link (DBusList **list)
425 * Gets the last link in the list.
426 * This is a constant-time operation.
428 * @param list address of the list head.
429 * @returns the last link, or #NULL for an empty list.
432 _dbus_list_get_last_link (DBusList **list)
437 return (*list)->prev;
441 * Gets the last data in the list.
442 * This is a constant-time operation.
444 * @param list address of the list head.
445 * @returns the last data in the list, or #NULL for an empty list.
448 _dbus_list_get_last (DBusList **list)
453 return (*list)->prev->data;
457 * Gets the first data in the list.
458 * This is a constant-time operation.
460 * @param list address of the list head.
461 * @returns the first data in the list, or #NULL for an empty list.
464 _dbus_list_get_first (DBusList **list)
469 return (*list)->data;
473 * Removes the first value in the list and returns it. This is a
474 * constant-time operation.
476 * @param list address of the list head.
477 * @returns the first data in the list, or #NULL for an empty list.
480 _dbus_list_pop_first (DBusList **list)
485 link = _dbus_list_get_first_link (list);
490 _dbus_list_remove_link (list, link);
496 * Removes the last value in the list and returns it. This is a
497 * constant-time operation.
499 * @param list address of the list head.
500 * @returns the last data in the list, or #NULL for an empty list.
503 _dbus_list_pop_last (DBusList **list)
508 link = _dbus_list_get_last_link (list);
513 _dbus_list_remove_link (list, link);
519 * Copies a list. This is a linear-time operation. If there isn't
520 * enough memory to copy the entire list, the destination list will be
523 * @param list address of the head of the list to copy.
524 * @param dest address where the copied list should be placed.
525 * @returns #TRUE on success, #FALSE if not enough memory.
528 _dbus_list_copy (DBusList **list,
533 _dbus_assert (list != dest);
540 if (!_dbus_list_append (dest, link->data))
542 /* free what we have so far */
543 _dbus_list_clear (dest);
547 link = _dbus_list_get_next_link (list, link);
554 * Gets the length of a list. This is a linear-time
557 * @param list address of the head of the list
558 * @returns number of elements in the list.
561 _dbus_list_get_length (DBusList **list)
573 link = _dbus_list_get_next_link (list, link);
580 * Calls the given function for each element in the list. The
581 * function is passed the list element as its first argument, and the
582 * given data as its second argument.
584 * @param list address of the head of the list.
585 * @param function function to call for each element.
586 * @param data extra data for the function.
590 _dbus_list_foreach (DBusList **list,
591 DBusForeachFunction function,
599 DBusList *next = _dbus_list_get_next_link (list, link);
601 (* function) (link->data, data);
609 #ifdef DBUS_BUILD_TESTS
610 #include "dbus-test.h"
614 verify_list (DBusList **list)
624 if (link->next == link)
626 _dbus_assert (link->prev == link);
627 _dbus_assert (*list == link);
635 _dbus_assert (link->prev->next == link);
636 _dbus_assert (link->next->prev == link);
639 while (link != *list);
641 _dbus_assert (length == _dbus_list_get_length (list));
645 is_ascending_sequence (DBusList **list)
650 prev = _DBUS_INT_MIN;
652 link = _dbus_list_get_first_link (list);
655 int v = _DBUS_POINTER_TO_INT (link->data);
662 link = _dbus_list_get_next_link (list, link);
669 is_descending_sequence (DBusList **list)
674 prev = _DBUS_INT_MAX;
676 link = _dbus_list_get_first_link (list);
679 int v = _DBUS_POINTER_TO_INT (link->data);
686 link = _dbus_list_get_next_link (list, link);
693 all_even_values (DBusList **list)
697 link = _dbus_list_get_first_link (list);
700 int v = _DBUS_POINTER_TO_INT (link->data);
705 link = _dbus_list_get_next_link (list, link);
712 all_odd_values (DBusList **list)
716 link = _dbus_list_get_first_link (list);
719 int v = _DBUS_POINTER_TO_INT (link->data);
724 link = _dbus_list_get_next_link (list, link);
731 lists_equal (DBusList **list1,
737 link1 = _dbus_list_get_first_link (list1);
738 link2 = _dbus_list_get_first_link (list2);
739 while (link1 && link2)
741 if (link1->data != link2->data)
744 link1 = _dbus_list_get_next_link (list1, link1);
745 link2 = _dbus_list_get_next_link (list2, link2);
755 * @ingroup DBusListInternals
756 * Unit test for DBusList
757 * @returns #TRUE on success.
760 _dbus_list_test (void)
773 /* Test append and prepend */
778 if (!_dbus_list_append (&list1, _DBUS_INT_TO_POINTER (i)))
779 _dbus_assert_not_reached ("could not allocate for append");
781 if (!_dbus_list_prepend (&list2, _DBUS_INT_TO_POINTER (i)))
782 _dbus_assert_not_reached ("count not allocate for prepend");
785 verify_list (&list1);
786 verify_list (&list2);
788 _dbus_assert (_dbus_list_get_length (&list1) == i);
789 _dbus_assert (_dbus_list_get_length (&list2) == i);
792 _dbus_assert (is_ascending_sequence (&list1));
793 _dbus_assert (is_descending_sequence (&list2));
795 /* Test list clear */
796 _dbus_list_clear (&list1);
797 _dbus_list_clear (&list2);
799 verify_list (&list1);
800 verify_list (&list2);
802 /* Test get_first, get_last, pop_first, pop_last */
807 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (i));
808 _dbus_list_prepend (&list2, _DBUS_INT_TO_POINTER (i));
821 got_data1 = _dbus_list_get_last (&list1);
822 got_data2 = _dbus_list_get_first (&list2);
824 data1 = _dbus_list_pop_last (&list1);
825 data2 = _dbus_list_pop_first (&list2);
827 _dbus_assert (got_data1 == data1);
828 _dbus_assert (got_data2 == data2);
830 _dbus_assert (_DBUS_POINTER_TO_INT (data1) == i);
831 _dbus_assert (_DBUS_POINTER_TO_INT (data2) == i);
833 verify_list (&list1);
834 verify_list (&list2);
836 _dbus_assert (is_ascending_sequence (&list1));
837 _dbus_assert (is_descending_sequence (&list2));
842 _dbus_assert (list1 == NULL);
843 _dbus_assert (list2 == NULL);
850 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (i));
851 _dbus_list_prepend (&list2, _DBUS_INT_TO_POINTER (i));
854 verify_list (&list1);
855 verify_list (&list2);
857 _dbus_assert (_dbus_list_get_length (&list1) == i);
858 _dbus_assert (_dbus_list_get_length (&list2) == i);
861 _dbus_assert (is_ascending_sequence (&list1));
862 _dbus_assert (is_descending_sequence (&list2));
865 link2 = _dbus_list_get_first_link (&list2);
866 while (link2 != NULL)
868 verify_list (&link2); /* pretend this link is the head */
870 _dbus_assert (_DBUS_POINTER_TO_INT (link2->data) == i);
872 link2 = _dbus_list_get_next_link (&list2, link2);
877 link1 = _dbus_list_get_first_link (&list1);
878 while (link1 != NULL)
880 verify_list (&link1); /* pretend this link is the head */
882 _dbus_assert (_DBUS_POINTER_TO_INT (link1->data) == i);
884 link1 = _dbus_list_get_next_link (&list1, link1);
888 _dbus_list_clear (&list1);
889 _dbus_list_clear (&list2);
896 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (i));
897 _dbus_list_prepend (&list2, _DBUS_INT_TO_POINTER (i));
906 if (!_dbus_list_remove (&list1, _DBUS_INT_TO_POINTER (i)))
907 _dbus_assert_not_reached ("element should have been in list");
908 if (!_dbus_list_remove (&list2, _DBUS_INT_TO_POINTER (i)))
909 _dbus_assert_not_reached ("element should have been in list");
911 verify_list (&list1);
912 verify_list (&list2);
917 _dbus_assert (all_odd_values (&list1));
918 _dbus_assert (all_odd_values (&list2));
920 _dbus_list_clear (&list1);
921 _dbus_list_clear (&list2);
923 /* test removing the other half of the elements */
928 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (i));
929 _dbus_list_prepend (&list2, _DBUS_INT_TO_POINTER (i));
938 if (!_dbus_list_remove (&list1, _DBUS_INT_TO_POINTER (i)))
939 _dbus_assert_not_reached ("element should have been in list");
940 if (!_dbus_list_remove (&list2, _DBUS_INT_TO_POINTER (i)))
941 _dbus_assert_not_reached ("element should have been in list");
943 verify_list (&list1);
944 verify_list (&list2);
949 _dbus_assert (all_even_values (&list1));
950 _dbus_assert (all_even_values (&list2));
952 /* clear list using remove_link */
953 while (list1 != NULL)
955 _dbus_list_remove_link (&list1, list1);
956 verify_list (&list1);
958 while (list2 != NULL)
960 _dbus_list_remove_link (&list2, list2);
961 verify_list (&list2);
964 /* Test remove link more generally */
968 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (i));
969 _dbus_list_prepend (&list2, _DBUS_INT_TO_POINTER (i));
974 link2 = _dbus_list_get_first_link (&list2);
975 while (link2 != NULL)
977 DBusList *next = _dbus_list_get_next_link (&list2, link2);
979 _dbus_assert (_DBUS_POINTER_TO_INT (link2->data) == i);
982 _dbus_list_remove_link (&list2, link2);
984 verify_list (&list2);
990 _dbus_assert (all_odd_values (&list2));
991 _dbus_list_clear (&list2);
994 link1 = _dbus_list_get_first_link (&list1);
995 while (link1 != NULL)
997 DBusList *next = _dbus_list_get_next_link (&list1, link1);
999 _dbus_assert (_DBUS_POINTER_TO_INT (link1->data) == i);
1002 _dbus_list_remove_link (&list1, link1);
1004 verify_list (&list1);
1010 _dbus_assert (all_even_values (&list1));
1011 _dbus_list_clear (&list1);
1013 /* Test copying a list */
1017 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (i));
1018 _dbus_list_prepend (&list2, _DBUS_INT_TO_POINTER (i));
1022 /* bad pointers, because they are allowed in the copy dest */
1023 copy1 = _DBUS_INT_TO_POINTER (0x342234);
1024 copy2 = _DBUS_INT_TO_POINTER (23);
1026 _dbus_list_copy (&list1, ©1);
1027 verify_list (&list1);
1028 verify_list (©1);
1029 _dbus_assert (lists_equal (&list1, ©1));
1031 _dbus_list_copy (&list2, ©2);
1032 verify_list (&list2);
1033 verify_list (©2);
1034 _dbus_assert (lists_equal (&list2, ©2));
1036 /* Now test copying empty lists */
1037 _dbus_list_clear (&list1);
1038 _dbus_list_clear (&list2);
1039 _dbus_list_clear (©1);
1040 _dbus_list_clear (©2);
1042 /* bad pointers, because they are allowed in the copy dest */
1043 copy1 = _DBUS_INT_TO_POINTER (0x342234);
1044 copy2 = _DBUS_INT_TO_POINTER (23);
1046 _dbus_list_copy (&list1, ©1);
1047 verify_list (&list1);
1048 verify_list (©1);
1049 _dbus_assert (lists_equal (&list1, ©1));
1051 _dbus_list_copy (&list2, ©2);
1052 verify_list (&list2);
1053 verify_list (©2);
1054 _dbus_assert (lists_equal (&list2, ©2));
1056 _dbus_list_clear (&list1);
1057 _dbus_list_clear (&list2);
1059 /* insert_before on empty list */
1060 _dbus_list_insert_before (&list1, NULL,
1061 _DBUS_INT_TO_POINTER (0));
1062 verify_list (&list1);
1064 /* inserting before first element */
1065 _dbus_list_insert_before (&list1, list1,
1066 _DBUS_INT_TO_POINTER (2));
1067 verify_list (&list1);
1068 _dbus_assert (is_descending_sequence (&list1));
1070 /* inserting in the middle */
1071 _dbus_list_insert_before (&list1, list1->next,
1072 _DBUS_INT_TO_POINTER (1));
1073 verify_list (&list1);
1074 _dbus_assert (is_descending_sequence (&list1));
1076 /* using insert_before to append */
1077 _dbus_list_insert_before (&list1, NULL,
1078 _DBUS_INT_TO_POINTER (-1));
1079 verify_list (&list1);
1080 _dbus_assert (is_descending_sequence (&list1));
1082 _dbus_list_clear (&list1);
1084 /* insert_after on empty list */
1085 _dbus_list_insert_after (&list1, NULL,
1086 _DBUS_INT_TO_POINTER (0));
1087 verify_list (&list1);
1089 /* inserting after first element */
1090 _dbus_list_insert_after (&list1, list1,
1091 _DBUS_INT_TO_POINTER (1));
1092 verify_list (&list1);
1093 _dbus_assert (is_ascending_sequence (&list1));
1095 /* inserting at the end */
1096 _dbus_list_insert_after (&list1, list1->next,
1097 _DBUS_INT_TO_POINTER (2));
1098 verify_list (&list1);
1099 _dbus_assert (is_ascending_sequence (&list1));
1101 /* using insert_after to prepend */
1102 _dbus_list_insert_after (&list1, NULL,
1103 _DBUS_INT_TO_POINTER (-1));
1104 verify_list (&list1);
1105 _dbus_assert (is_ascending_sequence (&list1));
1107 _dbus_list_clear (&list1);