VirtualBox

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

最後變更 在這個檔案從1858是 1858,由 bird 提交於 16 年 前

kmk: Added hash_find_slot_prehashed for the benefit of the strcache and includedep.

  • 屬性 svn:eol-style 設為 native
檔案大小: 9.4 KB
 
1/* hash.c -- hash table maintenance
2 Copyright (C) 1995, 1999, 2002 Free Software Foundation, Inc.
3 Written by Greg McGary <gkm@gnu.org> <greg@mcgary.org>
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
248void
249hash_delete_items (struct hash_table *ht)
250{
251 void **vec = ht->ht_vec;
252 void **end = &vec[ht->ht_size];
253 for (; vec < end; vec++)
254 *vec = 0;
255 ht->ht_fill = 0;
256 ht->ht_collisions = 0;
257 ht->ht_lookups = 0;
258 ht->ht_rehashes = 0;
259 ht->ht_empty_slots = ht->ht_size;
260}
261
262void
263hash_free (struct hash_table *ht, int free_items)
264{
265 if (free_items)
266 hash_free_items (ht);
267 else
268 {
269 ht->ht_fill = 0;
270 ht->ht_empty_slots = ht->ht_size;
271 }
272 free (ht->ht_vec);
273 ht->ht_vec = 0;
274 ht->ht_capacity = 0;
275}
276
277void
278hash_map (struct hash_table *ht, hash_map_func_t map)
279{
280 void **slot;
281 void **end = &ht->ht_vec[ht->ht_size];
282
283 for (slot = ht->ht_vec; slot < end; slot++)
284 {
285 if (!HASH_VACANT (*slot))
286 (*map) (*slot);
287 }
288}
289
290void
291hash_map_arg (struct hash_table *ht, hash_map_arg_func_t map, void *arg)
292{
293 void **slot;
294 void **end = &ht->ht_vec[ht->ht_size];
295
296 for (slot = ht->ht_vec; slot < end; slot++)
297 {
298 if (!HASH_VACANT (*slot))
299 (*map) (*slot, arg);
300 }
301}
302
303/* Double the size of the hash table in the event of overflow... */
304
305static void
306hash_rehash (struct hash_table *ht)
307{
308 unsigned long old_ht_size = ht->ht_size;
309 void **old_vec = ht->ht_vec;
310 void **ovp;
311
312 if (ht->ht_fill >= ht->ht_capacity)
313 {
314 ht->ht_size *= 2;
315 ht->ht_capacity = ht->ht_size - (ht->ht_size >> 4);
316 }
317 ht->ht_rehashes++;
318 ht->ht_vec = (void **) CALLOC (struct token *, ht->ht_size);
319
320 for (ovp = old_vec; ovp < &old_vec[old_ht_size]; ovp++)
321 {
322 if (! HASH_VACANT (*ovp))
323 {
324 void **slot = hash_find_slot (ht, *ovp);
325 *slot = *ovp;
326 }
327 }
328 ht->ht_empty_slots = ht->ht_size - ht->ht_fill;
329 free (old_vec);
330}
331
332void
333hash_print_stats (struct hash_table *ht, FILE *out_FILE)
334{
335 /* GKM FIXME: honor NO_FLOAT */
336 fprintf (out_FILE, _("Load=%ld/%ld=%.0f%%, "), ht->ht_fill, ht->ht_size,
337 100.0 * (double) ht->ht_fill / (double) ht->ht_size);
338 fprintf (out_FILE, _("Rehash=%d, "), ht->ht_rehashes);
339 fprintf (out_FILE, _("Collisions=%ld/%ld=%.0f%%"), ht->ht_collisions, ht->ht_lookups,
340 (ht->ht_lookups
341 ? (100.0 * (double) ht->ht_collisions / (double) ht->ht_lookups)
342 : 0));
343}
344
345/* Dump all items into a NULL-terminated vector. Use the
346 user-supplied vector, or malloc one. */
347
348void **
349hash_dump (struct hash_table *ht, void **vector_0, qsort_cmp_t compare)
350{
351 void **vector;
352 void **slot;
353 void **end = &ht->ht_vec[ht->ht_size];
354
355 if (vector_0 == 0)
356 vector_0 = MALLOC (void *, ht->ht_fill + 1);
357 vector = vector_0;
358
359 for (slot = ht->ht_vec; slot < end; slot++)
360 if (!HASH_VACANT (*slot))
361 *vector++ = *slot;
362 *vector = 0;
363
364 if (compare)
365 qsort (vector_0, ht->ht_fill, sizeof (void *), compare);
366 return vector_0;
367}
368
369/* Round a given number up to the nearest power of 2. */
370
371static unsigned long
372round_up_2 (unsigned long n)
373{
374 n |= (n >> 1);
375 n |= (n >> 2);
376 n |= (n >> 4);
377 n |= (n >> 8);
378 n |= (n >> 16);
379
380#if !defined(HAVE_LIMITS_H) || ULONG_MAX > 4294967295
381 /* We only need this on systems where unsigned long is >32 bits. */
382 n |= (n >> 32);
383#endif
384
385 return n + 1;
386}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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