]>
Commit | Line | Data |
---|---|---|
3eb9473e | 1 | /*++\r |
2 | \r | |
3 | Copyright (c) 2004 - 2006, Intel Corporation \r | |
4 | All rights reserved. This program and the accompanying materials \r | |
5 | are licensed and made available under the terms and conditions of the BSD License \r | |
6 | which accompanies this distribution. The full text of the license may be found at \r | |
7 | http://opensource.org/licenses/bsd-license.php \r | |
8 | \r | |
9 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r | |
10 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r | |
11 | \r | |
12 | \r | |
13 | Module Name:\r | |
14 | \r | |
15 | LinkedList.c\r | |
16 | \r | |
17 | Abstract: \r | |
18 | \r | |
19 | Linked List Library Functions.\r | |
20 | \r | |
21 | --*/\r | |
22 | \r | |
c7f33ca4 | 23 | #include "BaseLibInternals.h"\r |
3eb9473e | 24 | \r |
25 | /**\r | |
26 | Worker function that locates the Node in the List\r | |
27 | \r | |
28 | By searching the List, finds the location of the Node in List. At the same time,\r | |
29 | verifies the validity of this list.\r | |
30 | \r | |
31 | If List is NULL, then ASSERT().\r | |
32 | If List->ForwardLink is NULL, then ASSERT().\r | |
33 | If List->backLink is NULL, then ASSERT().\r | |
34 | If Node is NULL, then ASSERT();\r | |
35 | If PcdMaximumLinkedListLenth is not zero, and prior to insertion the number\r | |
36 | of nodes in ListHead, including the ListHead node, is greater than or\r | |
37 | equal to PcdMaximumLinkedListLength, then ASSERT().\r | |
38 | \r | |
39 | @param List A pointer to a node in a linked list.\r | |
40 | @param Node A pointer to one nod.\r | |
41 | \r | |
42 | @retval TRUE Node is in List\r | |
43 | @retval FALSE Node isn't in List, or List is invalid\r | |
44 | \r | |
45 | **/\r | |
46 | BOOLEAN\r | |
47 | IsNodeInList (\r | |
48 | IN CONST LIST_ENTRY *List,\r | |
49 | IN CONST LIST_ENTRY *Node\r | |
50 | )\r | |
51 | {\r | |
52 | UINTN Count;\r | |
53 | CONST LIST_ENTRY *Ptr;\r | |
54 | BOOLEAN Found;\r | |
55 | \r | |
56 | //\r | |
57 | // Test the validity of List and Node\r | |
58 | //\r | |
59 | ASSERT (List != NULL);\r | |
60 | ASSERT (List->ForwardLink != NULL);\r | |
61 | ASSERT (List->BackLink != NULL);\r | |
62 | ASSERT (Node != NULL);\r | |
63 | \r | |
64 | Count = PcdGet32 (PcdMaximumLinkedListLength);\r | |
65 | \r | |
66 | Ptr = List;\r | |
67 | do {\r | |
68 | Ptr = Ptr->ForwardLink;\r | |
69 | Count--;\r | |
70 | } while ((Ptr != List) && (Ptr != Node) && (Count > 0));\r | |
71 | Found = (BOOLEAN)(Ptr == Node);\r | |
72 | \r | |
73 | if (PcdGet32 (PcdMaximumLinkedListLength) > 0) {\r | |
74 | while ((Count > 0) && (Ptr != List)) {\r | |
75 | Ptr = Ptr->ForwardLink;\r | |
76 | Count--;\r | |
77 | }\r | |
78 | ASSERT (Count > 0);\r | |
79 | }\r | |
80 | \r | |
81 | return Found;\r | |
82 | }\r | |
83 | \r | |
84 | /**\r | |
85 | Initializes the head node of a doubly linked list, and returns the pointer to\r | |
86 | the head node of the doubly linked list.\r | |
87 | \r | |
88 | Initializes the forward and backward links of a new linked list. After\r | |
89 | initializing a linked list with this function, the other linked list\r | |
90 | functions may be used to add and remove nodes from the linked list. It is up\r | |
91 | to the caller of this function to allocate the memory for ListHead.\r | |
92 | \r | |
93 | If ListHead is NULL, then ASSERT().\r | |
94 | \r | |
95 | @param ListHead A pointer to the head node of a new doubly linked list.\r | |
96 | \r | |
97 | @return ListHead\r | |
98 | \r | |
99 | **/\r | |
100 | LIST_ENTRY *\r | |
101 | EFIAPI\r | |
102 | GlueInitializeListHead (\r | |
103 | IN OUT LIST_ENTRY *List\r | |
104 | )\r | |
105 | \r | |
106 | {\r | |
107 | ASSERT (List != NULL);\r | |
108 | \r | |
109 | List->ForwardLink = List;\r | |
110 | List->BackLink = List;\r | |
111 | return List;\r | |
112 | }\r | |
113 | \r | |
114 | /**\r | |
115 | Adds a node to the beginning of a doubly linked list, and returns the pointer\r | |
116 | to the head node of the doubly linked list.\r | |
117 | \r | |
118 | Adds the node Entry at the beginning of the doubly linked list denoted by\r | |
119 | ListHead, and returns ListHead.\r | |
120 | \r | |
121 | If ListHead is NULL, then ASSERT().\r | |
122 | If Entry is NULL, then ASSERT().\r | |
123 | If ListHead was not initialized with InitializeListHead(), then ASSERT().\r | |
124 | If PcdMaximumLinkedListLenth is not zero, and prior to insertion the number\r | |
125 | of nodes in ListHead, including the ListHead node, is greater than or\r | |
126 | equal to PcdMaximumLinkedListLength, then ASSERT().\r | |
127 | \r | |
128 | @param ListHead A pointer to the head node of a doubly linked list.\r | |
129 | @param Entry A pointer to a node that is to be inserted at the beginning\r | |
130 | of a doubly linked list.\r | |
131 | \r | |
132 | @return ListHead\r | |
133 | \r | |
134 | **/\r | |
135 | LIST_ENTRY *\r | |
136 | EFIAPI\r | |
137 | GlueInsertHeadList (\r | |
138 | IN OUT LIST_ENTRY *List,\r | |
139 | IN OUT LIST_ENTRY *Entry\r | |
140 | )\r | |
141 | {\r | |
142 | //\r | |
143 | // ASSERT List not too long and Entry is not one of the nodes of List\r | |
144 | //\r | |
145 | ASSERT (!IsNodeInList (List, Entry));\r | |
146 | \r | |
147 | Entry->ForwardLink = List->ForwardLink;\r | |
148 | Entry->BackLink = List;\r | |
149 | Entry->ForwardLink->BackLink = Entry;\r | |
150 | List->ForwardLink = Entry;\r | |
151 | return List;\r | |
152 | }\r | |
153 | \r | |
154 | /**\r | |
155 | Adds a node to the end of a doubly linked list, and returns the pointer to\r | |
156 | the head node of the doubly linked list.\r | |
157 | \r | |
158 | Adds the node Entry to the end of the doubly linked list denoted by ListHead,\r | |
159 | and returns ListHead.\r | |
160 | \r | |
161 | If ListHead is NULL, then ASSERT().\r | |
162 | If Entry is NULL, then ASSERT().\r | |
163 | If ListHead was not initialized with InitializeListHead(), then ASSERT().\r | |
164 | If PcdMaximumLinkedListLenth is not zero, and prior to insertion the number\r | |
165 | of nodes in ListHead, including the ListHead node, is greater than or\r | |
166 | equal to PcdMaximumLinkedListLength, then ASSERT().\r | |
167 | \r | |
168 | @param ListHead A pointer to the head node of a doubly linked list.\r | |
169 | @param Entry A pointer to a node that is to be added at the end of the\r | |
170 | doubly linked list.\r | |
171 | \r | |
172 | @return ListHead\r | |
173 | \r | |
174 | **/\r | |
175 | LIST_ENTRY *\r | |
176 | EFIAPI\r | |
177 | GlueInsertTailList (\r | |
178 | IN OUT LIST_ENTRY *List,\r | |
179 | IN OUT LIST_ENTRY *Entry\r | |
180 | )\r | |
181 | {\r | |
182 | //\r | |
183 | // ASSERT List not too long and Entry is not one of the nodes of List\r | |
184 | //\r | |
185 | ASSERT (!IsNodeInList (List, Entry));\r | |
186 | \r | |
187 | Entry->ForwardLink = List;\r | |
188 | Entry->BackLink = List->BackLink;\r | |
189 | Entry->BackLink->ForwardLink = Entry;\r | |
190 | List->BackLink = Entry;\r | |
191 | return List;\r | |
192 | }\r | |
193 | \r | |
194 | /**\r | |
195 | Retrieves the first node of a doubly linked list.\r | |
196 | \r | |
197 | Returns the first node of a doubly linked list. List must have been\r | |
198 | initialized with InitializeListHead(). If List is empty, then NULL is\r | |
199 | returned.\r | |
200 | \r | |
201 | If List is NULL, then ASSERT().\r | |
202 | If List was not initialized with InitializeListHead(), then ASSERT().\r | |
203 | If PcdMaximumLinkedListLenth is not zero, and the number of nodes\r | |
204 | in List, including the List node, is greater than or equal to\r | |
205 | PcdMaximumLinkedListLength, then ASSERT().\r | |
206 | \r | |
207 | @param List A pointer to the head node of a doubly linked list.\r | |
208 | \r | |
209 | @return The first node of a doubly linked list.\r | |
210 | @retval NULL The list is empty.\r | |
211 | \r | |
212 | **/\r | |
213 | LIST_ENTRY *\r | |
214 | EFIAPI\r | |
215 | GlueGetFirstNode (\r | |
216 | IN CONST LIST_ENTRY *List\r | |
217 | )\r | |
218 | {\r | |
219 | //\r | |
220 | // ASSERT List not too long\r | |
221 | //\r | |
222 | ASSERT (IsNodeInList (List, List));\r | |
223 | \r | |
224 | return List->ForwardLink;\r | |
225 | }\r | |
226 | \r | |
227 | /**\r | |
228 | Retrieves the next node of a doubly linked list.\r | |
229 | \r | |
230 | Returns the node of a doubly linked list that follows Node. List must have\r | |
231 | been initialized with InitializeListHead(). If List is empty, then List is\r | |
232 | returned.\r | |
233 | \r | |
234 | If List is NULL, then ASSERT().\r | |
235 | If Node is NULL, then ASSERT().\r | |
236 | If List was not initialized with InitializeListHead(), then ASSERT().\r | |
237 | If PcdMaximumLinkedListLenth is not zero, and List contains more than\r | |
238 | PcdMaximumLinkedListLenth nodes, then ASSERT().\r | |
239 | If Node is not a node in List, then ASSERT().\r | |
240 | \r | |
241 | @param List A pointer to the head node of a doubly linked list.\r | |
242 | @param Node A pointer to a node in the doubly linked list.\r | |
243 | \r | |
244 | @return Pointer to the next node if one exists. Otherwise a null value which\r | |
245 | is actually List is returned.\r | |
246 | \r | |
247 | **/\r | |
248 | LIST_ENTRY *\r | |
249 | EFIAPI\r | |
250 | GlueGetNextNode (\r | |
251 | IN CONST LIST_ENTRY *List,\r | |
252 | IN CONST LIST_ENTRY *Node\r | |
253 | )\r | |
254 | {\r | |
255 | //\r | |
256 | // ASSERT List not too long and Node is one of the nodes of List\r | |
257 | //\r | |
258 | ASSERT (IsNodeInList (List, Node));\r | |
259 | \r | |
260 | return Node->ForwardLink;\r | |
261 | }\r | |
262 | \r | |
263 | /**\r | |
264 | Checks to see if a doubly linked list is empty or not.\r | |
265 | \r | |
266 | Checks to see if the doubly linked list is empty. If the linked list contains\r | |
267 | zero nodes, this function returns TRUE. Otherwise, it returns FALSE.\r | |
268 | \r | |
269 | If ListHead is NULL, then ASSERT().\r | |
270 | If ListHead was not initialized with InitializeListHead(), then ASSERT().\r | |
271 | If PcdMaximumLinkedListLenth is not zero, and the number of nodes\r | |
272 | in List, including the List node, is greater than or equal to\r | |
273 | PcdMaximumLinkedListLength, then ASSERT().\r | |
274 | \r | |
275 | @param ListHead A pointer to the head node of a doubly linked list.\r | |
276 | \r | |
277 | @retval TRUE The linked list is empty.\r | |
278 | @retval FALSE The linked list is not empty.\r | |
279 | \r | |
280 | **/\r | |
281 | BOOLEAN\r | |
282 | EFIAPI\r | |
283 | GlueIsListEmpty (\r | |
284 | IN CONST LIST_ENTRY *List\r | |
285 | )\r | |
286 | {\r | |
287 | //\r | |
288 | // ASSERT List not too long\r | |
289 | //\r | |
290 | ASSERT (IsNodeInList (List, List));\r | |
291 | \r | |
292 | return (BOOLEAN)(List->ForwardLink == List);\r | |
293 | }\r | |
294 | \r | |
295 | /**\r | |
296 | Determines if a node in a doubly linked list is null.\r | |
297 | \r | |
298 | Returns FALSE if Node is one of the nodes in the doubly linked list specified\r | |
299 | by List. Otherwise, TRUE is returned. List must have been initialized with\r | |
300 | InitializeListHead().\r | |
301 | \r | |
302 | If List is NULL, then ASSERT().\r | |
303 | If Node is NULL, then ASSERT().\r | |
304 | If List was not initialized with InitializeListHead(), then ASSERT().\r | |
305 | If PcdMaximumLinkedListLenth is not zero, and the number of nodes\r | |
306 | in List, including the List node, is greater than or equal to\r | |
307 | PcdMaximumLinkedListLength, then ASSERT().\r | |
308 | If Node is not a node in List and Node is not equal to List, then ASSERT().\r | |
309 | \r | |
310 | @param List A pointer to the head node of a doubly linked list.\r | |
311 | @param Node A pointer to a node in the doubly linked list.\r | |
312 | \r | |
313 | @retval TRUE Node is one of the nodes in the doubly linked list.\r | |
314 | @retval FALSE Node is not one of the nodes in the doubly linked list.\r | |
315 | \r | |
316 | **/\r | |
317 | BOOLEAN\r | |
318 | EFIAPI\r | |
319 | GlueIsNull (\r | |
320 | IN CONST LIST_ENTRY *List,\r | |
321 | IN CONST LIST_ENTRY *Node\r | |
322 | )\r | |
323 | {\r | |
324 | //\r | |
325 | // ASSERT List not too long and Node is one of the nodes of List\r | |
326 | //\r | |
327 | ASSERT (IsNodeInList (List, Node));\r | |
328 | \r | |
329 | return (BOOLEAN)(Node == List);\r | |
330 | }\r | |
331 | \r | |
332 | /**\r | |
333 | Determines if a node the last node in a doubly linked list.\r | |
334 | \r | |
335 | Returns TRUE if Node is the last node in the doubly linked list specified by\r | |
336 | List. Otherwise, FALSE is returned. List must have been initialized with\r | |
337 | InitializeListHead().\r | |
338 | \r | |
339 | If List is NULL, then ASSERT().\r | |
340 | If Node is NULL, then ASSERT().\r | |
341 | If List was not initialized with InitializeListHead(), then ASSERT().\r | |
342 | If PcdMaximumLinkedListLenth is not zero, and the number of nodes\r | |
343 | in List, including the List node, is greater than or equal to\r | |
344 | PcdMaximumLinkedListLength, then ASSERT().\r | |
345 | If Node is not a node in List, then ASSERT().\r | |
346 | \r | |
347 | @param List A pointer to the head node of a doubly linked list.\r | |
348 | @param Node A pointer to a node in the doubly linked list.\r | |
349 | \r | |
350 | @retval TRUE Node is the last node in the linked list.\r | |
351 | @retval FALSE Node is not the last node in the linked list.\r | |
352 | \r | |
353 | **/\r | |
354 | BOOLEAN\r | |
355 | EFIAPI\r | |
356 | GlueIsNodeAtEnd (\r | |
357 | IN CONST LIST_ENTRY *List,\r | |
358 | IN CONST LIST_ENTRY *Node\r | |
359 | )\r | |
360 | {\r | |
361 | //\r | |
362 | // ASSERT List not too long and Node is one of the nodes of List\r | |
363 | //\r | |
364 | ASSERT (IsNodeInList (List, Node));\r | |
365 | \r | |
366 | return (BOOLEAN)(!IsNull (List, Node) && List->BackLink == Node);\r | |
367 | }\r | |
368 | \r | |
369 | /**\r | |
370 | Swaps the location of two nodes in a doubly linked list, and returns the\r | |
371 | first node after the swap.\r | |
372 | \r | |
373 | If FirstEntry is identical to SecondEntry, then SecondEntry is returned.\r | |
374 | Otherwise, the location of the FirstEntry node is swapped with the location\r | |
375 | of the SecondEntry node in a doubly linked list. SecondEntry must be in the\r | |
376 | same double linked list as FirstEntry and that double linked list must have\r | |
377 | been initialized with InitializeListHead(). SecondEntry is returned after the\r | |
378 | nodes are swapped.\r | |
379 | \r | |
380 | If FirstEntry is NULL, then ASSERT().\r | |
381 | If SecondEntry is NULL, then ASSERT().\r | |
382 | If SecondEntry and FirstEntry are not in the same linked list, then ASSERT().\r | |
383 | If PcdMaximumLinkedListLength is not zero, and the number of nodes in the\r | |
384 | linked list containing the FirstEntry and SecondEntry nodes, including\r | |
385 | the FirstEntry and SecondEntry nodes, is greater than or equal to\r | |
386 | PcdMaximumLinkedListLength, then ASSERT().\r | |
387 | \r | |
388 | @param FirstEntry A pointer to a node in a linked list.\r | |
389 | @param SecondEntry A pointer to another node in the same linked list.\r | |
390 | \r | |
391 | **/\r | |
392 | LIST_ENTRY *\r | |
393 | EFIAPI\r | |
394 | GlueSwapListEntries (\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 | |
416 | // If FirstEntry immediately follows SecondEntry, FirstEntry willl be placed\r | |
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 | |
457 | @return Entry\r | |
458 | \r | |
459 | **/\r | |
460 | LIST_ENTRY *\r | |
461 | EFIAPI\r | |
462 | GlueRemoveEntryList (\r | |
463 | IN CONST LIST_ENTRY *Entry\r | |
464 | )\r | |
465 | {\r | |
466 | ASSERT (!IsListEmpty (Entry));\r | |
467 | \r | |
468 | Entry->ForwardLink->BackLink = Entry->BackLink;\r | |
469 | Entry->BackLink->ForwardLink = Entry->ForwardLink;\r | |
470 | return Entry->ForwardLink;\r | |
471 | }\r |