From patchwork Tue May 31 16:41:29 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Blake X-Patchwork-Id: 628328 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3rK08W1vGxz9t3s for ; Wed, 1 Jun 2016 02:59:39 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; secure) header.d=comcast.net header.i=@comcast.net header.b=caJqx2vL; dkim-atps=neutral Received: from localhost ([::1]:37310 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b7n0z-0001C5-4M for incoming@patchwork.ozlabs.org; Tue, 31 May 2016 12:59:37 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38737) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b7mjc-0001NT-Qt for qemu-devel@nongnu.org; Tue, 31 May 2016 12:41:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b7mja-0004Ra-Gj for qemu-devel@nongnu.org; Tue, 31 May 2016 12:41:39 -0400 Received: from resqmta-po-08v.sys.comcast.net ([2001:558:fe16:19:96:114:154:167]:46815) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b7mja-0004Qz-Ao for qemu-devel@nongnu.org; Tue, 31 May 2016 12:41:38 -0400 Received: from resomta-po-13v.sys.comcast.net ([96.114.154.237]) by comcast with SMTP id 7mj7biUWg7xvb7mjabuxEV; Tue, 31 May 2016 16:41:38 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=comcast.net; s=q20140121; t=1464712898; bh=Php3zMkBt8C5LJ8LXSp0bBvgoRAvZuL7kAV15q9V/NQ=; h=Received:Received:From:To:Subject:Date:Message-Id; b=caJqx2vLJzZrhST35GFgdPx/Gjf0AAJwbNcecOWnVZBU7mleglnjXuBgVPAGyB+u+ S4+c4ov6b/gbS1WG9aed5ng90295clJ5NqQZDu9UuCcLDIepfmxdwBHKYBuThn9uWt ReK+yjnJBqu3VjfjJLlJ/Kwhz73K2TDBmA6Jd3knAyFaDSfSaFn/Qv0Qio4LgcWxFn OLnfA8BxR9zM7qSqlJb2agf5m+S0ObQ4/6sooh+QFiiSH1yWSyLXmwjMKfsVS0hdU1 QNZUT5z9TszYdQhqULXR3iw8Fpaqty8Ab+NfXjfTdxy/nQqoP2OHEz2+mPgZ0mzfKg f5VVkUi2LpV7Q== Received: from red.redhat.com ([24.10.254.122]) by resomta-po-13v.sys.comcast.net with comcast id 14hX1t0092fD5rL014hdLR; Tue, 31 May 2016 16:41:37 +0000 From: Eric Blake To: qemu-devel@nongnu.org Date: Tue, 31 May 2016 10:41:29 -0600 Message-Id: <1464712890-14262-3-git-send-email-eblake@redhat.com> X-Mailer: git-send-email 2.5.5 In-Reply-To: <1464712890-14262-1-git-send-email-eblake@redhat.com> References: <1464712890-14262-1-git-send-email-eblake@redhat.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2001:558:fe16:19:96:114:154:167 Subject: [Qemu-devel] [PATCH v2 2/3] qapi: Simplify use of range.h X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: armbru@redhat.com, Michael Roth Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" Calling our function g_list_insert_sorted_merged is a misnomer, since we are NOT writing a glib function. Furthermore, we are making every caller pass the same comparator function of range_merge(): any caller that would try otherwise would break in weird ways since our internal call to ranges_can_merge() is hard-coded to operate only on ranges, rather than paying attention to the caller's comparator. Better is to fix things so that callers don't have to care about our internal comparator, by picking a function name and updating the parameter type away from a gratuitous use of void*, to make it obvious that we are operating specifically on a list of ranges and not a generic list. Plus, refactoring the code here will make it easier to plug a memory leak in the next patch. range_compare() is now internal only, and moves to the .c file. Signed-off-by: Eric Blake --- include/qemu/range.h | 16 +--------------- qapi/string-input-visitor.c | 17 ++++------------- qapi/string-output-visitor.c | 4 ++-- util/range.c | 20 ++++++++++++++++---- 4 files changed, 23 insertions(+), 34 deletions(-) diff --git a/include/qemu/range.h b/include/qemu/range.h index c10d56a..3970f00 100644 --- a/include/qemu/range.h +++ b/include/qemu/range.h @@ -79,20 +79,6 @@ static inline int ranges_overlap(uint64_t first1, uint64_t len1, return !(last2 < first1 || last1 < first2); } -GList *g_list_insert_sorted_merged(GList *list, gpointer data, - GCompareFunc func); - -static inline gint range_compare(gconstpointer a, gconstpointer b) -{ - Range *ra = (Range *)a, *rb = (Range *)b; - if (ra->begin == rb->begin && ra->end == rb->end) { - return 0; - } else if (range_get_last(ra->begin, ra->end) < - range_get_last(rb->begin, rb->end)) { - return -1; - } else { - return 1; - } -} +GList *range_list_insert(GList *list, Range *data); #endif diff --git a/qapi/string-input-visitor.c b/qapi/string-input-visitor.c index 30b5879..b546e5f 100644 --- a/qapi/string-input-visitor.c +++ b/qapi/string-input-visitor.c @@ -61,8 +61,7 @@ static int parse_str(StringInputVisitor *siv, const char *name, Error **errp) cur = g_malloc0(sizeof(*cur)); cur->begin = start; cur->end = start + 1; - siv->ranges = g_list_insert_sorted_merged(siv->ranges, cur, - range_compare); + siv->ranges = range_list_insert(siv->ranges, cur); cur = NULL; str = NULL; } else if (*endptr == '-') { @@ -76,10 +75,7 @@ static int parse_str(StringInputVisitor *siv, const char *name, Error **errp) cur = g_malloc0(sizeof(*cur)); cur->begin = start; cur->end = end + 1; - siv->ranges = - g_list_insert_sorted_merged(siv->ranges, - cur, - range_compare); + siv->ranges = range_list_insert(siv->ranges, cur); cur = NULL; str = NULL; } else if (*endptr == ',') { @@ -87,10 +83,7 @@ static int parse_str(StringInputVisitor *siv, const char *name, Error **errp) cur = g_malloc0(sizeof(*cur)); cur->begin = start; cur->end = end + 1; - siv->ranges = - g_list_insert_sorted_merged(siv->ranges, - cur, - range_compare); + siv->ranges = range_list_insert(siv->ranges, cur); cur = NULL; } else { goto error; @@ -103,9 +96,7 @@ static int parse_str(StringInputVisitor *siv, const char *name, Error **errp) cur = g_malloc0(sizeof(*cur)); cur->begin = start; cur->end = start + 1; - siv->ranges = g_list_insert_sorted_merged(siv->ranges, - cur, - range_compare); + siv->ranges = range_list_insert(siv->ranges, cur); cur = NULL; } else { goto error; diff --git a/qapi/string-output-visitor.c b/qapi/string-output-visitor.c index d013196..5ea395a 100644 --- a/qapi/string-output-visitor.c +++ b/qapi/string-output-visitor.c @@ -85,7 +85,7 @@ static void string_output_append(StringOutputVisitor *sov, int64_t a) Range *r = g_malloc0(sizeof(*r)); r->begin = a; r->end = a + 1; - sov->ranges = g_list_insert_sorted_merged(sov->ranges, r, range_compare); + sov->ranges = range_list_insert(sov->ranges, r); } static void string_output_append_range(StringOutputVisitor *sov, @@ -94,7 +94,7 @@ static void string_output_append_range(StringOutputVisitor *sov, Range *r = g_malloc0(sizeof(*r)); r->begin = s; r->end = e + 1; - sov->ranges = g_list_insert_sorted_merged(sov->ranges, r, range_compare); + sov->ranges = range_list_insert(sov->ranges, r); } static void format_string(StringOutputVisitor *sov, Range *r, bool next, diff --git a/util/range.c b/util/range.c index f775f2e..dd46092 100644 --- a/util/range.c +++ b/util/range.c @@ -44,14 +44,26 @@ static void range_merge(Range *range1, Range *range2) } } -GList *g_list_insert_sorted_merged(GList *list, gpointer data, - GCompareFunc func) +static gint range_compare(gconstpointer a, gconstpointer b) +{ + Range *ra = (Range *)a, *rb = (Range *)b; + if (ra->begin == rb->begin && ra->end == rb->end) { + return 0; + } else if (range_get_last(ra->begin, ra->end) < + range_get_last(rb->begin, rb->end)) { + return -1; + } else { + return 1; + } +} + +GList *range_list_insert(GList *list, Range *data) { GList *l, *next = NULL; Range *r, *nextr; if (!list) { - list = g_list_insert_sorted(list, data, func); + list = g_list_insert_sorted(list, data, range_compare); return list; } @@ -74,7 +86,7 @@ GList *g_list_insert_sorted_merged(GList *list, gpointer data, } if (!l) { - list = g_list_insert_sorted(list, data, func); + list = g_list_insert_sorted(list, data, range_compare); } return list;