From baa8f41b24179704944f278b5ae00231b1ca8246 Mon Sep 17 00:00:00 2001
From: Ben Pfaff <blp@nicira.com>
Date: Thu, 28 Jan 2010 14:21:31 -0800
Subject: [PATCH] New functions hmap_moved(), shash_moved().

To be used in ovs-vsctl in an upcoming commit.
---
 lib/hmap.c  | 16 +++++++++++-----
 lib/hmap.h  |  3 ++-
 lib/shash.c |  6 ++++++
 lib/shash.h |  1 +
 4 files changed, 20 insertions(+), 6 deletions(-)

diff --git a/lib/hmap.c b/lib/hmap.c
index d66cf271a..71943a74e 100644
--- a/lib/hmap.c
+++ b/lib/hmap.c
@@ -48,11 +48,17 @@ hmap_swap(struct hmap *a, struct hmap *b)
     struct hmap tmp = *a;
     *a = *b;
     *b = tmp;
-    if (a->buckets == &b->one) {
-        a->buckets = &a->one;
-    }
-    if (b->buckets == &a->one) {
-        b->buckets = &b->one;
+    hmap_moved(a);
+    hmap_moved(b);
+}
+
+/* Adjusts 'hmap' to compensate for having moved position in memory (e.g. due
+ * to realloc()). */
+void
+hmap_moved(struct hmap *hmap)
+{
+    if (!hmap->mask) {
+        hmap->buckets = &hmap->one;
     }
 }
 
diff --git a/lib/hmap.h b/lib/hmap.h
index 9bb3e5fd9..abf380be4 100644
--- a/lib/hmap.h
+++ b/lib/hmap.h
@@ -53,7 +53,7 @@ hmap_node_nullify(struct hmap_node *node)
 
 /* A hash map. */
 struct hmap {
-    struct hmap_node **buckets;
+    struct hmap_node **buckets; /* Must point to 'one' iff 'mask' == 0. */
     struct hmap_node *one;
     size_t mask;
     size_t n;
@@ -66,6 +66,7 @@ struct hmap {
 void hmap_init(struct hmap *);
 void hmap_destroy(struct hmap *);
 void hmap_swap(struct hmap *a, struct hmap *b);
+void hmap_moved(struct hmap *);
 static inline size_t hmap_count(const struct hmap *);
 static inline bool hmap_is_empty(const struct hmap *);
 
diff --git a/lib/shash.c b/lib/shash.c
index 53abbe43a..a5bfecf04 100644
--- a/lib/shash.c
+++ b/lib/shash.c
@@ -46,6 +46,12 @@ shash_swap(struct shash *a, struct shash *b)
     hmap_swap(&a->map, &b->map);
 }
 
+void
+shash_moved(struct shash *sh)
+{
+    hmap_moved(&sh->map);
+}
+
 void
 shash_clear(struct shash *sh)
 {
diff --git a/lib/shash.h b/lib/shash.h
index fd8b9d8e9..52cd4dca6 100644
--- a/lib/shash.h
+++ b/lib/shash.h
@@ -41,6 +41,7 @@ struct shash {
 void shash_init(struct shash *);
 void shash_destroy(struct shash *);
 void shash_swap(struct shash *, struct shash *);
+void shash_moved(struct shash *);
 void shash_clear(struct shash *);
 bool shash_is_empty(const struct shash *);
 size_t shash_count(const struct shash *);
-- 
2.47.0