[ovs-dev] [PATCH 1/2] smap: New macro SMAP_CONST2 for an immutable map of 2 key-value pairs.

Ben Pfaff blp at ovn.org
Fri Oct 7 17:02:03 UTC 2016


Future commits will add a user.

Signed-off-by: Ben Pfaff <blp at ovn.org>
---
 include/openvswitch/hmap.h | 10 +++++-----
 lib/smap.h                 | 26 +++++++++++++++++---------
 2 files changed, 22 insertions(+), 14 deletions(-)

diff --git a/include/openvswitch/hmap.h b/include/openvswitch/hmap.h
index ef272a6..8aea9c2 100644
--- a/include/openvswitch/hmap.h
+++ b/include/openvswitch/hmap.h
@@ -68,11 +68,11 @@ 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 }
+/* Initializer for an immutable struct hmap 'HMAP' that contains 'N' nodes
+ * linked together starting at 'NODE'.  The hmap only has a single chain of
+ * hmap_nodes, so 'N' should be small. */
+#define HMAP_CONST(HMAP, N, NODE) {                                 \
+        CONST_CAST(struct hmap_node **, &(HMAP)->one), NODE, 0, N }
 
 /* Initialization. */
 void hmap_init(struct hmap *);
diff --git a/lib/smap.h b/lib/smap.h
index 49d31cb..37eac47 100644
--- a/lib/smap.h
+++ b/lib/smap.h
@@ -54,16 +54,24 @@ struct smap_node {
  *
  * An smap initialized this way must not be modified or destroyed.
  *
- * The 'KEY' argument is evaluated multiple times.
+ * The 'KEY', 'K1', 'K2' arguments are 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) }
+#define SMAP_CONST1(SMAP, KEY, VALUE) (const struct smap) { \
+            HMAP_CONST(&(SMAP)->map, 1, SMAP_NODE(KEY, VALUE, NULL)) \
+        }
+#define SMAP_CONST2(SMAP, K1, V1, K2, V2) (const struct smap) {     \
+            HMAP_CONST(&(SMAP)->map, 2,                             \
+                       SMAP_NODE(K1, V1, SMAP_NODE(K2, V2, NULL)))  \
+        }
+#define SMAP_NODE(KEY, VALUE, NEXT)             \
+        &(struct smap_node) {                   \
+            .node = {                           \
+                .hash = hash_string(KEY, 0),    \
+                .next = (NEXT),                 \
+            },                                  \
+            .key = CONST_CAST(char *, KEY),     \
+            .value = CONST_CAST(char *, VALUE), \
+        }.node
 
 
 void smap_init(struct smap *);
-- 
2.1.3




More information about the dev mailing list