1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
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 2.1
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "dbus-internals.h"
25 #include "dbus-list.h"
26 #include "dbus-mempool.h"
27 #include "dbus-threads-internal.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 _DBUS_DEFINE_GLOBAL_LOCK (list);
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)
60 if (list_pool == NULL)
62 list_pool = _dbus_mem_pool_new (sizeof (DBusList), TRUE);
64 if (list_pool == NULL)
70 link = _dbus_mem_pool_alloc (list_pool);
73 _dbus_mem_pool_free (list_pool);
81 link = _dbus_mem_pool_alloc (list_pool);
93 free_link (DBusList *link)
96 if (_dbus_mem_pool_dealloc (list_pool, link))
98 _dbus_mem_pool_free (list_pool);
106 link_before (DBusList **list,
107 DBusList *before_this_link,
118 link->next = before_this_link;
119 link->prev = before_this_link->prev;
120 before_this_link->prev = link;
121 link->prev->next = link;
123 if (before_this_link == *list)
129 link_after (DBusList **list,
130 DBusList *after_this_link,
141 link->prev = after_this_link;
142 link->next = after_this_link->next;
143 after_this_link->next = link;
144 link->next->prev = link;
151 * @addtogroup DBusList
158 * A node in a linked list.
160 * DBusList is a circular list; that is, the tail of the list
161 * points back to the head of the list. The empty list is
162 * represented by a #NULL pointer.
166 * @def _dbus_list_get_next_link
168 * Gets the next link in the list, or #NULL if
169 * there are no more links. Used for iteration.
173 * link = _dbus_list_get_first_link (&list);
174 * while (link != NULL)
176 * printf ("value is %p\n", link->data);
177 * link = _dbus_list_get_next_link (&link);
181 * @param list address of the list head.
182 * @param link current link.
183 * @returns the next link, or %NULL if none.
188 * @def _dbus_list_get_prev_link
190 * Gets the previous link in the list, or #NULL if
191 * there are no more links. Used for iteration.
195 * link = _dbus_list_get_last_link (&list);
196 * while (link != NULL)
198 * printf ("value is %p\n", link->data);
199 * link = _dbus_list_get_prev_link (&link);
203 * @param list address of the list head.
204 * @param link current link.
205 * @returns the previous link, or %NULL if none.
210 * Allocates a linked list node. Useful for preallocating
211 * nodes and using _dbus_list_append_link() to avoid
214 * @param data the value to store in the link.
215 * @returns a newly allocated link.
218 _dbus_list_alloc_link (void *data)
220 return alloc_link (data);
224 * Frees a linked list node allocated with _dbus_list_alloc_link.
225 * Does not free the data in the node.
227 * @param link the list node
230 _dbus_list_free_link (DBusList *link)
237 * Appends a value to the list. May return #FALSE
238 * if insufficient memory exists to add a list link.
239 * This is a constant-time operation.
241 * @param list address of the list head.
242 * @param data the value to append.
243 * @returns #TRUE on success.
246 _dbus_list_append (DBusList **list,
249 if (!_dbus_list_prepend (list, data))
252 /* Now cycle the list forward one so the prepended node is the tail */
253 *list = (*list)->next;
259 * Prepends a value to the list. May return #FALSE
260 * if insufficient memory exists to add a list link.
261 * This is a constant-time operation.
263 * @param list address of the list head.
264 * @param data the value to prepend.
265 * @returns #TRUE on success.
268 _dbus_list_prepend (DBusList **list,
273 link = alloc_link (data);
277 link_before (list, *list, link);
283 * Appends a link to the list.
284 * Cannot fail due to out of memory.
285 * This is a constant-time operation.
287 * @param list address of the list head.
288 * @param link the link to append.
291 _dbus_list_append_link (DBusList **list,
294 _dbus_list_prepend_link (list, link);
296 /* Now cycle the list forward one so the prepended node is the tail */
297 *list = (*list)->next;
301 * Prepends a link to the list.
302 * Cannot fail due to out of memory.
303 * This is a constant-time operation.
305 * @param list address of the list head.
306 * @param link the link to prepend.
309 _dbus_list_prepend_link (DBusList **list,
312 link_before (list, *list, link);
315 #ifdef DBUS_BUILD_TESTS
317 * Inserts data into the list before the given existing link.
319 * @param list the list to modify
320 * @param before_this_link existing link to insert before, or #NULL to append
321 * @param data the value to insert
322 * @returns #TRUE on success, #FALSE if memory allocation fails
325 _dbus_list_insert_before (DBusList **list,
326 DBusList *before_this_link,
331 if (before_this_link == NULL)
332 return _dbus_list_append (list, data);
335 link = alloc_link (data);
339 link_before (list, before_this_link, link);
344 #endif /* DBUS_BUILD_TESTS */
347 * Inserts data into the list after the given existing link.
349 * @param list the list to modify
350 * @param after_this_link existing link to insert after, or #NULL to prepend
351 * @param data the value to insert
352 * @returns #TRUE on success, #FALSE if memory allocation fails
355 _dbus_list_insert_after (DBusList **list,
356 DBusList *after_this_link,
361 if (after_this_link == NULL)
362 return _dbus_list_prepend (list, data);
365 link = alloc_link (data);
369 link_after (list, after_this_link, link);
376 * Inserts a link into the list before the given existing link.
378 * @param list the list to modify
379 * @param before_this_link existing link to insert before, or #NULL to append
380 * @param link the link to insert
383 _dbus_list_insert_before_link (DBusList **list,
384 DBusList *before_this_link,
387 if (before_this_link == NULL)
388 _dbus_list_append_link (list, link);
390 link_before (list, before_this_link, link);
394 * Inserts a link into the list after the given existing link.
396 * @param list the list to modify
397 * @param after_this_link existing link to insert after, or #NULL to prepend
398 * @param link the link to insert
401 _dbus_list_insert_after_link (DBusList **list,
402 DBusList *after_this_link,
405 if (after_this_link == NULL)
406 _dbus_list_prepend_link (list, link);
408 link_after (list, after_this_link, link);
412 * Removes a value from the list. Only removes the
413 * first value equal to the given data pointer,
414 * even if multiple values exist which match.
415 * This is a linear-time operation.
417 * @param list address of the list head.
418 * @param data the value to remove.
419 * @returns #TRUE if a value was found to remove.
422 _dbus_list_remove (DBusList **list,
430 if (link->data == data)
432 _dbus_list_remove_link (list, link);
436 link = _dbus_list_get_next_link (list, link);
443 * Removes a value from the list. Only removes the
444 * last value equal to the given data pointer,
445 * even if multiple values exist which match.
446 * This is a linear-time operation.
448 * @param list address of the list head.
449 * @param data the value to remove.
450 * @returns #TRUE if a value was found to remove.
453 _dbus_list_remove_last (DBusList **list,
458 link = _dbus_list_find_last (list, data);
461 _dbus_list_remove_link (list, link);
469 * Finds a value in the list. Returns the last link
470 * with value equal to the given data pointer.
471 * This is a linear-time operation.
472 * Returns #NULL if no value found that matches.
474 * @param list address of the list head.
475 * @param data the value to find.
476 * @returns the link if found
479 _dbus_list_find_last (DBusList **list,
484 link = _dbus_list_get_last_link (list);
488 if (link->data == data)
491 link = _dbus_list_get_prev_link (list, link);
498 * Removes the given link from the list, but doesn't
499 * free it. _dbus_list_remove_link() both removes the
500 * link and also frees it.
502 * @param list the list
503 * @param link the link in the list
506 _dbus_list_unlink (DBusList **list,
509 if (link->next == link)
511 /* one-element list */
516 link->prev->next = link->next;
517 link->next->prev = link->prev;
528 * Removes a link from the list. This is a constant-time operation.
530 * @param list address of the list head.
531 * @param link the list link to remove.
534 _dbus_list_remove_link (DBusList **list,
537 _dbus_list_unlink (list, link);
542 * Frees all links in the list and sets the list head to #NULL. Does
543 * not free the data in each link, for obvious reasons. This is a
544 * linear-time operation.
546 * @param list address of the list head.
549 _dbus_list_clear (DBusList **list)
556 DBusList *next = _dbus_list_get_next_link (list, link);
567 * Gets the first link in the list.
568 * This is a constant-time operation.
570 * @param list address of the list head.
571 * @returns the first link, or #NULL for an empty list.
574 _dbus_list_get_first_link (DBusList **list)
580 * Gets the last link in the list.
581 * This is a constant-time operation.
583 * @param list address of the list head.
584 * @returns the last link, or #NULL for an empty list.
587 _dbus_list_get_last_link (DBusList **list)
592 return (*list)->prev;
596 * Gets the last data in the list.
597 * This is a constant-time operation.
599 * @param list address of the list head.
600 * @returns the last data in the list, or #NULL for an empty list.
603 _dbus_list_get_last (DBusList **list)
608 return (*list)->prev->data;
612 * Gets the first data in the list.
613 * This is a constant-time operation.
615 * @param list address of the list head.
616 * @returns the first data in the list, or #NULL for an empty list.
619 _dbus_list_get_first (DBusList **list)
624 return (*list)->data;
628 * Removes the first link in the list and returns it. This is a
629 * constant-time operation.
631 * @param list address of the list head.
632 * @returns the first link in the list, or #NULL for an empty list.
635 _dbus_list_pop_first_link (DBusList **list)
639 link = _dbus_list_get_first_link (list);
643 _dbus_list_unlink (list, link);
649 * Removes the first value in the list and returns it. This is a
650 * constant-time operation.
652 * @param list address of the list head.
653 * @returns the first data in the list, or #NULL for an empty list.
656 _dbus_list_pop_first (DBusList **list)
661 link = _dbus_list_get_first_link (list);
666 _dbus_list_remove_link (list, link);
672 * Removes the last value in the list and returns it. This is a
673 * constant-time operation.
675 * @param list address of the list head.
676 * @returns the last data in the list, or #NULL for an empty list.
679 _dbus_list_pop_last (DBusList **list)
684 link = _dbus_list_get_last_link (list);
689 _dbus_list_remove_link (list, link);
694 #ifdef DBUS_BUILD_TESTS
696 * Removes the last link in the list and returns it. This is a
697 * constant-time operation.
699 * @param list address of the list head.
700 * @returns the last link in the list, or #NULL for an empty list.
703 _dbus_list_pop_last_link (DBusList **list)
707 link = _dbus_list_get_last_link (list);
711 _dbus_list_unlink (list, link);
715 #endif /* DBUS_BUILD_TESTS */
718 * Copies a list. This is a linear-time operation. If there isn't
719 * enough memory to copy the entire list, the destination list will be
722 * @param list address of the head of the list to copy.
723 * @param dest address where the copied list should be placed.
724 * @returns #TRUE on success, #FALSE if not enough memory.
727 _dbus_list_copy (DBusList **list,
732 _dbus_assert (list != dest);
739 if (!_dbus_list_append (dest, link->data))
741 /* free what we have so far */
742 _dbus_list_clear (dest);
746 link = _dbus_list_get_next_link (list, link);
753 * Gets the length of a list. This is a linear-time
756 * @param list address of the head of the list
757 * @returns number of elements in the list.
760 _dbus_list_get_length (DBusList **list)
772 link = _dbus_list_get_next_link (list, link);
779 * Calls the given function for each element in the list. The
780 * function is passed the list element as its first argument, and the
781 * given data as its second argument.
783 * @param list address of the head of the list.
784 * @param function function to call for each element.
785 * @param data extra data for the function.
789 _dbus_list_foreach (DBusList **list,
790 DBusForeachFunction function,
798 DBusList *next = _dbus_list_get_next_link (list, link);
800 (* function) (link->data, data);
807 * Check whether length is exactly one.
809 * @param list the list
810 * @returns #TRUE if length is exactly one
813 _dbus_list_length_is_one (DBusList **list)
815 return (*list != NULL &&
816 (*list)->next == *list);
821 #ifdef DBUS_BUILD_TESTS
822 #include "dbus-test.h"
826 verify_list (DBusList **list)
836 if (link->next == link)
838 _dbus_assert (link->prev == link);
839 _dbus_assert (*list == link);
847 _dbus_assert (link->prev->next == link);
848 _dbus_assert (link->next->prev == link);
851 while (link != *list);
853 _dbus_assert (length == _dbus_list_get_length (list));
856 _dbus_assert (_dbus_list_length_is_one (list));
858 _dbus_assert (!_dbus_list_length_is_one (list));
862 is_ascending_sequence (DBusList **list)
867 prev = _DBUS_INT_MIN;
869 link = _dbus_list_get_first_link (list);
872 int v = _DBUS_POINTER_TO_INT (link->data);
879 link = _dbus_list_get_next_link (list, link);
886 is_descending_sequence (DBusList **list)
891 prev = _DBUS_INT_MAX;
893 link = _dbus_list_get_first_link (list);
896 int v = _DBUS_POINTER_TO_INT (link->data);
903 link = _dbus_list_get_next_link (list, link);
910 all_even_values (DBusList **list)
914 link = _dbus_list_get_first_link (list);
917 int v = _DBUS_POINTER_TO_INT (link->data);
922 link = _dbus_list_get_next_link (list, link);
929 all_odd_values (DBusList **list)
933 link = _dbus_list_get_first_link (list);
936 int v = _DBUS_POINTER_TO_INT (link->data);
941 link = _dbus_list_get_next_link (list, link);
948 lists_equal (DBusList **list1,
954 link1 = _dbus_list_get_first_link (list1);
955 link2 = _dbus_list_get_first_link (list2);
956 while (link1 && link2)
958 if (link1->data != link2->data)
961 link1 = _dbus_list_get_next_link (list1, link1);
962 link2 = _dbus_list_get_next_link (list2, link2);
972 * @ingroup DBusListInternals
973 * Unit test for DBusList
974 * @returns #TRUE on success.
977 _dbus_list_test (void)
990 /* Test append and prepend */
995 if (!_dbus_list_append (&list1, _DBUS_INT_TO_POINTER (i)))
996 _dbus_assert_not_reached ("could not allocate for append");
998 if (!_dbus_list_prepend (&list2, _DBUS_INT_TO_POINTER (i)))
999 _dbus_assert_not_reached ("count not allocate for prepend");
1002 verify_list (&list1);
1003 verify_list (&list2);
1005 _dbus_assert (_dbus_list_get_length (&list1) == i);
1006 _dbus_assert (_dbus_list_get_length (&list2) == i);
1009 _dbus_assert (is_ascending_sequence (&list1));
1010 _dbus_assert (is_descending_sequence (&list2));
1012 /* Test list clear */
1013 _dbus_list_clear (&list1);
1014 _dbus_list_clear (&list2);
1016 verify_list (&list1);
1017 verify_list (&list2);
1019 /* Test get_first, get_last, pop_first, pop_last */
1024 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (i));
1025 _dbus_list_prepend (&list2, _DBUS_INT_TO_POINTER (i));
1038 got_data1 = _dbus_list_get_last (&list1);
1039 got_data2 = _dbus_list_get_first (&list2);
1041 data1 = _dbus_list_pop_last (&list1);
1042 data2 = _dbus_list_pop_first (&list2);
1044 _dbus_assert (got_data1 == data1);
1045 _dbus_assert (got_data2 == data2);
1047 _dbus_assert (_DBUS_POINTER_TO_INT (data1) == i);
1048 _dbus_assert (_DBUS_POINTER_TO_INT (data2) == i);
1050 verify_list (&list1);
1051 verify_list (&list2);
1053 _dbus_assert (is_ascending_sequence (&list1));
1054 _dbus_assert (is_descending_sequence (&list2));
1059 _dbus_assert (list1 == NULL);
1060 _dbus_assert (list2 == NULL);
1062 /* Test get_first_link, get_last_link, pop_first_link, pop_last_link */
1067 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (i));
1068 _dbus_list_prepend (&list2, _DBUS_INT_TO_POINTER (i));
1075 DBusList *got_link1;
1076 DBusList *got_link2;
1084 got_link1 = _dbus_list_get_last_link (&list1);
1085 got_link2 = _dbus_list_get_first_link (&list2);
1087 link1 = _dbus_list_pop_last_link (&list1);
1088 link2 = _dbus_list_pop_first_link (&list2);
1090 _dbus_assert (got_link1 == link1);
1091 _dbus_assert (got_link2 == link2);
1093 data1 = link1->data;
1094 data2 = link2->data;
1096 _dbus_list_free_link (link1);
1097 _dbus_list_free_link (link2);
1099 _dbus_assert (_DBUS_POINTER_TO_INT (data1) == i);
1100 _dbus_assert (_DBUS_POINTER_TO_INT (data2) == i);
1102 verify_list (&list1);
1103 verify_list (&list2);
1105 _dbus_assert (is_ascending_sequence (&list1));
1106 _dbus_assert (is_descending_sequence (&list2));
1111 _dbus_assert (list1 == NULL);
1112 _dbus_assert (list2 == NULL);
1114 /* Test iteration */
1119 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (i));
1120 _dbus_list_prepend (&list2, _DBUS_INT_TO_POINTER (i));
1123 verify_list (&list1);
1124 verify_list (&list2);
1126 _dbus_assert (_dbus_list_get_length (&list1) == i);
1127 _dbus_assert (_dbus_list_get_length (&list2) == i);
1130 _dbus_assert (is_ascending_sequence (&list1));
1131 _dbus_assert (is_descending_sequence (&list2));
1134 link2 = _dbus_list_get_first_link (&list2);
1135 while (link2 != NULL)
1137 verify_list (&link2); /* pretend this link is the head */
1139 _dbus_assert (_DBUS_POINTER_TO_INT (link2->data) == i);
1141 link2 = _dbus_list_get_next_link (&list2, link2);
1146 link1 = _dbus_list_get_first_link (&list1);
1147 while (link1 != NULL)
1149 verify_list (&link1); /* pretend this link is the head */
1151 _dbus_assert (_DBUS_POINTER_TO_INT (link1->data) == i);
1153 link1 = _dbus_list_get_next_link (&list1, link1);
1158 link1 = _dbus_list_get_last_link (&list1);
1159 while (link1 != NULL)
1161 verify_list (&link1); /* pretend this link is the head */
1163 _dbus_assert (_DBUS_POINTER_TO_INT (link1->data) == i);
1165 link1 = _dbus_list_get_prev_link (&list1, link1);
1169 _dbus_list_clear (&list1);
1170 _dbus_list_clear (&list2);
1177 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (i));
1178 _dbus_list_prepend (&list2, _DBUS_INT_TO_POINTER (i));
1187 if (!_dbus_list_remove (&list1, _DBUS_INT_TO_POINTER (i)))
1188 _dbus_assert_not_reached ("element should have been in list");
1189 if (!_dbus_list_remove (&list2, _DBUS_INT_TO_POINTER (i)))
1190 _dbus_assert_not_reached ("element should have been in list");
1192 verify_list (&list1);
1193 verify_list (&list2);
1198 _dbus_assert (all_odd_values (&list1));
1199 _dbus_assert (all_odd_values (&list2));
1201 _dbus_list_clear (&list1);
1202 _dbus_list_clear (&list2);
1204 /* test removing the other half of the elements */
1209 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (i));
1210 _dbus_list_prepend (&list2, _DBUS_INT_TO_POINTER (i));
1219 if (!_dbus_list_remove (&list1, _DBUS_INT_TO_POINTER (i)))
1220 _dbus_assert_not_reached ("element should have been in list");
1221 if (!_dbus_list_remove (&list2, _DBUS_INT_TO_POINTER (i)))
1222 _dbus_assert_not_reached ("element should have been in list");
1224 verify_list (&list1);
1225 verify_list (&list2);
1230 _dbus_assert (all_even_values (&list1));
1231 _dbus_assert (all_even_values (&list2));
1233 /* clear list using remove_link */
1234 while (list1 != NULL)
1236 _dbus_list_remove_link (&list1, list1);
1237 verify_list (&list1);
1239 while (list2 != NULL)
1241 _dbus_list_remove_link (&list2, list2);
1242 verify_list (&list2);
1245 /* Test remove link more generally */
1249 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (i));
1250 _dbus_list_prepend (&list2, _DBUS_INT_TO_POINTER (i));
1255 link2 = _dbus_list_get_first_link (&list2);
1256 while (link2 != NULL)
1258 DBusList *next = _dbus_list_get_next_link (&list2, link2);
1260 _dbus_assert (_DBUS_POINTER_TO_INT (link2->data) == i);
1263 _dbus_list_remove_link (&list2, link2);
1265 verify_list (&list2);
1271 _dbus_assert (all_odd_values (&list2));
1272 _dbus_list_clear (&list2);
1275 link1 = _dbus_list_get_first_link (&list1);
1276 while (link1 != NULL)
1278 DBusList *next = _dbus_list_get_next_link (&list1, link1);
1280 _dbus_assert (_DBUS_POINTER_TO_INT (link1->data) == i);
1283 _dbus_list_remove_link (&list1, link1);
1285 verify_list (&list1);
1291 _dbus_assert (all_even_values (&list1));
1292 _dbus_list_clear (&list1);
1294 /* Test copying a list */
1298 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (i));
1299 _dbus_list_prepend (&list2, _DBUS_INT_TO_POINTER (i));
1303 /* bad pointers, because they are allowed in the copy dest */
1304 copy1 = _DBUS_INT_TO_POINTER (0x342234);
1305 copy2 = _DBUS_INT_TO_POINTER (23);
1307 _dbus_list_copy (&list1, ©1);
1308 verify_list (&list1);
1309 verify_list (©1);
1310 _dbus_assert (lists_equal (&list1, ©1));
1312 _dbus_list_copy (&list2, ©2);
1313 verify_list (&list2);
1314 verify_list (©2);
1315 _dbus_assert (lists_equal (&list2, ©2));
1317 /* Now test copying empty lists */
1318 _dbus_list_clear (&list1);
1319 _dbus_list_clear (&list2);
1320 _dbus_list_clear (©1);
1321 _dbus_list_clear (©2);
1323 /* bad pointers, because they are allowed in the copy dest */
1324 copy1 = _DBUS_INT_TO_POINTER (0x342234);
1325 copy2 = _DBUS_INT_TO_POINTER (23);
1327 _dbus_list_copy (&list1, ©1);
1328 verify_list (&list1);
1329 verify_list (©1);
1330 _dbus_assert (lists_equal (&list1, ©1));
1332 _dbus_list_copy (&list2, ©2);
1333 verify_list (&list2);
1334 verify_list (©2);
1335 _dbus_assert (lists_equal (&list2, ©2));
1337 _dbus_list_clear (&list1);
1338 _dbus_list_clear (&list2);
1340 /* insert_before on empty list */
1341 _dbus_list_insert_before (&list1, NULL,
1342 _DBUS_INT_TO_POINTER (0));
1343 verify_list (&list1);
1345 /* inserting before first element */
1346 _dbus_list_insert_before (&list1, list1,
1347 _DBUS_INT_TO_POINTER (2));
1348 verify_list (&list1);
1349 _dbus_assert (is_descending_sequence (&list1));
1351 /* inserting in the middle */
1352 _dbus_list_insert_before (&list1, list1->next,
1353 _DBUS_INT_TO_POINTER (1));
1354 verify_list (&list1);
1355 _dbus_assert (is_descending_sequence (&list1));
1357 /* using insert_before to append */
1358 _dbus_list_insert_before (&list1, NULL,
1359 _DBUS_INT_TO_POINTER (-1));
1360 verify_list (&list1);
1361 _dbus_assert (is_descending_sequence (&list1));
1363 _dbus_list_clear (&list1);
1365 /* insert_after on empty list */
1366 _dbus_list_insert_after (&list1, NULL,
1367 _DBUS_INT_TO_POINTER (0));
1368 verify_list (&list1);
1370 /* inserting after first element */
1371 _dbus_list_insert_after (&list1, list1,
1372 _DBUS_INT_TO_POINTER (1));
1373 verify_list (&list1);
1374 _dbus_assert (is_ascending_sequence (&list1));
1376 /* inserting at the end */
1377 _dbus_list_insert_after (&list1, list1->next,
1378 _DBUS_INT_TO_POINTER (2));
1379 verify_list (&list1);
1380 _dbus_assert (is_ascending_sequence (&list1));
1382 /* using insert_after to prepend */
1383 _dbus_list_insert_after (&list1, NULL,
1384 _DBUS_INT_TO_POINTER (-1));
1385 verify_list (&list1);
1386 _dbus_assert (is_ascending_sequence (&list1));
1388 _dbus_list_clear (&list1);
1390 /* using remove_last */
1391 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (2));
1392 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (1));
1393 _dbus_list_append (&list1, _DBUS_INT_TO_POINTER (3));
1395 _dbus_list_remove_last (&list1, _DBUS_INT_TO_POINTER (2));
1397 verify_list (&list1);
1398 _dbus_assert (is_ascending_sequence (&list1));
1400 _dbus_list_clear (&list1);