mirror of
https://mirrors.bfsu.edu.cn/git/linux.git
synced 2024-11-14 07:44:21 +08:00
lib/bsearch: Provide __always_inline variant
For code that needs the ultimate performance (it can inline the @cmp function too) or simply needs to avoid calling external functions for whatever reason, provide an __always_inline variant of bsearch(). [ tglx: Renamed to __inline_bsearch() as suggested by Andy ] Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Reviewed-by: Alexandre Chartre <alexandre.chartre@oracle.com> Acked-by: Andy Lutomirski <luto@kernel.org> Link: https://lkml.kernel.org/r/20200505135313.624443814@linutronix.de
This commit is contained in:
parent
ef882bfef9
commit
df65bba1dc
@ -4,7 +4,29 @@
|
|||||||
|
|
||||||
#include <linux/types.h>
|
#include <linux/types.h>
|
||||||
|
|
||||||
void *bsearch(const void *key, const void *base, size_t num, size_t size,
|
static __always_inline
|
||||||
cmp_func_t cmp);
|
void *__inline_bsearch(const void *key, const void *base, size_t num, size_t size, cmp_func_t cmp)
|
||||||
|
{
|
||||||
|
const char *pivot;
|
||||||
|
int result;
|
||||||
|
|
||||||
|
while (num > 0) {
|
||||||
|
pivot = base + (num >> 1) * size;
|
||||||
|
result = cmp(key, pivot);
|
||||||
|
|
||||||
|
if (result == 0)
|
||||||
|
return (void *)pivot;
|
||||||
|
|
||||||
|
if (result > 0) {
|
||||||
|
base = pivot + size;
|
||||||
|
num--;
|
||||||
|
}
|
||||||
|
num >>= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
extern void *bsearch(const void *key, const void *base, size_t num, size_t size, cmp_func_t cmp);
|
||||||
|
|
||||||
#endif /* _LINUX_BSEARCH_H */
|
#endif /* _LINUX_BSEARCH_H */
|
||||||
|
@ -28,27 +28,9 @@
|
|||||||
* the key and elements in the array are of the same type, you can use
|
* the key and elements in the array are of the same type, you can use
|
||||||
* the same comparison function for both sort() and bsearch().
|
* the same comparison function for both sort() and bsearch().
|
||||||
*/
|
*/
|
||||||
void *bsearch(const void *key, const void *base, size_t num, size_t size,
|
void *bsearch(const void *key, const void *base, size_t num, size_t size, cmp_func_t cmp)
|
||||||
cmp_func_t cmp)
|
|
||||||
{
|
{
|
||||||
const char *pivot;
|
return __inline_bsearch(key, base, num, size, cmp);
|
||||||
int result;
|
|
||||||
|
|
||||||
while (num > 0) {
|
|
||||||
pivot = base + (num >> 1) * size;
|
|
||||||
result = cmp(key, pivot);
|
|
||||||
|
|
||||||
if (result == 0)
|
|
||||||
return (void *)pivot;
|
|
||||||
|
|
||||||
if (result > 0) {
|
|
||||||
base = pivot + size;
|
|
||||||
num--;
|
|
||||||
}
|
|
||||||
num >>= 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
EXPORT_SYMBOL(bsearch);
|
EXPORT_SYMBOL(bsearch);
|
||||||
NOKPROBE_SYMBOL(bsearch);
|
NOKPROBE_SYMBOL(bsearch);
|
||||||
|
Loading…
Reference in New Issue
Block a user