2 * Copyright © 2009 Intel Corporation
3 * Copyright © 1988-2004 Keith Packard and Bart Massey.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Except as contained in this notice, the names of the authors
25 * or their institutions shall not be used in advertising or
26 * otherwise to promote the sale, use or other dealings in this
27 * Software without prior written authorization from the
31 * Eric Anholt <eric@anholt.net>
32 * Keith Packard <keithp@keithp.com>
46 struct hash_entry *table;
52 uint32_t deleted_entries;
55 #define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
58 * From Knuth -- a good choice for hash/rehash values is p, p-2 where
59 * p and p-2 are both prime. These tables are sized to have an extra 10%
60 * free to avoid exponential performance degradation as the hash table fills
63 static const uint32_t deleted_data;
66 uint32_t max_entries, size, rehash;
81 { 16384, 18043, 18041 },
82 { 32768, 36109, 36107 },
83 { 65536, 72091, 72089 },
84 { 131072, 144409, 144407 },
85 { 262144, 288361, 288359 },
86 { 524288, 576883, 576881 },
87 { 1048576, 1153459, 1153457 },
88 { 2097152, 2307163, 2307161 },
89 { 4194304, 4613893, 4613891 },
90 { 8388608, 9227641, 9227639 },
91 { 16777216, 18455029, 18455027 },
92 { 33554432, 36911011, 36911009 },
93 { 67108864, 73819861, 73819859 },
94 { 134217728, 147639589, 147639587 },
95 { 268435456, 295279081, 295279079 },
96 { 536870912, 590559793, 590559791 },
97 { 1073741824, 1181116273, 1181116271},
98 { 2147483648ul, 2362232233ul, 2362232231ul}
102 entry_is_free(struct hash_entry *entry)
104 return entry->data == NULL;
108 entry_is_deleted(struct hash_entry *entry)
110 return entry->data == &deleted_data;
114 entry_is_present(struct hash_entry *entry)
116 return entry->data != NULL && entry->data != &deleted_data;
120 hash_table_create(void)
122 struct hash_table *ht;
124 ht = malloc(sizeof(*ht));
129 ht->size = hash_sizes[ht->size_index].size;
130 ht->rehash = hash_sizes[ht->size_index].rehash;
131 ht->max_entries = hash_sizes[ht->size_index].max_entries;
132 ht->table = calloc(ht->size, sizeof(*ht->table));
134 ht->deleted_entries = 0;
136 if (ht->table == NULL) {
145 * Frees the given hash table.
148 hash_table_destroy(struct hash_table *ht)
158 * Finds a hash table entry with the given key and hash of that key.
160 * Returns NULL if no entry is found. Note that the data pointer may be
161 * modified by the user.
164 hash_table_search(struct hash_table *ht, uint32_t hash)
166 uint32_t hash_address;
168 hash_address = hash % ht->size;
170 uint32_t double_hash;
172 struct hash_entry *entry = ht->table + hash_address;
174 if (entry_is_free(entry)) {
176 } else if (entry_is_present(entry) && entry->hash == hash) {
180 double_hash = 1 + hash % ht->rehash;
182 hash_address = (hash_address + double_hash) % ht->size;
183 } while (hash_address != hash % ht->size);
189 hash_table_for_each(struct hash_table *ht,
190 hash_table_iterator_func_t func, void *data)
192 struct hash_entry *entry;
195 for (i = 0; i < ht->size; i++) {
196 entry = ht->table + i;
197 if (entry_is_present(entry))
198 func(entry->data, data);
203 hash_table_lookup(struct hash_table *ht, uint32_t hash)
205 struct hash_entry *entry;
207 entry = hash_table_search(ht, hash);
215 hash_table_rehash(struct hash_table *ht, int new_size_index)
217 struct hash_table old_ht;
218 struct hash_entry *table, *entry;
220 if (new_size_index >= ARRAY_SIZE(hash_sizes))
223 table = calloc(hash_sizes[new_size_index].size, sizeof(*ht->table));
230 ht->size_index = new_size_index;
231 ht->size = hash_sizes[ht->size_index].size;
232 ht->rehash = hash_sizes[ht->size_index].rehash;
233 ht->max_entries = hash_sizes[ht->size_index].max_entries;
235 ht->deleted_entries = 0;
237 for (entry = old_ht.table;
238 entry != old_ht.table + old_ht.size;
240 if (entry_is_present(entry)) {
241 hash_table_insert(ht, entry->hash, entry->data);
249 * Inserts the data with the given hash into the table.
251 * Note that insertion may rearrange the table on a resize or rehash,
252 * so previously found hash_entries are no longer valid after this function.
255 hash_table_insert(struct hash_table *ht, uint32_t hash, void *data)
257 uint32_t hash_address;
259 if (ht->entries >= ht->max_entries) {
260 hash_table_rehash(ht, ht->size_index + 1);
261 } else if (ht->deleted_entries + ht->entries >= ht->max_entries) {
262 hash_table_rehash(ht, ht->size_index);
265 hash_address = hash % ht->size;
267 struct hash_entry *entry = ht->table + hash_address;
268 uint32_t double_hash;
270 if (!entry_is_present(entry)) {
271 if (entry_is_deleted(entry))
272 ht->deleted_entries--;
279 double_hash = 1 + hash % ht->rehash;
281 hash_address = (hash_address + double_hash) % ht->size;
282 } while (hash_address != hash % ht->size);
284 /* We could hit here if a required resize failed. An unchecked-malloc
285 * application could ignore this result.
291 * This function deletes the given hash table entry.
293 * Note that deletion doesn't otherwise modify the table, so an iteration over
294 * the table deleting entries is safe.
297 hash_table_remove(struct hash_table *ht, uint32_t hash)
299 struct hash_entry *entry;
301 entry = hash_table_search(ht, hash);
303 entry->data = (void *) &deleted_data;
305 ht->deleted_entries++;