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