From: Ben Pfaff Date: Thu, 28 Jan 2010 22:21:31 +0000 (-0800) Subject: New functions hmap_moved(), shash_moved(). X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=baa8f41b24179704944f278b5ae00231b1ca8246;p=openvswitch New functions hmap_moved(), shash_moved(). To be used in ovs-vsctl in an upcoming commit. --- diff --git a/lib/hmap.c b/lib/hmap.c index d66cf271..71943a74 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 9bb3e5fd..abf380be 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 53abbe43..a5bfecf0 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 fd8b9d8e..52cd4dca 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 *);