-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaddrsup.c
3049 lines (2295 loc) · 70.2 KB
/
addrsup.c
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
/*++
Copyright (c) Microsoft Corporation. All rights reserved.
You may only use this code if you agree to the terms of the Windows Research Kernel Source Code License agreement (see License.txt).
If you do not agree to the terms, do not use the code.
Module Name:
addrsup.c
Abstract:
This module implements a new version of the generic table package
based on balanced binary trees (later named AVL), as described in
Knuth, "The Art of Computer Programming, Volume 3, Sorting and Searching",
and refers directly to algorithms as they are presented in the second
edition Copyrighted in 1973.
Used rtl\avltable.c as a starting point, adding the following:
- Use less memory for structures as these are nonpaged & heavily used.
- Caller allocates the pool to reduce mutex hold times.
- Various VAD-specific customizations/optimizations.
- Hints.
Environment:
Kernel mode only, working set mutex held, APCs disabled.
--*/
#include "mi.h"
#if !defined (_USERMODE)
#define PRINT
#define COUNT_BALANCE_MAX(a)
#else
extern MM_AVL_TABLE MmSectionBasedRoot;
#endif
#if (_MSC_VER >= 800)
#pragma warning(disable:4010) // Allow pretty pictures without the noise
#endif
TABLE_SEARCH_RESULT
MiFindNodeOrParent(
IN PMM_AVL_TABLE Table,
IN ULONG_PTR StartingVpn,
OUT PMMADDRESS_NODE *NodeOrParent
);
VOID
MiPromoteNode(
IN PMMADDRESS_NODE C
);
ULONG
MiRebalanceNode(
IN PMMADDRESS_NODE S
);
PMMADDRESS_NODE
MiRealSuccessor(
IN PMMADDRESS_NODE Links
);
PMMADDRESS_NODE
MiRealPredecessor(
IN PMMADDRESS_NODE Links
);
VOID
MiInitializeVadTableAvl(
IN PMM_AVL_TABLE Table
);
PVOID
MiEnumerateGenericTableWithoutSplayingAvl(
IN PMM_AVL_TABLE Table,
IN PVOID *RestartKey
);
#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE,MiCheckForConflictingNode)
#pragma alloc_text(PAGE,MiRealSuccessor)
#pragma alloc_text(PAGE,MiRealPredecessor)
#pragma alloc_text(PAGE,MiInitializeVadTableAvl)
#pragma alloc_text(PAGE,MiFindEmptyAddressRangeInTree)
#pragma alloc_text(PAGE,MiFindEmptyAddressRangeDownTree)
#pragma alloc_text(PAGE,MiFindEmptyAddressRangeDownBasedTree)
#endif
//
// Various Rtl macros that reference Parent use private versions here since
// Parent is overloaded with Balance.
//
//
// The macro function Parent takes as input a pointer to a splay link in a
// tree and returns a pointer to the splay link of the parent of the input
// node. If the input node is the root of the tree the return value is
// equal to the input value.
//
// PRTL_SPLAY_LINKS
// MiParent (
// PRTL_SPLAY_LINKS Links
// );
//
#define MiParent(Links) ( \
(PRTL_SPLAY_LINKS)(SANITIZE_PARENT_NODE((Links)->u1.Parent)) \
)
//
// The macro function IsLeftChild takes as input a pointer to a splay link
// in a tree and returns TRUE if the input node is the left child of its
// parent, otherwise it returns FALSE.
//
// BOOLEAN
// MiIsLeftChild (
// PRTL_SPLAY_LINKS Links
// );
//
#define MiIsLeftChild(Links) ( \
(RtlLeftChild(MiParent(Links)) == (PRTL_SPLAY_LINKS)(Links)) \
)
//
// The macro function IsRightChild takes as input a pointer to a splay link
// in a tree and returns TRUE if the input node is the right child of its
// parent, otherwise it returns FALSE.
//
// BOOLEAN
// MiIsRightChild (
// PRTL_SPLAY_LINKS Links
// );
//
#define MiIsRightChild(Links) ( \
(RtlRightChild(MiParent(Links)) == (PRTL_SPLAY_LINKS)(Links)) \
)
#if DBG
//
// Build a table of the best case efficiency of a balanced binary tree,
// holding the most possible nodes that can possibly be held in a binary
// tree with a given number of levels. The answer is always (2**n) - 1.
//
// (Used for debug only.)
//
ULONG MiBestCaseFill[33] = {
0, 1, 3, 7,
0xf, 0x1f, 0x3f, 0x7f,
0xff, 0x1ff, 0x3ff, 0x7ff,
0xfff, 0x1fff, 0x3fff, 0x7fff,
0xffff, 0x1ffff, 0x3ffff, 0x7ffff,
0xfffff, 0x1fffff, 0x3fffff, 0x7fffff,
0xffffff, 0x1ffffff, 0x3ffffff, 0x7ffffff,
0xfffffff, 0x1fffffff, 0x3fffffff, 0x7fffffff,
0xffffffff
};
//
// Build a table of the worst case efficiency of a balanced binary tree,
// holding the fewest possible nodes that can possibly be contained in a
// balanced binary tree with the given number of levels. After the first
// two levels, each level n is obviously occupied by a root node, plus
// one subtree the size of level n-1, and another subtree which is the
// size of n-2, i.e.:
//
// MiWorstCaseFill[n] = 1 + MiWorstCaseFill[n-1] + MiWorstCaseFill[n-2]
//
// The efficiency of a typical balanced binary tree will normally fall
// between the two extremes, typically closer to the best case. Note
// however that even with the worst case, it only takes 32 compares to
// find an element in a worst case tree populated with ~3.5M nodes.
//
// Unbalanced trees and splay trees, on the other hand, can and will sometimes
// degenerate to a straight line, requiring on average n/2 compares to
// find a node.
//
// A specific case is one where the nodes are inserted in collated order.
// In this case an unbalanced or a splay tree will generate a straight
// line, yet the balanced binary tree will always create a perfectly
// balanced tree (best-case fill) in this situation.
//
// (Used for debug only.)
//
ULONG MiWorstCaseFill[33] = {
0, 1, 2, 4,
7, 12, 20, 33,
54, 88, 143, 232,
376, 609, 986, 1596,
2583, 4180, 6764, 10945,
17710, 28656, 46367, 75024,
121392, 196417, 317810, 514228,
832039, 1346268, 2178308, 3524577,
5702886
};
#endif
TABLE_SEARCH_RESULT
MiFindNodeOrParent(
IN PMM_AVL_TABLE Table,
IN ULONG_PTR StartingVpn,
OUT PMMADDRESS_NODE *NodeOrParent
)
/*++
Routine Description:
This routine is used by all of the routines of the generic
table package to locate the a node in the tree. It will
find and return (via the NodeOrParent parameter) the node
with the given key, or if that node is not in the tree it
will return (via the NodeOrParent parameter) a pointer to
the parent.
Arguments:
Table - The generic table to search for the key.
StartingVpn - The starting virtual page number.
NodeOrParent - Will be set to point to the node containing the
the key or what should be the parent of the node
if it were in the tree. Note that this will *NOT*
be set if the search result is TableEmptyTree.
Return Value:
TABLE_SEARCH_RESULT - TableEmptyTree: The tree was empty. NodeOrParent
is *not* altered.
TableFoundNode: A node with the key is in the tree.
NodeOrParent points to that node.
TableInsertAsLeft: Node with key was not found.
NodeOrParent points to what would
be parent. The node would be the
left child.
TableInsertAsRight: Node with key was not found.
NodeOrParent points to what would
be parent. The node would be
the right child.
Environment:
Kernel mode. The PFN lock is held for some of the tables.
--*/
{
#if DBG
ULONG NumberCompares = 0;
#endif
PMMADDRESS_NODE Child;
PMMADDRESS_NODE NodeToExamine;
if (Table->NumberGenericTableElements == 0) {
return TableEmptyTree;
}
NodeToExamine = (PMMADDRESS_NODE)Table->BalancedRoot.RightChild;
do {
//
// Make sure the depth of tree is correct.
//
ASSERT(++NumberCompares <= Table->DepthOfTree);
//
// Compare the buffer with the key in the tree element.
//
if (StartingVpn < NodeToExamine->StartingVpn) {
Child = NodeToExamine->LeftChild;
if (Child != NULL) {
NodeToExamine = Child;
}
else {
//
// Node is not in the tree. Set the output
// parameter to point to what would be its
// parent and return which child it would be.
//
*NodeOrParent = NodeToExamine;
return TableInsertAsLeft;
}
}
else if (StartingVpn <= NodeToExamine->EndingVpn) {
//
// This is the node.
//
*NodeOrParent = NodeToExamine;
return TableFoundNode;
}
else {
Child = NodeToExamine->RightChild;
if (Child != NULL) {
NodeToExamine = Child;
}
else {
//
// Node is not in the tree. Set the output
// parameter to point to what would be its
// parent and return which child it would be.
//
*NodeOrParent = NodeToExamine;
return TableInsertAsRight;
}
}
} while (TRUE);
}
PMMADDRESS_NODE
MiCheckForConflictingNode(
IN ULONG_PTR StartVpn,
IN ULONG_PTR EndVpn,
IN PMM_AVL_TABLE Table
)
/*++
Routine Description:
The function determines if any addresses between a given starting and
ending address is contained within a virtual address descriptor.
Arguments:
StartVpn - Supplies the virtual address to locate a containing
descriptor.
EndVpn - Supplies the virtual address to locate a containing
descriptor.
Return Value:
Returns a pointer to the first conflicting virtual address descriptor
if one is found, otherwise a NULL value is returned.
--*/
{
PMMADDRESS_NODE Node;
if (Table->NumberGenericTableElements == 0) {
return NULL;
}
Node = (PMMADDRESS_NODE)Table->BalancedRoot.RightChild;
ASSERT(Node != NULL);
do {
if (Node == NULL) {
return NULL;
}
if (StartVpn > Node->EndingVpn) {
Node = Node->RightChild;
}
else if (EndVpn < Node->StartingVpn) {
Node = Node->LeftChild;
}
else {
//
// The starting address is less than or equal to the end VA
// and the ending address is greater than or equal to the
// start va. Return this node.
//
return Node;
}
} while (TRUE);
}
PMMADDRESS_NODE
FASTCALL
MiGetFirstNode(
IN PMM_AVL_TABLE Table
)
/*++
Routine Description:
This function locates the virtual address descriptor which contains
the address range which logically is first within the address space.
Arguments:
None.
Return Value:
Returns a pointer to the virtual address descriptor containing the
first address range, NULL if none.
--*/
{
PMMADDRESS_NODE First;
if (Table->NumberGenericTableElements == 0) {
return NULL;
}
First = (PMMADDRESS_NODE)Table->BalancedRoot.RightChild;
ASSERT(First != NULL);
while (First->LeftChild != NULL) {
First = First->LeftChild;
}
return First;
}
VOID
MiPromoteNode(
IN PMMADDRESS_NODE C
)
/*++
Routine Description:
This routine performs the fundamental adjustment required for balancing
the binary tree during insert and delete operations. Simply put, the
designated node is promoted in such a way that it rises one level in
the tree and its parent drops one level in the tree, becoming now the
child of the designated node. Generally the path length to the subtree
"opposite" the original parent. Balancing occurs as the caller chooses
which nodes to promote according to the balanced tree algorithms from
Knuth.
This is not the same as a splay operation, typically a splay "promotes"
a designated node twice.
Note that the pointer to the root node of the tree is assumed to be
contained in a MMADDRESS_NODE structure itself, to allow the
algorithms below to change the root of the tree without checking
for special cases. Note also that this is an internal routine,
and the caller guarantees that it never requests to promote the
root itself.
This routine only updates the tree links; the caller must update
the balance factors as appropriate.
Arguments:
C - pointer to the child node to be promoted in the tree.
Return Value:
None.
--*/
{
PMMADDRESS_NODE P;
PMMADDRESS_NODE G;
//
// Capture the current parent and grandparent (may be the root).
//
P = SANITIZE_PARENT_NODE(C->u1.Parent);
G = SANITIZE_PARENT_NODE(P->u1.Parent);
//
// Break down the promotion into two cases based upon whether C
// is a left or right child.
//
if (P->LeftChild == C) {
//
// This promotion looks like this:
//
// G G
// | |
// P C
// / \ => / \
// C z x P
// / \ / \
// x y y z
//
P->LeftChild = C->RightChild;
if (P->LeftChild != NULL) {
P->LeftChild->u1.Parent = MI_MAKE_PARENT(P, P->LeftChild->u1.Balance);
}
C->RightChild = P;
//
// Fall through to update parent and G <-> C relationship in
// common code.
//
}
else {
ASSERT(P->RightChild == C);
//
// This promotion looks like this:
//
// G G
// | |
// P C
// / \ => / \
// x C P z
// / \ / \
// y z x y
//
P->RightChild = C->LeftChild;
if (P->RightChild != NULL) {
P->RightChild->u1.Parent = MI_MAKE_PARENT(P, P->RightChild->u1.Balance);
}
C->LeftChild = P;
}
//
// Update parent of P, for either case above.
//
P->u1.Parent = MI_MAKE_PARENT(C, P->u1.Balance);
//
// Finally update G <-> C links for either case above.
//
if (G->LeftChild == P) {
G->LeftChild = C;
}
else {
ASSERT(G->RightChild == P);
G->RightChild = C;
}
C->u1.Parent = MI_MAKE_PARENT(G, C->u1.Balance);
}
ULONG
MiRebalanceNode(
IN PMMADDRESS_NODE S
)
/*++
Routine Description:
This routine performs a rebalance around the input node S, for which the
Balance factor has just effectively become +2 or -2. When called, the
Balance factor still has a value of +1 or -1, but the respective longer
side has just become one longer as the result of an insert or delete
operation.
This routine effectively implements steps A7.iii (test for Case 1 or
Case 2) and steps A8 and A9 of Knuth's balanced insertion algorithm,
plus it handles Case 3 identified in the delete section, which can
only happen on deletes.
The trick is, to convince yourself that while traveling from the
insertion point at the bottom of the tree up, that there are only
these two cases, and that when traveling up from the deletion point,
that there are just these three cases. Knuth says it is obvious!
Arguments:
S - pointer to the node which has just become unbalanced.
Return Value:
TRUE if Case 3 was detected (causes delete algorithm to terminate).
Environment:
Kernel mode. The PFN lock is held for some of the tables.
--*/
{
PMMADDRESS_NODE R, P;
SCHAR a;
PRINT("rebalancing node %p bal=%x start=%x end=%x\n",
S,
S->u1.Balance,
S->StartingVpn,
S->EndingVpn);
//
// The parent node is never the argument node.
//
ASSERT(SANITIZE_PARENT_NODE(S->u1.Parent) != S);
//
// Capture which side is unbalanced.
//
a = (SCHAR)S->u1.Balance;
if (a == +1) {
R = S->RightChild;
}
else {
R = S->LeftChild;
}
//
// If the balance of R and S are the same (Case 1 in Knuth) then a single
// promotion of R will do the single rotation. (Step A8, A10)
//
// Here is a diagram of the Case 1 transformation, for a == +1 (a mirror
// image transformation occurs when a == -1), and where the subtree
// heights are h and h+1 as shown (++ indicates the node out of balance):
//
// | |
// S++ R
// / \ / \
// (h) R+ ==> S (h+1)
// / \ / \
// (h) (h+1) (h) (h)
//
// Note that on an insert we can hit this case by inserting an item in the
// right subtree of R. The original height of the subtree before the insert
// was h+2, and it is still h+2 after the rebalance, so insert rebalancing
// may terminate.
//
// On a delete we can hit this case by deleting a node from the left subtree
// of S. The height of the subtree before the delete was h+3, and after the
// rebalance it is h+2, so rebalancing must continue up the tree.
//
if ((SCHAR)R->u1.Balance == a) {
MiPromoteNode(R);
R->u1.Balance = 0;
S->u1.Balance = 0;
return FALSE;
}
//
// Otherwise, we have to promote the appropriate child of R twice (Case 2
// in Knuth). (Step A9, A10)
//
// Here is a diagram of the Case 2 transformation, for a == +1 (a mirror
// image transformation occurs when a == -1), and where the subtree
// heights are h and h-1 as shown. There are actually two minor subcases,
// differing only in the original balance of P (++ indicates the node out
// of balance).
//
// | |
// S++ P
// / \ / \
// / \ / \
// / \ / \
// (h) R- ==> S- R
// / \ / \ / \
// P+ (h) (h)(h-1)(h) (h)
// / \
// (h-1) (h)
//
//
// | |
// S++ P
// / \ / \
// / \ / \
// / \ / \
// (h) R- ==> S R+
// / \ / \ / \
// P- (h) (h) (h)(h-1)(h)
// / \
// (h) (h-1)
//
// Note that on an insert we can hit this case by inserting an item in the
// left subtree of R. The original height of the subtree before the insert
// was h+2, and it is still h+2 after the rebalance, so insert rebalancing
// may terminate.
//
// On a delete we can hit this case by deleting a node from the left subtree
// of S. The height of the subtree before the delete was h+3, and after the
// rebalance it is h+2, so rebalancing must continue up the tree.
//
if ((SCHAR)R->u1.Balance == -a) {
//
// Pick up the appropriate child P for the double rotation (Link(-a,R)).
//
if (a == 1) {
P = R->LeftChild;
}
else {
P = R->RightChild;
}
//
// Promote him twice to implement the double rotation.
//
MiPromoteNode(P);
MiPromoteNode(P);
//
// Now adjust the balance factors.
//
S->u1.Balance = 0;
R->u1.Balance = 0;
if ((SCHAR)P->u1.Balance == a) {
PRINT("REBADJ A: Node %p, Bal %x -> %x\n", S, S->u1.Balance, -a);
COUNT_BALANCE_MAX((SCHAR)-a);
S->u1.Balance = (ULONG_PTR)-a;
}
else if ((SCHAR)P->u1.Balance == -a) {
PRINT("REBADJ B: Node %p, Bal %x -> %x\n", R, R->u1.Balance, a);
COUNT_BALANCE_MAX((SCHAR)a);
R->u1.Balance = (ULONG_PTR)a;
}
P->u1.Balance = 0;
return FALSE;
}
//
// Otherwise this is Case 3 which can only happen on Delete (identical
// to Case 1 except R->u1.Balance == 0). We do a single rotation, adjust
// the balance factors appropriately, and return TRUE. Note that the
// balance of S stays the same.
//
// Here is a diagram of the Case 3 transformation, for a == +1 (a mirror
// image transformation occurs when a == -1), and where the subtree
// heights are h and h+1 as shown (++ indicates the node out of balance):
//
// | |
// S++ R-
// / \ / \
// (h) R ==> S+ (h+1)
// / \ / \
// (h+1)(h+1) (h) (h+1)
//
// This case can not occur on an insert, because it is impossible for
// a single insert to balance R, yet somehow grow the right subtree of
// S at the same time. As we move up the tree adjusting balance factors
// after an insert, we terminate the algorithm if a node becomes balanced,
// because that means the subtree length did not change!
//
// On a delete we can hit this case by deleting a node from the left
// subtree of S. The height of the subtree before the delete was h+3,
// and after the rebalance it is still h+3, so rebalancing may terminate
// in the delete path.
//
MiPromoteNode(R);
PRINT("REBADJ C: Node %p, Bal %x -> %x\n", R, R->u1.Balance, -a);
COUNT_BALANCE_MAX((SCHAR)-a);
R->u1.Balance = -a;
return TRUE;
}
// VOID
// FASTCALL
// MiRemoveNode (
// IN PMMADDRESS_NODE NodeToDelete,
// IN PMM_AVL_TABLE Table
// )
// /*++
// Routine Description:
// This routine deletes the specified node from the balanced tree, rebalancing
// as necessary. If the NodeToDelete has at least one NULL child pointers,
// then it is chosen as the EasyDelete, otherwise a subtree predecessor or
// successor is found as the EasyDelete. In either case the EasyDelete is
// deleted and the tree is rebalanced. Finally if the NodeToDelete was
// different than the EasyDelete, then the EasyDelete is linked back into the
// tree in place of the NodeToDelete.
// Arguments:
// NodeToDelete - Pointer to the node which the caller wishes to delete.
// Table - The generic table in which the delete is to occur.
// Return Value:
// None.
// Environment:
// Kernel mode. The PFN lock is held for some of the tables.
// --*/
// {
// PMMADDRESS_NODE Parent;
// PMMADDRESS_NODE EasyDelete;
// PMMADDRESS_NODE P;
// SCHAR a;
// //
// // If the NodeToDelete has at least one NULL child pointer, then we can
// // delete it directly.
// //
// if ((NodeToDelete->LeftChild == NULL) ||
// (NodeToDelete->RightChild == NULL)) {
// EasyDelete = NodeToDelete;
// }
// //
// // Otherwise, we may as well pick the longest side to delete from (if one is
// // is longer), as that reduces the probability that we will have to
// // rebalance.
// //
// else if ((SCHAR) NodeToDelete->u1.Balance >= 0) {
// //
// // Pick up the subtree successor.
// //
// EasyDelete = NodeToDelete->RightChild;
// while (EasyDelete->LeftChild != NULL) {
// EasyDelete = EasyDelete->LeftChild;
// }
// }
// else {
// //
// // Pick up the subtree predecessor.
// //
// EasyDelete = NodeToDelete->LeftChild;
// while (EasyDelete->RightChild != NULL) {
// EasyDelete = EasyDelete->RightChild;
// }
// }
// //
// // Rebalancing must know which side of the first parent the delete occurred
// // on. Assume it is the left side and otherwise correct below.
// //
// a = -1;
// //
// // Now we can do the simple deletion for the no left child case.
// //
// if (EasyDelete->LeftChild == NULL) {
// Parent = SANITIZE_PARENT_NODE (EasyDelete->u1.Parent);
// if (MiIsLeftChild(EasyDelete)) {
// Parent->LeftChild = EasyDelete->RightChild;
// }
// else {
// Parent->RightChild = EasyDelete->RightChild;
// a = 1;
// }
// if (EasyDelete->RightChild != NULL) {
// EasyDelete->RightChild->u1.Parent = MI_MAKE_PARENT (Parent, EasyDelete->RightChild->u1.Balance);
// }
// //
// // Now we can do the simple deletion for the no right child case,
// // plus we know there is a left child.
// //
// }
// else {
// Parent = SANITIZE_PARENT_NODE (EasyDelete->u1.Parent);
// if (MiIsLeftChild(EasyDelete)) {
// Parent->LeftChild = EasyDelete->LeftChild;
// }
// else {
// Parent->RightChild = EasyDelete->LeftChild;
// a = 1;
// }
// EasyDelete->LeftChild->u1.Parent = MI_MAKE_PARENT (Parent,
// EasyDelete->LeftChild->u1.Balance);
// }
// //
// // For delete rebalancing, set the balance at the root to 0 to properly
// // terminate the rebalance without special tests, and to be able to detect
// // if the depth of the tree actually decreased.
// //
// Table->BalancedRoot.u1.Balance = 0;
// P = SANITIZE_PARENT_NODE (EasyDelete->u1.Parent);
// //
// // Loop until the tree is balanced.
// //
// while (TRUE) {
// //
// // First handle the case where the tree became more balanced. Zero
// // the balance factor, calculate a for the next loop and move on to
// // the parent.
// //
// if ((SCHAR) P->u1.Balance == a) {
// P->u1.Balance = 0;
// //
// // If this node is curently balanced, we can show it is now unbalanced
// // and terminate the scan since the subtree length has not changed.
// // (This may be the root, since we set Balance to 0 above!)
// //
// }
// else if (P->u1.Balance == 0) {
// PRINT("REBADJ D: Node %p, Bal %x -> %x\n", P, P->u1.Balance, -a);
// COUNT_BALANCE_MAX ((SCHAR)-a);
// P->u1.Balance = -a;
// //
// // If we shortened the depth all the way back to the root, then
// // the tree really has one less level.
// //
// if (Table->BalancedRoot.u1.Balance != 0) {
// Table->DepthOfTree -= 1;
// }
// break;
// //
// // Otherwise we made the short side 2 levels less than the long side,
// // and rebalancing is required. On return, some node has been promoted
// // to above node P. If Case 3 from Knuth was not encountered, then we
// // want to effectively resume rebalancing from P's original parent which
// // is effectively its grandparent now.
// //
// }
// else {
// //
// // We are done if Case 3 was hit, i.e., the depth of this subtree is
// // now the same as before the delete.
// //