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