]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseLib/LinkedList.c
Removed MdePkg usage of ModuleName: in file headers
[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
44IsNodeInList (\r
45 IN CONST LIST_ENTRY *List,\r
46 IN CONST LIST_ENTRY *Node\r
47 )\r
48{\r
49 UINTN Count;\r
50 CONST LIST_ENTRY *Ptr;\r
51 BOOLEAN Found;\r
52\r
53 //\r
54 // Test the validity of List and Node\r
55 //\r
56 ASSERT (List != NULL);\r
57 ASSERT (List->ForwardLink != NULL);\r
58 ASSERT (List->BackLink != NULL);\r
59 ASSERT (Node != NULL);\r
60\r
61 Count = PcdGet32 (PcdMaximumLinkedListLength);\r
62\r
63 Ptr = List;\r
64 do {\r
65 Ptr = Ptr->ForwardLink;\r
66 Count--;\r
67 } while ((Ptr != List) && (Ptr != Node) && (Count > 0));\r
68 Found = (BOOLEAN)(Ptr == Node);\r
69\r
70 if (PcdGet32 (PcdMaximumLinkedListLength) > 0) {\r
71 while ((Count > 0) && (Ptr != List)) {\r
72 Ptr = Ptr->ForwardLink;\r
73 Count--;\r
74 }\r
75 ASSERT (Count > 0);\r
76 }\r
77\r
78 return Found;\r
79}\r
80\r
81/**\r
82 Initializes the head node of a doubly linked list, and returns the pointer to\r
83 the head node of the doubly linked list.\r
84\r
85 Initializes the forward and backward links of a new linked list. After\r
86 initializing a linked list with this function, the other linked list\r
87 functions may be used to add and remove nodes from the linked list. It is up\r
88 to the caller of this function to allocate the memory for ListHead.\r
89\r
90 If ListHead is NULL, then ASSERT().\r
91\r
92 @param ListHead A pointer to the head node of a new doubly linked list.\r
93\r
94 @return ListHead\r
95\r
96**/\r
97LIST_ENTRY *\r
98EFIAPI\r
99InitializeListHead (\r
100 IN OUT LIST_ENTRY *List\r
101 )\r
102\r
103{\r
104 ASSERT (List != NULL);\r
105\r
106 List->ForwardLink = List;\r
107 List->BackLink = List;\r
108 return List;\r
109}\r
110\r
111/**\r
112 Adds a node to the beginning of a doubly linked list, and returns the pointer\r
113 to the head node of the doubly linked list.\r
114\r
115 Adds the node Entry at the beginning of the doubly linked list denoted by\r
116 ListHead, and returns ListHead.\r
117\r
118 If ListHead is NULL, then ASSERT().\r
119 If Entry is NULL, then ASSERT().\r
120 If ListHead was not initialized with InitializeListHead(), then ASSERT().\r
121 If PcdMaximumLinkedListLenth is not zero, and prior to insertion the number\r
122 of nodes in ListHead, including the ListHead node, is greater than or\r
123 equal to PcdMaximumLinkedListLength, then ASSERT().\r
124\r
125 @param ListHead A pointer to the head node of a doubly linked list.\r
126 @param Entry A pointer to a node that is to be inserted at the beginning\r
127 of a doubly linked list.\r
128\r
129 @return ListHead\r
130\r
131**/\r
132LIST_ENTRY *\r
133EFIAPI\r
134InsertHeadList (\r
135 IN OUT LIST_ENTRY *List,\r
136 IN OUT LIST_ENTRY *Entry\r
137 )\r
138{\r
139 //\r
140 // ASSERT List not too long and Entry is not one of the nodes of List\r
141 //\r
142 ASSERT (!IsNodeInList (List, Entry));\r
143\r
144 Entry->ForwardLink = List->ForwardLink;\r
145 Entry->BackLink = List;\r
146 Entry->ForwardLink->BackLink = Entry;\r
147 List->ForwardLink = Entry;\r
148 return List;\r
149}\r
150\r
151/**\r
152 Adds a node to the end of a doubly linked list, and returns the pointer to\r
153 the head node of the doubly linked list.\r
154\r
155 Adds the node Entry to the end of the doubly linked list denoted by ListHead,\r
156 and returns ListHead.\r
157\r
158 If ListHead is NULL, then ASSERT().\r
159 If Entry is NULL, then ASSERT().\r
160 If ListHead was not initialized with InitializeListHead(), then ASSERT().\r
161 If PcdMaximumLinkedListLenth is not zero, and prior to insertion the number\r
162 of nodes in ListHead, including the ListHead node, is greater than or\r
163 equal to PcdMaximumLinkedListLength, then ASSERT().\r
164\r
165 @param ListHead A pointer to the head node of a doubly linked list.\r
166 @param Entry A pointer to a node that is to be added at the end of the\r
167 doubly linked list.\r
168\r
169 @return ListHead\r
170\r
171**/\r
172LIST_ENTRY *\r
173EFIAPI\r
174InsertTailList (\r
175 IN OUT LIST_ENTRY *List,\r
176 IN OUT LIST_ENTRY *Entry\r
177 )\r
178{\r
179 //\r
180 // ASSERT List not too long and Entry is not one of the nodes of List\r
181 //\r
182 ASSERT (!IsNodeInList (List, Entry));\r
183\r
184 Entry->ForwardLink = List;\r
185 Entry->BackLink = List->BackLink;\r
186 Entry->BackLink->ForwardLink = Entry;\r
187 List->BackLink = Entry;\r
188 return List;\r
189}\r
190\r
191/**\r
192 Retrieves the first node of a doubly linked list.\r
193\r
194 Returns the first node of a doubly linked list. List must have been\r
195 initialized with InitializeListHead(). If List is empty, then NULL is\r
196 returned.\r
197\r
198 If List is NULL, then ASSERT().\r
199 If List was not initialized with InitializeListHead(), then ASSERT().\r
200 If PcdMaximumLinkedListLenth is not zero, and the number of nodes\r
201 in List, including the List node, is greater than or equal to\r
202 PcdMaximumLinkedListLength, then ASSERT().\r
203\r
204 @param List A pointer to the head node of a doubly linked list.\r
205\r
206 @return The first node of a doubly linked list.\r
207 @retval NULL The list is empty.\r
208\r
209**/\r
210LIST_ENTRY *\r
211EFIAPI\r
212GetFirstNode (\r
213 IN CONST LIST_ENTRY *List\r
214 )\r
215{\r
216 //\r
217 // ASSERT List not too long\r
218 //\r
219 ASSERT (IsNodeInList (List, List));\r
220\r
221 return List->ForwardLink;\r
222}\r
223\r
224/**\r
225 Retrieves the next node of a doubly linked list.\r
226\r
227 Returns the node of a doubly linked list that follows Node. List must have\r
228 been initialized with InitializeListHead(). If List is empty, then List is\r
229 returned.\r
230\r
231 If List is NULL, then ASSERT().\r
232 If Node is NULL, then ASSERT().\r
233 If List was not initialized with InitializeListHead(), then ASSERT().\r
234 If PcdMaximumLinkedListLenth is not zero, and List contains more than\r
235 PcdMaximumLinkedListLenth nodes, then ASSERT().\r
236 If Node is not a node in List, then ASSERT().\r
237\r
238 @param List A pointer to the head node of a doubly linked list.\r
239 @param Node A pointer to a node in the doubly linked list.\r
240\r
241 @return Pointer to the next node if one exists. Otherwise a null value which\r
242 is actually List is returned.\r
243\r
244**/\r
245LIST_ENTRY *\r
246EFIAPI\r
247GetNextNode (\r
248 IN CONST LIST_ENTRY *List,\r
249 IN CONST LIST_ENTRY *Node\r
250 )\r
251{\r
252 //\r
253 // ASSERT List not too long and Node is one of the nodes of List\r
254 //\r
255 ASSERT (IsNodeInList (List, Node));\r
256\r
257 return Node->ForwardLink;\r
258}\r
259\r
260/**\r
261 Checks to see if a doubly linked list is empty or not.\r
262\r
263 Checks to see if the doubly linked list is empty. If the linked list contains\r
264 zero nodes, this function returns TRUE. Otherwise, it returns FALSE.\r
265\r
266 If ListHead is NULL, then ASSERT().\r
267 If ListHead was not initialized with InitializeListHead(), then ASSERT().\r
268 If PcdMaximumLinkedListLenth is not zero, and the number of nodes\r
269 in List, including the List node, is greater than or equal to\r
270 PcdMaximumLinkedListLength, then ASSERT().\r
271\r
272 @param ListHead A pointer to the head node of a doubly linked list.\r
273\r
274 @retval TRUE The linked list is empty.\r
275 @retval FALSE The linked list is not empty.\r
276\r
277**/\r
278BOOLEAN\r
279EFIAPI\r
280IsListEmpty (\r
281 IN CONST LIST_ENTRY *List\r
282 )\r
283{\r
284 //\r
285 // ASSERT List not too long\r
286 //\r
287 ASSERT (IsNodeInList (List, List));\r
288\r
289 return (BOOLEAN)(List->ForwardLink == List);\r
290}\r
291\r
292/**\r
293 Determines if a node in a doubly linked list is null.\r
294\r
295 Returns FALSE if Node is one of the nodes in the doubly linked list specified\r
296 by List. Otherwise, TRUE is returned. List must have been initialized with\r
297 InitializeListHead().\r
298\r
299 If List is NULL, then ASSERT().\r
300 If Node is NULL, then ASSERT().\r
301 If List was not initialized with InitializeListHead(), then ASSERT().\r
302 If PcdMaximumLinkedListLenth is not zero, and the number of nodes\r
303 in List, including the List node, is greater than or equal to\r
304 PcdMaximumLinkedListLength, then ASSERT().\r
305 If Node is not a node in List and Node is not equal to List, then ASSERT().\r
306\r
307 @param List A pointer to the head node of a doubly linked list.\r
308 @param Node A pointer to a node in the doubly linked list.\r
309\r
310 @retval TRUE Node is one of the nodes in the doubly linked list.\r
311 @retval FALSE Node is not one of the nodes in the doubly linked list.\r
312\r
313**/\r
314BOOLEAN\r
315EFIAPI\r
316IsNull (\r
317 IN CONST LIST_ENTRY *List,\r
318 IN CONST LIST_ENTRY *Node\r
319 )\r
320{\r
321 //\r
322 // ASSERT List not too long and Node is one of the nodes of List\r
323 //\r
324 ASSERT (IsNodeInList (List, Node));\r
325\r
326 return (BOOLEAN)(Node == List);\r
327}\r
328\r
329/**\r
330 Determines if a node the last node in a doubly linked list.\r
331\r
332 Returns TRUE if Node is the last node in the doubly linked list specified by\r
333 List. Otherwise, FALSE is returned. List must have been initialized with\r
334 InitializeListHead().\r
335\r
336 If List is NULL, then ASSERT().\r
337 If Node is NULL, then ASSERT().\r
338 If List was not initialized with InitializeListHead(), then ASSERT().\r
339 If PcdMaximumLinkedListLenth is not zero, and the number of nodes\r
340 in List, including the List node, is greater than or equal to\r
341 PcdMaximumLinkedListLength, then ASSERT().\r
342 If Node is not a node in List, then ASSERT().\r
343\r
344 @param List A pointer to the head node of a doubly linked list.\r
345 @param Node A pointer to a node in the doubly linked list.\r
346\r
347 @retval TRUE Node is the last node in the linked list.\r
348 @retval FALSE Node is not the last node in the linked list.\r
349\r
350**/\r
351BOOLEAN\r
352EFIAPI\r
353IsNodeAtEnd (\r
354 IN CONST LIST_ENTRY *List,\r
355 IN CONST LIST_ENTRY *Node\r
356 )\r
357{\r
358 //\r
359 // ASSERT List not too long and Node is one of the nodes of List\r
360 //\r
361 ASSERT (IsNodeInList (List, Node));\r
362\r
363 return (BOOLEAN)(!IsNull (List, Node) && List->BackLink == Node);\r
364}\r
365\r
366/**\r
367 Swaps the location of two nodes in a doubly linked list, and returns the\r
368 first node after the swap.\r
369\r
370 If FirstEntry is identical to SecondEntry, then SecondEntry is returned.\r
371 Otherwise, the location of the FirstEntry node is swapped with the location\r
372 of the SecondEntry node in a doubly linked list. SecondEntry must be in the\r
373 same double linked list as FirstEntry and that double linked list must have\r
374 been initialized with InitializeListHead(). SecondEntry is returned after the\r
375 nodes are swapped.\r
376\r
377 If FirstEntry is NULL, then ASSERT().\r
378 If SecondEntry is NULL, then ASSERT().\r
379 If SecondEntry and FirstEntry are not in the same linked list, then ASSERT().\r
380 If PcdMaximumLinkedListLength is not zero, and the number of nodes in the\r
381 linked list containing the FirstEntry and SecondEntry nodes, including\r
382 the FirstEntry and SecondEntry nodes, is greater than or equal to\r
383 PcdMaximumLinkedListLength, then ASSERT().\r
384\r
385 @param FirstEntry A pointer to a node in a linked list.\r
386 @param SecondEntry A pointer to another node in the same linked list.\r
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
413 // If FirstEntry immediately follows SecondEntry, FirstEntry willl be placed\r
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
454 @return Entry\r
455\r
456**/\r
457LIST_ENTRY *\r
458EFIAPI\r
459RemoveEntryList (\r
460 IN CONST LIST_ENTRY *Entry\r
461 )\r
462{\r
463 ASSERT (!IsListEmpty (Entry));\r
464\r
465 Entry->ForwardLink->BackLink = Entry->BackLink;\r
466 Entry->BackLink->ForwardLink = Entry->ForwardLink;\r
467 return Entry->ForwardLink;\r
468}\r