VirtualBox

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

Last change on this file since 1891 was 1864, checked in by bird, 16 years ago

kmk: use alloc caches for variables, variable sets and varaible set lists.

  • Property svn:eol-style set to native
File size: 10.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
126#ifdef CONFIG_WITH_VALUE_LENGTH
127/* A variant of hash_find_slot that takes the hash values as arguments.
128 The HASH_2 argument is optional, pass 0 if not available.
129 Not having to call ht_hash_1 and perhaps also not ht_hash_2 does save
130 a whole bunch of cycles in some of the kBuild use cases (strcache sees
131 serious usage there). */
132void **
133hash_find_slot_prehashed (struct hash_table *ht, const void *key,
134 unsigned int hash_1, unsigned int hash_2)
135{
136 void **slot;
137 void **deleted_slot = 0;
138
139 ht->ht_lookups++;
140#ifdef CONFIG_WITH_MAKE_STATS
141 make_stats_ht_lookups++;
142#endif
143 for (;;)
144 {
145 hash_1 &= (ht->ht_size - 1);
146 slot = &ht->ht_vec[hash_1];
147
148 if (*slot == 0)
149 return (deleted_slot ? deleted_slot : slot);
150 if (*slot == hash_deleted_item)
151 {
152 if (deleted_slot == 0)
153 deleted_slot = slot;
154 }
155 else
156 {
157 if (key == *slot)
158 return slot;
159 if ((*ht->ht_compare) (key, *slot) == 0)
160 return slot;
161 ht->ht_collisions++;
162#ifdef CONFIG_WITH_MAKE_STATS
163 make_stats_ht_collisions++;
164#endif
165 }
166 if (!hash_2)
167 hash_2 = (*ht->ht_hash_2) (key) | 1;
168 hash_1 += hash_2;
169 }
170}
171
172#endif /* CONFIG_WITH_VALUE_LENGTH */
173
174void *
175hash_find_item (struct hash_table *ht, const void *key)
176{
177 void **slot = hash_find_slot (ht, key);
178 return ((HASH_VACANT (*slot)) ? 0 : *slot);
179}
180
181void *
182hash_insert (struct hash_table *ht, const void *item)
183{
184 void **slot = hash_find_slot (ht, item);
185 const void *old_item = slot ? *slot : 0;
186 hash_insert_at (ht, item, slot);
187 return (void *)((HASH_VACANT (old_item)) ? 0 : old_item);
188}
189
190void *
191hash_insert_at (struct hash_table *ht, const void *item, const void *slot)
192{
193 const void *old_item = *(void **) slot;
194 if (HASH_VACANT (old_item))
195 {
196 ht->ht_fill++;
197 if (old_item == 0)
198 ht->ht_empty_slots--;
199 old_item = item;
200 }
201 *(void const **) slot = item;
202 if (ht->ht_empty_slots < ht->ht_size - ht->ht_capacity)
203 {
204 hash_rehash (ht);
205 return (void *) hash_find_slot (ht, item);
206 }
207 else
208 return (void *) slot;
209}
210
211void *
212hash_delete (struct hash_table *ht, const void *item)
213{
214 void **slot = hash_find_slot (ht, item);
215 return hash_delete_at (ht, slot);
216}
217
218void *
219hash_delete_at (struct hash_table *ht, const void *slot)
220{
221 void *item = *(void **) slot;
222 if (!HASH_VACANT (item))
223 {
224 *(void const **) slot = hash_deleted_item;
225 ht->ht_fill--;
226 return item;
227 }
228 else
229 return 0;
230}
231
232void
233hash_free_items (struct hash_table *ht)
234{
235 void **vec = ht->ht_vec;
236 void **end = &vec[ht->ht_size];
237 for (; vec < end; vec++)
238 {
239 void *item = *vec;
240 if (!HASH_VACANT (item))
241 free (item);
242 *vec = 0;
243 }
244 ht->ht_fill = 0;
245 ht->ht_empty_slots = ht->ht_size;
246}
247
248#ifdef CONFIG_WITH_ALLOC_CACHES
249void
250hash_free_items_cached (struct hash_table *ht, struct alloccache *cache)
251{
252 void **vec = ht->ht_vec;
253 void **end = &vec[ht->ht_size];
254 for (; vec < end; vec++)
255 {
256 void *item = *vec;
257 if (!HASH_VACANT (item))
258 alloccache_free (cache, item);
259 *vec = 0;
260 }
261 ht->ht_fill = 0;
262 ht->ht_empty_slots = ht->ht_size;
263}
264#endif /* CONFIG_WITH_ALLOC_CACHES */
265
266void
267hash_delete_items (struct hash_table *ht)
268{
269 void **vec = ht->ht_vec;
270 void **end = &vec[ht->ht_size];
271 for (; vec < end; vec++)
272 *vec = 0;
273 ht->ht_fill = 0;
274 ht->ht_collisions = 0;
275 ht->ht_lookups = 0;
276 ht->ht_rehashes = 0;
277 ht->ht_empty_slots = ht->ht_size;
278}
279
280void
281hash_free (struct hash_table *ht, int free_items)
282{
283 if (free_items)
284 hash_free_items (ht);
285 else
286 {
287 ht->ht_fill = 0;
288 ht->ht_empty_slots = ht->ht_size;
289 }
290 free (ht->ht_vec);
291 ht->ht_vec = 0;
292 ht->ht_capacity = 0;
293}
294
295#ifdef CONFIG_WITH_ALLOC_CACHES
296void
297hash_free_cached (struct hash_table *ht, int free_items, struct alloccache *cache)
298{
299 if (free_items)
300 hash_free_items_cached (ht, cache);
301 else
302 {
303 ht->ht_fill = 0;
304 ht->ht_empty_slots = ht->ht_size;
305 }
306 free (ht->ht_vec);
307 ht->ht_vec = 0;
308 ht->ht_capacity = 0;
309}
310#endif /* CONFIG_WITH_ALLOC_CACHES */
311
312void
313hash_map (struct hash_table *ht, hash_map_func_t map)
314{
315 void **slot;
316 void **end = &ht->ht_vec[ht->ht_size];
317
318 for (slot = ht->ht_vec; slot < end; slot++)
319 {
320 if (!HASH_VACANT (*slot))
321 (*map) (*slot);
322 }
323}
324
325void
326hash_map_arg (struct hash_table *ht, hash_map_arg_func_t map, void *arg)
327{
328 void **slot;
329 void **end = &ht->ht_vec[ht->ht_size];
330
331 for (slot = ht->ht_vec; slot < end; slot++)
332 {
333 if (!HASH_VACANT (*slot))
334 (*map) (*slot, arg);
335 }
336}
337
338/* Double the size of the hash table in the event of overflow... */
339
340static void
341hash_rehash (struct hash_table *ht)
342{
343 unsigned long old_ht_size = ht->ht_size;
344 void **old_vec = ht->ht_vec;
345 void **ovp;
346
347 if (ht->ht_fill >= ht->ht_capacity)
348 {
349 ht->ht_size *= 2;
350 ht->ht_capacity = ht->ht_size - (ht->ht_size >> 4);
351 }
352 ht->ht_rehashes++;
353 ht->ht_vec = (void **) CALLOC (struct token *, ht->ht_size);
354
355 for (ovp = old_vec; ovp < &old_vec[old_ht_size]; ovp++)
356 {
357 if (! HASH_VACANT (*ovp))
358 {
359 void **slot = hash_find_slot (ht, *ovp);
360 *slot = *ovp;
361 }
362 }
363 ht->ht_empty_slots = ht->ht_size - ht->ht_fill;
364 free (old_vec);
365}
366
367void
368hash_print_stats (struct hash_table *ht, FILE *out_FILE)
369{
370 /* GKM FIXME: honor NO_FLOAT */
371 fprintf (out_FILE, _("Load=%ld/%ld=%.0f%%, "), ht->ht_fill, ht->ht_size,
372 100.0 * (double) ht->ht_fill / (double) ht->ht_size);
373 fprintf (out_FILE, _("Rehash=%d, "), ht->ht_rehashes);
374 fprintf (out_FILE, _("Collisions=%ld/%ld=%.0f%%"), ht->ht_collisions, ht->ht_lookups,
375 (ht->ht_lookups
376 ? (100.0 * (double) ht->ht_collisions / (double) ht->ht_lookups)
377 : 0));
378}
379
380/* Dump all items into a NULL-terminated vector. Use the
381 user-supplied vector, or malloc one. */
382
383void **
384hash_dump (struct hash_table *ht, void **vector_0, qsort_cmp_t compare)
385{
386 void **vector;
387 void **slot;
388 void **end = &ht->ht_vec[ht->ht_size];
389
390 if (vector_0 == 0)
391 vector_0 = MALLOC (void *, ht->ht_fill + 1);
392 vector = vector_0;
393
394 for (slot = ht->ht_vec; slot < end; slot++)
395 if (!HASH_VACANT (*slot))
396 *vector++ = *slot;
397 *vector = 0;
398
399 if (compare)
400 qsort (vector_0, ht->ht_fill, sizeof (void *), compare);
401 return vector_0;
402}
403
404/* Round a given number up to the nearest power of 2. */
405
406static unsigned long
407round_up_2 (unsigned long n)
408{
409 n |= (n >> 1);
410 n |= (n >> 2);
411 n |= (n >> 4);
412 n |= (n >> 8);
413 n |= (n >> 16);
414
415#if !defined(HAVE_LIMITS_H) || ULONG_MAX > 4294967295
416 /* We only need this on systems where unsigned long is >32 bits. */
417 n |= (n >> 32);
418#endif
419
420 return n + 1;
421}
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