xref: /linux/lib/bsearch.c (revision df65bba1)
1a10e763bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21a94dc35STim Abbott /*
31a94dc35STim Abbott  * A generic implementation of binary search for the Linux kernel
41a94dc35STim Abbott  *
51a94dc35STim Abbott  * Copyright (C) 2008-2009 Ksplice, Inc.
61a94dc35STim Abbott  * Author: Tim Abbott <tabbott@ksplice.com>
71a94dc35STim Abbott  */
81a94dc35STim Abbott 
98bc3bcc9SPaul Gortmaker #include <linux/export.h>
101a94dc35STim Abbott #include <linux/bsearch.h>
1102106f88SAndrea Righi #include <linux/kprobes.h>
121a94dc35STim Abbott 
131a94dc35STim Abbott /*
141a94dc35STim Abbott  * bsearch - binary search an array of elements
151a94dc35STim Abbott  * @key: pointer to item being searched for
161a94dc35STim Abbott  * @base: pointer to first element to search
171a94dc35STim Abbott  * @num: number of elements
181a94dc35STim Abbott  * @size: size of each element
191a94dc35STim Abbott  * @cmp: pointer to comparison function
201a94dc35STim Abbott  *
211a94dc35STim Abbott  * This function does a binary search on the given array.  The
221a94dc35STim Abbott  * contents of the array should already be in ascending sorted order
231a94dc35STim Abbott  * under the provided comparison function.
241a94dc35STim Abbott  *
251a94dc35STim Abbott  * Note that the key need not have the same type as the elements in
261a94dc35STim Abbott  * the array, e.g. key could be a string and the comparison function
271a94dc35STim Abbott  * could compare the string with the struct's name field.  However, if
281a94dc35STim Abbott  * the key and elements in the array are of the same type, you can use
291a94dc35STim Abbott  * the same comparison function for both sort() and bsearch().
301a94dc35STim Abbott  */
bsearch(const void * key,const void * base,size_t num,size_t size,cmp_func_t cmp)31*df65bba1SPeter Zijlstra void *bsearch(const void *key, const void *base, size_t num, size_t size, cmp_func_t cmp)
321a94dc35STim Abbott {
33*df65bba1SPeter Zijlstra 	return __inline_bsearch(key, base, num, size, cmp);
341a94dc35STim Abbott }
351a94dc35STim Abbott EXPORT_SYMBOL(bsearch);
3602106f88SAndrea Righi NOKPROBE_SYMBOL(bsearch);
37