From patchwork Wed Jul 20 13:53:58 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shanker Donthineni X-Patchwork-Id: 9239643 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 2070460574 for ; Wed, 20 Jul 2016 13:56:44 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0ED7927BF7 for ; Wed, 20 Jul 2016 13:56:44 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0102227BFF; Wed, 20 Jul 2016 13:56:43 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 8421727BF7 for ; Wed, 20 Jul 2016 13:56:43 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.85_2 #1 (Red Hat Linux)) id 1bPryC-0008Gy-2H; Wed, 20 Jul 2016 13:55:28 +0000 Received: from smtp.codeaurora.org ([198.145.29.96]) by bombadil.infradead.org with esmtps (Exim 4.85_2 #1 (Red Hat Linux)) id 1bPrxC-0006iw-1W for linux-arm-kernel@lists.infradead.org; Wed, 20 Jul 2016 13:54:29 +0000 Received: by smtp.codeaurora.org (Postfix, from userid 1000) id 5082961348; Wed, 20 Jul 2016 13:54:06 +0000 (UTC) Received: from shankerd-ubuntu.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: shankerd@smtp.codeaurora.org) by smtp.codeaurora.org (Postfix) with ESMTPSA id C469B611FD; Wed, 20 Jul 2016 13:54:04 +0000 (UTC) From: Shanker Donthineni To: Marc Zyngier , linux-kernel , linux-arm-kernel Subject: [PATCH V3 2/4] xen: Add generic implementation of binary search Date: Wed, 20 Jul 2016 08:53:58 -0500 Message-Id: <1469022840-2142-3-git-send-email-shankerd@codeaurora.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1469022840-2142-1-git-send-email-shankerd@codeaurora.org> References: <1469022840-2142-1-git-send-email-shankerd@codeaurora.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160720_065426_228257_F6E0C444 X-CRM114-Status: GOOD ( 21.60 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Thomas Gleixner , Philip Elcan , Shanker Donthineni , Jason Cooper , Vikram Sethi MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP This patch adds the generic implementation of binary search algorithm which is copied from Linux kernel v4.7-rc7. No functional changes. Signed-off-by: Shanker Donthineni Reviewed-by: Andrew Cooper Reviewed-by: Julien Grall --- Changes since v2: Fixed the compilation issue due to a missing ';'. Fixed typo. Changes since v1: Removed the header file xen/include/xen/bsearch.h. Defined function bsearch() prototype in the header file xen/lib.h. xen/common/Makefile | 1 + xen/common/bsearch.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ xen/include/xen/lib.h | 3 +++ 3 files changed, 55 insertions(+) create mode 100644 xen/common/bsearch.c diff --git a/xen/common/Makefile b/xen/common/Makefile index dbf00c6..f8123c2 100644 --- a/xen/common/Makefile +++ b/xen/common/Makefile @@ -43,6 +43,7 @@ obj-y += schedule.o obj-y += shutdown.o obj-y += softirq.o obj-y += sort.o +obj-y += bsearch.o obj-y += smp.o obj-y += spinlock.o obj-y += stop_machine.o diff --git a/xen/common/bsearch.c b/xen/common/bsearch.c new file mode 100644 index 0000000..7090930 --- /dev/null +++ b/xen/common/bsearch.c @@ -0,0 +1,51 @@ +/* + * A generic implementation of binary search for the Linux kernel + * + * Copyright (C) 2008-2009 Ksplice, Inc. + * Author: Tim Abbott + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; version 2. + */ + +#include + +/* + * bsearch - binary search an array of elements + * @key: pointer to item being searched for + * @base: pointer to first element to search + * @num: number of elements + * @size: size of each element + * @cmp: pointer to comparison function + * + * This function does a binary search on the given array. The + * contents of the array should already be in ascending sorted order + * under the provided comparison function. + * + * Note that the key need not have the same type as the elements in + * the array, e.g. key could be a string and the comparison function + * could compare the string with the struct's name field. However, if + * the key and elements in the array are of the same type, you can use + * the same comparison function for both sort() and bsearch(). + */ +void *bsearch(const void *key, const void *base, size_t num, size_t size, + int (*cmp)(const void *key, const void *elt)) +{ + size_t start = 0, end = num; + int result; + + while (start < end) { + size_t mid = start + (end - start) / 2; + + result = cmp(key, base + mid * size); + if (result < 0) + end = mid; + else if (result > 0) + start = mid + 1; + else + return (void *)base + mid * size; + } + + return NULL; +} diff --git a/xen/include/xen/lib.h b/xen/include/xen/lib.h index b1b0fb2..b90d582 100644 --- a/xen/include/xen/lib.h +++ b/xen/include/xen/lib.h @@ -153,4 +153,7 @@ void dump_execstate(struct cpu_user_regs *); void init_constructors(void); +void *bsearch(const void *key, const void *base, size_t num, size_t size, + int (*cmp)(const void *key, const void *elt)); + #endif /* __LIB_H__ */