summaryrefslogtreecommitdiffstats
path: root/DynamicTablesPkg/Library/Common
diff options
context:
space:
mode:
authorKrzysztof Koch <krzysztof.koch@arm.com>2019-04-09 10:41:40 +0100
committerSami Mujawar <sami.mujawar@arm.com>2019-06-10 20:44:31 +0100
commitc1b53091f60fb301a71238e6ef38834d48757d96 (patch)
tree836d52ece145da3ac79bfddce62063da307855c8 /DynamicTablesPkg/Library/Common
parent75bf10a68914a50b1d5c57f63a8561fa52dc0973 (diff)
downloadedk2-c1b53091f60fb301a71238e6ef38834d48757d96.tar.gz
edk2-c1b53091f60fb301a71238e6ef38834d48757d96.tar.bz2
edk2-c1b53091f60fb301a71238e6ef38834d48757d96.zip
DynamicTablesPkg: Add code for finding duplicate values in arrays
Added generic function for detecting duplicate values in an array. Also defined a function prototype to test if two objects are equal. The prototype is used as an argument to the 'FindDuplicateValues' function. Signed-off-by: Krzysztof Koch <krzysztof.koch@arm.com> Reviewed-by: Alexei Fedorov <Alexei.Fedorov@arm.com> Reviewed-by: Sami Mujawar <sami.mujawar@arm.com>
Diffstat (limited to 'DynamicTablesPkg/Library/Common')
-rw-r--r--DynamicTablesPkg/Library/Common/TableHelperLib/TableHelper.c64
1 files changed, 64 insertions, 0 deletions
diff --git a/DynamicTablesPkg/Library/Common/TableHelperLib/TableHelper.c b/DynamicTablesPkg/Library/Common/TableHelperLib/TableHelper.c
index 3938302b6d..fc6cf3b088 100644
--- a/DynamicTablesPkg/Library/Common/TableHelperLib/TableHelper.c
+++ b/DynamicTablesPkg/Library/Common/TableHelperLib/TableHelper.c
@@ -13,6 +13,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
// Module specific include files.
#include <AcpiTableGenerator.h>
#include <ConfigurationManagerObject.h>
+#include <Library/TableHelperLib.h>
#include <Protocol/ConfigurationManagerProtocol.h>
/** The GetCgfMgrInfo function gets the CM_STD_OBJ_CONFIGURATION_MANAGER_INFO
@@ -180,3 +181,66 @@ AddAcpiHeader (
error_handler:
return Status;
}
+
+/**
+ Test and report if a duplicate entry exists in the given array of comparable
+ elements.
+
+ @param [in] Array Array of elements to test for duplicates.
+ @param [in] Count Number of elements in Array.
+ @param [in] ElementSize Size of an element in bytes
+ @param [in] EqualTestFunction The function to call to check if any two
+ elements are equal.
+
+ @retval TRUE A duplicate element was found or one of
+ the input arguments is invalid.
+ @retval FALSE Every element in Array is unique.
+**/
+BOOLEAN
+EFIAPI
+FindDuplicateValue (
+ IN CONST VOID * Array,
+ IN CONST UINTN Count,
+ IN CONST UINTN ElementSize,
+ IN PFN_IS_EQUAL EqualTestFunction
+ )
+{
+ UINTN Index1;
+ UINTN Index2;
+ UINT8 * Element1;
+ UINT8 * Element2;
+
+ if (Array == NULL) {
+ DEBUG ((DEBUG_ERROR, "ERROR: FindDuplicateValues: Array is NULL.\n"));
+ return TRUE;
+ }
+
+ if (ElementSize == 0) {
+ DEBUG ((DEBUG_ERROR, "ERROR: FindDuplicateValues: ElementSize is 0.\n"));
+ return TRUE;
+ }
+
+ if (EqualTestFunction == NULL) {
+ DEBUG ((
+ DEBUG_ERROR,
+ "ERROR: FindDuplicateValues: EqualTestFunction is NULL.\n"
+ ));
+ return TRUE;
+ }
+
+ if (Count < 2) {
+ return FALSE;
+ }
+
+ for (Index1 = 0; Index1 < Count - 1; Index1++) {
+ for (Index2 = Index1 + 1; Index2 < Count; Index2++) {
+ Element1 = (UINT8*)Array + (Index1 * ElementSize);
+ Element2 = (UINT8*)Array + (Index2 * ElementSize);
+
+ if (EqualTestFunction (Element1, Element2, Index1, Index2)) {
+ return TRUE;
+ }
+ }
+ }
+ return FALSE;
+}