diff mbox

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

Message ID 1475859724-27803-1-git-send-email-blp@ovn.org
State Accepted
Headers show

Commit Message

Ben Pfaff Oct. 7, 2016, 5:02 p.m. UTC
Future commits will add a user.

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

Comments

Andy Zhou Oct. 7, 2016, 9:37 p.m. UTC | #1
On Fri, Oct 7, 2016 at 10:02 AM, Ben Pfaff <blp@ovn.org> wrote:

> Future commits will add a user.
>
> Signed-off-by: Ben Pfaff <blp@ovn.org>
>

Acked-by: Andy Zhou <azhou@ovn.org>

This is the first time I encountered a linked-list being built statically.
Very cool trick!
Ben Pfaff Oct. 7, 2016, 10:06 p.m. UTC | #2
On Fri, Oct 07, 2016 at 02:37:29PM -0700, Andy Zhou wrote:
> On Fri, Oct 7, 2016 at 10:02 AM, Ben Pfaff <blp@ovn.org> wrote:
> 
> > Future commits will add a user.
> >
> > Signed-off-by: Ben Pfaff <blp@ovn.org>
> >
> 
> Acked-by: Andy Zhou <azhou@ovn.org>
> 
> This is the first time I encountered a linked-list being built statically.
> Very cool trick!

Thanks for the review!

I was kind of surprised that it could work, but it did.

I don't think it's possible to build a doubly linked list in the same
way.
diff mbox

Patch

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 *);