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