summaryrefslogtreecommitdiff
path: root/ipc/ipc_splay.c
blob: 062a69f813d9f24334b6de68d6f8610e94dc0647 (plain)
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
/* 
 * Mach Operating System
 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
 * All Rights Reserved.
 * 
 * Permission to use, copy, modify and distribute this software and its
 * documentation is hereby granted, provided that both the copyright
 * notice and this permission notice appear in all copies of the
 * software, derivative works or modified versions, and any portions
 * thereof, and that both notices appear in supporting documentation.
 * 
 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
 * 
 * Carnegie Mellon requests users of this software to return to
 * 
 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
 *  School of Computer Science
 *  Carnegie Mellon University
 *  Pittsburgh PA 15213-3890
 * 
 * any improvements or extensions that they make and grant Carnegie Mellon
 * the rights to redistribute these changes.
 */
/*
 */
/*
 *	File:	ipc/ipc_splay.c
 *	Author:	Rich Draves
 *	Date:	1989
 *
 *	Primitive splay tree operations.
 */

#include <mach/port.h>
#include <kern/assert.h>
#include <kern/macros.h>
#include <ipc/ipc_entry.h>
#include <ipc/ipc_splay.h>

/*
 *	Splay trees are self-adjusting binary search trees.
 *	They have the following attractive properties:
 *		1) Space efficient; only two pointers per entry.
 *		2) Robust performance; amortized O(log n) per operation.
 *		3) Recursion not needed.
 *	This makes them a good fall-back data structure for those
 *	entries that don't fit into the lookup table.
 *
 *	The paper by Sleator and Tarjan, JACM v. 32, no. 3, pp. 652-686,
 *	describes the splaying operation.  ipc_splay_prim_lookup
 *	and ipc_splay_prim_assemble implement the top-down splay
 *	described on p. 669.
 *
 *	The tree is stored in an unassembled form.  If ist_root is null,
 *	then the tree has no entries.  Otherwise, ist_name records
 *	the value used for the last lookup.  ist_root points to the
 *	middle tree obtained from the top-down splay.  ist_ltree and
 *	ist_rtree point to left and right subtrees, whose entries
 *	are all smaller (larger) than those in the middle tree.
 *	ist_ltreep and ist_rtreep are pointers to fields in the
 *	left and right subtrees.  ist_ltreep points to the rchild field
 *	of the largest entry in ltree, and ist_rtreep points to the
 *	lchild field of the smallest entry in rtree.  The pointed-to
 *	fields aren't initialized.  If the left (right) subtree is null,
 *	then ist_ltreep (ist_rtreep) points to the ist_ltree (ist_rtree)
 *	field in the splay structure itself.
 *
 *	The primary advantage of the unassembled form is that repeated
 *	unsuccessful lookups are efficient.  In particular, an unsuccessful
 *	lookup followed by an insert only requires one splaying operation.
 *
 *	The traversal algorithm works via pointer inversion.
 *	When descending down the tree, child pointers are reversed
 *	to point back to the parent entry.  When ascending,
 *	the pointers are restored to their original value.
 *
 *	The biggest potential problem with the splay tree implementation
 *	is that the operations, even lookup, require an exclusive lock.
 *	If IPC spaces are protected with exclusive locks, then
 *	the splay tree doesn't require its own lock, and ist_lock/ist_unlock
 *	needn't do anything.  If IPC spaces are protected with read/write
 *	locks then ist_lock/ist_unlock should provide exclusive access.
 *
 *	If it becomes important to let lookups run in parallel,
 *	or if the restructuring makes lookups too expensive, then
 *	there is hope.  Use a read/write lock on the splay tree.
 *	Keep track of the number of entries in the tree.  When doing
 *	a lookup, first try a non-restructuring lookup with a read lock held,
 *	with a bound (based on log of size of the tree) on the number of
 *	entries to traverse.  If the lookup runs up against the bound,
 *	then take a write lock and do a reorganizing lookup.
 *	This way, if lookups only access roughly balanced parts
 *	of the tree, then lookups run in parallel and do no restructuring.
 *
 *	The traversal algorithm currently requires an exclusive lock.
 *	If that is a problem, the tree could be changed from an lchild/rchild
 *	representation to a leftmost child/right sibling representation.
 *	In conjunction with non-restructing lookups, this would let
 *	lookups and traversals all run in parallel.  But this representation
 *	is more complicated and would slow down the operations.
 */

/*
 *	Boundary values to hand to ipc_splay_prim_lookup:
 */

#define	MACH_PORT_SMALLEST	((mach_port_t) 0)
#define MACH_PORT_LARGEST	((mach_port_t) ~0)

/*
 *	Routine:	ipc_splay_prim_lookup
 *	Purpose:
 *		Searches for the node labeled name in the splay tree.
 *		Returns three nodes (treep, ltreep, rtreep) and
 *		two pointers to nodes (ltreepp, rtreepp).
 *
 *		ipc_splay_prim_lookup splits the supplied tree into
 *		three subtrees, left, middle, and right, returned
 *		in ltreep, treep, and rtreep.
 *
 *		If name is present in the tree, then it is at
 *		the root of the middle tree.  Otherwise, the root
 *		of the middle tree is the last node traversed.
 *
 *		ipc_splay_prim_lookup returns a pointer into
 *		the left subtree, to the rchild field of its
 *		largest node, in ltreepp.  It returns a pointer
 *		into the right subtree, to the lchild field of its
 *		smallest node, in rtreepp.
 */

static void
ipc_splay_prim_lookup(
	mach_port_t		name,
	ipc_tree_entry_t	tree,
	ipc_tree_entry_t	*treep,
	ipc_tree_entry_t	*ltreep,
	ipc_tree_entry_t	**ltreepp,
	ipc_tree_entry_t	*rtreep,
	ipc_tree_entry_t	**rtreepp)
{
	mach_port_t tname;			/* temp name */
	ipc_tree_entry_t lchild, rchild;	/* temp child pointers */

	assert(tree != ITE_NULL);

#define	link_left					\
MACRO_BEGIN						\
	*ltreep = tree;					\
	ltreep = &tree->ite_rchild;			\
	tree = *ltreep;					\
MACRO_END

#define	link_right					\
MACRO_BEGIN						\
	*rtreep = tree;					\
	rtreep = &tree->ite_lchild;			\
	tree = *rtreep;					\
MACRO_END

#define rotate_left					\
MACRO_BEGIN						\
	ipc_tree_entry_t temp = tree;			\
							\
	tree = temp->ite_rchild;			\
	temp->ite_rchild = tree->ite_lchild;		\
	tree->ite_lchild = temp;			\
MACRO_END

#define rotate_right					\
MACRO_BEGIN						\
	ipc_tree_entry_t temp = tree;			\
							\
	tree = temp->ite_lchild;			\
	temp->ite_lchild = tree->ite_rchild;		\
	tree->ite_rchild = temp;			\
MACRO_END

	while (name != (tname = tree->ite_name)) {
		if (name < tname) {
			/* descend to left */

			lchild = tree->ite_lchild;
			if (lchild == ITE_NULL)
				break;
			tname = lchild->ite_name;

			if ((name < tname) &&
			    (lchild->ite_lchild != ITE_NULL))
				rotate_right;
			link_right;
			if ((name > tname) &&
			    (lchild->ite_rchild != ITE_NULL))
				link_left;
		} else {
			/* descend to right */

			rchild = tree->ite_rchild;
			if (rchild == ITE_NULL)
				break;
			tname = rchild->ite_name;

			if ((name > tname) &&
			    (rchild->ite_rchild != ITE_NULL))
				rotate_left;
			link_left;
			if ((name < tname) &&
			    (rchild->ite_lchild != ITE_NULL))
				link_right;
		}

		assert(tree != ITE_NULL);
	}

	*treep = tree;
	*ltreepp = ltreep;
	*rtreepp = rtreep;

#undef	link_left
#undef	link_right
#undef	rotate_left
#undef	rotate_right
}

/*
 *	Routine:	ipc_splay_prim_assemble
 *	Purpose:
 *		Assembles the results of ipc_splay_prim_lookup
 *		into a splay tree with the found node at the root.
 *
 *		ltree and rtree are by-reference so storing
 *		through ltreep and rtreep can change them.
 */

static void
ipc_splay_prim_assemble(
	ipc_tree_entry_t	tree,
	ipc_tree_entry_t	*ltree,
	ipc_tree_entry_t	*ltreep,
	ipc_tree_entry_t	*rtree,
	ipc_tree_entry_t	*rtreep)
{
	assert(tree != ITE_NULL);

	*ltreep = tree->ite_lchild;
	*rtreep = tree->ite_rchild;

	tree->ite_lchild = *ltree;
	tree->ite_rchild = *rtree;
}

/*
 *	Routine:	ipc_splay_tree_init
 *	Purpose:
 *		Initialize a raw splay tree for use.
 */

void
ipc_splay_tree_init(
	ipc_splay_tree_t	splay)
{
	splay->ist_root = ITE_NULL;
}

/*
 *	Routine:	ipc_splay_tree_pick
 *	Purpose:
 *		Picks and returns a random entry in a splay tree.
 *		Returns FALSE if the splay tree is empty.
 */

boolean_t
ipc_splay_tree_pick(
	ipc_splay_tree_t	splay,
	mach_port_t		*namep,
	ipc_tree_entry_t	*entryp)
{
	ipc_tree_entry_t root;

	ist_lock(splay);

	root = splay->ist_root;
	if (root != ITE_NULL) {
		*namep = root->ite_name;
		*entryp = root;
	}

	ist_unlock(splay);

	return root != ITE_NULL;
}

/*
 *	Routine:	ipc_splay_tree_lookup
 *	Purpose:
 *		Finds an entry in a splay tree.
 *		Returns ITE_NULL if not found.
 */

ipc_tree_entry_t
ipc_splay_tree_lookup(
	ipc_splay_tree_t	splay,
	mach_port_t		name)
{
	ipc_tree_entry_t root;

	ist_lock(splay);

	root = splay->ist_root;
	if (root != ITE_NULL) {
		if (splay->ist_name != name) {
			ipc_splay_prim_assemble(root,
				&splay->ist_ltree, splay->ist_ltreep,
				&splay->ist_rtree, splay->ist_rtreep);
			ipc_splay_prim_lookup(name, root, &root,
				&splay->ist_ltree, &splay->ist_ltreep,
				&splay->ist_rtree, &splay->ist_rtreep);
			splay->ist_name = name;
			splay->ist_root = root;
		}

		if (name != root->ite_name)
			root = ITE_NULL;
	}

	ist_unlock(splay);

	return root;
}

/*
 *	Routine:	ipc_splay_tree_insert
 *	Purpose:
 *		Inserts a new entry into a splay tree.
 *		The caller supplies a new entry.
 *		The name can't already be present in the tree.
 */

void
ipc_splay_tree_insert(
	ipc_splay_tree_t	splay,
	mach_port_t		name,
	ipc_tree_entry_t	entry)
{
	ipc_tree_entry_t root;

	assert(entry != ITE_NULL);

	ist_lock(splay);

	root = splay->ist_root;
	if (root == ITE_NULL) {
		entry->ite_lchild = ITE_NULL;
		entry->ite_rchild = ITE_NULL;
	} else {
		if (splay->ist_name != name) {
			ipc_splay_prim_assemble(root,
				&splay->ist_ltree, splay->ist_ltreep,
				&splay->ist_rtree, splay->ist_rtreep);
			ipc_splay_prim_lookup(name, root, &root,
				&splay->ist_ltree, &splay->ist_ltreep,
				&splay->ist_rtree, &splay->ist_rtreep);
		}

		assert(root->ite_name != name);

		if (name < root->ite_name) {
			assert(root->ite_lchild == ITE_NULL);

			*splay->ist_ltreep = ITE_NULL;
			*splay->ist_rtreep = root;
		} else {
			assert(root->ite_rchild == ITE_NULL);

			*splay->ist_ltreep = root;
			*splay->ist_rtreep = ITE_NULL;
		}

		entry->ite_lchild = splay->ist_ltree;
		entry->ite_rchild = splay->ist_rtree;
	}

	entry->ite_name = name;
	splay->ist_root = entry;
	splay->ist_name = name;
	splay->ist_ltreep = &splay->ist_ltree;
	splay->ist_rtreep = &splay->ist_rtree;

	ist_unlock(splay);
}

/*
 *	Routine:	ipc_splay_tree_delete
 *	Purpose:
 *		Deletes an entry from a splay tree.
 *		The name must be present in the tree.
 *		Frees the entry.
 *
 *		The "entry" argument isn't currently used.
 *		Other implementations might want it, though.
 */

void
ipc_splay_tree_delete(
	ipc_splay_tree_t	splay,
	mach_port_t		name,
	ipc_tree_entry_t	entry)
{
	ipc_tree_entry_t root, saved;

	ist_lock(splay);

	root = splay->ist_root;
	assert(root != ITE_NULL);

	if (splay->ist_name != name) {
		ipc_splay_prim_assemble(root,
			&splay->ist_ltree, splay->ist_ltreep,
			&splay->ist_rtree, splay->ist_rtreep);
		ipc_splay_prim_lookup(name, root, &root,
			&splay->ist_ltree, &splay->ist_ltreep,
			&splay->ist_rtree, &splay->ist_rtreep);
	}

	assert(root->ite_name == name);
	assert(root == entry);

	*splay->ist_ltreep = root->ite_lchild;
	*splay->ist_rtreep = root->ite_rchild;
	ite_free(root);

	root = splay->ist_ltree;
	saved = splay->ist_rtree;

	if (root == ITE_NULL)
		root = saved;
	else if (saved != ITE_NULL) {
		/*
		 *	Find the largest node in the left subtree, and splay it
		 *	to the root.  Then add the saved right subtree.
		 */

		ipc_splay_prim_lookup(MACH_PORT_LARGEST, root, &root,
			&splay->ist_ltree, &splay->ist_ltreep,
			&splay->ist_rtree, &splay->ist_rtreep);
		ipc_splay_prim_assemble(root,
			&splay->ist_ltree, splay->ist_ltreep,
			&splay->ist_rtree, splay->ist_rtreep);

		assert(root->ite_rchild == ITE_NULL);
		root->ite_rchild = saved;
	}

	splay->ist_root = root;
	if (root != ITE_NULL) {
		splay->ist_name = root->ite_name;
		splay->ist_ltreep = &splay->ist_ltree;
		splay->ist_rtreep = &splay->ist_rtree;
	}

	ist_unlock(splay);
}

/*
 *	Routine:	ipc_splay_tree_split
 *	Purpose:
 *		Split a splay tree.  Puts all entries smaller than "name"
 *		into a new tree, "small".
 *
 *		Doesn't do locking on "small", because nobody else
 *		should be fiddling with the uninitialized tree.
 */

void
ipc_splay_tree_split(
	ipc_splay_tree_t	splay,
	mach_port_t		name,
	ipc_splay_tree_t	small)
{
	ipc_tree_entry_t root;

	ipc_splay_tree_init(small);

	ist_lock(splay);

	root = splay->ist_root;
	if (root != ITE_NULL) {
		/* lookup name, to get it (or last traversed) to the top */

		if (splay->ist_name != name) {
			ipc_splay_prim_assemble(root,
				&splay->ist_ltree, splay->ist_ltreep,
				&splay->ist_rtree, splay->ist_rtreep);
			ipc_splay_prim_lookup(name, root, &root,
				&splay->ist_ltree, &splay->ist_ltreep,
				&splay->ist_rtree, &splay->ist_rtreep);
		}

		if (root->ite_name < name) {
			/* root goes into small */

			*splay->ist_ltreep = root->ite_lchild;
			*splay->ist_rtreep = ITE_NULL;
			root->ite_lchild = splay->ist_ltree;
			assert(root->ite_rchild == ITE_NULL);

			small->ist_root = root;
			small->ist_name = root->ite_name;
			small->ist_ltreep = &small->ist_ltree;
			small->ist_rtreep = &small->ist_rtree;

			/* rtree goes into splay */

			root = splay->ist_rtree;
			splay->ist_root = root;
			if (root != ITE_NULL) {
				splay->ist_name = root->ite_name;
				splay->ist_ltreep = &splay->ist_ltree;
				splay->ist_rtreep = &splay->ist_rtree;
			}
		} else {
			/* root stays in splay */

			*splay->ist_ltreep = root->ite_lchild;
			root->ite_lchild = ITE_NULL;

			splay->ist_root = root;
			splay->ist_name = name;
			splay->ist_ltreep = &splay->ist_ltree;

			/* ltree goes into small */

			root = splay->ist_ltree;
			small->ist_root = root;
			if (root != ITE_NULL) {
				small->ist_name = root->ite_name;
				small->ist_ltreep = &small->ist_ltree;
				small->ist_rtreep = &small->ist_rtree;
			}
		}		
	}

	ist_unlock(splay);
}

/*
 *	Routine:	ipc_splay_tree_join
 *	Purpose:
 *		Joins two splay trees.  Merges the entries in "small",
 *		which must all be smaller than the entries in "splay",
 *		into "splay".
 */

void
ipc_splay_tree_join(
	ipc_splay_tree_t	splay,
	ipc_splay_tree_t	small)
{
	ipc_tree_entry_t sroot;

	/* pull entries out of small */

	ist_lock(small);

	sroot = small->ist_root;
	if (sroot != ITE_NULL) {
		ipc_splay_prim_assemble(sroot,
			&small->ist_ltree, small->ist_ltreep,
			&small->ist_rtree, small->ist_rtreep);
		small->ist_root = ITE_NULL;
	}

	ist_unlock(small);

	/* put entries, if any, into splay */

	if (sroot != ITE_NULL) {
		ipc_tree_entry_t root;

		ist_lock(splay);

		root = splay->ist_root;
		if (root == ITE_NULL) {
			root = sroot;
		} else {
			/* get smallest entry in splay tree to top */

			if (splay->ist_name != MACH_PORT_SMALLEST) {
				ipc_splay_prim_assemble(root,
					&splay->ist_ltree, splay->ist_ltreep,
					&splay->ist_rtree, splay->ist_rtreep);
				ipc_splay_prim_lookup(MACH_PORT_SMALLEST,
					root, &root,
					&splay->ist_ltree, &splay->ist_ltreep,
					&splay->ist_rtree, &splay->ist_rtreep);
			}

			ipc_splay_prim_assemble(root,
				&splay->ist_ltree, splay->ist_ltreep,
				&splay->ist_rtree, splay->ist_rtreep);

			assert(root->ite_lchild == ITE_NULL);
			assert(sroot->ite_name < root->ite_name);
			root->ite_lchild = sroot;
		}

		splay->ist_root = root;
		splay->ist_name = root->ite_name;
		splay->ist_ltreep = &splay->ist_ltree;
		splay->ist_rtreep = &splay->ist_rtree;

		ist_unlock(splay);
	}
}

/*
 *	Routine:	ipc_splay_tree_bounds
 *	Purpose:
 *		Given a name, returns the largest value present
 *		in the tree that is smaller than or equal to the name,
 *		or ~0 if no such value exists.  Similarly, returns
 *		the smallest value present that is greater than or
 *		equal to the name, or 0 if no such value exists.
 *
 *		Hence, if
 *		lower = upper, then lower = name = upper
 *				and name is present in the tree
 *		lower = ~0 and upper = 0,
 *				then the tree is empty
 *		lower = ~0 and upper > 0, then name < upper
 *				and upper is smallest value in tree
 *		lower < ~0 and upper = 0, then lower < name
 *				and lower is largest value in tree
 *		lower < ~0 and upper > 0, then lower < name < upper
 *				and they are tight bounds on name
 *
 *		(Note MACH_PORT_SMALLEST = 0 and MACH_PORT_LARGEST = ~0.)
 */

void
ipc_splay_tree_bounds(
	ipc_splay_tree_t	splay,
	mach_port_t		name,
	mach_port_t		*lowerp, 
	mach_port_t		*upperp)
{
	ipc_tree_entry_t root;

	ist_lock(splay);

	root = splay->ist_root;
	if (root == ITE_NULL) {
		*lowerp = MACH_PORT_LARGEST;
		*upperp = MACH_PORT_SMALLEST;
	} else {
		mach_port_t rname;

		if (splay->ist_name != name) {
			ipc_splay_prim_assemble(root,
				&splay->ist_ltree, splay->ist_ltreep,
				&splay->ist_rtree, splay->ist_rtreep);
			ipc_splay_prim_lookup(name, root, &root,
				&splay->ist_ltree, &splay->ist_ltreep,
				&splay->ist_rtree, &splay->ist_rtreep);
			splay->ist_name = name;
			splay->ist_root = root;
		}

		rname = root->ite_name;

		/*
		 *	OK, it's a hack.  We convert the ltreep and rtreep
		 *	pointers back into real entry pointers,
		 *	so we can pick the names out of the entries.
		 */

		if (rname <= name)
			*lowerp = rname;
		else if (splay->ist_ltreep == &splay->ist_ltree)
			*lowerp = MACH_PORT_LARGEST;
		else {
			ipc_tree_entry_t entry;

			entry = (ipc_tree_entry_t)
				((char *)splay->ist_ltreep -
				 ((char *)&root->ite_rchild -
				  (char *)root));
			*lowerp = entry->ite_name;
		}

		if (rname >= name)
			*upperp = rname;
		else if (splay->ist_rtreep == &splay->ist_rtree)
			*upperp = MACH_PORT_SMALLEST;
		else {
			ipc_tree_entry_t entry;

			entry = (ipc_tree_entry_t)
				((char *)splay->ist_rtreep -
				 ((char *)&root->ite_lchild -
				  (char *)root));
			*upperp = entry->ite_name;
		}
	}

	ist_unlock(splay);
}

/*
 *	Routine:	ipc_splay_traverse_start
 *	Routine:	ipc_splay_traverse_next
 *	Routine:	ipc_splay_traverse_finish
 *	Purpose:
 *		Perform a symmetric order traversal of a splay tree.
 *	Usage:
 *		for (entry = ipc_splay_traverse_start(splay);
 *		     entry != ITE_NULL;
 *		     entry = ipc_splay_traverse_next(splay, delete)) {
 *			do something with entry
 *		}
 *		ipc_splay_traverse_finish(splay);
 *
 *		If "delete" is TRUE, then the current entry
 *		is removed from the tree and deallocated.
 *
 *		During the traversal, the splay tree is locked.
 */

ipc_tree_entry_t
ipc_splay_traverse_start(
	ipc_splay_tree_t	splay)
{
	ipc_tree_entry_t current, parent;

	ist_lock(splay);

	current = splay->ist_root;
	if (current != ITE_NULL) {
		ipc_splay_prim_assemble(current,
			&splay->ist_ltree, splay->ist_ltreep,
			&splay->ist_rtree, splay->ist_rtreep);

		parent = ITE_NULL;

		while (current->ite_lchild != ITE_NULL) {
			ipc_tree_entry_t next;

			next = current->ite_lchild;
			current->ite_lchild = parent;
			parent = current;
			current = next;
		}

		splay->ist_ltree = current;
		splay->ist_rtree = parent;
	}

	return current;
}

ipc_tree_entry_t
ipc_splay_traverse_next(
	ipc_splay_tree_t	splay,
	boolean_t		delete)
{
	ipc_tree_entry_t current, parent;

	/* pick up where traverse_entry left off */

	current = splay->ist_ltree;
	parent = splay->ist_rtree;
	assert(current != ITE_NULL);

	if (!delete)
		goto traverse_right;

	/* we must delete current and patch the tree */

	if (current->ite_lchild == ITE_NULL) {
		if (current->ite_rchild == ITE_NULL) {
			/* like traverse_back, but with deletion */

			if (parent == ITE_NULL) {
				ite_free(current);

				splay->ist_root = ITE_NULL;
				return ITE_NULL;
			}

			if (current->ite_name < parent->ite_name) {
				ite_free(current);

				current = parent;
				parent = current->ite_lchild;
				current->ite_lchild = ITE_NULL;
				goto traverse_entry;
			} else {
				ite_free(current);

				current = parent;
				parent = current->ite_rchild;
				current->ite_rchild = ITE_NULL;
				goto traverse_back;
			}
		} else {
			ipc_tree_entry_t prev;

			prev = current;
			current = current->ite_rchild;
			ite_free(prev);
			goto traverse_left;
		}
	} else {
		if (current->ite_rchild == ITE_NULL) {
			ipc_tree_entry_t prev;

			prev = current;
			current = current->ite_lchild;
			ite_free(prev);
			goto traverse_back;
		} else {
			ipc_tree_entry_t prev;
			ipc_tree_entry_t ltree, rtree;
			ipc_tree_entry_t *ltreep, *rtreep;

			/* replace current with largest of left children */

			prev = current;
			ipc_splay_prim_lookup(MACH_PORT_LARGEST,
				current->ite_lchild, &current,
				&ltree, &ltreep, &rtree, &rtreep);
			ipc_splay_prim_assemble(current,
				&ltree, ltreep, &rtree, rtreep);

			assert(current->ite_rchild == ITE_NULL);
			current->ite_rchild = prev->ite_rchild;
			ite_free(prev);
			goto traverse_right;
		}
	}
	/*NOTREACHED*/

	/*
	 *	A state machine:  for each entry, we
	 *		1) traverse left subtree
	 *		2) traverse the entry
	 *		3) traverse right subtree
	 *		4) traverse back to parent
	 */

    traverse_left:
	if (current->ite_lchild != ITE_NULL) {
		ipc_tree_entry_t next;

		next = current->ite_lchild;
		current->ite_lchild = parent;
		parent = current;
		current = next;
		goto traverse_left;
	}

    traverse_entry:
	splay->ist_ltree = current;
	splay->ist_rtree = parent;
	return current;

    traverse_right:
	if (current->ite_rchild != ITE_NULL) {
		ipc_tree_entry_t next;

		next = current->ite_rchild;
		current->ite_rchild = parent;
		parent = current;
		current = next;
		goto traverse_left;
	}

    traverse_back:
	if (parent == ITE_NULL) {
		splay->ist_root = current;
		return ITE_NULL;
	}

	if (current->ite_name < parent->ite_name) {
		ipc_tree_entry_t prev;

		prev = current;
		current = parent;
		parent = current->ite_lchild;
		current->ite_lchild = prev;
		goto traverse_entry;
	} else {
		ipc_tree_entry_t prev;

		prev = current;
		current = parent;
		parent = current->ite_rchild;
		current->ite_rchild = prev;
		goto traverse_back;
	}
}

void
ipc_splay_traverse_finish(
	ipc_splay_tree_t	splay)
{
	ipc_tree_entry_t root;

	root = splay->ist_root;
	if (root != ITE_NULL) {
		splay->ist_name = root->ite_name;
		splay->ist_ltreep = &splay->ist_ltree;
		splay->ist_rtreep = &splay->ist_rtree;
	}

	ist_unlock(splay);
}