From patchwork Thu Mar 15 20:30:22 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andre Przywara X-Patchwork-Id: 131848 Delivered-To: patch@linaro.org Received: by 10.46.84.17 with SMTP id i17csp1579219ljb; Thu, 15 Mar 2018 13:33:21 -0700 (PDT) X-Google-Smtp-Source: AG47ELtxXForTnHGjQmyJiBqPfI6VsEWcb7m/dsyD74dnu/mQi7SnhgWeW57nMhwWSreOxUz/bHB X-Received: by 10.107.168.226 with SMTP id e95mr6831344ioj.205.1521146001329; Thu, 15 Mar 2018 13:33:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521146001; cv=none; d=google.com; s=arc-20160816; b=bPwFZbGG5htTpoMB7/2NR+UtOHtHxbkuWj1whiVmT407P5d98yvgOpVxH33vXL8xkW IimJeWODFQheLQH703og79VHbkUA9WqlxJ90m67cmVGWNGf7NN2L+rGxSSFze3rWeA8i eBl50T8ii6R64POSzL6X/dOdcPm6Y9CPnbPe+UYaDIgq2tckY17P5qD+edrEE50a0SBs DKM0zkaYY9T4nudrk7dk3PgX4h5CFr5prhVYI10Wkd9rFqgShrm9BgSftEduWNXPORQq gK/FuKiFYQcKTJ3i7JYpaHXZ0B8XBjuDgyurIu543j6EDyVgm02ovnoaalcLkJqtIuf5 XX7A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version :list-subscribe:list-help:list-post:list-unsubscribe:list-id :precedence:subject:cc:references:in-reply-to:message-id:date:to :from:dkim-signature:arc-authentication-results; bh=Xrjp3RMGq0zEvU/MSNomlAQ2bZNO3adZiuJllRK2DIE=; b=F2GX3bZAofjoO7Z/mntLM+kLEXZB/kpCUu4EC5t6rvHWxiqCPZdPmxa9egz+5Cfwsb 7T2C2GBgm3B1BJOAmVWuTWR2ORDf9oXNa4eEe9arAQaWwNQjoRL1MDbAFo+8VLLANoc8 yfnTlQyj3RDoBMXr9B84Bpo2JCQ1iBwdD8pQTc1Pb+ekKHzg1qfuQ3GtEnZySx1hbWhX NBXFMa3oCnthfnTVVOE76sW5ommBMb8bMXGaWhQjBq5nSz5PH/1zY/VCFLvyEfPDR+gm IXGR8VaZX8DaY7cA0akVKWlWIhJJyMUbO2ArS4QTzuw5stDf/FkNKDgB/pI4NYfqZBTj NJSQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=LC7VeNH3; spf=pass (google.com: best guess record for domain of xen-devel-bounces@lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.xenproject.org (lists.xenproject.org. [192.237.175.120]) by mx.google.com with ESMTPS id 80-v6si2948761itm.121.2018.03.15.13.33.21 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 15 Mar 2018 13:33:21 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of xen-devel-bounces@lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=LC7VeNH3; spf=pass (google.com: best guess record for domain of xen-devel-bounces@lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ewZX5-0003RN-P7; Thu, 15 Mar 2018 20:31:27 +0000 Received: from us1-rack-dfw2.inumbo.com ([104.130.134.6]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ewZX4-0003P6-Rl for xen-devel@lists.xenproject.org; Thu, 15 Mar 2018 20:31:26 +0000 X-Inumbo-ID: d11dec27-288f-11e8-9728-bc764e045a96 Received: from mail-wm0-x241.google.com (unknown [2a00:1450:400c:c09::241]) by us1-rack-dfw2.inumbo.com (Halon) with ESMTPS id d11dec27-288f-11e8-9728-bc764e045a96; Thu, 15 Mar 2018 21:31:19 +0100 (CET) Received: by mail-wm0-x241.google.com with SMTP id s206so24730985wme.0 for ; Thu, 15 Mar 2018 13:31:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=kwpNoXQTNExhFesBZGLdmGl4sN77xx5nCEyYniPtRIU=; b=LC7VeNH3cAXntQm58FeBBUR+D1OW9PF/aRrUQBey4OsRstWBD1aDK0eogLx5Ojuxao TMGRWCZpEldjTJKt13cBzExdb94rkCOk4IlNMySOZlR+Mj2aomiwzekuf4AcDeXnwTqA dq2OrIqb6cka2O0FT6g/53lRvJmT37LA9lGSc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=kwpNoXQTNExhFesBZGLdmGl4sN77xx5nCEyYniPtRIU=; b=Lj5pGAa6p4IpRxla5j4PNNfyrXctHq3LVC84Zb1GGGaE+6jUkSGUDL1tuDK+EdCYvQ U2A10Wd9A4aRy9ZilbI+UbcmRhIoivioLltbSIk1lHSHr6C6jF1Qixvorksb7JaCOrqD Kvsy/ye1YwQfANuNO0IIDJPO2ivIj/rlwkixa2nnNTYcxYhfvAkwFqb1szn7bQ4GM2JT 1kPIrJ//JLghbIV7LmNq5aJQh8o9TMGFcxe8k23do7IEzY5H6Zcfni52g9XECfnxmkFS W3pMNYSEiclguOl80lJBADW9CRD06UVfH9+SC0AZmJU3X4wlVM6B7ZDOwr/53ca1SM2u mpBg== X-Gm-Message-State: AElRT7Ec6u0zlvsInnJRAOvDUs/vA93ZfTfygCOimfrEhB9+L9TGNxUz rRQDv3Tr8n9X0bdw8Dy+9UCXIg== X-Received: by 10.28.45.9 with SMTP id t9mr5775616wmt.20.1521145883857; Thu, 15 Mar 2018 13:31:23 -0700 (PDT) Received: from e104803-lin.lan (mail.andrep.de. [217.160.17.100]) by smtp.gmail.com with ESMTPSA id w125sm3217102wmw.20.2018.03.15.13.31.22 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 15 Mar 2018 13:31:23 -0700 (PDT) From: Andre Przywara To: Stefano Stabellini , Julien Grall Date: Thu, 15 Mar 2018 20:30:22 +0000 Message-Id: <20180315203050.19791-18-andre.przywara@linaro.org> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20180315203050.19791-1-andre.przywara@linaro.org> References: <20180315203050.19791-1-andre.przywara@linaro.org> Cc: Wei Liu , George Dunlap , Andrew Cooper , Ian Jackson , Tim Deegan , Jan Beulich , xen-devel@lists.xenproject.org Subject: [Xen-devel] [PATCH v2 17/45] Add list_sort() routine from Linux X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" This pulls in Linux' list_sort.c, which is a merge sort implementation for linked lists. Apart from adding a full featured license header and adjusting the #include file, nothing has been changed in this code. Signed-off-by: Andre Przywara --- Changelog v1 ... v2: - split out to just contain Linux code dump xen/common/list_sort.c | 157 ++++++++++++++++++++++++++++++++++++++++++++ xen/include/xen/list_sort.h | 11 ++++ 2 files changed, 168 insertions(+) create mode 100644 xen/common/list_sort.c create mode 100644 xen/include/xen/list_sort.h diff --git a/xen/common/list_sort.c b/xen/common/list_sort.c new file mode 100644 index 0000000000..af2b2f6519 --- /dev/null +++ b/xen/common/list_sort.c @@ -0,0 +1,157 @@ +/* + * list_sort.c: merge sort implementation for linked lists + * Copied from the Linux kernel (lib/list_sort.c) + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; If not, see . + */ + +#include +#include + +#define MAX_LIST_LENGTH_BITS 20 + +/* + * Returns a list organized in an intermediate format suited + * to chaining of merge() calls: null-terminated, no reserved or + * sentinel head node, "prev" links not maintained. + */ +static struct list_head *merge(void *priv, + int (*cmp)(void *priv, struct list_head *a, + struct list_head *b), + struct list_head *a, struct list_head *b) +{ + struct list_head head, *tail = &head; + + while (a && b) { + /* if equal, take 'a' -- important for sort stability */ + if ((*cmp)(priv, a, b) <= 0) { + tail->next = a; + a = a->next; + } else { + tail->next = b; + b = b->next; + } + tail = tail->next; + } + tail->next = a?:b; + return head.next; +} + +/* + * Combine final list merge with restoration of standard doubly-linked + * list structure. This approach duplicates code from merge(), but + * runs faster than the tidier alternatives of either a separate final + * prev-link restoration pass, or maintaining the prev links + * throughout. + */ +static void merge_and_restore_back_links(void *priv, + int (*cmp)(void *priv, struct list_head *a, + struct list_head *b), + struct list_head *head, + struct list_head *a, struct list_head *b) +{ + struct list_head *tail = head; + u8 count = 0; + + while (a && b) { + /* if equal, take 'a' -- important for sort stability */ + if ((*cmp)(priv, a, b) <= 0) { + tail->next = a; + a->prev = tail; + a = a->next; + } else { + tail->next = b; + b->prev = tail; + b = b->next; + } + tail = tail->next; + } + tail->next = a ? : b; + + do { + /* + * In worst cases this loop may run many iterations. + * Continue callbacks to the client even though no + * element comparison is needed, so the client's cmp() + * routine can invoke cond_resched() periodically. + */ + if (unlikely(!(++count))) + (*cmp)(priv, tail->next, tail->next); + + tail->next->prev = tail; + tail = tail->next; + } while (tail->next); + + tail->next = head; + head->prev = tail; +} + +/** + * list_sort - sort a list + * @priv: private data, opaque to list_sort(), passed to @cmp + * @head: the list to sort + * @cmp: the elements comparison function + * + * This function implements "merge sort", which has O(nlog(n)) + * complexity. + * + * The comparison function @cmp must return a negative value if @a + * should sort before @b, and a positive value if @a should sort after + * @b. If @a and @b are equivalent, and their original relative + * ordering is to be preserved, @cmp must return 0. + */ +void list_sort(void *priv, struct list_head *head, + int (*cmp)(void *priv, struct list_head *a, + struct list_head *b)) +{ + struct list_head *part[MAX_LIST_LENGTH_BITS+1]; /* sorted partial lists + -- last slot is a sentinel */ + int lev; /* index into part[] */ + int max_lev = 0; + struct list_head *list; + + if (list_empty(head)) + return; + + memset(part, 0, sizeof(part)); + + head->prev->next = NULL; + list = head->next; + + while (list) { + struct list_head *cur = list; + list = list->next; + cur->next = NULL; + + for (lev = 0; part[lev]; lev++) { + cur = merge(priv, cmp, part[lev], cur); + part[lev] = NULL; + } + if (lev > max_lev) { + if (unlikely(lev >= ARRAY_SIZE(part)-1)) { + dprintk(XENLOG_DEBUG, + "list too long for efficiency\n"); + lev--; + } + max_lev = lev; + } + part[lev] = cur; + } + + for (lev = 0; lev < max_lev; lev++) + if (part[lev]) + list = merge(priv, cmp, part[lev], list); + + merge_and_restore_back_links(priv, cmp, head, part[max_lev], list); +} +EXPORT_SYMBOL(list_sort); diff --git a/xen/include/xen/list_sort.h b/xen/include/xen/list_sort.h new file mode 100644 index 0000000000..13ce0a55ec --- /dev/null +++ b/xen/include/xen/list_sort.h @@ -0,0 +1,11 @@ +#ifndef _LINUX_LIST_SORT_H +#define _LINUX_LIST_SORT_H + +#include + +struct list_head; + +void list_sort(void *priv, struct list_head *head, + int (*cmp)(void *priv, struct list_head *a, + struct list_head *b)); +#endif