VirtualBox

source: kBuild/trunk/src/kmk/hash.c@ 1830

Last change on this file since 1830 was 1700, checked in by bird, 16 years ago

hash.c: spaces.

  • Property svn:eol-style set to native
File size: 8.2 KB
Line 
1/* hash.c -- hash table maintenance
2 Copyright (C) 1995, 1999, 2002 Free Software Foundation, Inc.
3 Written by Greg McGary <[email protected]> <[email protected]>
4
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation; either version 2, or (at your option)
8any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License along with
16this program; see the file COPYING. If not, write to the Free Software
17Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
18
19#include "make.h"
20#include "hash.h"
21
22#define CALLOC(t, n) ((t *) calloc (sizeof (t), (n)))
23#define MALLOC(t, n) ((t *) xmalloc (sizeof (t) * (n)))
24#define REALLOC(o, t, n) ((t *) xrealloc ((o), sizeof (t) * (n)))
25#define CLONE(o, t, n) ((t *) memcpy (MALLOC (t, (n)), (o), sizeof (t) * (n)))
26
27static void hash_rehash __P((struct hash_table* ht));
28static unsigned long round_up_2 __P((unsigned long rough));
29
30/* Implement double hashing with open addressing. The table size is
31 always a power of two. The secondary (`increment') hash function
32 is forced to return an odd-value, in order to be relatively prime
33 to the table size. This guarantees that the increment can
34 potentially hit every slot in the table during collision
35 resolution. */
36
37void *hash_deleted_item = &hash_deleted_item;
38
39/* Force the table size to be a power of two, possibly rounding up the
40 given size. */
41
42void
43hash_init (struct hash_table *ht, unsigned long size,
44 hash_func_t hash_1, hash_func_t hash_2, hash_cmp_func_t hash_cmp)
45{
46 ht->ht_size = round_up_2 (size);
47 ht->ht_empty_slots = ht->ht_size;
48 ht->ht_vec = (void**) CALLOC (struct token *, ht->ht_size);
49 if (ht->ht_vec == 0)
50 {
51 fprintf (stderr, _("can't allocate %ld bytes for hash table: memory exhausted"),
52 ht->ht_size * sizeof(struct token *));
53 exit (1);
54 }
55
56 ht->ht_capacity = ht->ht_size - (ht->ht_size / 16); /* 93.75% loading factor */
57 ht->ht_fill = 0;
58 ht->ht_collisions = 0;
59 ht->ht_lookups = 0;
60 ht->ht_rehashes = 0;
61 ht->ht_hash_1 = hash_1;
62 ht->ht_hash_2 = hash_2;
63 ht->ht_compare = hash_cmp;
64}
65
66/* Load an array of items into `ht'. */
67
68void
69hash_load (struct hash_table *ht, void *item_table,
70 unsigned long cardinality, unsigned long size)
71{
72 char *items = (char *) item_table;
73 while (cardinality--)
74 {
75 hash_insert (ht, items);
76 items += size;
77 }
78}
79
80/* Returns the address of the table slot matching `key'. If `key' is
81 not found, return the address of an empty slot suitable for
82 inserting `key'. The caller is responsible for incrementing
83 ht_fill on insertion. */
84
85void **
86hash_find_slot (struct hash_table *ht, const void *key)
87{
88 void **slot;
89 void **deleted_slot = 0;
90 unsigned int hash_2 = 0;
91 unsigned int hash_1 = (*ht->ht_hash_1) (key);
92
93 ht->ht_lookups++;
94#ifdef CONFIG_WITH_MAKE_STATS
95 make_stats_ht_lookups++;
96#endif
97 for (;;)
98 {
99 hash_1 &= (ht->ht_size - 1);
100 slot = &ht->ht_vec[hash_1];
101
102 if (*slot == 0)
103 return (deleted_slot ? deleted_slot : slot);
104 if (*slot == hash_deleted_item)
105 {
106 if (deleted_slot == 0)
107 deleted_slot = slot;
108 }
109 else
110 {
111 if (key == *slot)
112 return slot;
113 if ((*ht->ht_compare) (key, *slot) == 0)
114 return slot;
115 ht->ht_collisions++;
116#ifdef CONFIG_WITH_MAKE_STATS
117 make_stats_ht_collisions++;
118#endif
119 }
120 if (!hash_2)
121 hash_2 = (*ht->ht_hash_2) (key) | 1;
122 hash_1 += hash_2;
123 }
124}
125
126void *
127hash_find_item (struct hash_table *ht, const void *key)
128{
129 void **slot = hash_find_slot (ht, key);
130 return ((HASH_VACANT (*slot)) ? 0 : *slot);
131}
132
133void *
134hash_insert (struct hash_table *ht, const void *item)
135{
136 void **slot = hash_find_slot (ht, item);
137 const void *old_item = slot ? *slot : 0;
138 hash_insert_at (ht, item, slot);
139 return (void *)((HASH_VACANT (old_item)) ? 0 : old_item);
140}
141
142void *
143hash_insert_at (struct hash_table *ht, const void *item, const void *slot)
144{
145 const void *old_item = *(void **) slot;
146 if (HASH_VACANT (old_item))
147 {
148 ht->ht_fill++;
149 if (old_item == 0)
150 ht->ht_empty_slots--;
151 old_item = item;
152 }
153 *(void const **) slot = item;
154 if (ht->ht_empty_slots < ht->ht_size - ht->ht_capacity)
155 {
156 hash_rehash (ht);
157 return (void *) hash_find_slot (ht, item);
158 }
159 else
160 return (void *) slot;
161}
162
163void *
164hash_delete (struct hash_table *ht, const void *item)
165{
166 void **slot = hash_find_slot (ht, item);
167 return hash_delete_at (ht, slot);
168}
169
170void *
171hash_delete_at (struct hash_table *ht, const void *slot)
172{
173 void *item = *(void **) slot;
174 if (!HASH_VACANT (item))
175 {
176 *(void const **) slot = hash_deleted_item;
177 ht->ht_fill--;
178 return item;
179 }
180 else
181 return 0;
182}
183
184void
185hash_free_items (struct hash_table *ht)
186{
187 void **vec = ht->ht_vec;
188 void **end = &vec[ht->ht_size];
189 for (; vec < end; vec++)
190 {
191 void *item = *vec;
192 if (!HASH_VACANT (item))
193 free (item);
194 *vec = 0;
195 }
196 ht->ht_fill = 0;
197 ht->ht_empty_slots = ht->ht_size;
198}
199
200void
201hash_delete_items (struct hash_table *ht)
202{
203 void **vec = ht->ht_vec;
204 void **end = &vec[ht->ht_size];
205 for (; vec < end; vec++)
206 *vec = 0;
207 ht->ht_fill = 0;
208 ht->ht_collisions = 0;
209 ht->ht_lookups = 0;
210 ht->ht_rehashes = 0;
211 ht->ht_empty_slots = ht->ht_size;
212}
213
214void
215hash_free (struct hash_table *ht, int free_items)
216{
217 if (free_items)
218 hash_free_items (ht);
219 else
220 {
221 ht->ht_fill = 0;
222 ht->ht_empty_slots = ht->ht_size;
223 }
224 free (ht->ht_vec);
225 ht->ht_vec = 0;
226 ht->ht_capacity = 0;
227}
228
229void
230hash_map (struct hash_table *ht, hash_map_func_t map)
231{
232 void **slot;
233 void **end = &ht->ht_vec[ht->ht_size];
234
235 for (slot = ht->ht_vec; slot < end; slot++)
236 {
237 if (!HASH_VACANT (*slot))
238 (*map) (*slot);
239 }
240}
241
242void
243hash_map_arg (struct hash_table *ht, hash_map_arg_func_t map, void *arg)
244{
245 void **slot;
246 void **end = &ht->ht_vec[ht->ht_size];
247
248 for (slot = ht->ht_vec; slot < end; slot++)
249 {
250 if (!HASH_VACANT (*slot))
251 (*map) (*slot, arg);
252 }
253}
254
255/* Double the size of the hash table in the event of overflow... */
256
257static void
258hash_rehash (struct hash_table *ht)
259{
260 unsigned long old_ht_size = ht->ht_size;
261 void **old_vec = ht->ht_vec;
262 void **ovp;
263
264 if (ht->ht_fill >= ht->ht_capacity)
265 {
266 ht->ht_size *= 2;
267 ht->ht_capacity = ht->ht_size - (ht->ht_size >> 4);
268 }
269 ht->ht_rehashes++;
270 ht->ht_vec = (void **) CALLOC (struct token *, ht->ht_size);
271
272 for (ovp = old_vec; ovp < &old_vec[old_ht_size]; ovp++)
273 {
274 if (! HASH_VACANT (*ovp))
275 {
276 void **slot = hash_find_slot (ht, *ovp);
277 *slot = *ovp;
278 }
279 }
280 ht->ht_empty_slots = ht->ht_size - ht->ht_fill;
281 free (old_vec);
282}
283
284void
285hash_print_stats (struct hash_table *ht, FILE *out_FILE)
286{
287 /* GKM FIXME: honor NO_FLOAT */
288 fprintf (out_FILE, _("Load=%ld/%ld=%.0f%%, "), ht->ht_fill, ht->ht_size,
289 100.0 * (double) ht->ht_fill / (double) ht->ht_size);
290 fprintf (out_FILE, _("Rehash=%d, "), ht->ht_rehashes);
291 fprintf (out_FILE, _("Collisions=%ld/%ld=%.0f%%"), ht->ht_collisions, ht->ht_lookups,
292 (ht->ht_lookups
293 ? (100.0 * (double) ht->ht_collisions / (double) ht->ht_lookups)
294 : 0));
295}
296
297/* Dump all items into a NULL-terminated vector. Use the
298 user-supplied vector, or malloc one. */
299
300void **
301hash_dump (struct hash_table *ht, void **vector_0, qsort_cmp_t compare)
302{
303 void **vector;
304 void **slot;
305 void **end = &ht->ht_vec[ht->ht_size];
306
307 if (vector_0 == 0)
308 vector_0 = MALLOC (void *, ht->ht_fill + 1);
309 vector = vector_0;
310
311 for (slot = ht->ht_vec; slot < end; slot++)
312 if (!HASH_VACANT (*slot))
313 *vector++ = *slot;
314 *vector = 0;
315
316 if (compare)
317 qsort (vector_0, ht->ht_fill, sizeof (void *), compare);
318 return vector_0;
319}
320
321/* Round a given number up to the nearest power of 2. */
322
323static unsigned long
324round_up_2 (unsigned long n)
325{
326 n |= (n >> 1);
327 n |= (n >> 2);
328 n |= (n >> 4);
329 n |= (n >> 8);
330 n |= (n >> 16);
331
332#if !defined(HAVE_LIMITS_H) || ULONG_MAX > 4294967295
333 /* We only need this on systems where unsigned long is >32 bits. */
334 n |= (n >> 32);
335#endif
336
337 return n + 1;
338}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette