smap: New macro SMAP_CONST1 for initializing immutable 1-member smaps.
authorBen Pfaff <blp@nicira.com>
Wed, 9 Sep 2015 01:39:25 +0000 (18:39 -0700)
committerBen Pfaff <blp@nicira.com>
Wed, 9 Sep 2015 01:41:09 +0000 (18:41 -0700)
Reviewing the ovn-controller code I started to notice a common pattern:

    struct smap ext_ids = SMAP_INITIALIZER(&ext_ids);
    smap_add(&ext_ids, "ovn-patch-port", network);
    ovsrec_port_set_external_ids(port, &ext_ids);
    smap_destroy(&ext_ids);

This seemed like a bit too much code for something as simple as
initializing an smap with a single key-value pair.  This commit allows the
code to be reduced to just:

    const struct smap ids = SMAP_CONST1(&ids, "ovn-patch-port", network);
    ovsrec_port_set_external_ids(port, &ids);

This new form also eliminates multiple memory allocation and free
operations, but I doubt that has any real effect on performance;
the primary goal here is code readability.

Signed-off-by: Ben Pfaff <blp@nicira.com>
Acked-by: Russell Bryant <rbryant@redhat.com>
lib/hmap.h
lib/smap.h
ovn/controller/encaps.c
ovn/controller/ovn-controller.c
ovn/northd/ovn-northd.c
utilities/ovs-vsctl.c

index cab567e..d2327cb 100644 (file)
@@ -68,6 +68,12 @@ struct hmap {
 #define HMAP_INITIALIZER(HMAP) \
     { (struct hmap_node **const) &(HMAP)->one, NULL, 0, 0 }
 
+/* Initializer for an immutable struct hmap 'HMAP' that contains a single
+ * 'NODE'. */
+#define HMAP_CONST1(HMAP, NODE) {                                   \
+        CONST_CAST(struct hmap_node **, &(HMAP)->one), NODE, 0, 1 }
+#define HMAP_NODE_INIT(HASH) { HASH, NULL }
+
 /* Initialization. */
 void hmap_init(struct hmap *);
 void hmap_destroy(struct hmap *);
index 846e384..3bfbb71 100644 (file)
@@ -39,6 +39,25 @@ struct smap_node {
 #define SMAP_FOR_EACH_SAFE(SMAP_NODE, NEXT, SMAP) \
     HMAP_FOR_EACH_SAFE (SMAP_NODE, NEXT, node, &(SMAP)->map)
 
+/* Initializer for an immutable struct smap 'SMAP' that contains a single
+ * 'KEY'-'VALUE' pair, e.g.
+ *
+ *     const struct smap smap = SMAP1_CONST1(&smap, "key", "value");
+ *
+ * An smap initialized this way must not be modified or destroyed.
+ *
+ * The 'KEY' argument is evaluated multiple times.
+ */
+#define SMAP_CONST1(SMAP, KEY, VALUE) {                                 \
+        HMAP_CONST1(&(SMAP)->map,                                       \
+                   (&(struct smap_node) SMAP_NODE_INIT(KEY, VALUE).node)) \
+            }
+#define SMAP_NODE_INIT(KEY, VALUE) {                \
+        HMAP_NODE_INIT(hash_string(KEY, 0)),        \
+                       CONST_CAST(char *, KEY),     \
+                       CONST_CAST(char *, VALUE) }
+
+
 void smap_init(struct smap *);
 void smap_destroy(struct smap *);
 
index 070b741..c914e2a 100644 (file)
@@ -149,7 +149,6 @@ tunnel_add(struct tunnel_ctx *tc, const char *new_chassis_id,
     }
 
     /* No such port, so add one. */
-    struct smap external_ids = SMAP_INITIALIZER(&external_ids);
     struct smap options = SMAP_INITIALIZER(&options);
     struct ovsrec_port *port, **ports;
     struct ovsrec_interface *iface;
@@ -174,9 +173,8 @@ tunnel_add(struct tunnel_ctx *tc, const char *new_chassis_id,
     port = ovsrec_port_insert(tc->ovs_txn);
     ovsrec_port_set_name(port, port_name);
     ovsrec_port_set_interfaces(port, &iface, 1);
-    smap_add(&external_ids, "ovn-chassis-id", new_chassis_id);
-    ovsrec_port_set_external_ids(port, &external_ids);
-    smap_destroy(&external_ids);
+    const struct smap id = SMAP_CONST1(&id, "ovn-chassis-id", new_chassis_id);
+    ovsrec_port_set_external_ids(port, &id);
 
     ports = xmalloc(sizeof *tc->br_int->ports * (tc->br_int->n_ports + 1));
     for (i = 0; i < tc->br_int->n_ports; i++) {
index d705a16..8c858bb 100644 (file)
@@ -95,10 +95,8 @@ create_br_int(struct controller_ctx *ctx,
     bridge = ovsrec_bridge_insert(ctx->ovs_idl_txn);
     ovsrec_bridge_set_name(bridge, bridge_name);
     ovsrec_bridge_set_fail_mode(bridge, "secure");
-    struct smap other_config = SMAP_INITIALIZER(&other_config);
-    smap_add(&other_config, "disable-in-band", "true");
-    ovsrec_bridge_set_other_config(bridge, &other_config);
-    smap_destroy(&other_config);
+    const struct smap oc = SMAP_CONST1(&oc, "disable-in-band", "true");
+    ovsrec_bridge_set_other_config(bridge, &oc);
     ovsrec_bridge_set_ports(bridge, &port, 1);
 
     struct ovsrec_bridge **bridges;
@@ -201,19 +199,15 @@ create_patch_port(struct controller_ctx *ctx,
     iface = ovsrec_interface_insert(ctx->ovs_idl_txn);
     ovsrec_interface_set_name(iface, port_name);
     ovsrec_interface_set_type(iface, "patch");
-    struct smap options = SMAP_INITIALIZER(&options);
-    smap_add(&options, "peer", peer_port_name);
+    const struct smap options = SMAP_CONST1(&options, "peer", peer_port_name);
     ovsrec_interface_set_options(iface, &options);
-    smap_destroy(&options);
 
     struct ovsrec_port *port;
     port = ovsrec_port_insert(ctx->ovs_idl_txn);
     ovsrec_port_set_name(port, port_name);
     ovsrec_port_set_interfaces(port, &iface, 1);
-    struct smap ext_ids = SMAP_INITIALIZER(&ext_ids);
-    smap_add(&ext_ids, "ovn-patch-port", network);
-    ovsrec_port_set_external_ids(port, &ext_ids);
-    smap_destroy(&ext_ids);
+    const struct smap ids = SMAP_CONST1(&ids, "ovn-patch-port", network);
+    ovsrec_port_set_external_ids(port, &ids);
 
     struct ovsrec_port **ports;
     ports = xmalloc(sizeof *ports * (b1->n_ports + 1));
index befe122..f7004f7 100644 (file)
@@ -309,12 +309,10 @@ build_datapaths(struct northd_context *ctx, struct hmap *datapaths)
 
             od->sb = sbrec_datapath_binding_insert(ctx->ovnsb_txn);
 
-            struct smap external_ids = SMAP_INITIALIZER(&external_ids);
             char uuid_s[UUID_LEN + 1];
             sprintf(uuid_s, UUID_FMT, UUID_ARGS(&od->nb->header_.uuid));
-            smap_add(&external_ids, "logical-switch", uuid_s);
-            sbrec_datapath_binding_set_external_ids(od->sb, &external_ids);
-            smap_destroy(&external_ids);
+            const struct smap id = SMAP_CONST1(&id, "logical-switch", uuid_s);
+            sbrec_datapath_binding_set_external_ids(od->sb, &id);
 
             sbrec_datapath_binding_set_tunnel_key(od->sb, tunnel_key);
         }
@@ -888,13 +886,12 @@ build_lflows(struct northd_context *ctx, struct hmap *datapaths,
         sbrec_logical_flow_set_match(sbflow, lflow->match);
         sbrec_logical_flow_set_actions(sbflow, lflow->actions);
 
-        struct smap external_ids = SMAP_INITIALIZER(&external_ids);
-        smap_add(&external_ids, "stage-name",
-                 lflow->pipeline == P_IN ?
-                  ingress_stage_to_str(lflow->table_id) :
-                  egress_stage_to_str(lflow->table_id));
-        sbrec_logical_flow_set_external_ids(sbflow, &external_ids);
-        smap_destroy(&external_ids);
+        const struct smap ids = SMAP_CONST1(
+            &ids, "stage-name",
+            (lflow->pipeline == P_IN
+             ? ingress_stage_to_str(lflow->table_id)
+             : egress_stage_to_str(lflow->table_id)));
+        sbrec_logical_flow_set_external_ids(sbflow, &ids);
 
         ovn_lflow_destroy(&lflows, lflow);
     }
index e177060..7729500 100644 (file)
@@ -1091,10 +1091,8 @@ cmd_emer_reset(struct ctl_context *ctx)
         /* We only want to save the "hwaddr" key from other_config. */
         hwaddr = smap_get(&br->other_config, "hwaddr");
         if (hwaddr) {
-            struct smap smap = SMAP_INITIALIZER(&smap);
-            smap_add(&smap, "hwaddr", hwaddr);
+            const struct smap smap = SMAP_CONST1(&smap, "hwaddr", hwaddr);
             ovsrec_bridge_set_other_config(br, &smap);
-            smap_destroy(&smap);
         } else {
             ovsrec_bridge_set_other_config(br, NULL);
         }