]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdePkg/BaseLib: Add IsNodeInList() function.
authorMarvin.Haeuser@outlook.com <Marvin.Haeuser@outlook.com>
Thu, 3 Aug 2017 19:52:08 +0000 (03:52 +0800)
committerLiming Gao <liming.gao@intel.com>
Wed, 16 Aug 2017 08:55:30 +0000 (16:55 +0800)
This patch adds IsNodeInList() to BaseLib, which verifies the given
Node is part of the doubly-linked List provided.

V2:
  - Rename "List" to "FirstEntry" and "Node" to "SecondEntry" to clarify that
    "FirstEntry" does not need to be the doubly-linked list's head node.

V3:
  - Remove ASSERTs from IsNodeInList() which are present in
    InternalBaseLibIsListValid().

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/Include/Library/BaseLib.h
MdePkg/Library/BaseLib/BaseLibInternals.h
MdePkg/Library/BaseLib/LinkedList.c

index 791849b80406299c1a840418fbdffa081dbd4e50..40b96b761a85fc44abb3abe4dde768033ee12f38 100644 (file)
@@ -2868,6 +2868,33 @@ PathCleanUpDirectories(
 #define INITIALIZE_LIST_HEAD_VARIABLE(ListHead)  {&(ListHead), &(ListHead)}\r
 \r
 \r
+/**\r
+  Checks whether FirstEntry and SecondEntry are part of the same doubly-linked\r
+  list.\r
+\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 SecondEntry is NULL, then ASSERT();\r
+  If PcdMaximumLinkedListLength is not zero, and List contains more than\r
+  PcdMaximumLinkedListLength nodes, 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
+\r
+  @retval TRUE   SecondEntry is in the same doubly-linked list as FirstEntry.\r
+  @retval FALSE  SecondEntry isn't in the same doubly-linked list as FirstEntry,\r
+                 or FirstEntry is invalid.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+IsNodeInList (\r
+  IN      CONST LIST_ENTRY      *FirstEntry,\r
+  IN      CONST LIST_ENTRY      *SecondEntry\r
+  );\r
+\r
+\r
 /**\r
   Initializes the head node of a doubly linked list, and returns the pointer to\r
   the head node of the doubly linked list.\r
index ea387ce37d275bbab106f282f70882270e3dd190..9dca97a0dcc9dc4bf7bdc8c0b75eb222c4a16c11 100644 (file)
@@ -339,34 +339,6 @@ InternalSwitchStack (
   );\r
 \r
 \r
-/**\r
-  Worker function that locates the Node in the 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
-\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 PcdMaximumLinkedListLength is not zero, and prior to insertion the number\r
-  of nodes in ListHead, including the ListHead node, is greater than or\r
-  equal to PcdMaximumLinkedListLength, then ASSERT().\r
-\r
-  @param  List  A pointer to a node in a linked list.\r
-  @param  Node  A pointer to one nod.\r
-\r
-  @retval TRUE   Node is in List.\r
-  @retval FALSE  Node isn't in List, or List is invalid.\r
-\r
-**/\r
-BOOLEAN\r
-EFIAPI\r
-IsNodeInList (\r
-  IN      CONST LIST_ENTRY      *List,\r
-  IN      CONST LIST_ENTRY      *Node\r
-  );\r
-\r
 /**\r
   Worker function that returns a bit field from Operand.\r
 \r
index ba373f4b7be31c946ed4f14fc14d593fe9f0306d..af27b0dd9a5cf6c00881cb8f1a1ae09704433210 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Linked List Library Functions.\r
 \r
-  Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>\r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
@@ -112,6 +112,70 @@ InternalBaseLibIsNodeInList (
   return TRUE;\r
 }\r
 \r
+/**\r
+  Checks whether FirstEntry and SecondEntry are part of the same doubly-linked\r
+  list.\r
+\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 SecondEntry is NULL, then ASSERT();\r
+  If PcdMaximumLinkedListLength is not zero, and List contains more than\r
+  PcdMaximumLinkedListLength nodes, 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
+\r
+  @retval TRUE   SecondEntry is in the same doubly-linked list as FirstEntry.\r
+  @retval FALSE  SecondEntry isn't in the same doubly-linked list as FirstEntry,\r
+                 or FirstEntry is invalid.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+IsNodeInList (\r
+  IN      CONST LIST_ENTRY      *FirstEntry,\r
+  IN      CONST LIST_ENTRY      *SecondEntry\r
+  )\r
+{\r
+  UINTN             Count;\r
+  CONST LIST_ENTRY  *Ptr;\r
+\r
+  //\r
+  // ASSERT List not too long\r
+  //\r
+  ASSERT (InternalBaseLibIsListValid (FirstEntry));\r
+\r
+  ASSERT (SecondEntry != NULL);\r
+\r
+  Count = 0;\r
+  Ptr   = FirstEntry;\r
+\r
+  //\r
+  // Check to see if SecondEntry is a member of FirstEntry.  \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
+      //\r
+      // Return if the linked list is too long\r
+      //\r
+      if (Count == PcdGet32 (PcdMaximumLinkedListLength)) {\r
+        return (BOOLEAN)(Ptr == SecondEntry);\r
+      }\r
+    }\r
+\r
+    if (Ptr == SecondEntry) {\r
+      return TRUE;\r
+    }\r
+  } while (Ptr != FirstEntry);\r
+\r
+  return FALSE;\r
+}\r
+\r
 /**\r
   Initializes the head node of a doubly-linked list, and returns the pointer to\r
   the head node of the doubly-linked list.\r