2 Linked List Library Functions.
4 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "BaseLibInternals.h"
18 Worker function that locates the Node in the List.
20 By searching the List, finds the location of the Node in List. At the same time,
21 verifies the validity of this list.
23 If List is NULL, then ASSERT().
24 If List->ForwardLink is NULL, then ASSERT().
25 If List->backLink is NULL, then ASSERT().
26 If Node is NULL, then ASSERT().
27 If PcdVerifyNodeInList is TRUE and DoMembershipCheck is TRUE and Node
28 is in not a member of List, then return FALSE
29 If PcdMaximumLinkedListLenth is not zero, and List contains more than
30 PcdMaximumLinkedListLenth nodes, then ASSERT().
32 @param List A pointer to a node in a linked list.
33 @param Node A pointer to a node in a linked list.
34 @param VerifyNodeInList TRUE if a check should be made to see if Node is a
35 member of List. FALSE if no membership test should
38 @retval TRUE if PcdVerifyNodeInList is FALSE
39 @retval TRUE if DoMembershipCheck is FALSE
40 @retval TRUE if PcdVerifyNodeInList is TRUE and DoMembershipCheck is TRUE
41 and Node is a member of List.
42 @retval FALSE if PcdVerifyNodeInList is TRUE and DoMembershipCheck is TRUE
43 and Node is in not a member of List.
48 InternalBaseLibIsNodeInList (
49 IN CONST LIST_ENTRY
*List
,
50 IN CONST LIST_ENTRY
*Node
,
51 IN BOOLEAN VerifyNodeInList
55 CONST LIST_ENTRY
*Ptr
;
58 // Test the validity of List and Node
60 ASSERT (List
!= NULL
);
61 ASSERT (List
->ForwardLink
!= NULL
);
62 ASSERT (List
->BackLink
!= NULL
);
63 ASSERT (Node
!= NULL
);
68 if (FeaturePcdGet (PcdVerifyNodeInList
) && VerifyNodeInList
) {
70 // Check to see if Node is a member of List.
71 // Exit early if the number of nodes in List >= PcdMaximumLinkedListLength
74 Ptr
= Ptr
->ForwardLink
;
75 if (PcdGet32 (PcdMaximumLinkedListLength
) > 0) {
78 // ASSERT() if the linked list is too long
80 ASSERT (Count
< PcdGet32 (PcdMaximumLinkedListLength
));
83 // Return if the linked list is too long
85 if (Count
>= PcdGet32 (PcdMaximumLinkedListLength
)) {
86 return (BOOLEAN
)(Ptr
== Node
);
89 } while ((Ptr
!= List
) && (Ptr
!= Node
));
96 if (PcdGet32 (PcdMaximumLinkedListLength
) > 0) {
98 // Count the total number of nodes in List.
99 // Exit early if the number of nodes in List >= PcdMaximumLinkedListLength
102 Ptr
= Ptr
->ForwardLink
;
104 } while ((Ptr
!= List
) && (Count
< PcdGet32 (PcdMaximumLinkedListLength
)));
107 // ASSERT() if the linked list is too long
109 ASSERT (Count
< PcdGet32 (PcdMaximumLinkedListLength
));
116 Initializes the head node of a doubly-linked list, and returns the pointer to
117 the head node of the doubly-linked list.
119 Initializes the forward and backward links of a new linked list. After
120 initializing a linked list with this function, the other linked list
121 functions may be used to add and remove nodes from the linked list. It is up
122 to the caller of this function to allocate the memory for ListHead.
124 If ListHead is NULL, then ASSERT().
126 @param ListHead A pointer to the head node of a new doubly-linked list.
134 IN OUT LIST_ENTRY
*ListHead
138 ASSERT (ListHead
!= NULL
);
140 ListHead
->ForwardLink
= ListHead
;
141 ListHead
->BackLink
= ListHead
;
146 Adds a node to the beginning of a doubly-linked list, and returns the pointer
147 to the head node of the doubly-linked list.
149 Adds the node Entry at the beginning of the doubly-linked list denoted by
150 ListHead, and returns ListHead.
152 If ListHead is NULL, then ASSERT().
153 If Entry is NULL, then ASSERT().
154 If ListHead was not initialized with INTIALIZE_LIST_HEAD_VARIABLE() or
155 InitializeListHead(), then ASSERT().
156 If PcdMaximumLinkedListLenth is not zero, and prior to insertion the number
157 of nodes in ListHead, including the ListHead node, is greater than or
158 equal to PcdMaximumLinkedListLength, then ASSERT().
160 @param ListHead A pointer to the head node of a doubly-linked list.
161 @param Entry A pointer to a node that is to be inserted at the beginning
162 of a doubly-linked list.
170 IN OUT LIST_ENTRY
*ListHead
,
171 IN OUT LIST_ENTRY
*Entry
175 // ASSERT List not too long and Entry is not one of the nodes of List
177 ASSERT (InternalBaseLibIsNodeInList (ListHead
, Entry
, FALSE
));
179 Entry
->ForwardLink
= ListHead
->ForwardLink
;
180 Entry
->BackLink
= ListHead
;
181 Entry
->ForwardLink
->BackLink
= Entry
;
182 ListHead
->ForwardLink
= Entry
;
187 Adds a node to the end of a doubly-linked list, and returns the pointer to
188 the head node of the doubly-linked list.
190 Adds the node Entry to the end of the doubly-linked list denoted by ListHead,
191 and returns ListHead.
193 If ListHead is NULL, then ASSERT().
194 If Entry is NULL, then ASSERT().
195 If ListHead was not initialized with INTIALIZE_LIST_HEAD_VARIABLE() or
196 InitializeListHead(), then ASSERT().
197 If PcdMaximumLinkedListLenth is not zero, and prior to insertion the number
198 of nodes in ListHead, including the ListHead node, is greater than or
199 equal to PcdMaximumLinkedListLength, then ASSERT().
201 @param ListHead A pointer to the head node of a doubly-linked list.
202 @param Entry A pointer to a node that is to be added at the end of the
211 IN OUT LIST_ENTRY
*ListHead
,
212 IN OUT LIST_ENTRY
*Entry
216 // ASSERT List not too long and Entry is not one of the nodes of List
218 ASSERT (InternalBaseLibIsNodeInList (ListHead
, Entry
, FALSE
));
220 Entry
->ForwardLink
= ListHead
;
221 Entry
->BackLink
= ListHead
->BackLink
;
222 Entry
->BackLink
->ForwardLink
= Entry
;
223 ListHead
->BackLink
= Entry
;
228 Retrieves the first node of a doubly-linked list.
230 Returns the first node of a doubly-linked list. List must have been
231 initialized with INTIALIZE_LIST_HEAD_VARIABLE() or InitializeListHead().
232 If List is empty, then List is returned.
234 If List is NULL, then ASSERT().
235 If List was not initialized with INTIALIZE_LIST_HEAD_VARIABLE() or
236 InitializeListHead(), then ASSERT().
237 If PcdMaximumLinkedListLenth is not zero, and the number of nodes
238 in List, including the List node, is greater than or equal to
239 PcdMaximumLinkedListLength, then ASSERT().
241 @param List A pointer to the head node of a doubly-linked list.
243 @return The first node of a doubly-linked list.
244 @retval NULL The list is empty.
250 IN CONST LIST_ENTRY
*List
254 // ASSERT List not too long
256 ASSERT (InternalBaseLibIsNodeInList (List
, List
, FALSE
));
258 return List
->ForwardLink
;
262 Retrieves the next node of a doubly-linked list.
264 Returns the node of a doubly-linked list that follows Node.
265 List must have been initialized with INTIALIZE_LIST_HEAD_VARIABLE()
266 or InitializeListHead(). If List is empty, then List is returned.
268 If List is NULL, then ASSERT().
269 If Node is NULL, then ASSERT().
270 If List was not initialized with INTIALIZE_LIST_HEAD_VARIABLE() or
271 InitializeListHead(), then ASSERT().
272 If PcdMaximumLinkedListLenth is not zero, and List contains more than
273 PcdMaximumLinkedListLenth nodes, then ASSERT().
274 If PcdVerifyNodeInList is TRUE and Node is not a node in List, then ASSERT().
276 @param List A pointer to the head node of a doubly-linked list.
277 @param Node A pointer to a node in the doubly-linked list.
279 @return A pointer to the next node if one exists. Otherwise List is returned.
285 IN CONST LIST_ENTRY
*List
,
286 IN CONST LIST_ENTRY
*Node
290 // ASSERT List not too long and Node is one of the nodes of List
292 ASSERT (InternalBaseLibIsNodeInList (List
, Node
, TRUE
));
294 return Node
->ForwardLink
;
298 Retrieves the previous node of a doubly-linked list.
300 Returns the node of a doubly-linked list that precedes Node.
301 List must have been initialized with INTIALIZE_LIST_HEAD_VARIABLE()
302 or InitializeListHead(). If List is empty, then List is returned.
304 If List is NULL, then ASSERT().
305 If Node is NULL, then ASSERT().
306 If List was not initialized with INTIALIZE_LIST_HEAD_VARIABLE() or
307 InitializeListHead(), then ASSERT().
308 If PcdMaximumLinkedListLenth is not zero, and List contains more than
309 PcdMaximumLinkedListLenth nodes, then ASSERT().
310 If PcdVerifyNodeInList is TRUE and Node is not a node in List, then ASSERT().
312 @param List A pointer to the head node of a doubly-linked list.
313 @param Node A pointer to a node in the doubly-linked list.
315 @return A pointer to the previous node if one exists. Otherwise List is returned.
321 IN CONST LIST_ENTRY
*List
,
322 IN CONST LIST_ENTRY
*Node
326 // ASSERT List not too long and Node is one of the nodes of List
328 ASSERT (InternalBaseLibIsNodeInList (List
, Node
, TRUE
));
330 return Node
->BackLink
;
334 Checks to see if a doubly-linked list is empty or not.
336 Checks to see if the doubly-linked list is empty. If the linked list contains
337 zero nodes, this function returns TRUE. Otherwise, it returns FALSE.
339 If ListHead is NULL, then ASSERT().
340 If ListHead was not initialized with INTIALIZE_LIST_HEAD_VARIABLE() or
341 InitializeListHead(), then ASSERT().
342 If PcdMaximumLinkedListLenth is not zero, and the number of nodes
343 in List, including the List node, is greater than or equal to
344 PcdMaximumLinkedListLength, then ASSERT().
346 @param ListHead A pointer to the head node of a doubly-linked list.
348 @retval TRUE The linked list is empty.
349 @retval FALSE The linked list is not empty.
355 IN CONST LIST_ENTRY
*ListHead
359 // ASSERT List not too long
361 ASSERT (InternalBaseLibIsNodeInList (ListHead
, ListHead
, FALSE
));
363 return (BOOLEAN
)(ListHead
->ForwardLink
== ListHead
);
367 Determines if a node in a doubly-linked list is the head node of a the same
368 doubly-linked list. This function is typically used to terminate a loop that
369 traverses all the nodes in a doubly-linked list starting with the head node.
371 Returns TRUE if Node is equal to List. Returns FALSE if Node is one of the
372 nodes in the doubly-linked list specified by List. List must have been
373 initialized with INTIALIZE_LIST_HEAD_VARIABLE() or InitializeListHead().
375 If List is NULL, then ASSERT().
376 If Node is NULL, then ASSERT().
377 If List was not initialized with INTIALIZE_LIST_HEAD_VARIABLE() or InitializeListHead(),
379 If PcdMaximumLinkedListLenth is not zero, and the number of nodes
380 in List, including the List node, is greater than or equal to
381 PcdMaximumLinkedListLength, then ASSERT().
382 If PcdVerifyNodeInList is TRUE and Node is not a node in List and Node is not
383 equal to List, then ASSERT().
385 @param List A pointer to the head node of a doubly-linked list.
386 @param Node A pointer to a node in the doubly-linked list.
388 @retval TRUE Node is one of the nodes in the doubly-linked list.
389 @retval FALSE Node is not one of the nodes in the doubly-linked list.
395 IN CONST LIST_ENTRY
*List
,
396 IN CONST LIST_ENTRY
*Node
400 // ASSERT List not too long and Node is one of the nodes of List
402 ASSERT (InternalBaseLibIsNodeInList (List
, Node
, TRUE
));
404 return (BOOLEAN
)(Node
== List
);
408 Determines if a node the last node in a doubly-linked list.
410 Returns TRUE if Node is the last node in the doubly-linked list specified by
411 List. Otherwise, FALSE is returned. List must have been initialized with
412 INTIALIZE_LIST_HEAD_VARIABLE() or InitializeListHead().
414 If List is NULL, then ASSERT().
415 If Node is NULL, then ASSERT().
416 If List was not initialized with INTIALIZE_LIST_HEAD_VARIABLE() or
417 InitializeListHead(), then ASSERT().
418 If PcdMaximumLinkedListLenth is not zero, and the number of nodes
419 in List, including the List node, is greater than or equal to
420 PcdMaximumLinkedListLength, then ASSERT().
421 If PcdVerifyNodeInList is TRUE and Node is not a node in List, then ASSERT().
423 @param List A pointer to the head node of a doubly-linked list.
424 @param Node A pointer to a node in the doubly-linked list.
426 @retval TRUE Node is the last node in the linked list.
427 @retval FALSE Node is not the last node in the linked list.
433 IN CONST LIST_ENTRY
*List
,
434 IN CONST LIST_ENTRY
*Node
438 // ASSERT List not too long and Node is one of the nodes of List
440 ASSERT (InternalBaseLibIsNodeInList (List
, Node
, TRUE
));
442 return (BOOLEAN
)(!IsNull (List
, Node
) && List
->BackLink
== Node
);
446 Swaps the location of two nodes in a doubly-linked list, and returns the
447 first node after the swap.
449 If FirstEntry is identical to SecondEntry, then SecondEntry is returned.
450 Otherwise, the location of the FirstEntry node is swapped with the location
451 of the SecondEntry node in a doubly-linked list. SecondEntry must be in the
452 same double linked list as FirstEntry and that double linked list must have
453 been initialized with INTIALIZE_LIST_HEAD_VARIABLE() or InitializeListHead().
454 SecondEntry is returned after the nodes are swapped.
456 If FirstEntry is NULL, then ASSERT().
457 If SecondEntry is NULL, then ASSERT().
458 If PcdVerifyNodeInList is TRUE and SecondEntry and FirstEntry are not in the
459 same linked list, then ASSERT().
460 If PcdMaximumLinkedListLength is not zero, and the number of nodes in the
461 linked list containing the FirstEntry and SecondEntry nodes, including
462 the FirstEntry and SecondEntry nodes, is greater than or equal to
463 PcdMaximumLinkedListLength, then ASSERT().
465 @param FirstEntry A pointer to a node in a linked list.
466 @param SecondEntry A pointer to another node in the same linked list.
474 IN OUT LIST_ENTRY
*FirstEntry
,
475 IN OUT LIST_ENTRY
*SecondEntry
480 if (FirstEntry
== SecondEntry
) {
485 // ASSERT Entry1 and Entry2 are in the same linked list
487 ASSERT (InternalBaseLibIsNodeInList (FirstEntry
, SecondEntry
, TRUE
));
490 // Ptr is the node pointed to by FirstEntry->ForwardLink
492 Ptr
= RemoveEntryList (FirstEntry
);
495 // If FirstEntry immediately follows SecondEntry, FirstEntry will be placed
496 // immediately in front of SecondEntry
498 if (Ptr
->BackLink
== SecondEntry
) {
499 return InsertTailList (SecondEntry
, FirstEntry
);
503 // Ptr == SecondEntry means SecondEntry immediately follows FirstEntry,
504 // then there are no further steps necessary
506 if (Ptr
== InsertHeadList (SecondEntry
, FirstEntry
)) {
511 // Move SecondEntry to the front of Ptr
513 RemoveEntryList (SecondEntry
);
514 InsertTailList (Ptr
, SecondEntry
);
519 Removes a node from a doubly-linked list, and returns the node that follows
522 Removes the node Entry from a doubly-linked list. It is up to the caller of
523 this function to release the memory used by this node if that is required. On
524 exit, the node following Entry in the doubly-linked list is returned. If
525 Entry is the only node in the linked list, then the head node of the linked
528 If Entry is NULL, then ASSERT().
529 If Entry is the head node of an empty list, then ASSERT().
530 If PcdMaximumLinkedListLength is not zero, and the number of nodes in the
531 linked list containing Entry, including the Entry node, is greater than
532 or equal to PcdMaximumLinkedListLength, then ASSERT().
534 @param Entry A pointer to a node in a linked list.
542 IN CONST LIST_ENTRY
*Entry
545 ASSERT (!IsListEmpty (Entry
));
547 Entry
->ForwardLink
->BackLink
= Entry
->BackLink
;
548 Entry
->BackLink
->ForwardLink
= Entry
->ForwardLink
;
549 return Entry
->ForwardLink
;