The vector has the same functionality as the x2nrealloc
for insert. Use vector instead which slightly simplifies
the final code.

Signed-off-by: Ales Musil <amu...@redhat.com>
Acked-by: Mark Michelson <mmich...@redhat.com>
---
v4: Rebase on top of current main.
    Add ack from Mark.
v3: Rebase on top of current main.
v2: Rebase on top of current main.
---
 utilities/ovn-nbctl.c | 105 ++++++++++++++----------------------------
 utilities/ovn-sbctl.c |  52 +++++++++------------
 utilities/ovn-trace.c |  23 ++++-----
 3 files changed, 66 insertions(+), 114 deletions(-)

diff --git a/utilities/ovn-nbctl.c b/utilities/ovn-nbctl.c
index aa970a4fb..6c484561a 100644
--- a/utilities/ovn-nbctl.c
+++ b/utilities/ovn-nbctl.c
@@ -49,6 +49,7 @@
 #include "util.h"
 #include "openvswitch/vlog.h"
 #include "bitmap.h"
+#include "vec.h"
 
 VLOG_DEFINE_THIS_MODULE(nbctl);
 
@@ -2959,26 +2960,16 @@ nbctl_pre_meter_list(struct ctl_context *ctx)
 static void
 nbctl_meter_list(struct ctl_context *ctx)
 {
-    const struct nbrec_meter **meters = NULL;
+    struct vector meters =
+        VECTOR_EMPTY_INITIALIZER(const struct nbrec_meter *);
     const struct nbrec_meter *meter;
-    size_t n_capacity = 0;
-    size_t n_meters = 0;
 
     NBREC_METER_FOR_EACH (meter, ctx->idl) {
-        if (n_meters == n_capacity) {
-            meters = x2nrealloc(meters, &n_capacity, sizeof *meters);
-        }
-
-        meters[n_meters] = meter;
-        n_meters++;
-    }
-
-    if (n_meters) {
-        qsort(meters, n_meters, sizeof *meters, meter_cmp);
+        vector_push(&meters, &meter);
     }
+    vector_qsort(&meters, meter_cmp);
 
-    for (size_t i = 0; i < n_meters; i++) {
-        meter = meters[i];
+    VECTOR_FOR_EACH (&meters, meter) {
         ds_put_format(&ctx->output, "%s:", meter->name);
         if (meter->fair) {
             ds_put_format(&ctx->output, " (%s)",
@@ -3001,7 +2992,7 @@ nbctl_meter_list(struct ctl_context *ctx)
         ds_put_cstr(&ctx->output, "\n");
     }
 
-    free(meters);
+    vector_destroy(&meters);
 }
 
 static void
@@ -4149,8 +4140,8 @@ nbctl_lr_policy_add(struct ctl_context *ctx)
         return;
     }
     const char *action = ctx->argv[4];
-    size_t n_nexthops = 0;
-    char **nexthops = NULL;
+    struct vector nexthops = VECTOR_EMPTY_INITIALIZER(char *);
+    const struct nbrec_bfd **bfd_sessions = NULL;
     char *chain_s = shash_find_data(&ctx->options, "--chain");
 
     bool reroute = false;
@@ -4200,9 +4191,6 @@ nbctl_lr_policy_add(struct ctl_context *ctx)
         char *nexthops_arg = xstrdup(ctx->argv[5]);
         char *save_ptr, *next_hop, *token;
 
-        n_nexthops = 0;
-        size_t n_allocs = 0;
-
         bool nexthops_is_ipv4 = true;
         for (token = strtok_r(nexthops_arg, ",", &save_ptr);
             token != NULL; token = strtok_r(NULL, ",", &save_ptr)) {
@@ -4211,18 +4199,11 @@ nbctl_lr_policy_add(struct ctl_context *ctx)
             if (!next_hop) {
                 ctl_error(ctx, "bad next hop argument: %s", ctx->argv[5]);
                 free(nexthops_arg);
-                for (i = 0; i < n_nexthops; i++) {
-                    free(nexthops[i]);
-                }
-                free(nexthops);
-                return;
-            }
-            if (n_nexthops == n_allocs) {
-                nexthops = x2nrealloc(nexthops, &n_allocs, sizeof *nexthops);
+                goto free_nexthops;
             }
 
             bool is_ipv4 = strchr(next_hop, '.') ? true : false;
-            if (n_nexthops == 0) {
+            if (vector_is_empty(&nexthops)) {
                 nexthops_is_ipv4 = is_ipv4;
             }
 
@@ -4231,14 +4212,10 @@ nbctl_lr_policy_add(struct ctl_context *ctx)
                           "addr family : %s", ctx->argv[5]);
                 free(nexthops_arg);
                 free(next_hop);
-                for (i = 0; i < n_nexthops; i++) {
-                    free(nexthops[i]);
-                }
-                free(nexthops);
-                return;
+                goto free_nexthops;
             }
-            nexthops[n_nexthops] = next_hop;
-            n_nexthops++;
+
+            vector_push(&nexthops, &next_hop);
         }
 
         free(nexthops_arg);
@@ -4260,7 +4237,7 @@ nbctl_lr_policy_add(struct ctl_context *ctx)
 
     if (reroute) {
         nbrec_logical_router_policy_set_nexthops(
-            policy, (const char **)nexthops, n_nexthops);
+            policy, vector_get_array(&nexthops), vector_len(&nexthops));
     }
 
     /* Parse the options. */
@@ -4275,11 +4252,7 @@ nbctl_lr_policy_add(struct ctl_context *ctx)
             ctl_error(ctx, "No value specified for the option : %s", key);
             smap_destroy(&options);
             free(key);
-            for (i = 0; i < n_nexthops; i++) {
-                free(nexthops[i]);
-            }
-            free(nexthops);
-            return;
+            goto free_nexthops;
         }
         free(key);
     }
@@ -4289,7 +4262,6 @@ nbctl_lr_policy_add(struct ctl_context *ctx)
     nbrec_logical_router_update_policies_addvalue(lr, policy);
 
     struct shash_node *bfd = shash_find(&ctx->options, "--bfd");
-    const struct nbrec_bfd **bfd_sessions = NULL;
 
     if (bfd) {
         if (!reroute) {
@@ -4297,24 +4269,24 @@ nbctl_lr_policy_add(struct ctl_context *ctx)
             goto free_nexthops;
         }
 
-        bfd_sessions = xcalloc(n_nexthops, sizeof *bfd_sessions);
+        bfd_sessions = xcalloc(vector_len(&nexthops), sizeof *bfd_sessions);
         size_t j, n_bfd_sessions = 0;
 
-        for (i = 0; i < n_nexthops; i++) {
+        const char *nexthop;
+        VECTOR_FOR_EACH (&nexthops, nexthop) {
             for (j = 0; j < lr->n_ports; j++) {
-                if (ip_in_lrp_networks(lr->ports[j], nexthops[i])) {
+                if (ip_in_lrp_networks(lr->ports[j], nexthop)) {
                     break;
                 }
             }
 
             if (j == lr->n_ports) {
-                ctl_error(ctx, "out lrp not found for %s nexthop",
-                          nexthops[i]);
+                ctl_error(ctx, "out lrp not found for %s nexthop", nexthop);
                 goto free_nexthops;
             }
 
             struct in6_addr nexthop_v6;
-            bool is_nexthop_v6 = ipv6_parse(nexthops[i], &nexthop_v6);
+            bool is_nexthop_v6 = ipv6_parse(nexthop, &nexthop_v6);
             const struct nbrec_bfd *iter, *nb_bt = NULL;
 
             NBREC_BFD_FOR_EACH (iter, ctx->idl) {
@@ -4328,7 +4300,7 @@ nbctl_lr_policy_add(struct ctl_context *ctx)
                 /* match endpoint ip. */
                 if ((is_nexthop_v6 &&
                      !ipv6_addr_equals(&nexthop_v6, &dst_ipv6)) ||
-                    strcmp(iter->dst_ip, nexthops[i])) {
+                    strcmp(iter->dst_ip, nexthop)) {
                     continue;
                 }
 
@@ -4344,7 +4316,7 @@ nbctl_lr_policy_add(struct ctl_context *ctx)
             /* Create the BFD session if it does not already exist. */
             if (!nb_bt) {
                 nb_bt = nbrec_bfd_insert(ctx->txn);
-                nbrec_bfd_set_dst_ip(nb_bt, nexthops[i]);
+                nbrec_bfd_set_dst_ip(nb_bt, nexthop);
                 nbrec_bfd_set_logical_port(nb_bt, lr->ports[j]->name);
             }
 
@@ -4363,10 +4335,11 @@ nbctl_lr_policy_add(struct ctl_context *ctx)
 
 free_nexthops:
     free(bfd_sessions);
-    for (i = 0; i < n_nexthops; i++) {
-        free(nexthops[i]);
+    char *nexthop;
+    VECTOR_FOR_EACH (&nexthops, nexthop) {
+        free(nexthop);
     }
-    free(nexthops);
+    vector_destroy(&nexthops);
 }
 
 static void
@@ -8082,26 +8055,16 @@ static void
 nbctl_mirror_list(struct ctl_context *ctx)
 {
 
-    const struct nbrec_mirror **mirrors = NULL;
+    struct vector mirrors =
+        VECTOR_EMPTY_INITIALIZER(const struct nbrec_mirror *);
     const struct nbrec_mirror *mirror;
-    size_t n_capacity = 0;
-    size_t n_mirrors = 0;
 
     NBREC_MIRROR_FOR_EACH (mirror, ctx->idl) {
-        if (n_mirrors == n_capacity) {
-            mirrors = x2nrealloc(mirrors, &n_capacity, sizeof *mirrors);
-        }
-
-        mirrors[n_mirrors] = mirror;
-        n_mirrors++;
-    }
-
-    if (n_mirrors) {
-        qsort(mirrors, n_mirrors, sizeof *mirrors, mirror_cmp);
+        vector_push(&mirrors, &mirror);
     }
+    vector_qsort(&mirrors, mirror_cmp);
 
-    for (size_t i = 0; i < n_mirrors; i++) {
-        mirror = mirrors[i];
+    VECTOR_FOR_EACH (&mirrors, mirror) {
         bool is_lport = !strcmp(mirror->type, "lport");
         bool is_local = !strcmp(mirror->type, "local");
 
@@ -8126,7 +8089,7 @@ nbctl_mirror_list(struct ctl_context *ctx)
         ds_put_cstr(&ctx->output, "\n");
     }
 
-    free(mirrors);
+    vector_destroy(&mirrors);
 }
 
 static const struct ctl_table_class tables[NBREC_N_TABLES] = {
diff --git a/utilities/ovn-sbctl.c b/utilities/ovn-sbctl.c
index b00b8cd04..60cc39149 100644
--- a/utilities/ovn-sbctl.c
+++ b/utilities/ovn-sbctl.c
@@ -61,6 +61,7 @@
 #include "unixctl.h"
 #include "util.h"
 #include "svec.h"
+#include "vec.h"
 
 VLOG_DEFINE_THIS_MODULE(sbctl);
 
@@ -991,17 +992,15 @@ cmd_lflow_list_load_balancers(struct ctl_context *ctx, 
struct vconn *vconn,
 }
 
 static void
-sbctl_lflow_add(struct sbctl_lflow **lflows,
-                size_t *n_flows, size_t *n_capacity,
+sbctl_lflow_add(struct vector *lflows,
                 const struct sbrec_logical_flow *lflow,
                 const struct sbrec_datapath_binding *dp)
 {
-    if (*n_flows == *n_capacity) {
-        *lflows = x2nrealloc(*lflows, n_capacity, sizeof **lflows);
-    }
-    (*lflows)[*n_flows].lflow = lflow;
-    (*lflows)[*n_flows].dp = dp;
-    (*n_flows)++;
+    struct sbctl_lflow sbctl_lflow = (struct sbctl_lflow) {
+        .lflow = lflow,
+        .dp = dp,
+    };
+    vector_push(lflows, &sbctl_lflow);
 }
 
 static void
@@ -1032,14 +1031,13 @@ print_lflows_count(int64_t table_id, const char *name,
 }
 
 static void
-print_lflow_counters(size_t n_flows, struct sbctl_lflow *lflows, struct ds *s)
+print_lflow_counters(struct vector *lflows, struct ds *s)
 {
     const struct sbctl_lflow *curr, *prev = NULL;
     long table_lflows = 0;
     long dp_lflows = 0;
-    for (size_t i = 0; i < n_flows; i++) {
+    VECTOR_FOR_EACH_PTR (lflows, curr) {
        bool new_datapath = false;
-       curr = &lflows[i];
        if (!prev
             || prev->dp != curr->dp
             || strcmp(prev->lflow->pipeline, curr->lflow->pipeline)) {
@@ -1070,7 +1068,7 @@ print_lflow_counters(size_t n_flows, struct sbctl_lflow 
*lflows, struct ds *s)
         }
         prev = curr;
     }
-    if (n_flows > 0) {
+    if (!vector_is_empty(lflows)) {
         print_lflows_count(prev->lflow->table_id,
                            smap_get_def(&prev->lflow->external_ids,
                                         "stage-name", ""),
@@ -1079,7 +1077,8 @@ print_lflow_counters(size_t n_flows, struct sbctl_lflow 
*lflows, struct ds *s)
                            prev->lflow->pipeline, dp_lflows, s);
 
     }
-    ds_put_format(s, "Total number of logical flows = %"PRIuSIZE"\n", n_flows);
+    ds_put_format(s, "Total number of logical flows = %"PRIuSIZE"\n",
+                  vector_len(lflows));
 }
 
 static void
@@ -1087,6 +1086,8 @@ cmd_lflow_list(struct ctl_context *ctx)
 {
     const char *cmd = ctx->argv[0];
     const struct sbrec_datapath_binding *datapath = NULL;
+    struct vector lflows = VECTOR_EMPTY_INITIALIZER(struct sbctl_lflow);
+
     if (ctx->argc > 1) {
         const struct ovsdb_idl_row *row;
         char *error = ctl_get_row(ctx, &sbrec_table_datapath_binding,
@@ -1103,7 +1104,7 @@ cmd_lflow_list(struct ctl_context *ctx)
             ctx->argv++;
         } else if (!strcmp(cmd, "count-flows")) {
             /* datapath is defined, but isn't found */
-            print_lflow_counters(0, NULL, &ctx->output);
+            print_lflow_counters(&lflows, &ctx->output);
             return;
        }
 
@@ -1120,9 +1121,6 @@ cmd_lflow_list(struct ctl_context *ctx)
     struct vconn *vconn = sbctl_open_vconn(&ctx->options);
     bool stats = shash_find(&ctx->options, "--stats") != NULL;
 
-    struct sbctl_lflow *lflows = NULL;
-    size_t n_flows = 0;
-    size_t n_capacity = 0;
     const struct sbrec_logical_flow *lflow;
     const struct sbrec_logical_dp_group *dp_group;
     SBREC_LOGICAL_FLOW_FOR_EACH (lflow, ctx->idl) {
@@ -1133,35 +1131,29 @@ cmd_lflow_list(struct ctl_context *ctx)
             continue;
         }
         if (datapath) {
-            sbctl_lflow_add(&lflows, &n_flows, &n_capacity, lflow, datapath);
+            sbctl_lflow_add(&lflows, lflow, datapath);
             continue;
         }
         if (lflow->logical_datapath) {
-            sbctl_lflow_add(&lflows, &n_flows, &n_capacity,
-                            lflow, lflow->logical_datapath);
+            sbctl_lflow_add(&lflows, lflow, lflow->logical_datapath);
         }
         dp_group = lflow->logical_dp_group;
         for (size_t i = 0; dp_group && i < dp_group->n_datapaths; i++) {
-            sbctl_lflow_add(&lflows, &n_flows, &n_capacity,
-                            lflow, dp_group->datapaths[i]);
+            sbctl_lflow_add(&lflows, lflow, dp_group->datapaths[i]);
         }
     }
 
-    if (n_flows) {
-        qsort(lflows, n_flows, sizeof *lflows, sbctl_lflow_cmp);
-    }
+    vector_qsort(&lflows, sbctl_lflow_cmp);
 
     if (!strcmp(cmd, "count-flows")) {
-        print_lflow_counters(n_flows, lflows, &ctx->output);
+        print_lflow_counters(&lflows, &ctx->output);
         goto cleanup;
     }
 
     bool print_uuid = shash_find(&ctx->options, "--uuid") != NULL;
 
     const struct sbctl_lflow *curr, *prev = NULL;
-    for (size_t i = 0; i < n_flows; i++) {
-        curr = &lflows[i];
-
+    VECTOR_FOR_EACH_PTR (&lflows, curr) {
         /* Figure out whether to print this particular flow.  By default, we
          * print all flows, but if any UUIDs were listed on the command line
          * then we only print the matching ones. */
@@ -1220,7 +1212,7 @@ cmd_lflow_list(struct ctl_context *ctx)
 
 cleanup:
     vconn_close(vconn);
-    free(lflows);
+    vector_destroy(&lflows);
 }
 
 static void
diff --git a/utilities/ovn-trace.c b/utilities/ovn-trace.c
index d135a6cae..a435b0ff1 100644
--- a/utilities/ovn-trace.c
+++ b/utilities/ovn-trace.c
@@ -47,6 +47,7 @@
 #include "unixctl.h"
 #include "util.h"
 #include "random.h"
+#include "vec.h"
 
 VLOG_DEFINE_THIS_MODULE(ovntrace);
 
@@ -429,8 +430,7 @@ struct ovntrace_datapath {
 
     struct ovs_list mcgroups;   /* Contains "struct ovntrace_mcgroup"s. */
 
-    struct ovntrace_flow **flows;
-    size_t n_flows, allocated_flows;
+    struct vector flows; /* Vector of struct ovntrace_flow *. */
 
     struct hmap mac_bindings;   /* Contains "struct ovntrace_mac_binding"s. */
     struct hmap fdbs;   /* Contains "struct ovntrace_fdb"s. */
@@ -719,6 +719,7 @@ read_datapaths(void)
                              : shorten_uuid(dp->name2 ? dp->name2 : dp->name));
 
         dp->tunnel_key = sbdb->tunnel_key;
+        dp->flows = VECTOR_EMPTY_INITIALIZER(struct ovntrace_flow *);
 
         ovs_list_init(&dp->mcgroups);
         hmap_init(&dp->mac_bindings);
@@ -1070,11 +1071,7 @@ parse_lflow_for_datapath(const struct sbrec_logical_flow 
*sblf,
         flow->ovnacts_len = ovnacts.size;
         flow->ovnacts = ofpbuf_steal_data(&ovnacts);
 
-        if (dp->n_flows >= dp->allocated_flows) {
-            dp->flows = x2nrealloc(dp->flows, &dp->allocated_flows,
-                                   sizeof *dp->flows);
-        }
-        dp->flows[dp->n_flows++] = flow;
+        vector_push(&dp->flows, &flow);
 }
 
 static void
@@ -1101,9 +1098,9 @@ read_flows(void)
         }
     }
 
-    const struct ovntrace_datapath *dp;
+    struct ovntrace_datapath *dp;
     HMAP_FOR_EACH (dp, sb_uuid_node, &datapaths) {
-        qsort(dp->flows, dp->n_flows, sizeof *dp->flows, compare_flow);
+        vector_qsort(&dp->flows, compare_flow);
     }
 }
 
@@ -1291,8 +1288,8 @@ ovntrace_flow_lookup(const struct ovntrace_datapath *dp,
                      const struct flow *uflow,
                      uint8_t table_id, enum ovnact_pipeline pipeline)
 {
-    for (size_t i = 0; i < dp->n_flows; i++) {
-        const struct ovntrace_flow *flow = dp->flows[i];
+    const struct ovntrace_flow *flow;
+    VECTOR_FOR_EACH (&dp->flows, flow) {
         if (flow->pipeline == pipeline &&
             flow->table_id == table_id &&
             expr_evaluate(flow->match, uflow, ovntrace_lookup_port, dp)) {
@@ -1306,8 +1303,8 @@ static char *
 ovntrace_stage_name(const struct ovntrace_datapath *dp,
                     uint8_t table_id, enum ovnact_pipeline pipeline)
 {
-    for (size_t i = 0; i < dp->n_flows; i++) {
-        const struct ovntrace_flow *flow = dp->flows[i];
+    const struct ovntrace_flow *flow;
+    VECTOR_FOR_EACH (&dp->flows, flow) {
         if (flow->pipeline == pipeline && flow->table_id == table_id) {
             return xstrdup(flow->stage_name);;
         }
-- 
2.49.0

_______________________________________________
dev mailing list
d...@openvswitch.org
https://mail.openvswitch.org/mailman/listinfo/ovs-dev

Reply via email to