hmap: New function hmap_clear().
authorBen Pfaff <blp@nicira.com>
Mon, 19 Jul 2010 18:22:10 +0000 (11:22 -0700)
committerBen Pfaff <blp@nicira.com>
Fri, 1 Oct 2010 17:25:10 +0000 (10:25 -0700)
lib/hmap.c
lib/hmap.h

index 1b4816d..6bc5ea7 100644 (file)
@@ -18,6 +18,7 @@
 #include "hmap.h"
 #include <assert.h>
 #include <stdint.h>
+#include <string.h>
 #include "coverage.h"
 #include "random.h"
 #include "util.h"
@@ -42,6 +43,22 @@ hmap_destroy(struct hmap *hmap)
     }
 }
 
+/* Removes all node from 'hmap', leaving it ready to accept more nodes.  Does
+ * not free memory allocated for 'hmap'.
+ *
+ * This function is appropriate when 'hmap' will soon have about as many
+ * elements as it before.  If 'hmap' will likely have fewer elements than
+ * before, use hmap_destroy() followed by hmap_clear() to save memory and
+ * iteration time. */
+void
+hmap_clear(struct hmap *hmap)
+{
+    if (hmap->n > 0) {
+        hmap->n = 0;
+        memset(hmap->buckets, 0, (hmap->mask + 1) * sizeof *hmap->buckets);
+    }
+}
+
 /* Exchanges hash maps 'a' and 'b'. */
 void
 hmap_swap(struct hmap *a, struct hmap *b)
index d567499..92aff7f 100644 (file)
@@ -69,6 +69,7 @@ struct hmap {
 /* Initialization. */
 void hmap_init(struct hmap *);
 void hmap_destroy(struct hmap *);
+void hmap_clear(struct hmap *);
 void hmap_swap(struct hmap *a, struct hmap *b);
 void hmap_moved(struct hmap *hmap);
 static inline size_t hmap_count(const struct hmap *);