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