File: lists.c

package info (click to toggle)
evms 2.5.2-1.sarge2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 14,248 kB
  • ctags: 15,488
  • sloc: ansic: 201,340; perl: 12,421; sh: 4,262; makefile: 1,516; yacc: 316; sed: 16
file content (1135 lines) | stat: -rw-r--r-- 24,480 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
/*
 *
 *   (C) Copyright IBM Corp. 2002, 2003
 *
 *   This program is free software;  you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 *   the GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program;  if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * Module: list.c
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <pthread.h>

#include "fullengine.h"
#include "lists.h"
#include "engine.h"
#include "memman.h"


#define INTERNAL_LIST_FOR_EACH(list, el)				\
        for ((el) = ELEMENT_PTR((list)->links.next);			\
	    (((el) != NULL) && ((el) != ELEMENT_PTR(&(list)->links)));	\
	    (el) = ELEMENT_PTR(el->links.next))


/* Insert new_link before link. */
static inline void _insert_link_before(links_t * new_link, links_t * link) {
	link->prev->next = new_link;
	new_link->prev = link->prev;
	link->prev = new_link;
	new_link->next = link;
}

/* Insert new_link after link. */
static inline void _insert_link_after(links_t * new_link, links_t * link) {
	link->next->prev = new_link;
	new_link->next = link->next;
	link->next = new_link;
	new_link->prev = link;
}

static inline void _remove_link(links_t * link) {
	link->prev->next = link->next;
	link->next->prev = link->prev;
	link->next = NULL;
	link->prev = NULL;
}

static inline void set_list_owner(anchor_t * list) {

	list_element_t el;

	INTERNAL_LIST_FOR_EACH(list, el) {
		el->anchor = list;
	}
}

static boolean isa_valid_anchor(anchor_t * anchor) {

	LOG_PROC_EXTRA_ENTRY();

	if (anchor == NULL) {
		LOG_DEBUG("List is NULL.\n");
		LOG_PROC_EXTRA_EXIT_BOOLEAN(FALSE);
		return FALSE;
	}

	if ((anchor->links.next == NULL) ||
	    (anchor->links.prev == NULL)) {
		if (anchor->links.next == NULL) {
			LOG_ERROR("List's next pointer is NULL.\n");
		}
		if (anchor->links.next == NULL) {
			LOG_ERROR("List's previous pointer is NULL.\n");
		}
		LOG_PROC_EXTRA_EXIT_BOOLEAN(FALSE);
		return FALSE;
	}

	LOG_PROC_EXTRA_EXIT_BOOLEAN(TRUE);
	return TRUE;
}


/*
 * Allocate an anchor for a list.
 */
anchor_t * allocate_list(void) {

	anchor_t * anchor;

	LOG_PROC_EXTRA_ENTRY();

	anchor = engine_alloc(sizeof(anchor_t));

	if (anchor != NULL) {
		anchor->links.next = &anchor->links;
		anchor->links.prev = &anchor->links;
	}

	LOG_PROC_EXTRA_EXIT_PTR(anchor);
	return anchor;
}


/*
 * How many elements are in the list?
 */
uint list_count(anchor_t * anchor) {

	LOG_PROC_EXTRA_ENTRY();

	if (!isa_valid_anchor(anchor)) {
		LOG_PROC_EXTRA_EXIT_INT(0);
		return 0;
	}

	LOG_PROC_EXTRA_EXIT_INT(anchor->count);
	return anchor->count;
}


/*
 * Does this list have no elements?
 */
boolean list_empty(anchor_t * anchor) {

	boolean result;

	LOG_PROC_EXTRA_ENTRY();

	if (!isa_valid_anchor(anchor)) {
		LOG_PROC_EXTRA_EXIT_BOOLEAN(TRUE);
		return TRUE;
	}

	LOG_EXTRA("List has %u element%s.\n", anchor->count, (anchor->count) == 1 ? "" : "s");
	result = (anchor->count == 0);

	LOG_PROC_EXTRA_EXIT_BOOLEAN(result);
	return result;
}


/*
 * Find a given thing in the list.  If the thing is in the list, return
 * the element corresponding to the thing.  If the thing is not in the
 * list, return NULL.
 * The "compare" function is used for finding the thing in the list.
 * Each thing in the list will be compared against the thing passed to
 * find_in_list().  When the compare function returns 0, find_in_list()
 * will know that it has found the thing and will return the element
 * corresponding to the thing.  If "compare" is NULL, find_in_list()
 * will compare the given thing pointer agains the thing pointers in
 * the list.
 */
element_t * find_in_list(anchor_t         * anchor,
			 void             * thing,
			 compare_function_t compare,
			 void             * user_data) {

	element_t * element;

	LOG_PROC_EXTRA_ENTRY();

	if (!isa_valid_anchor(anchor)) {
		LOG_PROC_EXTRA_EXIT_PTR(NULL);
		return NULL;
	}

	if (compare != NULL) {
		INTERNAL_LIST_FOR_EACH(anchor, element) {
			if (compare(thing, element->thing, user_data) == 0) {
				LOG_PROC_EXTRA_EXIT_PTR(element);
				return element;
			}
		}
	} else {
		INTERNAL_LIST_FOR_EACH(anchor, element) {
			if (element->thing == thing) {
				LOG_PROC_EXTRA_EXIT_PTR(element);
				return element;
			}
		}
	}

	LOG_PROC_EXTRA_EXIT_PTR(NULL);
	return NULL;
}


/*
 * Delete all the elements in the list, but leave the list anchor.
 * The things in the list are not freed.
 */
void delete_all_elements(anchor_t * anchor) {

	element_t * el;

	LOG_PROC_EXTRA_ENTRY();

	if (!isa_valid_anchor(anchor)) {
		LOG_PROC_EXTRA_EXIT_VOID();
		return;
	}

	el = ELEMENT_PTR(anchor->links.next);
	while ((el != NULL) && (&el->links != &anchor->links)) {
		element_t * next_el = ELEMENT_PTR(el->links.next);

		_remove_link(&el->links);
		memset(el, 0, sizeof(*el));
		engine_free(el);

		el = next_el;
	}

	anchor->count = 0;

	LOG_PROC_EXTRA_EXIT_VOID();
	return;
}


/*
 * Destroy the list -- free the list anchor and all the list
 * elements.  The things in the list are not freed.
 */
void destroy_list(anchor_t * anchor) {

	LOG_PROC_EXTRA_ENTRY();

	if (!isa_valid_anchor(anchor)) {
		LOG_PROC_EXTRA_EXIT_VOID();
		return;
	}

	delete_all_elements(anchor);
	memset(anchor, 0, sizeof(*anchor));
	engine_free(anchor);

	LOG_PROC_EXTRA_EXIT_VOID();
	return;
}


/*
 * Get the thing corresponding to element.
 */
void * get_thing(element_t * element) {

	LOG_PROC_EXTRA_ENTRY();

	if (element == NULL) {
		LOG_PROC_EXTRA_EXIT_PTR(NULL);
		return NULL;
	}

	LOG_PROC_EXTRA_EXIT_PTR(element->thing);
	return element->thing;
}


/*
 * Get the element after the given element in the list.
 */
element_t * next_element(element_t * element) {

	element_t * next_el;

	LOG_PROC_EXTRA_ENTRY();

	if (element == NULL) {
		LOG_PROC_EXTRA_EXIT_PTR(NULL);
		return NULL;
	}

	next_el = ELEMENT_PTR(element->links.next);

	if (&next_el->links == &element->anchor->links) {
		LOG_PROC_EXTRA_EXIT_PTR(NULL);
		return NULL;
	}

	LOG_PROC_EXTRA_EXIT_PTR(next_el);
	return next_el;
}


/*
 * Get the next thing in the list.  *element will be replaced with the
 * element for the next thing.
 */
void * next_thing(element_t * * element) {

	element_t * next_el;
	void * thing;

	LOG_PROC_EXTRA_ENTRY();

	if (*element == NULL) {
		LOG_PROC_EXTRA_EXIT_PTR(NULL);
		return NULL;
	}

	next_el = next_element(*element);

	if (next_el != NULL) {
		*element = next_el;
		thing = next_el->thing;

	} else {
		*element = NULL;
		thing = NULL;
	}

	LOG_PROC_EXTRA_EXIT_PTR(thing);
	return thing;
}


/*
 * Get the element before the given element in the list.
 */
element_t * previous_element(element_t * element) {

	element_t * prev_el;

	LOG_PROC_EXTRA_ENTRY();

	if (element == NULL) {
		LOG_PROC_EXTRA_EXIT_PTR(NULL);
		return NULL;
	}

	prev_el = ELEMENT_PTR(element->links.prev);

	if (&prev_el->links == &element->anchor->links) {
		LOG_PROC_EXTRA_EXIT_PTR(NULL);
		return NULL;
	}

	LOG_PROC_EXTRA_EXIT_PTR(prev_el);
	return prev_el;
}


/*
 * Get the previous thing in the list.  *element will be replaced with
 * the element for the previous thing.
 */
void * previous_thing(element_t * * element) {

	element_t * prev_el;
	void * thing;

	LOG_PROC_EXTRA_ENTRY();

	if (element == NULL) {
		LOG_PROC_EXTRA_EXIT_PTR(NULL);
		return NULL;
	}

	prev_el = previous_element(*element);

	if (prev_el != NULL) {
		*element = prev_el;
		thing = prev_el->thing;

	} else {
		*element = NULL;
		thing = NULL;
	}

	LOG_PROC_EXTRA_EXIT_PTR(thing);
	return thing;
}


/*
 * Get the first thing in the list.  *element will be set to the
 * element for the thing.
 */
void * first_thing(anchor_t    * anchor,
		   element_t * * element) {

	element_t * first_element;
	void * thing;

	LOG_PROC_EXTRA_ENTRY();

	if (!isa_valid_anchor(anchor)) {
		if (element != NULL) {
			*element = NULL;
		}
		LOG_PROC_EXTRA_EXIT_PTR(NULL);
		return NULL;
	}

	first_element = ELEMENT_PTR(anchor->links.next);

	if ((first_element != NULL) && (&first_element->links != &anchor->links)) {
		if (element != NULL) {
			*element = first_element;
		}
		thing = first_element->thing;

	} else {
		if (element != NULL) {
			*element = NULL;
		}
		thing = NULL;
	}

	LOG_PROC_EXTRA_EXIT_PTR(thing);
	return thing;
}


/*
 * Get the last thing in the list.  *element will be set to the
 * element for the thing.
 */
void * last_thing(anchor_t    * anchor,
		  element_t * * element) {

	element_t * last_element;
	void * thing;

	LOG_PROC_EXTRA_ENTRY();

	if (!isa_valid_anchor(anchor)) {
		if (element != NULL) {
			*element = NULL;
		}
		LOG_PROC_EXTRA_EXIT_PTR(NULL);
		return NULL;
	}

	last_element = ELEMENT_PTR(anchor->links.prev);

	if ((last_element != NULL) && (&last_element->links != &anchor->links)) {
		if (element != NULL) {
			*element = last_element;
		}
		thing = last_element->thing;

	} else {
		if (element != NULL) {
			*element = NULL;
		}
		thing = NULL;
	}

	LOG_PROC_EXTRA_EXIT_PTR(thing);
	return thing;
}


/*
 * An internal service routine to insert an element into a list.
 * As an internal function, it is guaranteed that anchor and element are
 * not NULL.
 */
static void _insert_element(anchor_t     * anchor,
			    element_t    * element,
			    insert_flags_t flags,
			    element_t    * ref_element) {

	LOG_PROC_EXTRA_ENTRY();

	if (flags & INSERT_BEFORE) {
		if (ref_element == NULL) {
			ref_element = ELEMENT_PTR(anchor->links.next);
		}
		_insert_link_before(&element->links, &ref_element->links);

	} else {
		if (ref_element == NULL) {
			ref_element = ELEMENT_PTR(anchor->links.prev);
		}
		_insert_link_after(&element->links, &ref_element->links);
	}

	element->anchor = anchor;

	anchor->count++;

	LOG_PROC_EXTRA_EXIT_VOID();
	return;
}


/*
 * Insert an element (with its thing) into the list.  The element MUST
 * NOT be in any list.  The flags indicate where and how the element is
 * inserted.  If the insert succeeds, the element is returned, else NULL
 * is returned.
 * If flags say INSERT_AFTER, the element will be inserted after the
 * ref_element.  If ref_element is NULL, the element will be inserted at
 * the end of the list.
 * If flags say INSERT_BEFORE, the element will be inserted before the
 * ref_element.  If ref_element is NULL, the element will be inserted at
 * the beginning of the list.
 * If the EXCLUSIVE_INSERT flag is set, the Engine will make sure there
 * is only one element in the list that contains the thing.  If there is
 * another element in the list that has the same thing as the element to
 * be inserted, the element in that is already the list is returned.
 */
element_t * insert_element(anchor_t     * anchor,
			   element_t    * element,
			   insert_flags_t flags,
			   element_t    * ref_element) {

	LOG_PROC_EXTRA_ENTRY();

	if (!isa_valid_anchor(anchor)) {
		LOG_PROC_EXTRA_EXIT_PTR(NULL);
		return NULL;
	}

	if (element == NULL) {
		LOG_ERROR("Element to insert is NULL.\n");
		LOG_PROC_EXTRA_EXIT_PTR(NULL);
		return NULL;
	}

	if ((element->links.next != NULL) || (element->links.prev != NULL)) {
		LOG_ERROR("Element is already in a list.\n");
		if (element->links.next != NULL) {
			LOG_EXTRA("Element's next pointer is not NULL.\n");
		}
		if (element->links.prev != NULL) {
			LOG_EXTRA("Element's previous pointer is not NULL.\n");
		}
		LOG_PROC_EXTRA_EXIT_PTR(NULL);
		return NULL;
	}

	if (flags & EXCLUSIVE_INSERT) {
		element_t * el = find_in_list(anchor, element->thing, NULL, NULL);

		if (el != NULL) {
			/* Found the thing in the list.  Return its element. */
			LOG_PROC_EXTRA_EXIT_PTR(el);
			return el;
		}
	}

	_insert_element(anchor, element, flags, ref_element);

	LOG_PROC_EXTRA_EXIT_PTR(element);
	return element;
}


/*
 * Insert a thing into the list.  The flags indicate where and how the
 * thing is inserted.  The list element for the new thing is returned.
 * If flags say INSERT_AFTER, the thing will be inserted after the given
 * element.  If ref_element is NULL, the thing will be inserted at the
 * end of the list.
 * If flags say INSERT_BEFORE, the thing will be inserted before the
 * given element.  If ref_element is NULL, the thing will be inserted at
 * the beginning of the list.
 * If the EXCLUSIVE_INSERT flag is set, the Engine will make sure there
 * is only one element in the list that contains the thing.
 */
element_t * insert_thing(anchor_t     * anchor,
			 void         * thing,
			 insert_flags_t flags,
			 element_t    * ref_element) {

	element_t * new_element;

	LOG_PROC_EXTRA_ENTRY();

	if (!isa_valid_anchor(anchor)) {
		LOG_PROC_EXTRA_EXIT_PTR(NULL);
		return NULL;
	}

	if (flags & EXCLUSIVE_INSERT) {
		element_t * el = find_in_list(anchor, thing, NULL, NULL);

		if (el != NULL) {
			/* Found the thing in the list.  Return its element. */
			LOG_PROC_EXTRA_EXIT_PTR(el);
			return el;
		}
	}

	/* Need to make a new element for the thing. */
	new_element = engine_alloc(sizeof(element_t));

	if (new_element == NULL) {
		LOG_CRITICAL("Error getting memory for a new element.\n");
		LOG_PROC_EXTRA_EXIT_PTR(NULL);
		return NULL;
	}

	new_element->thing = thing;

	_insert_element(anchor, new_element, flags, ref_element);

	LOG_PROC_EXTRA_EXIT_PTR(new_element);
	return new_element;
}


/*
 * Remove the given element from its list.  The element is not freed.
 */
void remove_element(element_t * element) {

	LOG_PROC_EXTRA_ENTRY();

	if (element == NULL) {
		LOG_PROC_EXTRA_EXIT_VOID();
		return;
	}

	if ((element->links.next != NULL) &&
	    (element->links.prev != NULL)) {
		_remove_link(&element->links);

	} else {
		element->links.next = NULL;
		element->links.prev = NULL;
	}

	if (element->anchor != NULL) {
		element->anchor->count--;
		element->anchor = NULL;
	}

	LOG_PROC_EXTRA_EXIT_VOID();
	return;
}


/*
 * Remove the given element from its list, if it is in one, and free it.
 */
void delete_element(element_t * element) {

	LOG_PROC_EXTRA_ENTRY();

	if (element == NULL) {
		LOG_PROC_EXTRA_EXIT_VOID();
		return;
	}

	if ((element->links.next != NULL) &&
	    (element->links.prev != NULL)) {
		_remove_link(&element->links);

	} else {
		element->links.next = NULL;
		element->links.prev = NULL;
	}

	if (element->anchor != NULL) {
		element->anchor->count--;
		element->anchor = NULL;
	}

	memset(element, 0, sizeof(*element));
	engine_free(element);

	LOG_PROC_EXTRA_EXIT_VOID();
	return;
}


/*
 * Remove the given thing from the list.  The thing is not freed.
 * If the thing appears multiple times in the list, all occurrences will
 * be removed.
 */
void remove_thing(anchor_t * anchor,
		  void     * thing) {

	element_t * el;

	LOG_PROC_EXTRA_ENTRY();

	if (!isa_valid_anchor(anchor)) {
		LOG_PROC_EXTRA_EXIT_VOID();
		return;
	}

	el = ELEMENT_PTR(anchor->links.next);
	while (&el->links != &anchor->links) {
		element_t * next_el = ELEMENT_PTR(el->links.next);

		if (el->thing == thing) {
			delete_element(el);
		}

		el = next_el;
	}

	LOG_PROC_EXTRA_EXIT_VOID();
	return;
}


/*
 * Replace the thing in the list with the new_thing.  If thing
 * appears multiple times in the list, all occurrences will
 * be replaced.
 */
int replace_thing(anchor_t * anchor,
		  void     * thing,
		  void     * new_thing) {

	int rc = ENOENT;
	links_t * link;

	LOG_PROC_EXTRA_ENTRY();

	if (!isa_valid_anchor(anchor)) {
		LOG_PROC_EXTRA_EXIT_INT(EINVAL);
		return EINVAL;
	}

	for (link = anchor->links.next; link != &anchor->links; link = link->next) {
		element_t * element = (element_t *) link;
		if (element->thing == thing) {
			element->thing = new_thing;
			rc = 0;
		}
	}

	LOG_PROC_EXTRA_EXIT_INT(rc);
	return rc;
}


/*
 * Make a copy of the given list.
 */
anchor_t * copy_list(anchor_t * anchor) {

	anchor_t  * new_anchor;
	element_t * element;
	element_t * new_element;

	LOG_PROC_EXTRA_ENTRY();

	if (!isa_valid_anchor(anchor)) {
		LOG_PROC_EXTRA_EXIT_PTR(NULL);
		return NULL;
	}

	new_anchor = allocate_list();

	if (new_anchor != NULL) {
		INTERNAL_LIST_FOR_EACH(anchor, element) {
			new_element = engine_alloc(sizeof(element_t));

			if (new_element == NULL) {
				LOG_CRITICAL("Unable to get memory for a new list element.\n");
				destroy_list(new_anchor);
				new_anchor = NULL;
				break;
			}

			new_element->thing = element->thing;
			new_element->anchor = new_anchor;

			_insert_link_before(&new_element->links, &new_anchor->links);
			new_anchor->count++;
		}

	} else {
		LOG_CRITICAL("Unable to get memory for a new list anchor.\n");
	}

	LOG_PROC_EXTRA_EXIT_PTR(new_anchor);
	return new_anchor;
}


/*
 * Staple list2 onto the end of list1.
 * As an internal function, it is guaranteed that anchor1 and anchor2 are
 * not NULL.
 */
static inline void _append_list(anchor_t * anchor1,
				anchor_t * anchor2) {

	LOG_PROC_EXTRA_ENTRY();

	if (list_empty(anchor2)) {
		/* Nothing to append */
                LOG_PROC_EXTRA_EXIT_VOID();
		return;
	}

	if (list_empty(anchor1)) {
		/* Just move the elements from anchor2 to anchor1 */
		*anchor1 = *anchor2;

		anchor2->links.prev->next = &anchor1->links;
		anchor2->links.next->prev = &anchor1->links;

		anchor2->links.next = &anchor2->links;
		anchor2->links.prev = &anchor2->links;

		anchor2->count = 0;

	} else {
		anchor2->links.next->prev = anchor1->links.prev;
		anchor2->links.prev->next = &anchor1->links;

		anchor1->links.prev->next = anchor2->links.next;
		anchor1->links.prev = anchor2->links.prev;

		anchor1->count += anchor2->count;
	}

	/* Mark all the elements in list1 to as belonging to list1. */
	set_list_owner(anchor1);

	/* Make list 2 an empty list. */
	anchor2->links.next = &anchor2->links;
	anchor2->links.prev = &anchor2->links;
	anchor2->count = 0;

	LOG_PROC_EXTRA_EXIT_VOID();
	return;
}


/*
 * Append a copy of the elements in list2 to list1.
 */
int concatenate_lists(anchor_t * anchor1,
		      anchor_t * anchor2) {

	anchor_t * copy_anchor;

	LOG_PROC_EXTRA_ENTRY();

	if (!isa_valid_anchor(anchor1) || !isa_valid_anchor(anchor2)) {
		LOG_PROC_EXTRA_EXIT_INT(EINVAL);
		return EINVAL;
	}

	copy_anchor = copy_list(anchor2);

	if (copy_anchor != NULL) {
		_append_list(anchor1, copy_anchor);
		destroy_list(copy_anchor);

	} else {
		LOG_CRITICAL("Error copying list2.\n");
		LOG_PROC_EXTRA_EXIT_INT(ENOMEM);
		return ENOMEM;
	}

	LOG_PROC_EXTRA_EXIT_INT(0);
	return 0;
}


/*
 * Merge list2 into list1.  If the compare function is NULL, list2 is
 * appended to list1.
 * As an internal function, it is guaranteed that anchor1 and anchor2 are
 * not NULL.
 */
static int _merge_lists(anchor_t         * anchor1,
			anchor_t         * anchor2,
			compare_function_t compare,
			void             * user_data) {

	anchor_t  * tmp_anchor;
	element_t * element1;
	element_t * element2;
	element_t * insert_element;

	LOG_PROC_EXTRA_ENTRY();

	if (list_empty(anchor1) || list_empty(anchor2)) {
		/* One of the lists is empty.  The merge is a simple append. */
		_append_list(anchor1, anchor2);
		LOG_PROC_EXTRA_EXIT_INT(0);
		return 0;
	}

	tmp_anchor = allocate_list();
	
	if (tmp_anchor == NULL) {
		LOG_CRITICAL("Unable to allocate a temporary list anchor.\n");
		LOG_PROC_EXTRA_EXIT_INT(ENOMEM);
		return ENOMEM;
	}

	/*
	 * Transfer the first list to the temporary list anchor and
	 * clear the list from the first list anchor.
	 */
	*tmp_anchor = *anchor1;
	anchor1->links.next->prev = &tmp_anchor->links;
	anchor1->links.prev->next = &tmp_anchor->links;
	anchor1->links.next = &anchor1->links;
	anchor1->links.prev = &anchor1->links;
	anchor1->count = 0;

	/* Mark all the elements in temp list to as belonging to tmp_anchor. */
	set_list_owner(tmp_anchor);

	/*
	 * Insert members of either list, based on the comparison,
	 * back into the first list.
	 */
	element1 = ELEMENT_PTR(tmp_anchor->links.next);
	element2 = ELEMENT_PTR(anchor2->links.next);

	while ((&element1->links != &tmp_anchor->links) ||
	       (&element2->links != &anchor2->links)) {
		if (&element1->links == &tmp_anchor->links) {
			/* List one is empty.  The link to insert
			 * is the one from list two.
			 */
			insert_element = element2;
			element2 = ELEMENT_PTR(element2->links.next);

		} else if (&element2->links == &anchor2->links) {
			/* List two is empty.  The link to insert
			 * is the one from list one.
			 */
			insert_element = element1;
			element1 = ELEMENT_PTR(element1->links.next);

		} else {
			/*
			 * Must compare the two things to see which
			 * one goes first.
			 */
			if (compare(element1->thing, element2->thing, user_data) <= 0) {
				insert_element = element1;
				element1 = ELEMENT_PTR(element1->links.next);

			} else {
				insert_element = element2;
				element2 = ELEMENT_PTR(element2->links.next);
			}
		}

		remove_element(insert_element);
		_insert_element(anchor1,
				insert_element,
				INSERT_BEFORE,
				ELEMENT_PTR(&anchor1->links));
	}

	destroy_list(tmp_anchor);

	LOG_PROC_EXTRA_EXIT_INT(0);
	return 0;
}


int merge_lists(anchor_t         * anchor1,
		anchor_t         * anchor2,
		compare_function_t compare,
		void             * user_data) {

	LOG_PROC_EXTRA_ENTRY();

	if (!isa_valid_anchor(anchor1)) {
		LOG_PROC_EXTRA_EXIT_INT(EINVAL);
		return EINVAL;
	}

	if (!isa_valid_anchor(anchor2)) {
		LOG_ERROR("list2 is not valid.  Nothing to merge.\n");
		LOG_PROC_EXTRA_EXIT_INT(0);
		return 0;
	}

	if (compare == NULL) {
		/* No compare function given.  Append list2 to list1. */
		_append_list(anchor1, anchor2);
		LOG_PROC_EXTRA_EXIT_INT(0);
		return 0;

	} else {
		int rc = _merge_lists(anchor1, anchor2, compare, user_data);
		LOG_PROC_EXTRA_EXIT_INT(rc);
		return rc;
	}
}


/*
 * Quick sort the list.
 * As an internal function, it is guaranteed that anchor is valid and
 * compare is not NULL.
 */
static int _qsort_list(anchor_t         * anchor,
		       compare_function_t compare,
		       void             * user_data) {

	int rc = 0;
	anchor_t * tmp_anchor;

	links_t * link;
	uint list_count;
	uint i;

	LOG_PROC_EXTRA_ENTRY();

	if (anchor->count <= 1) {
		/* This sort is easy.  We're already done. */
		LOG_PROC_EXTRA_EXIT_INT(0);
		return 0;
	}

	tmp_anchor = allocate_list();
	if (tmp_anchor == NULL) {
		LOG_CRITICAL("Error getting memory for a temporary anchor.\n");
		LOG_PROC_EXTRA_EXIT_INT(ENOMEM);
		return ENOMEM;
	}

	/*
	 * Split the list into two (nearly) equal lists.
	 */
	list_count = anchor->count / 2;

	for (i = 0, link = anchor->links.next;
	    i < list_count;
	    i++, link = link->next);

	tmp_anchor->links.next = link;
	tmp_anchor->links.prev = anchor->links.prev;
	anchor->links.prev->next = &tmp_anchor->links;
	anchor->links.prev = link->prev;
	link->prev->next = &anchor->links;
	link->prev = &tmp_anchor->links;

	tmp_anchor->count = anchor->count - list_count;
	anchor->count = list_count;

	/* Mark all the elements in temp list to as belonging to tmp_anchor. */
	set_list_owner(tmp_anchor);

	/* Sort the two sub-lists. */
	rc = _qsort_list(anchor, compare, user_data);

	if (rc == 0) {
		rc = _qsort_list(tmp_anchor, compare, user_data);
	}

	if (rc == 0) {
		/*
		 * Merge the lists back together using the comparison function provided.
		 */
		rc = _merge_lists(anchor, tmp_anchor, compare, user_data);
	}

	LOG_PROC_EXTRA_EXIT_INT(rc);
	return rc;
}


/*
 * Sort the elements in the list.
 */
int sort_list(anchor_t         * anchor,
	      compare_function_t compare,
	      void             * user_data) {

	int rc = 0;
	anchor_t * copy;

	LOG_PROC_EXTRA_ENTRY();

	if (!isa_valid_anchor(anchor)) {
		LOG_PROC_EXTRA_EXIT_INT(0);
		return 0;
	}

	if (compare == NULL) {
		LOG_ERROR("A compare function was not given.\n");
		LOG_PROC_EXTRA_EXIT_INT(EINVAL);
		return EINVAL;
	}

	/* Make a copy of the list in case something goes wrong. */
	copy = copy_list(anchor);
	if (copy == NULL) {
		LOG_CRITICAL("Error making a backup copy of the list.\n");
		LOG_PROC_EXTRA_EXIT_INT(ENOMEM);
		return ENOMEM;
	}

	rc = _qsort_list(anchor, compare, user_data);

	if (rc != 0) {
		/*
		 * The sort failed.  Throw out the working list and replace it
		 * with the backup copy.
		 */
		delete_all_elements(anchor);
		_append_list(anchor, copy);
	}

	destroy_list(copy);

	LOG_PROC_EXTRA_EXIT_INT(0);
	return rc;
}