Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc2ad45.
[cascardo/ovs.git] / lib / hmap.c
1 /*
2  * Copyright (c) 2008, 2009 Nicira Networks.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include <config.h>
18 #include "hmap.h"
19 #include <assert.h>
20 #include <stdint.h>
21 #include "coverage.h"
22 #include "util.h"
23
24 /* Initializes 'hmap' as an empty hash table. */
25 void
26 hmap_init(struct hmap *hmap)
27 {
28     hmap->buckets = &hmap->one;
29     hmap->one = NULL;
30     hmap->mask = 0;
31     hmap->n = 0;
32 }
33
34 /* Frees memory reserved by 'hmap'.  It is the client's responsibility to free
35  * the nodes themselves, if necessary. */
36 void
37 hmap_destroy(struct hmap *hmap)
38 {
39     if (hmap && hmap->buckets != &hmap->one) {
40         free(hmap->buckets);
41     }
42 }
43
44 /* Exchanges hash maps 'a' and 'b'. */
45 void
46 hmap_swap(struct hmap *a, struct hmap *b)
47 {
48     struct hmap tmp = *a;
49     *a = *b;
50     *b = tmp;
51     if (a->buckets == &b->one) {
52         a->buckets = &a->one;
53     }
54     if (b->buckets == &a->one) {
55         b->buckets = &b->one;
56     }
57 }
58
59 static void
60 resize(struct hmap *hmap, size_t new_mask)
61 {
62     struct hmap tmp;
63     size_t i;
64
65     assert(!(new_mask & (new_mask + 1)));
66     assert(new_mask != SIZE_MAX);
67
68     hmap_init(&tmp);
69     if (new_mask) {
70         tmp.buckets = xmalloc(sizeof *tmp.buckets * (new_mask + 1));
71         tmp.mask = new_mask;
72         for (i = 0; i <= tmp.mask; i++) {
73             tmp.buckets[i] = NULL;
74         }
75     }
76     for (i = 0; i <= hmap->mask; i++) {
77         struct hmap_node *node, *next;
78         int count = 0;
79         for (node = hmap->buckets[i]; node; node = next) {
80             next = node->next;
81             hmap_insert_fast(&tmp, node, node->hash);
82             count++;
83         }
84         if (count > 5) {
85             COVERAGE_INC(hmap_pathological);
86         }
87     }
88     hmap_swap(hmap, &tmp);
89     hmap_destroy(&tmp);
90 }
91
92 static size_t
93 calc_mask(size_t capacity)
94 {
95     size_t mask = capacity / 2;
96     mask |= mask >> 1;
97     mask |= mask >> 2;
98     mask |= mask >> 4;
99     mask |= mask >> 8;
100     mask |= mask >> 16;
101 #if SIZE_MAX > UINT32_MAX
102     mask |= mask >> 32;
103 #endif
104
105     /* If we need to dynamically allocate buckets we might as well allocate at
106      * least 4 of them. */
107     mask |= (mask & 1) << 1;
108
109     return mask;
110 }
111
112 /* Expands 'hmap', if necessary, to optimize the performance of searches. */
113 void
114 hmap_expand(struct hmap *hmap)
115 {
116     size_t new_mask = calc_mask(hmap->n);
117     if (new_mask > hmap->mask) {
118         COVERAGE_INC(hmap_expand);
119         resize(hmap, new_mask);
120     }
121 }
122
123 /* Shrinks 'hmap', if necessary, to optimize the performance of iteration. */
124 void
125 hmap_shrink(struct hmap *hmap)
126 {
127     size_t new_mask = calc_mask(hmap->n);
128     if (new_mask < hmap->mask) {
129         COVERAGE_INC(hmap_shrink);
130         resize(hmap, new_mask);
131     }
132 }
133
134 /* Expands 'hmap', if necessary, to optimize the performance of searches when
135  * it has up to 'n' elements.  (But iteration will be slow in a hash map whose
136  * allocated capacity is much higher than its current number of nodes.)  */
137 void
138 hmap_reserve(struct hmap *hmap, size_t n)
139 {
140     size_t new_mask = calc_mask(n);
141     if (new_mask > hmap->mask) {
142         COVERAGE_INC(hmap_reserve);
143         resize(hmap, new_mask);
144     }
145 }