New functions hmap_moved(), shash_moved().
authorBen Pfaff <blp@nicira.com>
Thu, 28 Jan 2010 22:21:31 +0000 (14:21 -0800)
committerBen Pfaff <blp@nicira.com>
Fri, 29 Jan 2010 00:06:31 +0000 (16:06 -0800)
To be used in ovs-vsctl in an upcoming commit.

lib/hmap.c
lib/hmap.h
lib/shash.c
lib/shash.h

index d66cf27..71943a7 100644 (file)
@@ -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;
     }
 }
 
index 9bb3e5f..abf380b 100644 (file)
@@ -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 *);
 
index 53abbe4..a5bfecf 100644 (file)
@@ -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)
 {
index fd8b9d8..52cd4dc 100644 (file)
@@ -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 *);