2 * PROJECT: ReactOS Kernel
3 * LICENSE: BSD - See COPYING.ARM in the top level directory
4 * FILE: ntoskrnl/mm/ARM3/vadnode.c
5 * PURPOSE: ARM Memory Manager VAD Node Algorithms
6 * PROGRAMMERS: ReactOS Portable Systems Group
9 /* INCLUDES *******************************************************************/
15 #line 15 "ARMĀ³::VADNODE"
16 #define MODULE_INVOLVED_IN_ARM3
17 #include "../ARM3/miarm.h"
19 /* Include Mm version of AVL support */
20 #include "../ARM3/miavl.h"
21 #include "../../../lib/rtl/avlsupp.c"
23 /* FUNCTIONS ******************************************************************/
27 MiLocateAddress(IN PVOID VirtualAddress
)
31 PMM_AVL_TABLE Table
= &PsGetCurrentProcess()->VadRoot
;
32 TABLE_SEARCH_RESULT SearchResult
;
34 /* Start with the the hint */
35 FoundVad
= (PMMVAD
)Table
->NodeHint
;
36 if (!FoundVad
) return NULL
;
38 /* Check if this VPN is in the hint, if so, use it */
39 Vpn
= (ULONG_PTR
)VirtualAddress
>> PAGE_SHIFT
;
40 if ((Vpn
>= FoundVad
->StartingVpn
) && (Vpn
<= FoundVad
->EndingVpn
)) return FoundVad
;
42 /* VAD hint didn't work, go look for it */
43 SearchResult
= RtlpFindAvlTableNodeOrParent(Table
,
45 (PMMADDRESS_NODE
*)&FoundVad
);
46 if (SearchResult
!= TableFoundNode
) return NULL
;
48 /* We found it, update the hint */
49 ASSERT(FoundVad
!= NULL
);
50 ASSERT((Vpn
>= FoundVad
->StartingVpn
) && (Vpn
<= FoundVad
->EndingVpn
));
51 Table
->NodeHint
= FoundVad
;
57 MiCheckForConflictingNode(IN ULONG_PTR StartVpn
,
59 IN PMM_AVL_TABLE Table
)
61 PMMADDRESS_NODE CurrentNode
;
63 /* If the tree is empty, there is no conflict */
64 if (!Table
->NumberGenericTableElements
) return NULL
;
66 /* Start looping from the right */
67 CurrentNode
= RtlRightChildAvl(&Table
->BalancedRoot
);
68 ASSERT(CurrentNode
!= NULL
);
71 /* This address comes after */
72 if (StartVpn
> CurrentNode
->EndingVpn
)
74 /* Keep searching on the right */
75 CurrentNode
= RtlRightChildAvl(CurrentNode
);
77 else if (EndVpn
< CurrentNode
->StartingVpn
)
79 /* This address ends before the node starts, search on the left */
80 CurrentNode
= RtlLeftChildAvl(CurrentNode
);
84 /* This address is part of this node, return it */
89 /* Return either the conflicting node, or no node at all */
95 MiInsertNode(IN PMMADDRESS_NODE NewNode
,
96 IN PMM_AVL_TABLE Table
)
98 PMMADDRESS_NODE NodeOrParent
= NULL
;
99 TABLE_SEARCH_RESULT Result
;
101 /* Find the node's parent, and where to insert this node */
102 Result
= RtlpFindAvlTableNodeOrParent(Table
,
103 (PVOID
)NewNode
->StartingVpn
,
106 /* Insert it into the tree */
107 RtlpInsertAvlTreeNode(Table
, NewNode
, NodeOrParent
, Result
);
112 MiGetPreviousNode(IN PMMADDRESS_NODE Node
)
114 PMMADDRESS_NODE Parent
;
116 /* Get the left child */
117 if (RtlLeftChildAvl(Node
))
119 /* Get right-most child */
120 Node
= RtlLeftChildAvl(Node
);
121 while (RtlRightChildAvl(Node
)) Node
= RtlRightChildAvl(Node
);
125 Parent
= RtlParentAvl(Node
);
126 ASSERT(Parent
!= NULL
);
127 while (Parent
!= Node
)
129 /* The parent should be a right child, return the real predecessor */
130 if (RtlIsRightChildAvl(Node
))
132 /* Return it unless it's the root */
133 if (Parent
== RtlParentAvl(Parent
)) Parent
= NULL
;
137 /* Keep lopping until we find our parent */
139 Parent
= RtlParentAvl(Node
);
148 MiFindEmptyAddressRangeDownTree(IN SIZE_T Length
,
149 IN ULONG_PTR BoundaryAddress
,
150 IN ULONG_PTR Alignment
,
151 IN PMM_AVL_TABLE Table
,
154 PMMADDRESS_NODE Node
, PreviousNode
;
155 ULONG_PTR CandidateAddress
, EndAddress
;
156 ULONG AlignEndVpn
, CandidateVpn
, BoundaryVpn
, LowestVpn
, StartVpn
, EndVpn
;
157 PFN_NUMBER PageCount
;
160 ASSERT(BoundaryAddress
);
161 ASSERT(BoundaryAddress
<= ((ULONG_PTR
)MM_HIGHEST_VAD_ADDRESS
+ 1));
163 /* Compute page length, make sure the boundary address is valid */
164 Length
= PAGE_ROUND_UP(Length
);
165 if ((BoundaryAddress
+ 1) < Length
) return STATUS_NO_MEMORY
;
167 /* Compute the highest address to start at */
168 CandidateAddress
= ROUND_UP(BoundaryAddress
+ 1 - Length
, Alignment
);
170 /* Check if the table is empty */
171 if (!Table
->NumberGenericTableElements
)
173 /* Tree is empty, the candidate address is already the best one */
174 *Base
= CandidateAddress
;
175 return STATUS_SUCCESS
;
178 /* Starting from the root, go down until the right-most child */
179 Node
= RtlRightChildAvl(&Table
->BalancedRoot
);
180 while (RtlRightChildAvl(Node
)) Node
= RtlRightChildAvl(Node
);
182 /* Get the aligned ending address of this VPN */
183 EndAddress
= ROUND_UP((Node
->EndingVpn
<< PAGE_SHIFT
) | (PAGE_SIZE
- 1),
186 /* Can we fit the address without overflowing into the node? */
187 if ((EndAddress
< BoundaryAddress
) &&
188 ((BoundaryAddress
- EndAddress
) > Length
))
190 /* There is enough space to add our address */
191 *Base
= ROUND_UP(BoundaryAddress
- Length
, Alignment
);
192 return STATUS_SUCCESS
;
195 PageCount
= Length
>> PAGE_SHIFT
;
196 CandidateVpn
= CandidateAddress
>> PAGE_SHIFT
;
197 BoundaryVpn
= BoundaryAddress
>> PAGE_SHIFT
;
198 LowestVpn
= (ULONG_PTR
)MI_LOWEST_VAD_ADDRESS
>> PAGE_SHIFT
;
200 PreviousNode
= MiGetPreviousNode(Node
);
202 StartVpn
= Node
->StartingVpn
;
203 EndVpn
= PreviousNode
? PreviousNode
->EndingVpn
: 0;
204 AlignEndVpn
= ROUND_UP(EndVpn
+ 1, Alignment
>> PAGE_SHIFT
);
206 /* Loop until a gap is found */
207 for (PageCount
= Length
>> PAGE_SHIFT
,
208 CandidateVpn
= CandidateAddress
>> PAGE_SHIFT
,
209 BoundaryVpn
= BoundaryAddress
>> PAGE_SHIFT
,
210 LowestVpn
= (ULONG_PTR
)MI_LOWEST_VAD_ADDRESS
>> PAGE_SHIFT
,
211 PreviousNode
= MiGetPreviousNode(Node
),
212 StartVpn
= Node
->StartingVpn
,
213 EndVpn
= PreviousNode
? PreviousNode
->EndingVpn
: 0,
214 AlignEndVpn
= ROUND_UP(EndVpn
+ 1, Alignment
>> PAGE_SHIFT
);
217 PreviousNode
= MiGetPreviousNode(Node
),
218 StartVpn
= Node
->StartingVpn
,
219 EndVpn
= PreviousNode
? PreviousNode
->EndingVpn
: 0,
220 AlignEndVpn
= ROUND_UP(EndVpn
+ 1, Alignment
>> PAGE_SHIFT
))
222 /* Can we fit the address without overflowing into the node? */
223 if ((StartVpn
< CandidateVpn
) && ((StartVpn
- AlignEndVpn
) >= PageCount
))
225 /* Check if we can get our candidate address */
226 if ((CandidateVpn
> EndVpn
) && (BoundaryVpn
< StartVpn
))
229 *Base
= CandidateAddress
;
230 return STATUS_SUCCESS
;
233 /* Otherwise, can we fit it by changing the start address? */
234 if (StartVpn
> AlignEndVpn
)
236 /* It'll fit, compute the new base address for that to work */
237 *Base
= ROUND_UP((StartVpn
<< PAGE_SHIFT
) - Length
, Alignment
);
238 return STATUS_SUCCESS
;
242 PreviousNode
= MiGetPreviousNode(Node
);
243 StartVpn
= Node
->StartingVpn
;
244 EndVpn
= PreviousNode
? PreviousNode
->EndingVpn
: 0;
245 AlignEndVpn
= ROUND_UP(EndVpn
+ 1, Alignment
>> PAGE_SHIFT
);
248 /* See if we could squeeze into the last descriptor */
249 if ((StartVpn
> LowestVpn
) && ((StartVpn
- LowestVpn
) >= PageCount
))
251 /* Check if we can try our candidate address */
252 if (BoundaryVpn
< StartVpn
)
255 *Base
= CandidateAddress
;
256 return STATUS_SUCCESS
;
259 /* Otherwise, change the base address to what's needed to fit in */
260 *Base
= ROUND_UP((StartVpn
<< PAGE_SHIFT
) - Length
, Alignment
);
261 return STATUS_SUCCESS
;
264 /* No address space left at all */
265 return STATUS_NO_MEMORY
;