+/**\r
+ Safe destroy nodes in a linked list, and return the length of the list after all possible operations finished.\r
+\r
+ Destroy network child instance list by list traversals is not safe due to graph dependencies between nodes.\r
+ This function performs a safe traversal to destroy these nodes by checking to see if the node being destroyed\r
+ has been removed from the list or not.\r
+ If it has been removed, then restart the traversal from the head.\r
+ If it hasn't been removed, then continue with the next node directly.\r
+ This function will end the iterate and return the CallBack's last return value if error happens,\r
+ or retrun EFI_SUCCESS if 2 complete passes are made with no changes in the number of children in the list. \r
+\r
+ @param[in] List The head of the list.\r
+ @param[in] CallBack Pointer to the callback function to destroy one node in the list.\r
+ @param[in] Context Pointer to the callback function's context: corresponds to the\r
+ parameter Context in NET_DESTROY_LINK_LIST_CALLBACK.\r
+ @param[out] ListLength The length of the link list if the function returns successfully.\r
+\r
+ @retval EFI_SUCCESS Two complete passes are made with no changes in the number of children.\r
+ @retval EFI_INVALID_PARAMETER The input parameter is invalid.\r
+ @retval Others Return the CallBack's last return value.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+NetDestroyLinkList (\r
+ IN LIST_ENTRY *List,\r
+ IN NET_DESTROY_LINK_LIST_CALLBACK CallBack,\r
+ IN VOID *Context, OPTIONAL\r
+ OUT UINTN *ListLength OPTIONAL\r
+ )\r
+{\r
+ UINTN PreviousLength;\r
+ LIST_ENTRY *Entry;\r
+ LIST_ENTRY *Ptr;\r
+ UINTN Length;\r
+ EFI_STATUS Status;\r
+\r
+ if (List == NULL || CallBack == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ Length = 0;\r
+ do {\r
+ PreviousLength = Length;\r
+ Entry = GetFirstNode (List);\r
+ while (!IsNull (List, Entry)) {\r
+ Status = CallBack (Entry, Context);\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+ //\r
+ // Walk through the list to see whether the Entry has been removed or not.\r
+ // If the Entry still exists, just try to destroy the next one.\r
+ // If not, go back to the start point to iterate the list again.\r
+ //\r
+ for (Ptr = List->ForwardLink; Ptr != List; Ptr = Ptr->ForwardLink) {\r
+ if (Ptr == Entry) {\r
+ break;\r
+ }\r
+ }\r
+ if (Ptr == Entry) {\r
+ Entry = GetNextNode (List, Entry);\r
+ } else {\r
+ Entry = GetFirstNode (List);\r
+ }\r
+ }\r
+ for (Length = 0, Ptr = List->ForwardLink; Ptr != List; Length++, Ptr = Ptr->ForwardLink);\r
+ } while (Length != PreviousLength);\r
+\r
+ if (ListLength != NULL) {\r
+ *ListLength = Length;\r
+ }\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ This function checks the input Handle to see if it's one of these handles in ChildHandleBuffer.\r
+\r
+ @param[in] Handle Handle to be checked.\r
+ @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer.\r
+ @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL\r
+ if NumberOfChildren is 0.\r
+\r
+ @retval TRUE Found the input Handle in ChildHandleBuffer.\r
+ @retval FALSE Can't find the input Handle in ChildHandleBuffer.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+NetIsInHandleBuffer (\r
+ IN EFI_HANDLE Handle,\r
+ IN UINTN NumberOfChildren,\r
+ IN EFI_HANDLE *ChildHandleBuffer OPTIONAL\r
+ )\r
+{\r
+ UINTN Index;\r
+ \r
+ if (NumberOfChildren == 0 || ChildHandleBuffer == NULL) {\r
+ return FALSE;\r
+ }\r
+\r
+ for (Index = 0; Index < NumberOfChildren; Index++) {\r
+ if (Handle == ChildHandleBuffer[Index]) {\r
+ return TRUE;\r
+ }\r
+ }\r
+\r
+ return FALSE;\r
+}\r
+\r