2 * Copyright (c) 2008, 2009 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
24 /* A hash map node, to be embedded inside the data structure being mapped. */
26 size_t hash; /* Hash value. */
27 struct hmap_node *next; /* Next in linked list. */
30 /* Returns the hash value embedded in 'node'. */
31 static inline size_t hmap_node_hash(const struct hmap_node *node)
36 #define HMAP_NODE_NULL ((struct hmap_node *) 1)
38 /* Returns true if 'node' has been set to null by hmap_node_nullify() and has
39 * not been un-nullified by being inserted into an hmap. */
41 hmap_node_is_null(const struct hmap_node *node)
43 return node->next == HMAP_NODE_NULL;
46 /* Marks 'node' with a distinctive value that can be tested with
47 * hmap_node_is_null(). */
49 hmap_node_nullify(struct hmap_node *node)
51 node->next = HMAP_NODE_NULL;
56 struct hmap_node **buckets;
57 struct hmap_node *one;
62 /* Initializer for an empty hash map. */
63 #define HMAP_INITIALIZER(HMAP) { &(HMAP)->one, NULL, 0, 0 }
66 void hmap_init(struct hmap *);
67 void hmap_destroy(struct hmap *);
68 void hmap_swap(struct hmap *a, struct hmap *b);
69 static inline size_t hmap_count(const struct hmap *);
70 static inline bool hmap_is_empty(const struct hmap *);
72 /* Adjusting capacity. */
73 void hmap_expand(struct hmap *);
74 void hmap_shrink(struct hmap *);
75 void hmap_reserve(struct hmap *, size_t capacity);
77 /* Insertion and deletion. */
78 static inline void hmap_insert_fast(struct hmap *,
79 struct hmap_node *, size_t hash);
80 static inline void hmap_insert(struct hmap *, struct hmap_node *, size_t hash);
81 static inline void hmap_remove(struct hmap *, struct hmap_node *);
82 static inline void hmap_moved(struct hmap *,
83 struct hmap_node *, struct hmap_node *);
84 static inline void hmap_replace(struct hmap *, const struct hmap_node *old,
85 struct hmap_node *new);
89 * HMAP_FOR_EACH_WITH_HASH iterates NODE over all of the nodes in HMAP that
90 * have hash value equal to HASH. HMAP_FOR_EACH_IN_BUCKET iterates NODE over
91 * all of the nodes in HMAP that would fall in the same bucket as HASH. STRUCT
92 * and MEMBER must be the name of the struct that contains the 'struct
93 * hmap_node' and the name of the 'struct hmap_node' member, respectively.
95 * These macros may be used interchangeably to search for a particular value in
96 * an hmap, see, e.g. shash_find() for an example. Usually, using
97 * HMAP_FOR_EACH_WITH_HASH provides an optimization, because comparing a hash
98 * value is usually cheaper than comparing an entire hash map key. But for
99 * simple hash map keys, it makes sense to use HMAP_FOR_EACH_IN_BUCKET because
100 * it avoids doing two comparisons when a single simple comparison suffices.
102 * The loop should not change NODE to point to a different node or insert or
103 * delete nodes in HMAP (unless it "break"s out of the loop to terminate
106 * HASH is only evaluated once.
108 #define HMAP_FOR_EACH_WITH_HASH(NODE, STRUCT, MEMBER, HASH, HMAP) \
109 for ((NODE) = CONTAINER_OF(hmap_first_with_hash(HMAP, HASH), \
111 &(NODE)->MEMBER != NULL; \
112 (NODE) = CONTAINER_OF(hmap_next_with_hash(&(NODE)->MEMBER), \
114 #define HMAP_FOR_EACH_IN_BUCKET(NODE, STRUCT, MEMBER, HASH, HMAP) \
115 for ((NODE) = CONTAINER_OF(hmap_first_in_bucket(HMAP, HASH), \
117 &(NODE)->MEMBER != NULL; \
118 (NODE) = CONTAINER_OF(hmap_next_in_bucket(&(NODE)->MEMBER), \
121 static inline struct hmap_node *hmap_first_with_hash(const struct hmap *,
123 static inline struct hmap_node *hmap_next_with_hash(const struct hmap_node *);
124 static inline struct hmap_node *hmap_first_in_bucket(const struct hmap *,
126 static inline struct hmap_node *hmap_next_in_bucket(const struct hmap_node *);
130 * The _SAFE version is needed when NODE may be freed. It is not needed when
131 * NODE may be removed from the hash map but its members remain accessible and
133 #define HMAP_FOR_EACH(NODE, STRUCT, MEMBER, HMAP) \
134 for ((NODE) = CONTAINER_OF(hmap_first(HMAP), STRUCT, MEMBER); \
135 &(NODE)->MEMBER != NULL; \
136 (NODE) = CONTAINER_OF(hmap_next(HMAP, &(NODE)->MEMBER), \
139 #define HMAP_FOR_EACH_SAFE(NODE, NEXT, STRUCT, MEMBER, HMAP) \
140 for ((NODE) = CONTAINER_OF(hmap_first(HMAP), STRUCT, MEMBER); \
141 (&(NODE)->MEMBER != NULL \
142 ? (NEXT) = CONTAINER_OF(hmap_next(HMAP, &(NODE)->MEMBER), \
147 static inline struct hmap_node *hmap_first(const struct hmap *);
148 static inline struct hmap_node *hmap_next(const struct hmap *,
149 const struct hmap_node *);
151 /* Returns the number of nodes currently in 'hmap'. */
153 hmap_count(const struct hmap *hmap)
158 /* Returns the maximum number of nodes that 'hmap' may hold before it should be
161 hmap_capacity(const struct hmap *hmap)
163 return hmap->mask * 2 + 1;
166 /* Returns true if 'hmap' currently contains no nodes,
167 * false otherwise. */
169 hmap_is_empty(const struct hmap *hmap)
174 /* Inserts 'node', with the given 'hash', into 'hmap'. 'hmap' is never
175 * expanded automatically. */
177 hmap_insert_fast(struct hmap *hmap, struct hmap_node *node, size_t hash)
179 struct hmap_node **bucket = &hmap->buckets[hash & hmap->mask];
181 node->next = *bucket;
186 /* Inserts 'node', with the given 'hash', into 'hmap', and expands 'hmap' if
187 * necessary to optimize search performance. */
189 hmap_insert(struct hmap *hmap, struct hmap_node *node, size_t hash)
191 hmap_insert_fast(hmap, node, hash);
192 if (hmap->n / 2 > hmap->mask) {
197 /* Removes 'node' from 'hmap'. Does not shrink the hash table; call
198 * hmap_shrink() directly if desired. */
200 hmap_remove(struct hmap *hmap, struct hmap_node *node)
202 struct hmap_node **bucket = &hmap->buckets[node->hash & hmap->mask];
203 while (*bucket != node) {
204 bucket = &(*bucket)->next;
206 *bucket = node->next;
210 /* Adjusts 'hmap' to compensate for 'old_node' having moved position in memory
211 * to 'node' (e.g. due to realloc()). */
213 hmap_moved(struct hmap *hmap,
214 struct hmap_node *old_node, struct hmap_node *node)
216 struct hmap_node **bucket = &hmap->buckets[node->hash & hmap->mask];
217 while (*bucket != old_node) {
218 bucket = &(*bucket)->next;
223 /* Puts 'new' in the position in 'hmap' currently occupied by 'old'. The 'new'
224 * node must hash to the same value as 'old'. The client is responsible for
225 * ensuring that the replacement does not violate any client-imposed
226 * invariants (e.g. uniqueness of keys within a map).
228 * Afterward, 'old' is not part of 'hmap', and the client is responsible for
229 * freeing it (if this is desirable). */
231 hmap_replace(struct hmap *hmap,
232 const struct hmap_node *old, struct hmap_node *new)
234 struct hmap_node **bucket = &hmap->buckets[old->hash & hmap->mask];
235 while (*bucket != old) {
236 bucket = &(*bucket)->next;
239 new->hash = old->hash;
240 new->next = old->next;
243 static inline struct hmap_node *
244 hmap_next_with_hash__(const struct hmap_node *node, size_t hash)
246 while (node != NULL && node->hash != hash) {
249 return (struct hmap_node *) node;
252 /* Returns the first node in 'hmap' with the given 'hash', or a null pointer if
253 * no nodes have that hash value. */
254 static inline struct hmap_node *
255 hmap_first_with_hash(const struct hmap *hmap, size_t hash)
257 return hmap_next_with_hash__(hmap->buckets[hash & hmap->mask], hash);
260 /* Returns the first node in 'hmap' in the bucket in which the given 'hash'
261 * would land, or a null pointer if that bucket is empty. */
262 static inline struct hmap_node *
263 hmap_first_in_bucket(const struct hmap *hmap, size_t hash)
265 return hmap->buckets[hash & hmap->mask];
268 /* Returns the next node in the same bucket as 'node', or a null pointer if
269 * there are no more nodes in that bucket.
271 * If the hash map has been reallocated since 'node' was visited, some nodes
272 * may be skipped; if new nodes with the same hash value have been added, they
273 * will be skipped. (Removing 'node' from the hash map does not prevent
274 * calling this function, since node->next is preserved, although freeing
275 * 'node' of course does.) */
276 static inline struct hmap_node *
277 hmap_next_in_bucket(const struct hmap_node *node)
282 /* Returns the next node in the same hash map as 'node' with the same hash
283 * value, or a null pointer if no more nodes have that hash value.
285 * If the hash map has been reallocated since 'node' was visited, some nodes
286 * may be skipped; if new nodes with the same hash value have been added, they
287 * will be skipped. (Removing 'node' from the hash map does not prevent
288 * calling this function, since node->next is preserved, although freeing
289 * 'node' of course does.) */
290 static inline struct hmap_node *
291 hmap_next_with_hash(const struct hmap_node *node)
293 return hmap_next_with_hash__(node->next, node->hash);
296 static inline struct hmap_node *
297 hmap_next__(const struct hmap *hmap, size_t start)
300 for (i = start; i <= hmap->mask; i++) {
301 struct hmap_node *node = hmap->buckets[i];
309 /* Returns the first node in 'hmap', in arbitrary order, or a null pointer if
310 * 'hmap' is empty. */
311 static inline struct hmap_node *
312 hmap_first(const struct hmap *hmap)
314 return hmap_next__(hmap, 0);
317 /* Returns the next node in 'hmap' following 'node', in arbitrary order, or a
318 * null pointer if 'node' is the last node in 'hmap'.
320 * If the hash map has been reallocated since 'node' was visited, some nodes
321 * may be skipped or visited twice. (Removing 'node' from the hash map does
322 * not prevent calling this function, since node->next is preserved, although
323 * freeing 'node' of course does.) */
324 static inline struct hmap_node *
325 hmap_next(const struct hmap *hmap, const struct hmap_node *node)
329 : hmap_next__(hmap, (node->hash & hmap->mask) + 1));