00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
#include <nt.h>
00026
00027
#include <ntrtl.h>
00028
00029 #define SwapPointers(Ptr1, Ptr2) { \
00030
PVOID _SWAP_POINTER_TEMP; \
00031
_SWAP_POINTER_TEMP = (PVOID)(Ptr1); \
00032
(Ptr1) = (Ptr2); \
00033
(Ptr2) = _SWAP_POINTER_TEMP; \
00034
}
00035
00036 #define ParentsChildPointerAddress(Links) ( \
00037
RtlIsLeftChild(Links) ? \
00038
&(((Links)->Parent)->LeftChild) \
00039
: \
00040
&(((Links)->Parent)->RightChild) \
00041
)
00042
00043
VOID
00044
SwapSplayLinks (
00045 IN PRTL_SPLAY_LINKS Link1,
00046 IN PRTL_SPLAY_LINKS Link2
00047 );
00048
00049
00050 PRTL_SPLAY_LINKS
00051 RtlSplay (
00052 IN PRTL_SPLAY_LINKS Links
00053 )
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 {
00074 PRTL_SPLAY_LINKS
L;
00075 PRTL_SPLAY_LINKS P;
00076 PRTL_SPLAY_LINKS G;
00077
00078
00079
00080
00081
00082
00083
L = Links;
00084
00085
while (!RtlIsRoot(
L)) {
00086
00087 P = RtlParent(
L);
00088 G = RtlParent(P);
00089
00090
if (RtlIsLeftChild(
L)) {
00091
00092
if (RtlIsRoot(P)) {
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108 P->LeftChild =
L->RightChild;
00109
if (P->LeftChild !=
NULL) {P->LeftChild->Parent = P;}
00110
00111
00112
00113
00114
00115
L->RightChild = P;
00116 P->Parent =
L;
00117
00118
00119
00120
00121
00122
L->Parent =
L;
00123
00124 }
else if (RtlIsLeftChild(P)) {
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143 P->LeftChild =
L->RightChild;
00144
if (P->LeftChild !=
NULL) {P->LeftChild->Parent = P;}
00145
00146
00147
00148
00149
00150 G->LeftChild = P->RightChild;
00151
if (G->LeftChild !=
NULL) {G->LeftChild->Parent = G;}
00152
00153
00154
00155
00156
00157
if (RtlIsRoot(G)) {
00158
L->Parent =
L;
00159 }
else {
00160
L->Parent = G->Parent;
00161 *(
ParentsChildPointerAddress(G)) =
L;
00162 }
00163
00164
00165
00166
00167
00168
L->RightChild = P;
00169 P->Parent =
L;
00170
00171
00172
00173
00174
00175 P->RightChild = G;
00176 G->Parent = P;
00177
00178 }
else {
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197 G->RightChild =
L->LeftChild;
00198
if (G->RightChild !=
NULL) {G->RightChild->Parent = G;}
00199
00200
00201
00202
00203
00204 P->LeftChild =
L->RightChild;
00205
if (P->LeftChild !=
NULL) {P->LeftChild->Parent = P;}
00206
00207
00208
00209
00210
00211
if (RtlIsRoot(G)) {
00212
L->Parent =
L;
00213 }
else {
00214
L->Parent = G->Parent;
00215 *(
ParentsChildPointerAddress(G)) =
L;
00216 }
00217
00218
00219
00220
00221
00222
L->LeftChild = G;
00223 G->Parent =
L;
00224
00225
00226
00227
00228
00229
L->RightChild = P;
00230 P->Parent =
L;
00231
00232 }
00233
00234 }
else {
00235
00236
if (RtlIsRoot(P)) {
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252 P->RightChild =
L->LeftChild;
00253
if (P->RightChild !=
NULL) {P->RightChild->Parent = P;}
00254
00255
00256
00257
00258
00259
L->LeftChild = P;
00260 P->Parent =
L;
00261
00262
00263
00264
00265
00266
L->Parent =
L;
00267
00268 }
else if (RtlIsRightChild(P)) {
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287 G->RightChild = P->LeftChild;
00288
if (G->RightChild !=
NULL) {G->RightChild->Parent = G;}
00289
00290
00291
00292
00293
00294 P->RightChild =
L->LeftChild;
00295
if (P->RightChild !=
NULL) {P->RightChild->Parent = P;}
00296
00297
00298
00299
00300
00301
if (RtlIsRoot(G)) {
00302
L->Parent =
L;
00303 }
else {
00304
L->Parent = G->Parent;
00305 *(
ParentsChildPointerAddress(G)) =
L;
00306 }
00307
00308
00309
00310
00311
00312
L->LeftChild = P;
00313 P->Parent =
L;
00314
00315
00316
00317
00318
00319 P->LeftChild = G;
00320 G->Parent = P;
00321
00322 }
else {
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341 P->RightChild =
L->LeftChild;
00342
if (P->RightChild !=
NULL) {P->RightChild->Parent = P;}
00343
00344
00345
00346
00347
00348 G->LeftChild =
L->RightChild;
00349
if (G->LeftChild !=
NULL) {G->LeftChild->Parent = G;}
00350
00351
00352
00353
00354
00355
if (RtlIsRoot(G)) {
00356
L->Parent =
L;
00357 }
else {
00358
L->Parent = G->Parent;
00359 *(
ParentsChildPointerAddress(G)) =
L;
00360 }
00361
00362
00363
00364
00365
00366
L->LeftChild = P;
00367 P->Parent =
L;
00368
00369
00370
00371
00372
00373
L->RightChild = G;
00374 G->Parent =
L;
00375
00376 }
00377 }
00378 }
00379
00380
return L;
00381 }
00382
00383
00384 PRTL_SPLAY_LINKS
00385 RtlDelete (
00386 IN PRTL_SPLAY_LINKS Links
00387 )
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408 {
00409 PRTL_SPLAY_LINKS Predecessor;
00410 PRTL_SPLAY_LINKS Parent;
00411 PRTL_SPLAY_LINKS Child;
00412
00413 PRTL_SPLAY_LINKS *ParentChildPtr;
00414
00415
00416
00417
00418
00419
00420
00421
if ((RtlLeftChild(Links) !=
NULL) && (RtlRightChild(Links) !=
NULL)) {
00422
00423
00424
00425
00426
00427 Predecessor =
RtlSubtreePredecessor(Links);
00428
SwapSplayLinks(Predecessor, Links);
00429
00430 }
00431
00432
00433
00434
00435
00436
00437
00438
00439
if ((RtlLeftChild(Links) ==
NULL) && (RtlRightChild(Links) ==
NULL)) {
00440
00441
00442
00443
00444
00445
if (RtlIsRoot(Links)) {
00446
00447
return NULL;
00448 }
00449
00450
00451
00452
00453
00454
00455 Parent = RtlParent(Links);
00456
00457 ParentChildPtr =
ParentsChildPointerAddress(Links);
00458 *ParentChildPtr =
NULL;
00459
00460
return RtlSplay(Parent);
00461
00462 }
00463
00464
00465
00466
00467
00468
00469
00470
if (RtlLeftChild(Links) !=
NULL) {
00471 Child = RtlLeftChild(Links);
00472 }
else {
00473 Child = RtlRightChild(Links);
00474 }
00475
00476
00477
00478
00479
00480
00481
if (RtlIsRoot(Links)) {
00482 Child->Parent = Child;
00483
return Child;
00484 }
00485
00486
00487
00488
00489
00490
00491
00492 ParentChildPtr =
ParentsChildPointerAddress(Links);
00493 *ParentChildPtr = Child;
00494 Child->Parent = Links->Parent;
00495
00496
return RtlSplay(RtlParent(Child));
00497
00498 }
00499
00500
00501
VOID
00502 RtlDeleteNoSplay (
00503 IN PRTL_SPLAY_LINKS Links,
00504 IN OUT PRTL_SPLAY_LINKS *Root
00505 )
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531 {
00532 PRTL_SPLAY_LINKS Predecessor;
00533 PRTL_SPLAY_LINKS Parent;
00534 PRTL_SPLAY_LINKS Child;
00535
00536 PRTL_SPLAY_LINKS *ParentChildPtr;
00537
00538
00539
00540
00541
00542
00543
00544
if ((RtlLeftChild(Links) !=
NULL) && (RtlRightChild(Links) !=
NULL)) {
00545
00546
00547
00548
00549
00550 Predecessor =
RtlSubtreePredecessor(Links);
00551
00552
if (RtlIsRoot(Links)) {
00553
00554
00555
00556
00557
00558
00559 *Root = Predecessor;
00560 }
00561
00562
SwapSplayLinks(Predecessor, Links);
00563
00564 }
00565
00566
00567
00568
00569
00570
00571
00572
00573
if ((RtlLeftChild(Links) ==
NULL) && (RtlRightChild(Links) ==
NULL)) {
00574
00575
00576
00577
00578
00579
if (RtlIsRoot(Links)) {
00580
00581 *Root =
NULL;
00582
00583
return;
00584 }
00585
00586
00587
00588
00589
00590
00591 ParentChildPtr =
ParentsChildPointerAddress(Links);
00592 *ParentChildPtr =
NULL;
00593
00594
return;
00595 }
00596
00597
00598
00599
00600
00601
00602
00603
if (RtlLeftChild(Links) !=
NULL) {
00604 Child = RtlLeftChild(Links);
00605 }
else {
00606 Child = RtlRightChild(Links);
00607 }
00608
00609
00610
00611
00612
00613
00614
if (RtlIsRoot(Links)) {
00615 Child->Parent = Child;
00616
00617 *Root = Child;
00618
00619
return;
00620 }
00621
00622
00623
00624
00625
00626
00627 ParentChildPtr =
ParentsChildPointerAddress(Links);
00628 *ParentChildPtr = Child;
00629 Child->Parent = Links->Parent;
00630
00631
return;
00632 }
00633
00634
00635 PRTL_SPLAY_LINKS
00636 RtlSubtreeSuccessor (
00637 IN PRTL_SPLAY_LINKS Links
00638 )
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659 {
00660 PRTL_SPLAY_LINKS
Ptr;
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
if ((
Ptr = RtlRightChild(Links)) !=
NULL) {
00678
00679
while (RtlLeftChild(
Ptr) !=
NULL) {
00680
Ptr = RtlLeftChild(
Ptr);
00681 }
00682
00683
return Ptr;
00684
00685 }
00686
00687
00688
00689
00690
00691
00692
return NULL;
00693
00694 }
00695
00696
00697 PRTL_SPLAY_LINKS
00698 RtlSubtreePredecessor (
00699 IN PRTL_SPLAY_LINKS Links
00700 )
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721 {
00722 PRTL_SPLAY_LINKS
Ptr;
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
if ((
Ptr = RtlLeftChild(Links)) !=
NULL) {
00739
00740
while (RtlRightChild(
Ptr) !=
NULL) {
00741
Ptr = RtlRightChild(
Ptr);
00742 }
00743
00744
return Ptr;
00745
00746 }
00747
00748
00749
00750
00751
00752
00753
return NULL;
00754
00755 }
00756
00757
00758 PRTL_SPLAY_LINKS
00759 RtlRealSuccessor (
00760 IN PRTL_SPLAY_LINKS Links
00761 )
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781 {
00782 PRTL_SPLAY_LINKS
Ptr;
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
if ((
Ptr = RtlRightChild(Links)) !=
NULL) {
00800
00801
while (RtlLeftChild(
Ptr) !=
NULL) {
00802
Ptr = RtlLeftChild(
Ptr);
00803 }
00804
00805
return Ptr;
00806
00807 }
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
Ptr = Links;
00823
while (RtlIsRightChild(
Ptr)) {
00824
Ptr = RtlParent(
Ptr);
00825 }
00826
00827
if (RtlIsLeftChild(
Ptr)) {
00828
return RtlParent(
Ptr);
00829 }
00830
00831
00832
00833
00834
00835
00836
return NULL;
00837
00838 }
00839
00840
00841 PRTL_SPLAY_LINKS
00842 RtlRealPredecessor (
00843 IN PRTL_SPLAY_LINKS Links
00844 )
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865 {
00866 PRTL_SPLAY_LINKS
Ptr;
00867
00868
00869
00870
00871
00872
00873
00874
00875
00876
00877
00878
00879
00880
00881
00882
if ((
Ptr = RtlLeftChild(Links)) !=
NULL) {
00883
00884
while (RtlRightChild(
Ptr) !=
NULL) {
00885
Ptr = RtlRightChild(
Ptr);
00886 }
00887
00888
return Ptr;
00889
00890 }
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905
Ptr = Links;
00906
while (RtlIsLeftChild(
Ptr)) {
00907
Ptr = RtlParent(
Ptr);
00908 }
00909
00910
if (RtlIsRightChild(
Ptr)) {
00911
return RtlParent(
Ptr);
00912 }
00913
00914
00915
00916
00917
00918
00919
return NULL;
00920
00921 }
00922
00923
00924
VOID
00925 SwapSplayLinks (
00926 IN PRTL_SPLAY_LINKS Link1,
00927 IN PRTL_SPLAY_LINKS Link2
00928 )
00929
00930 {
00931 PRTL_SPLAY_LINKS *Parent1ChildPtr;
00932 PRTL_SPLAY_LINKS *Parent2ChildPtr;
00933
00934
00935
00936
00937
00938
00939
00940
00941
00942
00943
00944
00945
00946
00947
00948
00949
00950
00951
00952
if ((RtlIsRoot(Link1)) || (RtlParent(Link2) == Link1)) {
00953
SwapPointers(Link1, Link2);
00954 }
00955
00956
00957
00958
00959
00960
00961
00962
00963
00964
00965
00966
00967
00968
if (RtlParent(Link1) != Link2) {
00969
00970
if (!RtlIsRoot(Link2)) {
00971
00972
00973
00974
00975
00976
00977
00978
00979
00980 Parent1ChildPtr =
ParentsChildPointerAddress(Link1);
00981 Parent2ChildPtr =
ParentsChildPointerAddress(Link2);
00982
00983
SwapPointers(*Parent1ChildPtr, *Parent2ChildPtr);
00984
00985
SwapPointers(Link1->Parent, Link2->Parent);
00986
00987 }
else {
00988
00989
00990
00991
00992
00993
00994
00995
00996
00997 Parent1ChildPtr =
ParentsChildPointerAddress(Link1);
00998 *Parent1ChildPtr = Link2;
00999
01000 Link2->Parent = Link1->Parent;
01001
01002 Link1->Parent = Link1;
01003
01004 }
01005
01006
01007
01008
01009
01010
01011
01012
SwapPointers(Link1->LeftChild, Link2->LeftChild);
01013
SwapPointers(Link1->RightChild, Link2->RightChild);
01014
01015 }
else {
01016
01017
if (!RtlIsRoot(Link2)) {
01018
01019
01020
01021
01022
01023
01024
01025
01026 Parent2ChildPtr =
ParentsChildPointerAddress(Link2);
01027 *Parent2ChildPtr = Link1;
01028
01029 Link1->Parent = Link2->Parent;
01030
01031 }
else {
01032
01033
01034
01035
01036
01037
01038
01039 Link1->Parent = Link1;
01040
01041 }
01042
01043
01044
01045
01046
01047
01048
01049
01050
01051
01052
SwapPointers(Link1->LeftChild, Link2->LeftChild);
01053
SwapPointers(Link1->RightChild, Link2->RightChild);
01054
01055
if (Link1->LeftChild == Link1) {
01056 Link1->LeftChild = Link2;
01057 }
else {
01058 Link1->RightChild = Link2;
01059 }
01060
01061 }
01062
01063
01064
01065
01066
01067
01068
01069
if (Link1->LeftChild !=
NULL) {Link1->LeftChild->Parent = Link1;}
01070
if (Link1->RightChild !=
NULL) {Link1->RightChild->Parent = Link1;}
01071
if (Link2->LeftChild !=
NULL) {Link2->LeftChild->Parent = Link2;}
01072
if (Link2->RightChild !=
NULL) {Link2->RightChild->Parent = Link2;}
01073
01074 }