18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * A generic implementation of binary search for the Linux kernel 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2008-2009 Ksplice, Inc. 68c2ecf20Sopenharmony_ci * Author: Tim Abbott <tabbott@ksplice.com> 78c2ecf20Sopenharmony_ci */ 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci#include <linux/export.h> 108c2ecf20Sopenharmony_ci#include <linux/bsearch.h> 118c2ecf20Sopenharmony_ci#include <linux/kprobes.h> 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci/* 148c2ecf20Sopenharmony_ci * bsearch - binary search an array of elements 158c2ecf20Sopenharmony_ci * @key: pointer to item being searched for 168c2ecf20Sopenharmony_ci * @base: pointer to first element to search 178c2ecf20Sopenharmony_ci * @num: number of elements 188c2ecf20Sopenharmony_ci * @size: size of each element 198c2ecf20Sopenharmony_ci * @cmp: pointer to comparison function 208c2ecf20Sopenharmony_ci * 218c2ecf20Sopenharmony_ci * This function does a binary search on the given array. The 228c2ecf20Sopenharmony_ci * contents of the array should already be in ascending sorted order 238c2ecf20Sopenharmony_ci * under the provided comparison function. 248c2ecf20Sopenharmony_ci * 258c2ecf20Sopenharmony_ci * Note that the key need not have the same type as the elements in 268c2ecf20Sopenharmony_ci * the array, e.g. key could be a string and the comparison function 278c2ecf20Sopenharmony_ci * could compare the string with the struct's name field. However, if 288c2ecf20Sopenharmony_ci * the key and elements in the array are of the same type, you can use 298c2ecf20Sopenharmony_ci * the same comparison function for both sort() and bsearch(). 308c2ecf20Sopenharmony_ci */ 318c2ecf20Sopenharmony_civoid *bsearch(const void *key, const void *base, size_t num, size_t size, cmp_func_t cmp) 328c2ecf20Sopenharmony_ci{ 338c2ecf20Sopenharmony_ci return __inline_bsearch(key, base, num, size, cmp); 348c2ecf20Sopenharmony_ci} 358c2ecf20Sopenharmony_ciEXPORT_SYMBOL(bsearch); 368c2ecf20Sopenharmony_ciNOKPROBE_SYMBOL(bsearch); 37