netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Phil Sutter <phil@nwl.cc>
To: Pablo Neira Ayuso <pablo@netfilter.org>
Cc: netfilter-devel@vger.kernel.org, Florian Westphal <fw@strlen.de>
Subject: [nft PATCH 4/4] segtree: Refactor ei_insert()
Date: Thu, 23 Jan 2020 15:30:49 +0100	[thread overview]
Message-ID: <20200123143049.13888-5-phil@nwl.cc> (raw)
In-Reply-To: <20200123143049.13888-1-phil@nwl.cc>

With all simplifications in place, reorder the code to streamline it
further. Apart from making the second call to ei_lookup() conditional,
debug output is slightly enhanced.

Signed-off-by: Phil Sutter <phil@nwl.cc>
---
 src/segtree.c | 63 +++++++++++++++++++++++----------------------------
 1 file changed, 28 insertions(+), 35 deletions(-)

diff --git a/src/segtree.c b/src/segtree.c
index 3c0989e76093a..edec9f4ebf174 100644
--- a/src/segtree.c
+++ b/src/segtree.c
@@ -192,48 +192,41 @@ static int ei_insert(struct list_head *msgs, struct seg_tree *tree,
 {
 	struct elementary_interval *lei, *rei;
 
-	/*
-	 * Lookup the intervals containing the left and right endpoints.
-	 */
 	lei = ei_lookup(tree, new->left);
-	rei = ei_lookup(tree, new->right);
-
-	if (segtree_debug(tree->debug_mask))
-		pr_gmp_debug("insert: [%Zx %Zx]\n", new->left, new->right);
-
-	if (lei != NULL && rei != NULL && lei == rei) {
-		if (!merge)
-			goto err;
-
-		ei_destroy(new);
+	if (lei == NULL) {
+		/* no overlaps, just add the new interval */
+		if (segtree_debug(tree->debug_mask))
+			pr_gmp_debug("insert: [%Zx %Zx]\n",
+				     new->left, new->right);
+		__ei_insert(tree, new);
 		return 0;
-	} else {
-		if (lei != NULL) {
-			if (!merge)
-				goto err;
-			/*
-			 * Left endpoint is within lei, adjust it so we have:
-			 *
-			 * [lei_left, new_right]
-			 */
-			if (segtree_debug(tree->debug_mask)) {
-				pr_gmp_debug("adjust left [%Zx %Zx]\n",
-					     lei->left, lei->right);
-			}
+	}
 
-			mpz_set(lei->right, new->right);
-			ei_destroy(new);
-			return 0;
-		}
+	if (!merge) {
+		errno = EEXIST;
+		return expr_binary_error(msgs, lei->expr, new->expr,
+					 "conflicting intervals specified");
 	}
 
-	__ei_insert(tree, new);
+	/* caller sorted intervals, so rei is either equal to lei or NULL */
+	rei = ei_lookup(tree, new->right);
+	if (rei != lei) {
+		/*
+		 * Left endpoint is within lei, adjust it so we have:
+		 *
+		 * [lei_left, new_right]
+		 */
+		if (segtree_debug(tree->debug_mask))
+			pr_gmp_debug("adjust right: [%Zx %Zx]\n",
+				     lei->left, lei->right);
+		mpz_set(lei->right, new->right);
+	} else if (segtree_debug(tree->debug_mask)) {
+		pr_gmp_debug("skip new: [%Zx %Zx] for old: [%Zx %Zx]\n",
+			     new->left, new->right, lei->left, lei->right);
+	}
 
+	ei_destroy(new);
 	return 0;
-err:
-	errno = EEXIST;
-	return expr_binary_error(msgs, lei->expr, new->expr,
-				 "conflicting intervals specified");
 }
 
 /*
-- 
2.24.1


  parent reply	other threads:[~2020-01-23 14:30 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-23 14:30 [nft PATCH 0/4] Covscan-induced review of ei_insert() Phil Sutter
2020-01-23 14:30 ` [nft PATCH 1/4] segtree: Drop needless insertion in ei_insert() Phil Sutter
2020-01-28 11:22   ` Pablo Neira Ayuso
2020-01-23 14:30 ` [nft PATCH 2/4] segtree: Drop dead code " Phil Sutter
2020-01-28 11:24   ` Pablo Neira Ayuso
2020-01-23 14:30 ` [nft PATCH 3/4] segtree: Simplify overlap case " Phil Sutter
2020-01-28 11:29   ` Pablo Neira Ayuso
2020-01-23 14:30 ` Phil Sutter [this message]
2020-01-28 12:23   ` [nft PATCH 4/4] segtree: Refactor ei_insert() Pablo Neira Ayuso
2020-01-28 14:14     ` Phil Sutter
2020-01-28 15:42       ` Pablo Neira Ayuso
2020-01-28 15:55         ` Phil Sutter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200123143049.13888-5-phil@nwl.cc \
    --to=phil@nwl.cc \
    --cc=fw@strlen.de \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=pablo@netfilter.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).