| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2017 Elise Lennion <elise.lennion@gmail.com> | ||
| 3 | * | ||
| 4 | * This program is free software; you can redistribute it and/or modify | ||
| 5 | * it under the terms of the GNU General Public License version 2 (or any | ||
| 6 | * later) as published by the Free Software Foundation. | ||
| 7 | */ | ||
| 8 | |||
| 9 | #include <nft.h> | ||
| 10 | |||
| 11 | #include <expression.h> | ||
| 12 | #include <gmputil.h> | ||
| 13 | #include <list.h> | ||
| 14 | |||
| 15 | 130382 | static void concat_expr_msort_value(const struct expr *expr, mpz_t value) | |
| 16 | { | ||
| 17 | 130382 | unsigned int len = 0, ilen; | |
| 18 | const struct expr *i; | ||
| 19 | char data[512]; | ||
| 20 | |||
| 21 |
4/6✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 130382 times.
✗ Branch 2 (9→10) not taken.
✓ Branch 3 (9→11) taken 391228 times.
✓ Branch 4 (11→5) taken 260846 times.
✓ Branch 5 (11→12) taken 130382 times.
|
391228 | list_for_each_entry(i, &expr_concat(expr)->expressions, list) { |
| 22 | 260846 | ilen = div_round_up(i->len, BITS_PER_BYTE); | |
| 23 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 260846 times.
|
260846 | mpz_export_data(data + len, i->value, i->byteorder, ilen); |
| 24 | 260846 | len += ilen; | |
| 25 | } | ||
| 26 | |||
| 27 |
1/2✗ Branch 0 (12→13) not taken.
✓ Branch 1 (12→14) taken 130382 times.
|
130382 | mpz_import_data(value, data, BYTEORDER_HOST_ENDIAN, len); |
| 28 | 130382 | } | |
| 29 | |||
| 30 | 67764082 | static mpz_srcptr expr_msort_value(const struct expr *expr, mpz_t value) | |
| 31 | { | ||
| 32 |
6/7✓ Branch 0 (2→3) taken 33494456 times.
✓ Branch 1 (2→4) taken 775170 times.
✓ Branch 2 (2→5) taken 5526627 times.
✓ Branch 3 (2→6) taken 27837362 times.
✓ Branch 4 (2→7) taken 130382 times.
✓ Branch 5 (2→8) taken 85 times.
✗ Branch 6 (2→10) not taken.
|
67764082 | switch (expr->etype) { |
| 33 | 33494456 | case EXPR_SET_ELEM: | |
| 34 | 33494456 | return expr_msort_value(expr->key, value); | |
| 35 | 775170 | case EXPR_BINOP: | |
| 36 | case EXPR_MAPPING: | ||
| 37 | case EXPR_RANGE: | ||
| 38 | 775170 | return expr_msort_value(expr->left, value); | |
| 39 | 5526627 | case EXPR_VALUE: | |
| 40 | 5526627 | return expr->value; | |
| 41 | 27837362 | case EXPR_RANGE_VALUE: | |
| 42 | 27837362 | return expr->range.low; | |
| 43 | 130382 | case EXPR_CONCAT: | |
| 44 | 130382 | concat_expr_msort_value(expr, value); | |
| 45 | 130382 | break; | |
| 46 | 85 | case EXPR_SET_ELEM_CATCHALL: | |
| 47 | /* max value to ensure listing shows it in the last position */ | ||
| 48 | 85 | mpz_bitmask(value, expr->len); | |
| 49 | 85 | break; | |
| 50 | ✗ | default: | |
| 51 | ✗ | BUG("Unknown expression %s", expr_name(expr)); | |
| 52 | } | ||
| 53 | 130467 | return value; | |
| 54 | } | ||
| 55 | |||
| 56 | 16747228 | static int expr_msort_cmp(const struct expr *e1, const struct expr *e2) | |
| 57 | { | ||
| 58 | mpz_srcptr value1; | ||
| 59 | mpz_srcptr value2; | ||
| 60 | mpz_t value1_tmp; | ||
| 61 | mpz_t value2_tmp; | ||
| 62 | int ret; | ||
| 63 | |||
| 64 | 16747228 | mpz_init(value1_tmp); | |
| 65 | 16747228 | mpz_init(value2_tmp); | |
| 66 | 16747228 | value1 = expr_msort_value(e1, value1_tmp); | |
| 67 | 16747228 | value2 = expr_msort_value(e2, value2_tmp); | |
| 68 | 16747228 | ret = mpz_cmp(value1, value2); | |
| 69 | 16747228 | mpz_clear(value1_tmp); | |
| 70 | 16747228 | mpz_clear(value2_tmp); | |
| 71 | |||
| 72 | 16747228 | return ret; | |
| 73 | } | ||
| 74 | |||
| 75 | 2040843 | void list_splice_sorted(struct list_head *list, struct list_head *head) | |
| 76 | { | ||
| 77 | 2040843 | struct list_head *h = head->next; | |
| 78 | 2040843 | struct list_head *l = list->next; | |
| 79 | |||
| 80 |
2/2✓ Branch 0 (10→3) taken 17508768 times.
✓ Branch 1 (10→11) taken 2040843 times.
|
19549611 | while (l != list) { |
| 81 |
4/4✓ Branch 0 (3→4) taken 16747228 times.
✓ Branch 1 (3→6) taken 761540 times.
✓ Branch 2 (5→6) taken 15212583 times.
✓ Branch 3 (5→8) taken 1534645 times.
|
34255996 | if (h == head || |
| 82 | 16747228 | expr_msort_cmp(list_entry(l, typeof(struct expr), list), | |
| 83 | 16747228 | list_entry(h, typeof(struct expr), list)) <= 0) { | |
| 84 | 15974123 | l = l->next; | |
| 85 | 15974123 | list_add_tail(l->prev, h); | |
| 86 | 15974123 | continue; | |
| 87 | } | ||
| 88 | |||
| 89 | 1534645 | h = h->next; | |
| 90 | } | ||
| 91 | 2040843 | } | |
| 92 | |||
| 93 | 2035583 | static void list_cut_middle(struct list_head *list, struct list_head *head) | |
| 94 | { | ||
| 95 | 2035583 | struct list_head *s = head->next; | |
| 96 | 2035583 | struct list_head *e = head->prev; | |
| 97 | |||
| 98 |
2/2✓ Branch 0 (6→3) taken 14495463 times.
✓ Branch 1 (6→7) taken 2035583 times.
|
16531046 | while (e != s) { |
| 99 | 14495463 | e = e->prev; | |
| 100 | |||
| 101 |
2/2✓ Branch 0 (3→4) taken 12645244 times.
✓ Branch 1 (3→5) taken 1850219 times.
|
14495463 | if (e != s) |
| 102 | 12645244 | s = s->next; | |
| 103 | } | ||
| 104 | |||
| 105 | 2035583 | __list_cut_position(list, head, s); | |
| 106 | 2035583 | } | |
| 107 | |||
| 108 | 4086200 | void list_expr_sort(struct list_head *head) | |
| 109 | { | ||
| 110 | struct list_head *list; | ||
| 111 | 4086200 | LIST_HEAD(temp); | |
| 112 | |||
| 113 | 4086200 | list = &temp; | |
| 114 | |||
| 115 |
4/4✓ Branch 0 (3→4) taken 4083693 times.
✓ Branch 1 (3→6) taken 2507 times.
✓ Branch 2 (5→6) taken 2048110 times.
✓ Branch 3 (5→7) taken 2035583 times.
|
4086200 | if (list_empty(head) || list_is_singular(head)) |
| 116 | 2050617 | return; | |
| 117 | |||
| 118 | 2035583 | list_cut_middle(list, head); | |
| 119 | |||
| 120 | 2035583 | list_expr_sort(head); | |
| 121 | 2035583 | list_expr_sort(list); | |
| 122 | |||
| 123 | 2035583 | list_splice_sorted(list, head); | |
| 124 | } | ||
| 125 |