]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseLib/LinkedList.c
Modify MDE source code according to MDE library update.
[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
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
24dcb5e5 28 If List->BackLink is NULL, then ASSERT().\r
e1f414b6 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
aa0583c7 292 Determines if a node in a doubly linked list is the head node of a the same\r
293 doubly linked list. This function is typically used to terminate a loop that\r
294 traverses all the nodes in a doubly linked list starting with the head node.\r
e1f414b6 295\r
aa0583c7 296 Returns TRUE if Node is equal to List. Returns FALSE if Node is one of the\r
297 nodes in the doubly linked list specified by List. List must have been\r
298 initialized with InitializeListHead().\r
e1f414b6 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
24dcb5e5 389 @return SecondEntry after the nodes are swapped\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
24dcb5e5 416 // If FirstEntry immediately follows SecondEntry, FirstEntry will be placed\r
e1f414b6 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
24dcb5e5 457 @return The node following Entry in the doubly linked list.\r
458 If Entry is the only node in the linked list, then\r
459 the head node of the linked list is returned.\r
e1f414b6 460\r
461**/\r
462LIST_ENTRY *\r
463EFIAPI\r
464RemoveEntryList (\r
465 IN CONST LIST_ENTRY *Entry\r
466 )\r
467{\r
468 ASSERT (!IsListEmpty (Entry));\r
469\r
470 Entry->ForwardLink->BackLink = Entry->BackLink;\r
471 Entry->BackLink->ForwardLink = Entry->ForwardLink;\r
472 return Entry->ForwardLink;\r
473}\r