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