Merge commit '10a89ef04df5669c5cdd02f786150a7ab8454e01'
[sliver-openvswitch.git] / lib / hmap.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2012, 2013 Nicira, Inc.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <config.h>
18 #include "hmap.h"
19 #include <stdint.h>
20 #include <string.h>
21 #include "coverage.h"
22 #include "random.h"
23 #include "util.h"
24
25 COVERAGE_DEFINE(hmap_pathological);
26 COVERAGE_DEFINE(hmap_expand);
27 COVERAGE_DEFINE(hmap_shrink);
28 COVERAGE_DEFINE(hmap_reserve);
29
30 /* Initializes 'hmap' as an empty hash table. */
31 void
32 hmap_init(struct hmap *hmap)
33 {
34     hmap->buckets = &hmap->one;
35     hmap->one = NULL;
36     hmap->mask = 0;
37     hmap->n = 0;
38 }
39
40 /* Frees memory reserved by 'hmap'.  It is the client's responsibility to free
41  * the nodes themselves, if necessary. */
42 void
43 hmap_destroy(struct hmap *hmap)
44 {
45     if (hmap && hmap->buckets != &hmap->one) {
46         free(hmap->buckets);
47     }
48 }
49
50 /* Removes all node from 'hmap', leaving it ready to accept more nodes.  Does
51  * not free memory allocated for 'hmap'.
52  *
53  * This function is appropriate when 'hmap' will soon have about as many
54  * elements as it before.  If 'hmap' will likely have fewer elements than
55  * before, use hmap_destroy() followed by hmap_clear() to save memory and
56  * iteration time. */
57 void
58 hmap_clear(struct hmap *hmap)
59 {
60     if (hmap->n > 0) {
61         hmap->n = 0;
62         memset(hmap->buckets, 0, (hmap->mask + 1) * sizeof *hmap->buckets);
63     }
64 }
65
66 /* Exchanges hash maps 'a' and 'b'. */
67 void
68 hmap_swap(struct hmap *a, struct hmap *b)
69 {
70     struct hmap tmp = *a;
71     *a = *b;
72     *b = tmp;
73     hmap_moved(a);
74     hmap_moved(b);
75 }
76
77 /* Adjusts 'hmap' to compensate for having moved position in memory (e.g. due
78  * to realloc()). */
79 void
80 hmap_moved(struct hmap *hmap)
81 {
82     if (!hmap->mask) {
83         hmap->buckets = &hmap->one;
84     }
85 }
86
87 static void
88 resize(struct hmap *hmap, size_t new_mask)
89 {
90     struct hmap tmp;
91     size_t i;
92
93     ovs_assert(is_pow2(new_mask + 1));
94
95     hmap_init(&tmp);
96     if (new_mask) {
97         tmp.buckets = xmalloc(sizeof *tmp.buckets * (new_mask + 1));
98         tmp.mask = new_mask;
99         for (i = 0; i <= tmp.mask; i++) {
100             tmp.buckets[i] = NULL;
101         }
102     }
103     for (i = 0; i <= hmap->mask; i++) {
104         struct hmap_node *node, *next;
105         int count = 0;
106         for (node = hmap->buckets[i]; node; node = next) {
107             next = node->next;
108             hmap_insert_fast(&tmp, node, node->hash);
109             count++;
110         }
111         if (count > 5) {
112             COVERAGE_INC(hmap_pathological);
113         }
114     }
115     hmap_swap(hmap, &tmp);
116     hmap_destroy(&tmp);
117 }
118
119 static size_t
120 calc_mask(size_t capacity)
121 {
122     size_t mask = capacity / 2;
123     mask |= mask >> 1;
124     mask |= mask >> 2;
125     mask |= mask >> 4;
126     mask |= mask >> 8;
127     mask |= mask >> 16;
128 #if SIZE_MAX > UINT32_MAX
129     mask |= mask >> 32;
130 #endif
131
132     /* If we need to dynamically allocate buckets we might as well allocate at
133      * least 4 of them. */
134     mask |= (mask & 1) << 1;
135
136     return mask;
137 }
138
139 /* Expands 'hmap', if necessary, to optimize the performance of searches. */
140 void
141 hmap_expand(struct hmap *hmap)
142 {
143     size_t new_mask = calc_mask(hmap->n);
144     if (new_mask > hmap->mask) {
145         COVERAGE_INC(hmap_expand);
146         resize(hmap, new_mask);
147     }
148 }
149
150 /* Shrinks 'hmap', if necessary, to optimize the performance of iteration. */
151 void
152 hmap_shrink(struct hmap *hmap)
153 {
154     size_t new_mask = calc_mask(hmap->n);
155     if (new_mask < hmap->mask) {
156         COVERAGE_INC(hmap_shrink);
157         resize(hmap, new_mask);
158     }
159 }
160
161 /* Expands 'hmap', if necessary, to optimize the performance of searches when
162  * it has up to 'n' elements.  (But iteration will be slow in a hash map whose
163  * allocated capacity is much higher than its current number of nodes.)  */
164 void
165 hmap_reserve(struct hmap *hmap, size_t n)
166 {
167     size_t new_mask = calc_mask(n);
168     if (new_mask > hmap->mask) {
169         COVERAGE_INC(hmap_reserve);
170         resize(hmap, new_mask);
171     }
172 }
173
174 /* Adjusts 'hmap' to compensate for 'old_node' having moved position in memory
175  * to 'node' (e.g. due to realloc()). */
176 void
177 hmap_node_moved(struct hmap *hmap,
178                 struct hmap_node *old_node, struct hmap_node *node)
179 {
180     struct hmap_node **bucket = &hmap->buckets[node->hash & hmap->mask];
181     while (*bucket != old_node) {
182         bucket = &(*bucket)->next;
183     }
184     *bucket = node;
185 }
186
187 /* Chooses and returns a randomly selected node from 'hmap', which must not be
188  * empty.
189  *
190  * I wouldn't depend on this algorithm to be fair, since I haven't analyzed it.
191  * But it does at least ensure that any node in 'hmap' can be chosen. */
192 struct hmap_node *
193 hmap_random_node(const struct hmap *hmap)
194 {
195     struct hmap_node *bucket, *node;
196     size_t n, i;
197
198     /* Choose a random non-empty bucket. */
199     for (i = random_uint32(); ; i++) {
200         bucket = hmap->buckets[i & hmap->mask];
201         if (bucket) {
202             break;
203         }
204     }
205
206     /* Count nodes in bucket. */
207     n = 0;
208     for (node = bucket; node; node = node->next) {
209         n++;
210     }
211
212     /* Choose random node from bucket. */
213     i = random_range(n);
214     for (node = bucket; i-- > 0; node = node->next) {
215         continue;
216     }
217     return node;
218 }
219
220 /* Returns the next node in 'hmap' in hash order, or NULL if no nodes remain in
221  * 'hmap'.  Uses '*bucketp' and '*offsetp' to determine where to begin
222  * iteration, and stores new values to pass on the next iteration into them
223  * before returning.
224  *
225  * It's better to use plain HMAP_FOR_EACH and related functions, since they are
226  * faster and better at dealing with hmaps that change during iteration.
227  *
228  * Before beginning iteration, store 0 into '*bucketp' and '*offsetp'.
229  */
230 struct hmap_node *
231 hmap_at_position(const struct hmap *hmap,
232                  uint32_t *bucketp, uint32_t *offsetp)
233 {
234     size_t offset;
235     size_t b_idx;
236
237     offset = *offsetp;
238     for (b_idx = *bucketp; b_idx <= hmap->mask; b_idx++) {
239         struct hmap_node *node;
240         size_t n_idx;
241
242         for (n_idx = 0, node = hmap->buckets[b_idx]; node != NULL;
243              n_idx++, node = node->next) {
244             if (n_idx == offset) {
245                 if (node->next) {
246                     *bucketp = node->hash & hmap->mask;
247                     *offsetp = offset + 1;
248                 } else {
249                     *bucketp = (node->hash & hmap->mask) + 1;
250                     *offsetp = 0;
251                 }
252                 return node;
253             }
254         }
255         offset = 0;
256     }
257
258     *bucketp = 0;
259     *offsetp = 0;
260     return NULL;
261 }
262
263 /* Returns true if 'node' is in 'hmap', false otherwise. */
264 bool
265 hmap_contains(const struct hmap *hmap, const struct hmap_node *node)
266 {
267     struct hmap_node *p;
268
269     for (p = hmap_first_in_bucket(hmap, node->hash); p; p = p->next) {
270         if (p == node) {
271             return true;
272         }
273     }
274
275     return false;
276 }