summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2023-08-03 12:49:50 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2023-11-06 08:43:41 +0100
commit0357e8436ee6d4672052c375a740378c0a441be5 (patch)
tree1490714d944036bcb1e21c07e493634d8bde0ef8 /lib
parentc74176c1842656f6ea1b38d6e8716edc005895f5 (diff)
downloadbarebox-0357e8436ee6d4672052c375a740378c0a441be5.tar.gz
barebox-0357e8436ee6d4672052c375a740378c0a441be5.tar.xz
lib: Add generic binary search function
Add generic binary search function based on Linux-6.5-rc3. Link: https://lore.barebox.org/20230803105003.4088205-10-s.hauer@pengutronix.de Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'lib')
-rw-r--r--lib/Makefile1
-rw-r--r--lib/bsearch.c34
2 files changed, 35 insertions, 0 deletions
diff --git a/lib/Makefile b/lib/Makefile
index 791080b2d1..8817b5f47b 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -64,6 +64,7 @@ obj-y += wchar.o
obj-y += libfile.o
obj-y += bitmap.o
obj-y += gcd.o
+obj-y += bsearch.o
obj-pbl-y += hexdump.o
obj-$(CONFIG_FONTS) += fonts/
obj-$(CONFIG_BAREBOX_LOGO) += logo/
diff --git a/lib/bsearch.c b/lib/bsearch.c
new file mode 100644
index 0000000000..3b78cd31f4
--- /dev/null
+++ b/lib/bsearch.c
@@ -0,0 +1,34 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * A generic implementation of binary search for the Linux kernel
+ *
+ * Copyright (C) 2008-2009 Ksplice, Inc.
+ * Author: Tim Abbott <tabbott@ksplice.com>
+ */
+
+#include <module.h>
+#include <linux/bsearch.h>
+
+/*
+ * bsearch - binary search an array of elements
+ * @key: pointer to item being searched for
+ * @base: pointer to first element to search
+ * @num: number of elements
+ * @size: size of each element
+ * @cmp: pointer to comparison function
+ *
+ * This function does a binary search on the given array. The
+ * contents of the array should already be in ascending sorted order
+ * under the provided comparison function.
+ *
+ * Note that the key need not have the same type as the elements in
+ * the array, e.g. key could be a string and the comparison function
+ * could compare the string with the struct's name field. However, if
+ * the key and elements in the array are of the same type, you can use
+ * the same comparison function for both sort() and bsearch().
+ */
+void *bsearch(const void *key, const void *base, size_t num, size_t size, cmp_func_t cmp)
+{
+ return __inline_bsearch(key, base, num, size, cmp);
+}
+EXPORT_SYMBOL(bsearch);