/*
- * Copyright (c) 2008, 2009 Nicira Networks.
+ * Copyright (c) 2008, 2009, 2010 Nicira Networks.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
return node->hash;
}
+#define HMAP_NODE_NULL ((struct hmap_node *) 1)
+
+/* Returns true if 'node' has been set to null by hmap_node_nullify() and has
+ * not been un-nullified by being inserted into an hmap. */
+static inline bool
+hmap_node_is_null(const struct hmap_node *node)
+{
+ return node->next == HMAP_NODE_NULL;
+}
+
+/* Marks 'node' with a distinctive value that can be tested with
+ * hmap_node_is_null(). */
+static inline void
+hmap_node_nullify(struct hmap_node *node)
+{
+ node->next = HMAP_NODE_NULL;
+}
+
/* 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;
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 *);
struct hmap_node *, size_t hash);
static inline void hmap_insert(struct hmap *, struct hmap_node *, size_t hash);
static inline void hmap_remove(struct hmap *, struct hmap_node *);
-static inline void hmap_moved(struct hmap *,
- struct hmap_node *, struct hmap_node *);
+void hmap_node_moved(struct hmap *, struct hmap_node *, struct hmap_node *);
static inline void hmap_replace(struct hmap *, const struct hmap_node *old,
struct hmap_node *new);
hmap->n--;
}
-/* Adjusts 'hmap' to compensate for 'old_node' having moved position in memory
- * to 'node' (e.g. due to realloc()). */
-static inline void
-hmap_moved(struct hmap *hmap,
- struct hmap_node *old_node, struct hmap_node *node)
-{
- struct hmap_node **bucket = &hmap->buckets[node->hash & hmap->mask];
- while (*bucket != old_node) {
- bucket = &(*bucket)->next;
- }
- *bucket = node;
-}
-
/* Puts 'new' in the position in 'hmap' currently occupied by 'old'. The 'new'
* node must hash to the same value as 'old'. The client is responsible for
* ensuring that the replacement does not violate any client-imposed