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

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