Blame SOURCES/0027-segtree-Merge-get_set_interval_find-and-get_set_inte.patch

8fd843
From 40cdcccf0fc6f4d0d4c2248d4bd9bf3193a922e9 Mon Sep 17 00:00:00 2001
8fd843
From: Phil Sutter <psutter@redhat.com>
8fd843
Date: Tue, 30 Jun 2020 16:20:23 +0200
8fd843
Subject: [PATCH] segtree: Merge get_set_interval_find() and
8fd843
 get_set_interval_end()
8fd843
8fd843
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1832235
8fd843
Upstream Status: nftables commit f21e73d6700b8
8fd843
8fd843
commit f21e73d6700b873eb1a295f43bbad9caaca577e2
8fd843
Author: Phil Sutter <phil@nwl.cc>
8fd843
Date:   Thu Apr 30 13:57:35 2020 +0200
8fd843
8fd843
    segtree: Merge get_set_interval_find() and get_set_interval_end()
8fd843
8fd843
    Both functions were very similar already. Under the assumption that they
8fd843
    will always either see a range (or start of) that matches exactly or not
8fd843
    at all, reduce complexity and make get_set_interval_find() accept NULL
8fd843
    (left or) right values. This way it becomes a full replacement for
8fd843
    get_set_interval_end().
8fd843
8fd843
    Signed-off-by: Phil Sutter <phil@nwl.cc>
8fd843
---
bfbb76
 src/segtree.c | 63 +++++++++++++--------------------------------------
8fd843
 1 file changed, 16 insertions(+), 47 deletions(-)
8fd843
8fd843
diff --git a/src/segtree.c b/src/segtree.c
8fd843
index dc4db6b..6e1f696 100644
8fd843
--- a/src/segtree.c
8fd843
+++ b/src/segtree.c
8fd843
@@ -681,63 +681,31 @@ static struct expr *get_set_interval_find(const struct table *table,
8fd843
 {
8fd843
 	struct expr *range = NULL;
8fd843
 	struct set *set;
8fd843
-	mpz_t low, high;
8fd843
 	struct expr *i;
8fd843
+	mpz_t val;
8fd843
 
8fd843
 	set = set_lookup(table, set_name);
8fd843
-	mpz_init2(low, set->key->len);
8fd843
-	mpz_init2(high, set->key->len);
8fd843
+	mpz_init2(val, set->key->len);
8fd843
 
8fd843
 	list_for_each_entry(i, &set->init->expressions, list) {
8fd843
 		switch (i->key->etype) {
8fd843
 		case EXPR_RANGE:
8fd843
-			range_expr_value_low(low, i);
8fd843
-			range_expr_value_high(high, i);
8fd843
-			if (mpz_cmp(left->key->value, low) >= 0 &&
8fd843
-			    mpz_cmp(right->key->value, high) <= 0) {
8fd843
-				range = expr_clone(i->key);
8fd843
-				goto out;
8fd843
-			}
8fd843
-			break;
8fd843
-		default:
8fd843
-			break;
8fd843
-		}
8fd843
-	}
8fd843
-out:
8fd843
-	mpz_clear(low);
8fd843
-	mpz_clear(high);
8fd843
-
8fd843
-	return range;
8fd843
-}
8fd843
-
8fd843
-static struct expr *get_set_interval_end(const struct table *table,
8fd843
-					 const char *set_name,
8fd843
-					 struct expr *left)
8fd843
-{
8fd843
-	struct expr *i, *range = NULL;
8fd843
-	struct set *set;
8fd843
-	mpz_t low, high;
8fd843
+			range_expr_value_low(val, i);
8fd843
+			if (left && mpz_cmp(left->key->value, val))
8fd843
+				break;
8fd843
 
8fd843
-	set = set_lookup(table, set_name);
8fd843
-	mpz_init2(low, set->key->len);
8fd843
-	mpz_init2(high, set->key->len);
8fd843
+			range_expr_value_high(val, i);
8fd843
+			if (right && mpz_cmp(right->key->value, val))
8fd843
+				break;
8fd843
 
8fd843
-	list_for_each_entry(i, &set->init->expressions, list) {
8fd843
-		switch (i->key->etype) {
8fd843
-		case EXPR_RANGE:
8fd843
-			range_expr_value_low(low, i);
8fd843
-			if (mpz_cmp(low, left->key->value) == 0) {
8fd843
-				range = expr_clone(i->key);
8fd843
-				goto out;
8fd843
-			}
8fd843
-			break;
8fd843
+			range = expr_clone(i->key);
8fd843
+			goto out;
8fd843
 		default:
8fd843
 			break;
8fd843
 		}
8fd843
 	}
8fd843
 out:
8fd843
-	mpz_clear(low);
8fd843
-	mpz_clear(high);
8fd843
+	mpz_clear(val);
8fd843
 
8fd843
 	return range;
8fd843
 }
8fd843
@@ -767,9 +735,9 @@ int get_set_decompose(struct table *table, struct set *set)
8fd843
 			left = NULL;
8fd843
 		} else {
8fd843
 			if (left) {
8fd843
-				range = get_set_interval_end(table,
8fd843
-							     set->handle.set.name,
8fd843
-							     left);
8fd843
+				range = get_set_interval_find(table,
8fd843
+							      set->handle.set.name,
8fd843
+							      left, NULL);
8fd843
 				if (range)
8fd843
 					compound_expr_add(new_init, range);
8fd843
 				else
8fd843
@@ -780,7 +748,8 @@ int get_set_decompose(struct table *table, struct set *set)
8fd843
 		}
8fd843
 	}
8fd843
 	if (left) {
8fd843
-		range = get_set_interval_end(table, set->handle.set.name, left);
8fd843
+		range = get_set_interval_find(table, set->handle.set.name,
8fd843
+					      left, NULL);
8fd843
 		if (range)
8fd843
 			compound_expr_add(new_init, range);
8fd843
 		else
8fd843
-- 
bfbb76
2.31.1
8fd843