^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * A generic implementation of binary search for the Linux kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2008-2009 Ksplice, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author: Tim Abbott <tabbott@ksplice.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/bsearch.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kprobes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * bsearch - binary search an array of elements
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * @key: pointer to item being searched for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * @base: pointer to first element to search
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * @num: number of elements
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * @size: size of each element
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * @cmp: pointer to comparison function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * This function does a binary search on the given array. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * contents of the array should already be in ascending sorted order
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * under the provided comparison function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * Note that the key need not have the same type as the elements in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * the array, e.g. key could be a string and the comparison function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * could compare the string with the struct's name field. However, if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * the key and elements in the array are of the same type, you can use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * the same comparison function for both sort() and bsearch().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) void *bsearch(const void *key, const void *base, size_t num, size_t size, cmp_func_t cmp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return __inline_bsearch(key, base, num, size, cmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) EXPORT_SYMBOL(bsearch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) NOKPROBE_SYMBOL(bsearch);