]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseLib/LinkedList.c
1. added functions header for BaseUefiDecompressLi
[mirror_edk2.git] / MdePkg / Library / BaseLib / LinkedList.c
CommitLineData
878ddf1f 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
13 Module Name: LinkedList.c\r
14\r
15**/\r
16\r
1ea5ca46 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
25 If List->backLink is NULL, then ASSERT().\r
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
878ddf1f 38BOOLEAN\r
39EFIAPI\r
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
dc530c7b 57 Count = PcdGet32 (PcdMaximumLinkedListLength);\r
7d7c2b46 58\r
878ddf1f 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
dc530c7b 66 if (PcdGet32 (PcdMaximumLinkedListLength) > 0) {\r
878ddf1f 67 while ((Count > 0) && (Ptr != List)) {\r
68 Ptr = Ptr->ForwardLink;\r
7d7c2b46 69 Count--;\r
878ddf1f 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
88 @param ListHead A pointer to the head node of a new doubly linked list.\r
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
30817eb4 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
878ddf1f 120\r
121 @param ListHead A pointer to the head node of a doubly linked list.\r
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
30817eb4 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
878ddf1f 160\r
161 @param ListHead A pointer to the head node of a doubly linked list.\r
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
30817eb4 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
878ddf1f 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
30817eb4 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
878ddf1f 267\r
268 @param ListHead A pointer to the head node of a doubly linked list.\r
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
289 Determines if a node in a doubly linked list is null.\r
290\r
291 Returns FALSE if Node is one of the nodes in the doubly linked list specified\r
292 by List. Otherwise, TRUE is returned. List must have been initialized with\r
293 InitializeListHead().\r
294\r
295 If List is NULL, then ASSERT().\r
296 If Node is NULL, then ASSERT().\r
297 If List was not initialized with InitializeListHead(), then ASSERT().\r
30817eb4 298 If PcdMaximumLinkedListLenth is not zero, and the number of nodes \r
299 in List, including the List node, is greater than or equal to \r
300 PcdMaximumLinkedListLength, then ASSERT().\r
878ddf1f 301 If Node is not a node in List and Node is not equal to List, then ASSERT().\r
302\r
303 @param List A pointer to the head node of a doubly linked list.\r
24e25d11 304 @param Node A pointer to a node in the doubly linked list.\r
878ddf1f 305\r
306 @retval TRUE Node is one of the nodes in the doubly linked list.\r
307 @retval FALSE Node is not one of the nodes in the doubly linked list.\r
308\r
309**/\r
310BOOLEAN\r
311EFIAPI\r
312IsNull (\r
313 IN CONST LIST_ENTRY *List,\r
314 IN CONST LIST_ENTRY *Node\r
315 )\r
316{\r
317 //\r
318 // ASSERT List not too long and Node is one of the nodes of List\r
319 //\r
320 ASSERT (IsNodeInList (List, Node));\r
321\r
322 return (BOOLEAN)(Node == List);\r
323}\r
324\r
325/**\r
326 Determines if a node the last node in a doubly linked list.\r
327\r
328 Returns TRUE if Node is the last node in the doubly linked list specified by\r
329 List. Otherwise, FALSE is returned. List must have been initialized with\r
330 InitializeListHead().\r
331\r
332 If List is NULL, then ASSERT().\r
333 If Node is NULL, then ASSERT().\r
334 If List was not initialized with InitializeListHead(), then ASSERT().\r
30817eb4 335 If PcdMaximumLinkedListLenth is not zero, and the number of nodes \r
336 in List, including the List node, is greater than or equal to \r
337 PcdMaximumLinkedListLength, then ASSERT().\r
878ddf1f 338 If Node is not a node in List, then ASSERT().\r
339\r
340 @param List A pointer to the head node of a doubly linked list.\r
24e25d11 341 @param Node A pointer to a node in the doubly linked list.\r
878ddf1f 342\r
343 @retval TRUE Node is the last node in the linked list.\r
344 @retval FALSE Node is not the last node in the linked list.\r
345\r
346**/\r
347BOOLEAN\r
348EFIAPI\r
349IsNodeAtEnd (\r
350 IN CONST LIST_ENTRY *List,\r
351 IN CONST LIST_ENTRY *Node\r
352 )\r
353{\r
354 //\r
355 // ASSERT List not too long and Node is one of the nodes of List\r
356 //\r
357 ASSERT (IsNodeInList (List, Node));\r
358\r
359 return (BOOLEAN)(!IsNull (List, Node) && List->BackLink == Node);\r
360}\r
361\r
362/**\r
363 Swaps the location of two nodes in a doubly linked list, and returns the\r
364 first node after the swap.\r
365\r
366 If FirstEntry is identical to SecondEntry, then SecondEntry is returned.\r
367 Otherwise, the location of the FirstEntry node is swapped with the location\r
368 of the SecondEntry node in a doubly linked list. SecondEntry must be in the\r
369 same double linked list as FirstEntry and that double linked list must have\r
370 been initialized with InitializeListHead(). SecondEntry is returned after the\r
371 nodes are swapped.\r
372\r
373 If FirstEntry is NULL, then ASSERT().\r
374 If SecondEntry is NULL, then ASSERT().\r
375 If SecondEntry and FirstEntry are not in the same linked list, then ASSERT().\r
30817eb4 376 If PcdMaximumLinkedListLength is not zero, and the number of nodes in the\r
377 linked list containing the FirstEntry and SecondEntry nodes, including\r
378 the FirstEntry and SecondEntry nodes, is greater than or equal to \r
379 PcdMaximumLinkedListLength, then ASSERT().\r
878ddf1f 380\r
381 @param FirstEntry A pointer to a node in a linked list.\r
382 @param SecondEntry A pointer to another node in the same linked list.\r
383\r
384**/\r
385LIST_ENTRY *\r
386EFIAPI\r
387SwapListEntries (\r
388 IN OUT LIST_ENTRY *FirstEntry,\r
389 IN OUT LIST_ENTRY *SecondEntry\r
390 )\r
391{\r
392 LIST_ENTRY *Ptr;\r
393\r
394 if (FirstEntry == SecondEntry) {\r
395 return SecondEntry;\r
396 }\r
397\r
398 //\r
399 // ASSERT Entry1 and Entry2 are in the same linked list\r
400 //\r
401 ASSERT (IsNodeInList (FirstEntry, SecondEntry));\r
402\r
403 //\r
404 // Ptr is the node pointed to by FirstEntry->ForwardLink\r
405 //\r
406 Ptr = RemoveEntryList (FirstEntry);\r
407\r
408 //\r
409 // If FirstEntry immediately follows SecondEntry, FirstEntry willl be placed\r
410 // immediately in front of SecondEntry\r
411 //\r
412 if (Ptr->BackLink == SecondEntry) {\r
413 return InsertTailList (SecondEntry, FirstEntry);\r
414 }\r
415\r
416 //\r
417 // Ptr == SecondEntry means SecondEntry immediately follows FirstEntry,\r
418 // then there are no further steps necessary\r
419 //\r
420 if (Ptr == InsertHeadList (SecondEntry, FirstEntry)) {\r
421 return Ptr;\r
422 }\r
423\r
424 //\r
425 // Move SecondEntry to the front of Ptr\r
426 //\r
427 RemoveEntryList (SecondEntry);\r
428 InsertTailList (Ptr, SecondEntry);\r
429 return SecondEntry;\r
430}\r
431\r
432/**\r
433 Removes a node from a doubly linked list, and returns the node that follows\r
434 the removed node.\r
435\r
436 Removes the node Entry from a doubly linked list. It is up to the caller of\r
437 this function to release the memory used by this node if that is required. On\r
438 exit, the node following Entry in the doubly linked list is returned. If\r
439 Entry is the only node in the linked list, then the head node of the linked\r
440 list is returned.\r
441\r
442 If Entry is NULL, then ASSERT().\r
443 If Entry is the head node of an empty list, then ASSERT().\r
30817eb4 444 If PcdMaximumLinkedListLength is not zero, and the number of nodes in the \r
445 linked list containing Entry, including the Entry node, is greater than \r
446 or equal to PcdMaximumLinkedListLength, then ASSERT().\r
878ddf1f 447\r
448 @param Entry A pointer to a node in a linked list\r
449\r
450 @return Entry\r
451\r
452**/\r
453LIST_ENTRY *\r
454EFIAPI\r
455RemoveEntryList (\r
456 IN CONST LIST_ENTRY *Entry\r
457 )\r
458{\r
459 ASSERT (!IsListEmpty (Entry));\r
460\r
461 Entry->ForwardLink->BackLink = Entry->BackLink;\r
462 Entry->BackLink->ForwardLink = Entry->ForwardLink;\r
463 return Entry->ForwardLink;\r
464}\r