]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/BaseLib/LinkedList.c
MdePkg/BaseLib: Add IsNodeInList() function.
[mirror_edk2.git] / MdePkg / Library / BaseLib / LinkedList.c
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