]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseLib/LinkedList.c
Code scrub with spec.
[mirror_edk2.git] / MdePkg / Library / BaseLib / LinkedList.c
CommitLineData
e1f414b6 1/** @file\r
2 Linked List Library Functions.\r
3\r
aa0583c7 4 Copyright (c) 2006 - 2008, Intel Corporation<BR>\r
e1f414b6 5 All rights reserved. This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
e1f414b6 13**/\r
14\r
e1f414b6 15#include "BaseLibInternals.h"\r
16\r
17/**\r
18 Worker function that locates the Node in the List\r
19\r
20 By searching the List, finds the location of the Node in List. At the same time,\r
21 verifies the validity of this list.\r
22\r
23 If List is NULL, then ASSERT().\r
24 If List->ForwardLink is NULL, then ASSERT().\r
24dcb5e5 25 If List->BackLink is NULL, then ASSERT().\r
e1f414b6 26 If Node is NULL, then ASSERT();\r
27 If PcdMaximumLinkedListLenth is not zero, and prior to insertion the number\r
28 of nodes in ListHead, including the ListHead node, is greater than or\r
29 equal to PcdMaximumLinkedListLength, then ASSERT().\r
30\r
31 @param List A pointer to a node in a linked list.\r
32 @param Node A pointer to one nod.\r
33\r
34 @retval TRUE Node is in List\r
35 @retval FALSE Node isn't in List, or List is invalid\r
36\r
37**/\r
38BOOLEAN\r
38bbd3d9 39EFIAPI\r
e1f414b6 40IsNodeInList (\r
41 IN CONST LIST_ENTRY *List,\r
42 IN CONST LIST_ENTRY *Node\r
43 )\r
44{\r
45 UINTN Count;\r
46 CONST LIST_ENTRY *Ptr;\r
47 BOOLEAN Found;\r
48\r
49 //\r
50 // Test the validity of List and Node\r
51 //\r
52 ASSERT (List != NULL);\r
53 ASSERT (List->ForwardLink != NULL);\r
54 ASSERT (List->BackLink != NULL);\r
55 ASSERT (Node != NULL);\r
56\r
57 Count = PcdGet32 (PcdMaximumLinkedListLength);\r
58\r
59 Ptr = List;\r
60 do {\r
61 Ptr = Ptr->ForwardLink;\r
62 Count--;\r
63 } while ((Ptr != List) && (Ptr != Node) && (Count > 0));\r
64 Found = (BOOLEAN)(Ptr == Node);\r
65\r
66 if (PcdGet32 (PcdMaximumLinkedListLength) > 0) {\r
67 while ((Count > 0) && (Ptr != List)) {\r
68 Ptr = Ptr->ForwardLink;\r
69 Count--;\r
70 }\r
71 ASSERT (Count > 0);\r
72 }\r
73\r
74 return Found;\r
75}\r
76\r
77/**\r
78 Initializes the head node of a doubly linked list, and returns the pointer to\r
79 the head node of the doubly linked list.\r
80\r
81 Initializes the forward and backward links of a new linked list. After\r
82 initializing a linked list with this function, the other linked list\r
83 functions may be used to add and remove nodes from the linked list. It is up\r
84 to the caller of this function to allocate the memory for ListHead.\r
85\r
86 If ListHead is NULL, then ASSERT().\r
87\r
2a254b90 88 @param List A pointer to the head node of a new doubly linked list.\r
e1f414b6 89\r
90 @return ListHead\r
91\r
92**/\r
93LIST_ENTRY *\r
94EFIAPI\r
95InitializeListHead (\r
96 IN OUT LIST_ENTRY *List\r
97 )\r
98\r
99{\r
100 ASSERT (List != NULL);\r
101\r
102 List->ForwardLink = List;\r
103 List->BackLink = List;\r
104 return List;\r
105}\r
106\r
107/**\r
108 Adds a node to the beginning of a doubly linked list, and returns the pointer\r
109 to the head node of the doubly linked list.\r
110\r
111 Adds the node Entry at the beginning of the doubly linked list denoted by\r
112 ListHead, and returns ListHead.\r
113\r
114 If ListHead is NULL, then ASSERT().\r
115 If Entry is NULL, then ASSERT().\r
116 If ListHead was not initialized with InitializeListHead(), then ASSERT().\r
117 If PcdMaximumLinkedListLenth is not zero, and prior to insertion the number\r
118 of nodes in ListHead, including the ListHead node, is greater than or\r
119 equal to PcdMaximumLinkedListLength, then ASSERT().\r
120\r
2a254b90 121 @param List A pointer to the head node of a doubly linked list.\r
e1f414b6 122 @param Entry A pointer to a node that is to be inserted at the beginning\r
123 of a doubly linked list.\r
124\r
125 @return ListHead\r
126\r
127**/\r
128LIST_ENTRY *\r
129EFIAPI\r
130InsertHeadList (\r
131 IN OUT LIST_ENTRY *List,\r
132 IN OUT LIST_ENTRY *Entry\r
133 )\r
134{\r
135 //\r
136 // ASSERT List not too long and Entry is not one of the nodes of List\r
137 //\r
138 ASSERT (!IsNodeInList (List, Entry));\r
139\r
140 Entry->ForwardLink = List->ForwardLink;\r
141 Entry->BackLink = List;\r
142 Entry->ForwardLink->BackLink = Entry;\r
143 List->ForwardLink = Entry;\r
144 return List;\r
145}\r
146\r
147/**\r
148 Adds a node to the end of a doubly linked list, and returns the pointer to\r
149 the head node of the doubly linked list.\r
150\r
151 Adds the node Entry to the end of the doubly linked list denoted by ListHead,\r
152 and returns ListHead.\r
153\r
154 If ListHead is NULL, then ASSERT().\r
155 If Entry is NULL, then ASSERT().\r
156 If ListHead was not initialized with InitializeListHead(), then ASSERT().\r
157 If PcdMaximumLinkedListLenth is not zero, and prior to insertion the number\r
158 of nodes in ListHead, including the ListHead node, is greater than or\r
159 equal to PcdMaximumLinkedListLength, then ASSERT().\r
160\r
2a254b90 161 @param List A pointer to the head node of a doubly linked list.\r
e1f414b6 162 @param Entry A pointer to a node that is to be added at the end of the\r
163 doubly linked list.\r
164\r
165 @return ListHead\r
166\r
167**/\r
168LIST_ENTRY *\r
169EFIAPI\r
170InsertTailList (\r
171 IN OUT LIST_ENTRY *List,\r
172 IN OUT LIST_ENTRY *Entry\r
173 )\r
174{\r
175 //\r
176 // ASSERT List not too long and Entry is not one of the nodes of List\r
177 //\r
178 ASSERT (!IsNodeInList (List, Entry));\r
179\r
180 Entry->ForwardLink = List;\r
181 Entry->BackLink = List->BackLink;\r
182 Entry->BackLink->ForwardLink = Entry;\r
183 List->BackLink = Entry;\r
184 return List;\r
185}\r
186\r
187/**\r
188 Retrieves the first node of a doubly linked list.\r
189\r
190 Returns the first node of a doubly linked list. List must have been\r
191 initialized with InitializeListHead(). If List is empty, then NULL is\r
192 returned.\r
193\r
194 If List is NULL, then ASSERT().\r
195 If List was not initialized with InitializeListHead(), then ASSERT().\r
196 If PcdMaximumLinkedListLenth is not zero, and the number of nodes\r
197 in List, including the List node, is greater than or equal to\r
198 PcdMaximumLinkedListLength, then ASSERT().\r
199\r
200 @param List A pointer to the head node of a doubly linked list.\r
201\r
202 @return The first node of a doubly linked list.\r
203 @retval NULL The list is empty.\r
204\r
205**/\r
206LIST_ENTRY *\r
207EFIAPI\r
208GetFirstNode (\r
209 IN CONST LIST_ENTRY *List\r
210 )\r
211{\r
212 //\r
213 // ASSERT List not too long\r
214 //\r
215 ASSERT (IsNodeInList (List, List));\r
216\r
217 return List->ForwardLink;\r
218}\r
219\r
220/**\r
221 Retrieves the next node of a doubly linked list.\r
222\r
223 Returns the node of a doubly linked list that follows Node. List must have\r
224 been initialized with InitializeListHead(). If List is empty, then List is\r
225 returned.\r
226\r
227 If List is NULL, then ASSERT().\r
228 If Node is NULL, then ASSERT().\r
229 If List was not initialized with InitializeListHead(), then ASSERT().\r
230 If PcdMaximumLinkedListLenth is not zero, and List contains more than\r
231 PcdMaximumLinkedListLenth nodes, then ASSERT().\r
232 If Node is not a node in List, then ASSERT().\r
233\r
234 @param List A pointer to the head node of a doubly linked list.\r
235 @param Node A pointer to a node in the doubly linked list.\r
236\r
237 @return Pointer to the next node if one exists. Otherwise a null value which\r
238 is actually List is returned.\r
239\r
240**/\r
241LIST_ENTRY *\r
242EFIAPI\r
243GetNextNode (\r
244 IN CONST LIST_ENTRY *List,\r
245 IN CONST LIST_ENTRY *Node\r
246 )\r
247{\r
248 //\r
249 // ASSERT List not too long and Node is one of the nodes of List\r
250 //\r
251 ASSERT (IsNodeInList (List, Node));\r
252\r
253 return Node->ForwardLink;\r
254}\r
255\r
256/**\r
257 Checks to see if a doubly linked list is empty or not.\r
258\r
259 Checks to see if the doubly linked list is empty. If the linked list contains\r
260 zero nodes, this function returns TRUE. Otherwise, it returns FALSE.\r
261\r
262 If ListHead is NULL, then ASSERT().\r
263 If ListHead was not initialized with InitializeListHead(), then ASSERT().\r
264 If PcdMaximumLinkedListLenth is not zero, and the number of nodes\r
265 in List, including the List node, is greater than or equal to\r
266 PcdMaximumLinkedListLength, then ASSERT().\r
267\r
2a254b90 268 @param List A pointer to the head node of a doubly linked list.\r
e1f414b6 269\r
270 @retval TRUE The linked list is empty.\r
271 @retval FALSE The linked list is not empty.\r
272\r
273**/\r
274BOOLEAN\r
275EFIAPI\r
276IsListEmpty (\r
277 IN CONST LIST_ENTRY *List\r
278 )\r
279{\r
280 //\r
281 // ASSERT List not too long\r
282 //\r
283 ASSERT (IsNodeInList (List, List));\r
284\r
285 return (BOOLEAN)(List->ForwardLink == List);\r
286}\r
287\r
288/**\r
aa0583c7 289 Determines if a node in a doubly linked list is the head node of a the same\r
290 doubly linked list. This function is typically used to terminate a loop that\r
291 traverses all the nodes in a doubly linked list starting with the head node.\r
e1f414b6 292\r
aa0583c7 293 Returns TRUE if Node is equal to List. Returns FALSE if Node is one of the\r
294 nodes in the doubly linked list specified by List. List must have been\r
295 initialized with InitializeListHead().\r
e1f414b6 296\r
297 If List is NULL, then ASSERT().\r
298 If Node is NULL, then ASSERT().\r
299 If List was not initialized with InitializeListHead(), then ASSERT().\r
300 If PcdMaximumLinkedListLenth is not zero, and the number of nodes\r
301 in List, including the List node, is greater than or equal to\r
302 PcdMaximumLinkedListLength, then ASSERT().\r
303 If Node is not a node in List and Node is not equal to List, then ASSERT().\r
304\r
305 @param List A pointer to the head node of a doubly linked list.\r
306 @param Node A pointer to a node in the doubly linked list.\r
307\r
308 @retval TRUE Node is one of the nodes in the doubly linked list.\r
309 @retval FALSE Node is not one of the nodes in the doubly linked list.\r
310\r
311**/\r
312BOOLEAN\r
313EFIAPI\r
314IsNull (\r
315 IN CONST LIST_ENTRY *List,\r
316 IN CONST LIST_ENTRY *Node\r
317 )\r
318{\r
319 //\r
320 // ASSERT List not too long and Node is one of the nodes of List\r
321 //\r
322 ASSERT (IsNodeInList (List, Node));\r
323\r
324 return (BOOLEAN)(Node == List);\r
325}\r
326\r
327/**\r
328 Determines if a node the last node in a doubly linked list.\r
329\r
330 Returns TRUE if Node is the last node in the doubly linked list specified by\r
331 List. Otherwise, FALSE is returned. List must have been initialized with\r
332 InitializeListHead().\r
333\r
334 If List is NULL, then ASSERT().\r
335 If Node is NULL, then ASSERT().\r
336 If List was not initialized with InitializeListHead(), then ASSERT().\r
337 If PcdMaximumLinkedListLenth is not zero, and the number of nodes\r
338 in List, including the List node, is greater than or equal to\r
339 PcdMaximumLinkedListLength, then ASSERT().\r
340 If Node is not a node in List, then ASSERT().\r
341\r
342 @param List A pointer to the head node of a doubly linked list.\r
343 @param Node A pointer to a node in the doubly linked list.\r
344\r
345 @retval TRUE Node is the last node in the linked list.\r
346 @retval FALSE Node is not the last node in the linked list.\r
347\r
348**/\r
349BOOLEAN\r
350EFIAPI\r
351IsNodeAtEnd (\r
352 IN CONST LIST_ENTRY *List,\r
353 IN CONST LIST_ENTRY *Node\r
354 )\r
355{\r
356 //\r
357 // ASSERT List not too long and Node is one of the nodes of List\r
358 //\r
359 ASSERT (IsNodeInList (List, Node));\r
360\r
361 return (BOOLEAN)(!IsNull (List, Node) && List->BackLink == Node);\r
362}\r
363\r
364/**\r
365 Swaps the location of two nodes in a doubly linked list, and returns the\r
366 first node after the swap.\r
367\r
368 If FirstEntry is identical to SecondEntry, then SecondEntry is returned.\r
369 Otherwise, the location of the FirstEntry node is swapped with the location\r
370 of the SecondEntry node in a doubly linked list. SecondEntry must be in the\r
371 same double linked list as FirstEntry and that double linked list must have\r
372 been initialized with InitializeListHead(). SecondEntry is returned after the\r
373 nodes are swapped.\r
374\r
375 If FirstEntry is NULL, then ASSERT().\r
376 If SecondEntry is NULL, then ASSERT().\r
377 If SecondEntry and FirstEntry are not in the same linked list, then ASSERT().\r
378 If PcdMaximumLinkedListLength is not zero, and the number of nodes in the\r
379 linked list containing the FirstEntry and SecondEntry nodes, including\r
380 the FirstEntry and SecondEntry nodes, is greater than or equal to\r
381 PcdMaximumLinkedListLength, then ASSERT().\r
382\r
383 @param FirstEntry A pointer to a node in a linked list.\r
384 @param SecondEntry A pointer to another node in the same linked list.\r
38bbd3d9 385 \r
24dcb5e5 386 @return SecondEntry after the nodes are swapped\r
e1f414b6 387\r
388**/\r
389LIST_ENTRY *\r
390EFIAPI\r
391SwapListEntries (\r
392 IN OUT LIST_ENTRY *FirstEntry,\r
393 IN OUT LIST_ENTRY *SecondEntry\r
394 )\r
395{\r
396 LIST_ENTRY *Ptr;\r
397\r
398 if (FirstEntry == SecondEntry) {\r
399 return SecondEntry;\r
400 }\r
401\r
402 //\r
403 // ASSERT Entry1 and Entry2 are in the same linked list\r
404 //\r
405 ASSERT (IsNodeInList (FirstEntry, SecondEntry));\r
406\r
407 //\r
408 // Ptr is the node pointed to by FirstEntry->ForwardLink\r
409 //\r
410 Ptr = RemoveEntryList (FirstEntry);\r
411\r
412 //\r
24dcb5e5 413 // If FirstEntry immediately follows SecondEntry, FirstEntry will be placed\r
e1f414b6 414 // immediately in front of SecondEntry\r
415 //\r
416 if (Ptr->BackLink == SecondEntry) {\r
417 return InsertTailList (SecondEntry, FirstEntry);\r
418 }\r
419\r
420 //\r
421 // Ptr == SecondEntry means SecondEntry immediately follows FirstEntry,\r
422 // then there are no further steps necessary\r
423 //\r
424 if (Ptr == InsertHeadList (SecondEntry, FirstEntry)) {\r
425 return Ptr;\r
426 }\r
427\r
428 //\r
429 // Move SecondEntry to the front of Ptr\r
430 //\r
431 RemoveEntryList (SecondEntry);\r
432 InsertTailList (Ptr, SecondEntry);\r
433 return SecondEntry;\r
434}\r
435\r
436/**\r
437 Removes a node from a doubly linked list, and returns the node that follows\r
438 the removed node.\r
439\r
440 Removes the node Entry from a doubly linked list. It is up to the caller of\r
441 this function to release the memory used by this node if that is required. On\r
442 exit, the node following Entry in the doubly linked list is returned. If\r
443 Entry is the only node in the linked list, then the head node of the linked\r
444 list is returned.\r
445\r
446 If Entry is NULL, then ASSERT().\r
447 If Entry is the head node of an empty list, then ASSERT().\r
448 If PcdMaximumLinkedListLength is not zero, and the number of nodes in the\r
449 linked list containing Entry, including the Entry node, is greater than\r
450 or equal to PcdMaximumLinkedListLength, then ASSERT().\r
451\r
452 @param Entry A pointer to a node in a linked list\r
453\r
24dcb5e5 454 @return The node following Entry in the doubly linked list.\r
455 If Entry is the only node in the linked list, then\r
456 the head node of the linked list is returned.\r
e1f414b6 457\r
458**/\r
459LIST_ENTRY *\r
460EFIAPI\r
461RemoveEntryList (\r
462 IN CONST LIST_ENTRY *Entry\r
463 )\r
464{\r
465 ASSERT (!IsListEmpty (Entry));\r
466\r
467 Entry->ForwardLink->BackLink = Entry->BackLink;\r
468 Entry->BackLink->ForwardLink = Entry->ForwardLink;\r
469 return Entry->ForwardLink;\r
470}\r