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)
38 struct hmap_node **buckets;
39 struct hmap_node *one;
44 /* Initializer for an empty hash map. */
45 #define HMAP_INITIALIZER(HMAP) { &(HMAP)->one, NULL, 0, 0 }
48 void hmap_init(struct hmap *);
49 void hmap_destroy(struct hmap *);
50 void hmap_swap(struct hmap *a, struct hmap *b);
51 static inline size_t hmap_count(const struct hmap *);
52 static inline bool hmap_is_empty(const struct hmap *);
54 /* Adjusting capacity. */
55 void hmap_expand(struct hmap *);
56 void hmap_shrink(struct hmap *);
57 void hmap_reserve(struct hmap *, size_t capacity);
59 /* Insertion and deletion. */
60 static inline void hmap_insert_fast(struct hmap *,
61 struct hmap_node *, size_t hash);
62 static inline void hmap_insert(struct hmap *, struct hmap_node *, size_t hash);
63 static inline void hmap_remove(struct hmap *, struct hmap_node *);
64 static inline void hmap_moved(struct hmap *,
65 struct hmap_node *, struct hmap_node *);
66 static inline void hmap_replace(struct hmap *, const struct hmap_node *old,
67 struct hmap_node *new);
70 #define HMAP_FOR_EACH_WITH_HASH(NODE, STRUCT, MEMBER, HASH, HMAP) \
71 for ((NODE) = CONTAINER_OF(hmap_first_with_hash(HMAP, HASH), \
73 &(NODE)->MEMBER != NULL; \
74 (NODE) = CONTAINER_OF(hmap_next_with_hash(&(NODE)->MEMBER), \
77 static inline struct hmap_node *hmap_first_with_hash(const struct hmap *,
79 static inline struct hmap_node *hmap_next_with_hash(const struct hmap_node *);
83 * The _SAFE version is needed when NODE may be freed. It is not needed when
84 * NODE may be removed from the hash map but its members remain accessible and
86 #define HMAP_FOR_EACH(NODE, STRUCT, MEMBER, HMAP) \
87 for ((NODE) = CONTAINER_OF(hmap_first(HMAP), STRUCT, MEMBER); \
88 &(NODE)->MEMBER != NULL; \
89 (NODE) = CONTAINER_OF(hmap_next(HMAP, &(NODE)->MEMBER), \
92 #define HMAP_FOR_EACH_SAFE(NODE, NEXT, STRUCT, MEMBER, HMAP) \
93 for ((NODE) = CONTAINER_OF(hmap_first(HMAP), STRUCT, MEMBER); \
94 (&(NODE)->MEMBER != NULL \
95 ? (NEXT) = CONTAINER_OF(hmap_next(HMAP, &(NODE)->MEMBER), \
100 static inline struct hmap_node *hmap_first(const struct hmap *);
101 static inline struct hmap_node *hmap_next(const struct hmap *,
102 const struct hmap_node *);
104 /* Returns the number of nodes currently in 'hmap'. */
106 hmap_count(const struct hmap *hmap)
111 /* Returns the maximum number of nodes that 'hmap' may hold before it should be
114 hmap_capacity(const struct hmap *hmap)
116 return hmap->mask * 2 + 1;
119 /* Returns true if 'hmap' currently contains no nodes,
120 * false otherwise. */
122 hmap_is_empty(const struct hmap *hmap)
127 /* Inserts 'node', with the given 'hash', into 'hmap'. 'hmap' is never
128 * expanded automatically. */
130 hmap_insert_fast(struct hmap *hmap, struct hmap_node *node, size_t hash)
132 struct hmap_node **bucket = &hmap->buckets[hash & hmap->mask];
134 node->next = *bucket;
139 /* Inserts 'node', with the given 'hash', into 'hmap', and expands 'hmap' if
140 * necessary to optimize search performance. */
142 hmap_insert(struct hmap *hmap, struct hmap_node *node, size_t hash)
144 hmap_insert_fast(hmap, node, hash);
145 if (hmap->n / 2 > hmap->mask) {
150 /* Removes 'node' from 'hmap'. Does not shrink the hash table; call
151 * hmap_shrink() directly if desired. */
153 hmap_remove(struct hmap *hmap, struct hmap_node *node)
155 struct hmap_node **bucket = &hmap->buckets[node->hash & hmap->mask];
156 while (*bucket != node) {
157 bucket = &(*bucket)->next;
159 *bucket = node->next;
163 /* Adjusts 'hmap' to compensate for 'old_node' having moved position in memory
164 * to 'node' (e.g. due to realloc()). */
166 hmap_moved(struct hmap *hmap,
167 struct hmap_node *old_node, struct hmap_node *node)
169 struct hmap_node **bucket = &hmap->buckets[node->hash & hmap->mask];
170 while (*bucket != old_node) {
171 bucket = &(*bucket)->next;
176 /* Puts 'new' in the position in 'hmap' currently occupied by 'old'. The 'new'
177 * node must hash to the same value as 'old'. The client is responsible for
178 * ensuring that the replacement does not violate any client-imposed
179 * invariants (e.g. uniqueness of keys within a map).
181 * Afterward, 'old' is not part of 'hmap', and the client is responsible for
182 * freeing it (if this is desirable). */
184 hmap_replace(struct hmap *hmap,
185 const struct hmap_node *old, struct hmap_node *new)
187 struct hmap_node **bucket = &hmap->buckets[old->hash & hmap->mask];
188 while (*bucket != old) {
189 bucket = &(*bucket)->next;
192 new->hash = old->hash;
195 static inline struct hmap_node *
196 hmap_next_with_hash__(const struct hmap_node *node, size_t hash)
198 while (node != NULL && node->hash != hash) {
201 return (struct hmap_node *) node;
204 /* Returns the first node in 'hmap' with the given 'hash', or a null pointer if
205 * no nodes have that hash value. */
206 static inline struct hmap_node *
207 hmap_first_with_hash(const struct hmap *hmap, size_t hash)
209 return hmap_next_with_hash__(hmap->buckets[hash & hmap->mask], hash);
212 /* Returns the next node in the same hash map as 'node' with the same hash
213 * value, or a null pointer if no more nodes have that hash value.
215 * If the hash map has been reallocated since 'node' was visited, some nodes
216 * may be skipped; if new nodes with the same hash value have been added, they
217 * will be skipped. (Removing 'node' from the hash map does not prevent
218 * calling this function, since node->next is preserved, although freeing
219 * 'node' of course does.) */
220 static inline struct hmap_node *
221 hmap_next_with_hash(const struct hmap_node *node)
223 return hmap_next_with_hash__(node->next, node->hash);
226 static inline struct hmap_node *
227 hmap_next__(const struct hmap *hmap, size_t start)
230 for (i = start; i <= hmap->mask; i++) {
231 struct hmap_node *node = hmap->buckets[i];
239 /* Returns the first node in 'hmap', in arbitrary order, or a null pointer if
240 * 'hmap' is empty. */
241 static inline struct hmap_node *
242 hmap_first(const struct hmap *hmap)
244 return hmap_next__(hmap, 0);
247 /* Returns the next node in 'hmap' following 'node', in arbitrary order, or a
248 * null pointer if 'node' is the last node in 'hmap'.
250 * If the hash map has been reallocated since 'node' was visited, some nodes
251 * may be skipped or visited twice. (Removing 'node' from the hash map does
252 * not prevent calling this function, since node->next is preserved, although
253 * freeing 'node' of course does.) */
254 static inline struct hmap_node *
255 hmap_next(const struct hmap *hmap, const struct hmap_node *node)
259 : hmap_next__(hmap, (node->hash & hmap->mask) + 1));