]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdePkg/BaseLib: Update internal LinkedList verifications.
authorMarvin.Haeuser@outlook.com <Marvin.Haeuser@outlook.com>
Thu, 3 Aug 2017 19:52:15 +0000 (03:52 +0800)
committerLiming Gao <liming.gao@intel.com>
Wed, 16 Aug 2017 08:55:32 +0000 (16:55 +0800)
1) Replace InternalBaseLibIsNodeInList() with
   InternalBaseLibIsListValid().
   - The verification whether Node is within the doubly-linked List
     is now done by IsNodeInList().
   - Whether the list is valid is returned.

2) The comments within InsertHeadList() and InsertTailList() stated
   that it is checked whether Entry is not part of the doubly-linked
   list. This was not done as argument 3 of
   InternalBaseLibIsNodeInList() indicated whether the check is done,
   not whether to check if the node is or is not in the list. This
   has been fixed by using IsNodeInList() for the ASSERTs.

V2:
  - Fix IsListEmpty() to ASSERT when the passed list is invalid.
  - Introduce the VERIFY_IS_NODE_IN_LIST() macro to only verify whether the
    passed node is part of the list when PcdVerifyNodeInList is TRUE.

V3:
  - Introduce the ASSERT_VERIFY_NODE_IN_VALID_LIST() macro which,
    depending on the value of PcdVerifyNodeInList, verifies whether
    SecondEntry is or is not part of the same doubly-linked list as
    FirstEntry and unconditionally verifies whether the doubly-linked
    list FirstEntry is part of is valid. This prevents
    InternalBaseLibIsListValid() from being called twice when a
    function ASSERTs via the result of IsNodeInList(), as it calls
    InternalBaseLibIsListValid() already.
  - Remove the VERIFY_IS_NODE_IN_LIST() macro in favor of
    ASSERT_VERIFY_NODE_IN_VALID_LIST().

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Marvin Haeuser <Marvin.Haeuser@outlook.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
MdePkg/Library/BaseLib/LinkedList.c

index af27b0dd9a5cf6c00881cb8f1a1ae09704433210..30fd7009e0078b98506f54431ebd116f62d01c7f 100644 (file)
 #include "BaseLibInternals.h"\r
 \r
 /**\r
-  Worker function that locates the Node in the List.\r
+  If PcdVerifyNodeInList is TRUE, ASSERTs when SecondEntry is or is not part of\r
+  the same doubly-linked list as FirstEntry depending on the value of InList.\r
+  Independent of PcdVerifyNodeInList, ASSERTs when FirstEntry is not part of a\r
+  valid list.\r
 \r
-  By searching the List, finds the location of the Node in List. At the same time,\r
-  verifies the validity of this list.\r
+  If FirstEntry is NULL, then ASSERT().\r
+  If FirstEntry->ForwardLink is NULL, then ASSERT().\r
+  If FirstEntry->BackLink is NULL, then ASSERT().\r
+  If PcdMaximumLinkedListLength is not zero, and List contains more than\r
+  PcdMaximumLinkedListLength nodes, then ASSERT().\r
+  If PcdVerifyNodeInList is TRUE and SecondEntry is NULL, then ASSERT().\r
+\r
+  @param  FirstEntry   A pointer to a node in a linked list.\r
+  @param  SecondEntry  A pointer to the node to locate.\r
+  @param  InList       Defines whether to check if SecondEntry is or is not part\r
+                       of the same doubly-linked list as FirstEntry.\r
+\r
+**/\r
+#if !defined (MDEPKG_NDEBUG)\r
+  #define ASSERT_VERIFY_NODE_IN_VALID_LIST(FirstEntry, SecondEntry, InList)  \\r
+    do {                                                                     \\r
+      if (FeaturePcdGet (PcdVerifyNodeInList)) {                             \\r
+        ASSERT (InList == IsNodeInList ((FirstEntry), (SecondEntry)));       \\r
+      } else {                                                               \\r
+        ASSERT (InternalBaseLibIsListValid (FirstEntry));                    \\r
+      }                                                                      \\r
+    } while (FALSE)\r
+#else\r
+  #define ASSERT_VERIFY_NODE_IN_VALID_LIST(FirstEntry, SecondEntry, InList)\r
+#endif\r
+\r
+/**\r
+  Worker function that verifies the validity of this list.\r
 \r
   If List is NULL, then ASSERT().\r
   If List->ForwardLink is NULL, then ASSERT().\r
-  If List->backLink is NULL, then ASSERT().\r
-  If Node is NULL, then ASSERT().\r
-  If PcdVerifyNodeInList is TRUE and DoMembershipCheck is TRUE and Node \r
-  is in not a member of List, then return FALSE\r
+  If List->BackLink is NULL, then ASSERT().\r
   If PcdMaximumLinkedListLength is not zero, and List contains more than\r
   PcdMaximumLinkedListLength nodes, then ASSERT().\r
 \r
   @param  List              A pointer to a node in a linked list.\r
-  @param  Node              A pointer to a node in a linked list.\r
-  @param  VerifyNodeInList  TRUE if a check should be made to see if Node is a \r
-                            member of List.  FALSE if no membership test should \r
-                            be performed.\r
 \r
   @retval   TRUE if PcdVerifyNodeInList is FALSE\r
   @retval   TRUE if DoMembershipCheck is FALSE\r
 **/\r
 BOOLEAN\r
 EFIAPI\r
-InternalBaseLibIsNodeInList (\r
-  IN CONST LIST_ENTRY  *List,\r
-  IN CONST LIST_ENTRY  *Node,\r
-  IN BOOLEAN           VerifyNodeInList\r
+InternalBaseLibIsListValid (\r
+  IN CONST LIST_ENTRY  *List\r
   )\r
 {\r
   UINTN             Count;\r
@@ -60,40 +80,11 @@ InternalBaseLibIsNodeInList (
   ASSERT (List != NULL);\r
   ASSERT (List->ForwardLink != NULL);\r
   ASSERT (List->BackLink != NULL);\r
-  ASSERT (Node != NULL);\r
-\r
-  Count = 0;\r
-  Ptr   = List;\r
-\r
-  if (FeaturePcdGet (PcdVerifyNodeInList) && VerifyNodeInList) {\r
-    //\r
-    // Check to see if Node is a member of List.  \r
-    // Exit early if the number of nodes in List >= PcdMaximumLinkedListLength\r
-    //\r
-    do {\r
-      Ptr = Ptr->ForwardLink;\r
-      if (PcdGet32 (PcdMaximumLinkedListLength) > 0) {\r
-        Count++;\r
-        //\r
-        // ASSERT() if the linked list is too long\r
-        //\r
-        ASSERT (Count < PcdGet32 (PcdMaximumLinkedListLength));\r
-\r
-        //\r
-        // Return if the linked list is too long\r
-        //\r
-        if (Count >= PcdGet32 (PcdMaximumLinkedListLength)) {\r
-          return (BOOLEAN)(Ptr == Node);\r
-        }\r
-      }\r
-    } while ((Ptr != List) && (Ptr != Node)); \r
-\r
-    if (Ptr != Node) {\r
-      return FALSE;\r
-    }\r
-  }\r
 \r
   if (PcdGet32 (PcdMaximumLinkedListLength) > 0) {\r
+    Count = 0;\r
+    Ptr   = List;\r
+\r
     //\r
     // Count the total number of nodes in List.\r
     // Exit early if the number of nodes in List >= PcdMaximumLinkedListLength\r
@@ -104,9 +95,9 @@ InternalBaseLibIsNodeInList (
     } while ((Ptr != List) && (Count < PcdGet32 (PcdMaximumLinkedListLength)));\r
 \r
     //\r
-    // ASSERT() if the linked list is too long\r
+    // return whether linked list is too long\r
     //\r
-    ASSERT (Count < PcdGet32 (PcdMaximumLinkedListLength));\r
+    return (BOOLEAN)(Count < PcdGet32 (PcdMaximumLinkedListLength));\r
   }\r
 \r
   return TRUE;\r
@@ -238,7 +229,7 @@ InsertHeadList (
   //\r
   // ASSERT List not too long and Entry is not one of the nodes of List\r
   //\r
-  ASSERT (InternalBaseLibIsNodeInList (ListHead, Entry, FALSE));\r
+  ASSERT_VERIFY_NODE_IN_VALID_LIST (ListHead, Entry, FALSE);\r
   \r
   Entry->ForwardLink = ListHead->ForwardLink;\r
   Entry->BackLink = ListHead;\r
@@ -279,7 +270,7 @@ InsertTailList (
   //\r
   // ASSERT List not too long and Entry is not one of the nodes of List\r
   //\r
-  ASSERT (InternalBaseLibIsNodeInList (ListHead, Entry, FALSE));\r
+  ASSERT_VERIFY_NODE_IN_VALID_LIST (ListHead, Entry, FALSE);\r
   \r
   Entry->ForwardLink = ListHead;\r
   Entry->BackLink = ListHead->BackLink;\r
@@ -317,7 +308,7 @@ GetFirstNode (
   //\r
   // ASSERT List not too long\r
   //\r
-  ASSERT (InternalBaseLibIsNodeInList (List, List, FALSE));\r
+  ASSERT (InternalBaseLibIsListValid (List));\r
 \r
   return List->ForwardLink;\r
 }\r
@@ -353,7 +344,7 @@ GetNextNode (
   //\r
   // ASSERT List not too long and Node is one of the nodes of List\r
   //\r
-  ASSERT (InternalBaseLibIsNodeInList (List, Node, TRUE));\r
+  ASSERT_VERIFY_NODE_IN_VALID_LIST (List, Node, TRUE);\r
 \r
   return Node->ForwardLink;\r
 }\r
@@ -389,7 +380,7 @@ GetPreviousNode (
   //\r
   // ASSERT List not too long and Node is one of the nodes of List\r
   //\r
-  ASSERT (InternalBaseLibIsNodeInList (List, Node, TRUE));\r
+  ASSERT_VERIFY_NODE_IN_VALID_LIST (List, Node, TRUE);\r
  \r
   return Node->BackLink;\r
 }\r
@@ -422,7 +413,7 @@ IsListEmpty (
   //\r
   // ASSERT List not too long\r
   //\r
-  ASSERT (InternalBaseLibIsNodeInList (ListHead, ListHead, FALSE));\r
+  ASSERT (InternalBaseLibIsListValid (ListHead));\r
   \r
   return (BOOLEAN)(ListHead->ForwardLink == ListHead);\r
 }\r
@@ -463,7 +454,7 @@ IsNull (
   //\r
   // ASSERT List not too long and Node is one of the nodes of List\r
   //\r
-  ASSERT (InternalBaseLibIsNodeInList (List, Node, TRUE));\r
+  ASSERT_VERIFY_NODE_IN_VALID_LIST (List, Node, TRUE);\r
   \r
   return (BOOLEAN)(Node == List);\r
 }\r
@@ -501,7 +492,7 @@ IsNodeAtEnd (
   //\r
   // ASSERT List not too long and Node is one of the nodes of List\r
   //\r
-  ASSERT (InternalBaseLibIsNodeInList (List, Node, TRUE));\r
+  ASSERT_VERIFY_NODE_IN_VALID_LIST (List, Node, TRUE);\r
   \r
   return (BOOLEAN)(!IsNull (List, Node) && List->BackLink == Node);\r
 }\r
@@ -548,7 +539,7 @@ SwapListEntries (
   //\r
   // ASSERT Entry1 and Entry2 are in the same linked list\r
   //\r
-  ASSERT (InternalBaseLibIsNodeInList (FirstEntry, SecondEntry, TRUE));\r
+  ASSERT_VERIFY_NODE_IN_VALID_LIST (FirstEntry, SecondEntry, TRUE);\r
   \r
   //\r
   // Ptr is the node pointed to by FirstEntry->ForwardLink\r