diff options
Diffstat (limited to 'MdePkg/Library/BaseLib/LinkedList.c')
-rw-r--r-- | MdePkg/Library/BaseLib/LinkedList.c | 237 |
1 files changed, 143 insertions, 94 deletions
diff --git a/MdePkg/Library/BaseLib/LinkedList.c b/MdePkg/Library/BaseLib/LinkedList.c index 8b157da915ac..be2e628e9984 100644 --- a/MdePkg/Library/BaseLib/LinkedList.c +++ b/MdePkg/Library/BaseLib/LinkedList.c @@ -1,54 +1,68 @@ /** @file Linked List Library Functions. - Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR> - This program and the accompanying materials - are licensed and made available under the terms and conditions of the BSD License - which accompanies this distribution. The full text of the license may be found at - http://opensource.org/licenses/bsd-license.php. - - THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, - WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR> + SPDX-License-Identifier: BSD-2-Clause-Patent **/ #include "BaseLibInternals.h" /** - Worker function that locates the Node in the List. + If PcdVerifyNodeInList is TRUE, ASSERTs when SecondEntry is or is not part of + the same doubly-linked list as FirstEntry depending on the value of InList. + Independent of PcdVerifyNodeInList, ASSERTs when FirstEntry is not part of a + valid list. + + If FirstEntry is NULL, then ASSERT(). + If FirstEntry->ForwardLink is NULL, then ASSERT(). + If FirstEntry->BackLink is NULL, then ASSERT(). + If PcdMaximumLinkedListLength is not zero, and List contains more than + PcdMaximumLinkedListLength nodes, then ASSERT(). + If PcdVerifyNodeInList is TRUE and SecondEntry is NULL, then ASSERT(). - By searching the List, finds the location of the Node in List. At the same time, - verifies the validity of this list. + @param FirstEntry A pointer to a node in a linked list. + @param SecondEntry A pointer to the node to locate. + @param InList Defines whether to check if SecondEntry is or is not part + of the same doubly-linked list as FirstEntry. + +**/ +#if !defined (MDEPKG_NDEBUG) + #define ASSERT_VERIFY_NODE_IN_VALID_LIST(FirstEntry, SecondEntry, InList) \ + do { \ + if (FeaturePcdGet (PcdVerifyNodeInList)) { \ + ASSERT (InList == IsNodeInList ((FirstEntry), (SecondEntry))); \ + } else { \ + ASSERT (InternalBaseLibIsListValid (FirstEntry)); \ + } \ + } while (FALSE) +#else + #define ASSERT_VERIFY_NODE_IN_VALID_LIST(FirstEntry, SecondEntry, InList) +#endif + +/** + Worker function that verifies the validity of this list. If List is NULL, then ASSERT(). If List->ForwardLink is NULL, then ASSERT(). - If List->backLink is NULL, then ASSERT(). - If Node is NULL, then ASSERT(). - If PcdVerifyNodeInList is TRUE and DoMembershipCheck is TRUE and Node - is in not a member of List, then return FALSE + If List->BackLink is NULL, then ASSERT(). If PcdMaximumLinkedListLength is not zero, and List contains more than PcdMaximumLinkedListLength nodes, then ASSERT(). @param List A pointer to a node in a linked list. - @param Node A pointer to a node in a linked list. - @param VerifyNodeInList TRUE if a check should be made to see if Node is a - member of List. FALSE if no membership test should - be performed. @retval TRUE if PcdVerifyNodeInList is FALSE @retval TRUE if DoMembershipCheck is FALSE - @retval TRUE if PcdVerifyNodeInList is TRUE and DoMembershipCheck is TRUE + @retval TRUE if PcdVerifyNodeInList is TRUE and DoMembershipCheck is TRUE and Node is a member of List. - @retval FALSE if PcdVerifyNodeInList is TRUE and DoMembershipCheck is TRUE + @retval FALSE if PcdVerifyNodeInList is TRUE and DoMembershipCheck is TRUE and Node is in not a member of List. **/ BOOLEAN EFIAPI -InternalBaseLibIsNodeInList ( - IN CONST LIST_ENTRY *List, - IN CONST LIST_ENTRY *Node, - IN BOOLEAN VerifyNodeInList +InternalBaseLibIsListValid ( + IN CONST LIST_ENTRY *List ) { UINTN Count; @@ -60,40 +74,11 @@ InternalBaseLibIsNodeInList ( ASSERT (List != NULL); ASSERT (List->ForwardLink != NULL); ASSERT (List->BackLink != NULL); - ASSERT (Node != NULL); - - Count = 0; - Ptr = List; - - if (FeaturePcdGet (PcdVerifyNodeInList) && VerifyNodeInList) { - // - // Check to see if Node is a member of List. - // Exit early if the number of nodes in List >= PcdMaximumLinkedListLength - // - do { - Ptr = Ptr->ForwardLink; - if (PcdGet32 (PcdMaximumLinkedListLength) > 0) { - Count++; - // - // ASSERT() if the linked list is too long - // - ASSERT (Count < PcdGet32 (PcdMaximumLinkedListLength)); - - // - // Return if the linked list is too long - // - if (Count >= PcdGet32 (PcdMaximumLinkedListLength)) { - return (BOOLEAN)(Ptr == Node); - } - } - } while ((Ptr != List) && (Ptr != Node)); - - if (Ptr != Node) { - return FALSE; - } - } if (PcdGet32 (PcdMaximumLinkedListLength) > 0) { + Count = 0; + Ptr = List; + // // Count the total number of nodes in List. // Exit early if the number of nodes in List >= PcdMaximumLinkedListLength @@ -104,15 +89,79 @@ InternalBaseLibIsNodeInList ( } while ((Ptr != List) && (Count < PcdGet32 (PcdMaximumLinkedListLength))); // - // ASSERT() if the linked list is too long + // return whether linked list is too long // - ASSERT (Count < PcdGet32 (PcdMaximumLinkedListLength)); + return (BOOLEAN)(Count < PcdGet32 (PcdMaximumLinkedListLength)); } return TRUE; } /** + Checks whether FirstEntry and SecondEntry are part of the same doubly-linked + list. + + If FirstEntry is NULL, then ASSERT(). + If FirstEntry->ForwardLink is NULL, then ASSERT(). + If FirstEntry->BackLink is NULL, then ASSERT(). + If SecondEntry is NULL, then ASSERT(); + If PcdMaximumLinkedListLength is not zero, and List contains more than + PcdMaximumLinkedListLength nodes, then ASSERT(). + + @param FirstEntry A pointer to a node in a linked list. + @param SecondEntry A pointer to the node to locate. + + @retval TRUE SecondEntry is in the same doubly-linked list as FirstEntry. + @retval FALSE SecondEntry isn't in the same doubly-linked list as FirstEntry, + or FirstEntry is invalid. + +**/ +BOOLEAN +EFIAPI +IsNodeInList ( + IN CONST LIST_ENTRY *FirstEntry, + IN CONST LIST_ENTRY *SecondEntry + ) +{ + UINTN Count; + CONST LIST_ENTRY *Ptr; + + // + // ASSERT List not too long + // + ASSERT (InternalBaseLibIsListValid (FirstEntry)); + + ASSERT (SecondEntry != NULL); + + Count = 0; + Ptr = FirstEntry; + + // + // Check to see if SecondEntry is a member of FirstEntry. + // Exit early if the number of nodes in List >= PcdMaximumLinkedListLength + // + do { + Ptr = Ptr->ForwardLink; + if (PcdGet32 (PcdMaximumLinkedListLength) > 0) { + Count++; + + // + // Return if the linked list is too long + // + if (Count == PcdGet32 (PcdMaximumLinkedListLength)) { + return (BOOLEAN)(Ptr == SecondEntry); + } + } + + if (Ptr == SecondEntry) { + return TRUE; + } + } while (Ptr != FirstEntry); + + return FALSE; +} + +/** Initializes the head node of a doubly-linked list, and returns the pointer to the head node of the doubly-linked list. @@ -174,8 +223,8 @@ InsertHeadList ( // // ASSERT List not too long and Entry is not one of the nodes of List // - ASSERT (InternalBaseLibIsNodeInList (ListHead, Entry, FALSE)); - + ASSERT_VERIFY_NODE_IN_VALID_LIST (ListHead, Entry, FALSE); + Entry->ForwardLink = ListHead->ForwardLink; Entry->BackLink = ListHead; Entry->ForwardLink->BackLink = Entry; @@ -192,7 +241,7 @@ InsertHeadList ( If ListHead is NULL, then ASSERT(). If Entry is NULL, then ASSERT(). - If ListHead was not initialized with INTIALIZE_LIST_HEAD_VARIABLE() or + If ListHead was not initialized with INTIALIZE_LIST_HEAD_VARIABLE() or InitializeListHead(), then ASSERT(). If PcdMaximumLinkedListLength is not zero, and prior to insertion the number of nodes in ListHead, including the ListHead node, is greater than or @@ -215,8 +264,8 @@ InsertTailList ( // // ASSERT List not too long and Entry is not one of the nodes of List // - ASSERT (InternalBaseLibIsNodeInList (ListHead, Entry, FALSE)); - + ASSERT_VERIFY_NODE_IN_VALID_LIST (ListHead, Entry, FALSE); + Entry->ForwardLink = ListHead; Entry->BackLink = ListHead->BackLink; Entry->BackLink->ForwardLink = Entry; @@ -227,12 +276,12 @@ InsertTailList ( /** Retrieves the first node of a doubly-linked list. - Returns the first node of a doubly-linked list. List must have been + Returns the first node of a doubly-linked list. List must have been initialized with INTIALIZE_LIST_HEAD_VARIABLE() or InitializeListHead(). If List is empty, then List is returned. If List is NULL, then ASSERT(). - If List was not initialized with INTIALIZE_LIST_HEAD_VARIABLE() or + If List was not initialized with INTIALIZE_LIST_HEAD_VARIABLE() or InitializeListHead(), then ASSERT(). If PcdMaximumLinkedListLength is not zero, and the number of nodes in List, including the List node, is greater than or equal to @@ -253,7 +302,7 @@ GetFirstNode ( // // ASSERT List not too long // - ASSERT (InternalBaseLibIsNodeInList (List, List, FALSE)); + ASSERT (InternalBaseLibIsListValid (List)); return List->ForwardLink; } @@ -261,13 +310,13 @@ GetFirstNode ( /** Retrieves the next node of a doubly-linked list. - Returns the node of a doubly-linked list that follows Node. + Returns the node of a doubly-linked list that follows Node. List must have been initialized with INTIALIZE_LIST_HEAD_VARIABLE() or InitializeListHead(). If List is empty, then List is returned. If List is NULL, then ASSERT(). If Node is NULL, then ASSERT(). - If List was not initialized with INTIALIZE_LIST_HEAD_VARIABLE() or + If List was not initialized with INTIALIZE_LIST_HEAD_VARIABLE() or InitializeListHead(), then ASSERT(). If PcdMaximumLinkedListLength is not zero, and List contains more than PcdMaximumLinkedListLength nodes, then ASSERT(). @@ -289,31 +338,31 @@ GetNextNode ( // // ASSERT List not too long and Node is one of the nodes of List // - ASSERT (InternalBaseLibIsNodeInList (List, Node, TRUE)); + ASSERT_VERIFY_NODE_IN_VALID_LIST (List, Node, TRUE); return Node->ForwardLink; } /** Retrieves the previous node of a doubly-linked list. - - Returns the node of a doubly-linked list that precedes Node. + + Returns the node of a doubly-linked list that precedes Node. List must have been initialized with INTIALIZE_LIST_HEAD_VARIABLE() or InitializeListHead(). If List is empty, then List is returned. - + If List is NULL, then ASSERT(). If Node is NULL, then ASSERT(). - If List was not initialized with INTIALIZE_LIST_HEAD_VARIABLE() or + If List was not initialized with INTIALIZE_LIST_HEAD_VARIABLE() or InitializeListHead(), then ASSERT(). If PcdMaximumLinkedListLength is not zero, and List contains more than PcdMaximumLinkedListLength nodes, then ASSERT(). If PcdVerifyNodeInList is TRUE and Node is not a node in List, then ASSERT(). - + @param List A pointer to the head node of a doubly-linked list. @param Node A pointer to a node in the doubly-linked list. - + @return A pointer to the previous node if one exists. Otherwise List is returned. - + **/ LIST_ENTRY * EFIAPI @@ -325,8 +374,8 @@ GetPreviousNode ( // // ASSERT List not too long and Node is one of the nodes of List // - ASSERT (InternalBaseLibIsNodeInList (List, Node, TRUE)); - + ASSERT_VERIFY_NODE_IN_VALID_LIST (List, Node, TRUE); + return Node->BackLink; } @@ -337,7 +386,7 @@ GetPreviousNode ( zero nodes, this function returns TRUE. Otherwise, it returns FALSE. If ListHead is NULL, then ASSERT(). - If ListHead was not initialized with INTIALIZE_LIST_HEAD_VARIABLE() or + If ListHead was not initialized with INTIALIZE_LIST_HEAD_VARIABLE() or InitializeListHead(), then ASSERT(). If PcdMaximumLinkedListLength is not zero, and the number of nodes in List, including the List node, is greater than or equal to @@ -358,8 +407,8 @@ IsListEmpty ( // // ASSERT List not too long // - ASSERT (InternalBaseLibIsNodeInList (ListHead, ListHead, FALSE)); - + ASSERT (InternalBaseLibIsListValid (ListHead)); + return (BOOLEAN)(ListHead->ForwardLink == ListHead); } @@ -374,12 +423,12 @@ IsListEmpty ( If List is NULL, then ASSERT(). If Node is NULL, then ASSERT(). - If List was not initialized with INTIALIZE_LIST_HEAD_VARIABLE() or InitializeListHead(), + If List was not initialized with INTIALIZE_LIST_HEAD_VARIABLE() or InitializeListHead(), then ASSERT(). If PcdMaximumLinkedListLength is not zero, and the number of nodes in List, including the List node, is greater than or equal to PcdMaximumLinkedListLength, then ASSERT(). - If PcdVerifyNodeInList is TRUE and Node is not a node in List and Node is not + If PcdVerifyNodeInList is TRUE and Node is not a node in List and Node is not equal to List, then ASSERT(). @param List A pointer to the head node of a doubly-linked list. @@ -399,8 +448,8 @@ IsNull ( // // ASSERT List not too long and Node is one of the nodes of List // - ASSERT (InternalBaseLibIsNodeInList (List, Node, TRUE)); - + ASSERT_VERIFY_NODE_IN_VALID_LIST (List, Node, TRUE); + return (BOOLEAN)(Node == List); } @@ -437,8 +486,8 @@ IsNodeAtEnd ( // // ASSERT List not too long and Node is one of the nodes of List // - ASSERT (InternalBaseLibIsNodeInList (List, Node, TRUE)); - + ASSERT_VERIFY_NODE_IN_VALID_LIST (List, Node, TRUE); + return (BOOLEAN)(!IsNull (List, Node) && List->BackLink == Node); } @@ -450,12 +499,12 @@ IsNodeAtEnd ( Otherwise, the location of the FirstEntry node is swapped with the location of the SecondEntry node in a doubly-linked list. SecondEntry must be in the same double linked list as FirstEntry and that double linked list must have - been initialized with INTIALIZE_LIST_HEAD_VARIABLE() or InitializeListHead(). + been initialized with INTIALIZE_LIST_HEAD_VARIABLE() or InitializeListHead(). SecondEntry is returned after the nodes are swapped. If FirstEntry is NULL, then ASSERT(). If SecondEntry is NULL, then ASSERT(). - If PcdVerifyNodeInList is TRUE and SecondEntry and FirstEntry are not in the + If PcdVerifyNodeInList is TRUE and SecondEntry and FirstEntry are not in the same linked list, then ASSERT(). If PcdMaximumLinkedListLength is not zero, and the number of nodes in the linked list containing the FirstEntry and SecondEntry nodes, including @@ -464,7 +513,7 @@ IsNodeAtEnd ( @param FirstEntry A pointer to a node in a linked list. @param SecondEntry A pointer to another node in the same linked list. - + @return SecondEntry. **/ @@ -484,8 +533,8 @@ SwapListEntries ( // // ASSERT Entry1 and Entry2 are in the same linked list // - ASSERT (InternalBaseLibIsNodeInList (FirstEntry, SecondEntry, TRUE)); - + ASSERT_VERIFY_NODE_IN_VALID_LIST (FirstEntry, SecondEntry, TRUE); + // // Ptr is the node pointed to by FirstEntry->ForwardLink // @@ -543,7 +592,7 @@ RemoveEntryList ( ) { ASSERT (!IsListEmpty (Entry)); - + Entry->ForwardLink->BackLink = Entry->BackLink; Entry->BackLink->ForwardLink = Entry->ForwardLink; return Entry->ForwardLink; |