VirtualBox

source: kBuild/trunk/src/kmk/hash.h@ 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: 11.6 KB
Line 
1/* hash.h -- decls for hash table
2Copyright (C) 1995, 1999, 2002 Free Software Foundation, Inc.
3Written 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#ifndef _hash_h_
20#define _hash_h_
21
22#include <stdio.h>
23#include <ctype.h>
24
25#if defined __cplusplus || (defined __STDC__ && __STDC__) || defined WINDOWS32
26# if !defined __GLIBC__ || !defined __P
27# undef __P
28# define __P(protos) protos
29# endif
30#else /* Not C++ or ANSI C. */
31# undef __P
32# define __P(protos) ()
33/* We can get away without defining `const' here only because in this file
34 it is used only inside the prototype for `fnmatch', which is elided in
35 non-ANSI C where `const' is problematical. */
36#endif /* C++ or ANSI C. */
37
38typedef unsigned long (*hash_func_t) __P((void const *key));
39typedef int (*hash_cmp_func_t) __P((void const *x, void const *y));
40typedef void (*hash_map_func_t) __P((void const *item));
41typedef void (*hash_map_arg_func_t) __P((void const *item, void *arg));
42
43struct hash_table
44{
45 void **ht_vec;
46 unsigned long ht_size; /* total number of slots (power of 2) */
47 unsigned long ht_capacity; /* usable slots, limited by loading-factor */
48 unsigned long ht_fill; /* items in table */
49 unsigned long ht_empty_slots; /* empty slots not including deleted slots */
50 unsigned long ht_collisions; /* # of failed calls to comparison function */
51 unsigned long ht_lookups; /* # of queries */
52 unsigned int ht_rehashes; /* # of times we've expanded table */
53 hash_func_t ht_hash_1; /* primary hash function */
54 hash_func_t ht_hash_2; /* secondary hash function */
55 hash_cmp_func_t ht_compare; /* comparison function */
56};
57
58typedef int (*qsort_cmp_t) __P((void const *, void const *));
59
60void hash_init __P((struct hash_table *ht, unsigned long size,
61 hash_func_t hash_1, hash_func_t hash_2, hash_cmp_func_t hash_cmp));
62void hash_load __P((struct hash_table *ht, void *item_table,
63 unsigned long cardinality, unsigned long size));
64void **hash_find_slot __P((struct hash_table *ht, void const *key));
65#ifdef CONFIG_WITH_VALUE_LENGTH
66void **hash_find_slot_prehashed __P((struct hash_table *ht, const void *key,
67 unsigned int hash_1, unsigned int hash_2));
68#endif
69void *hash_find_item __P((struct hash_table *ht, void const *key));
70void *hash_insert __P((struct hash_table *ht, const void *item));
71void *hash_insert_at __P((struct hash_table *ht, const void *item, void const *slot));
72void *hash_delete __P((struct hash_table *ht, void const *item));
73void *hash_delete_at __P((struct hash_table *ht, void const *slot));
74void hash_delete_items __P((struct hash_table *ht));
75void hash_free_items __P((struct hash_table *ht));
76void hash_free __P((struct hash_table *ht, int free_items));
77#ifdef CONFIG_WITH_ALLOC_CACHES
78void hash_free_items_cached __P((struct hash_table *ht, struct alloccache *cache));
79void hash_free_cached __P((struct hash_table *ht, int free_items, struct alloccache *cache));
80#endif
81void hash_map __P((struct hash_table *ht, hash_map_func_t map));
82void hash_map_arg __P((struct hash_table *ht, hash_map_arg_func_t map, void *arg));
83void hash_print_stats __P((struct hash_table *ht, FILE *out_FILE));
84void **hash_dump __P((struct hash_table *ht, void **vector_0, qsort_cmp_t compare));
85
86extern void *hash_deleted_item;
87#define HASH_VACANT(item) ((item) == 0 || (void *) (item) == hash_deleted_item)
88
89
90
91/* hash and comparison macros for case-sensitive string keys. */
92
93#ifndef CONFIG_WITH_OPTIMIZATION_HACKS
94#define STRING_HASH_1(KEY, RESULT) do { \
95 unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
96 while (*++_key_) \
97 (RESULT) += (*_key_ << (_key_[1] & 0xf)); \
98} while (0)
99#else /* CONFIG_WITH_OPTIMIZATION_HACKS */
100# define STRING_HASH_1(KEY, RESULT) do { \
101 unsigned char const *_key_ = (unsigned char const *) (KEY); \
102 unsigned int _ch_ = *_key_; \
103 while (_ch_) \
104 { \
105 unsigned char _ch2_ = *++_key_; \
106 (RESULT) += (_ch_ << (_ch2_ & 0xf)); \
107 _ch_ = _ch2_; \
108 } \
109} while (0)
110#endif /* CONFIG_WITH_OPTIMIZATION_HACKS */
111#define return_STRING_HASH_1(KEY) do { \
112 unsigned long _result_ = 0; \
113 STRING_HASH_1 ((KEY), _result_); \
114 return _result_; \
115} while (0)
116
117#ifndef CONFIG_WITH_OPTIMIZATION_HACKS
118#define STRING_HASH_2(KEY, RESULT) do { \
119 unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
120 while (*++_key_) \
121 (RESULT) += (*_key_ << (_key_[1] & 0x7)); \
122} while (0)
123#else /* CONFIG_WITH_OPTIMIZATION_HACKS */
124# define STRING_HASH_2(KEY, RESULT) do { \
125 unsigned char const *_key_ = (unsigned char const *) (KEY); \
126 unsigned int _ch_ = *_key_; \
127 while (_ch_) \
128 { \
129 unsigned char _ch2_ = *++_key_; \
130 (RESULT) += (_ch_ << (_ch2_ & 0x7)); \
131 _ch_ = _ch2_; \
132 } \
133} while (0)
134#endif /* CONFIG_WITH_OPTIMIZATION_HACKS */
135#define return_STRING_HASH_2(KEY) do { \
136 unsigned long _result_ = 0; \
137 STRING_HASH_2 ((KEY), _result_); \
138 return _result_; \
139} while (0)
140
141#define STRING_COMPARE(X, Y, RESULT) do { \
142 RESULT = strcmp ((X), (Y)); \
143} while (0)
144#define return_STRING_COMPARE(X, Y) do { \
145 return strcmp ((X), (Y)); \
146} while (0)
147
148#ifndef CONFIG_WITH_OPTIMIZATION_HACKS
149#define STRING_N_HASH_1(KEY, N, RESULT) do { \
150 unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
151 int _n_ = (N); \
152 if (_n_) \
153 while (--_n_ && *++_key_) \
154 (RESULT) += (*_key_ << (_key_[1] & 0xf)); \
155 (RESULT) += *++_key_; \
156} while (0)
157#else /* CONFIG_WITH_OPTIMIZATION_HACKS */
158# define STRING_N_HASH_1(KEY, N, RESULT) do { \
159 unsigned char const *_key_ = (unsigned char const *) (KEY); \
160 unsigned int _ch_ = *_key_; \
161 int _n_ = (N); \
162 if (_n_) \
163 { \
164 for (;;) \
165 { \
166 unsigned char _ch2_; \
167 if (!--_n_) \
168 { \
169 (RESULT) += _ch_; \
170 break; \
171 } \
172 _ch2_ = *++_key_; \
173 (RESULT) += (_ch_ << (_ch2_ & 0xf)); \
174 _ch_ = _ch2_; \
175 if (!_ch_) break; \
176 } \
177 } \
178 else \
179 (RESULT) += _ch_; \
180} while (0)
181#endif /* CONFIG_WITH_OPTIMIZATION_HACKS */
182#define return_STRING_N_HASH_1(KEY, N) do { \
183 unsigned long _result_ = 0; \
184 STRING_N_HASH_1 ((KEY), (N), _result_); \
185 return _result_; \
186} while (0)
187
188#ifndef CONFIG_WITH_OPTIMIZATION_HACKS
189#define STRING_N_HASH_2(KEY, N, RESULT) do { \
190 unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
191 int _n_ = (N); \
192 if (_n_) \
193 while (--_n_ && *++_key_) \
194 (RESULT) += (*_key_ << (_key_[1] & 0x7)); \
195 (RESULT) += *++_key_; \
196} while (0)
197#else /* CONFIG_WITH_OPTIMIZATION_HACKS */
198# define STRING_N_HASH_2(KEY, N, RESULT) do { \
199 unsigned char const *_key_ = (unsigned char const *) (KEY); \
200 unsigned int _ch_ = *_key_; \
201 int _n_ = (N); \
202 if (_n_) \
203 { \
204 for (;;) \
205 { \
206 unsigned char _ch2_; \
207 if (!--_n_) \
208 { \
209 (RESULT) += _ch_; \
210 break; \
211 } \
212 _ch2_ = *++_key_; \
213 (RESULT) += (_ch_ << (_ch2_ & 0x7)); \
214 _ch_ = _ch2_; \
215 if (!_ch_) break; \
216 } \
217 } \
218 else \
219 (RESULT) += _ch_; \
220} while (0)
221#endif /* CONFIG_WITH_OPTIMIZATION_HACKS */
222#define return_STRING_N_HASH_2(KEY, N) do { \
223 unsigned long _result_ = 0; \
224 STRING_N_HASH_2 ((KEY), (N), _result_); \
225 return _result_; \
226} while (0)
227
228#define STRING_N_COMPARE(X, Y, N, RESULT) do { \
229 RESULT = strncmp ((X), (Y), (N)); \
230} while (0)
231#define return_STRING_N_COMPARE(X, Y, N) do { \
232 return strncmp ((X), (Y), (N)); \
233} while (0)
234
235#ifdef HAVE_CASE_INSENSITIVE_FS
236
237/* hash and comparison macros for case-insensitive string _key_s. */
238
239#if 1 /*ndef CONFIG_WITH_OPTIMIZATION_HACKS - testme */
240#define ISTRING_HASH_1(KEY, RESULT) do { \
241 unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
242 while (*++_key_) \
243 (RESULT) += ((isupper (*_key_) ? tolower (*_key_) : *_key_) << (_key_[1] & 0xf)); \
244} while (0)
245#else /* CONFIG_WITH_OPTIMIZATION_HACKS */
246#define ISTRING_HASH_1(KEY, RESULT) do { \
247 unsigned char const *_key_ = (unsigned char const *) (KEY); \
248 unsigned int _ch_ = *_key_;
249 while (_ch_) \
250 { \
251 unsigned _ch2_ = *++_key_; \
252 (RESULT) += ((isupper (_ch_) ? tolower (_ch_) : _ch_) << (_ch2_ & 0xf)); \
253 _ch_ = _ch2_; \
254 } \
255} while (0)
256#endif /* CONFIG_WITH_OPTIMIZATION_HACKS */
257#define return_ISTRING_HASH_1(KEY) do { \
258 unsigned long _result_ = 0; \
259 ISTRING_HASH_1 ((KEY), _result_); \
260 return _result_; \
261} while (0)
262
263#if 1 /* ndef CONFIG_WITH_OPTIMIZATION_HACKS - testme */
264#define ISTRING_HASH_2(KEY, RESULT) do { \
265 unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \
266 while (*++_key_) \
267 (RESULT) += ((isupper (*_key_) ? tolower (*_key_) : *_key_) << (_key_[1] & 0x7)); \
268} while (0)
269#else /* CONFIG_WITH_OPTIMIZATION_HACKS */
270#define ISTRING_HASH_2(KEY, RESULT) do { \
271 unsigned char const *_key_ = (unsigned char const *) (KEY); \
272 unsigned int _ch_ = *_key_;
273 while (_ch_) \
274 { \
275 unsigned _ch2_ = *++_key_; \
276 (RESULT) += ((isupper (_ch_) ? tolower (_ch_) : _ch_) << (_ch2_ & 0x7)); \
277 _ch_ = _ch2_; \
278 } \
279} while (0)
280#endif /* CONFIG_WITH_OPTIMIZATION_HACKS */
281#define return_ISTRING_HASH_2(KEY) do { \
282 unsigned long _result_ = 0; \
283 ISTRING_HASH_2 ((KEY), _result_); \
284 return _result_; \
285} while (0)
286
287#define ISTRING_COMPARE(X, Y, RESULT) do { \
288 RESULT = strcasecmp ((X), (Y)); \
289} while (0)
290#define return_ISTRING_COMPARE(X, Y) do { \
291 return strcasecmp ((X), (Y)); \
292} while (0)
293
294#else
295
296#define ISTRING_HASH_1(KEY, RESULT) STRING_HASH_1 ((KEY), (RESULT))
297#define return_ISTRING_HASH_1(KEY) return_STRING_HASH_1 (KEY)
298
299#define ISTRING_HASH_2(KEY, RESULT) STRING_HASH_2 ((KEY), (RESULT))
300#define return_ISTRING_HASH_2(KEY) return_STRING_HASH_2 (KEY)
301
302#define ISTRING_COMPARE(X, Y, RESULT) STRING_COMPARE ((X), (Y), (RESULT))
303#define return_ISTRING_COMPARE(X, Y) return_STRING_COMPARE ((X), (Y))
304
305#endif
306
307/* hash and comparison macros for integer _key_s. */
308
309#define INTEGER_HASH_1(KEY, RESULT) do { \
310 (RESULT) += ((unsigned long)(KEY)); \
311} while (0)
312#define return_INTEGER_HASH_1(KEY) do { \
313 unsigned long _result_ = 0; \
314 INTEGER_HASH_1 ((KEY), _result_); \
315 return _result_; \
316} while (0)
317
318#define INTEGER_HASH_2(KEY, RESULT) do { \
319 (RESULT) += ~((unsigned long)(KEY)); \
320} while (0)
321#define return_INTEGER_HASH_2(KEY) do { \
322 unsigned long _result_ = 0; \
323 INTEGER_HASH_2 ((KEY), _result_); \
324 return _result_; \
325} while (0)
326
327#define INTEGER_COMPARE(X, Y, RESULT) do { \
328 (RESULT) = X - Y; \
329} while (0)
330#define return_INTEGER_COMPARE(X, Y) do { \
331 int _result_; \
332 INTEGER_COMPARE (X, Y, _result_); \
333 return _result_; \
334} while (0)
335
336/* hash and comparison macros for address keys. */
337
338#define ADDRESS_HASH_1(KEY, RESULT) INTEGER_HASH_1 (((unsigned long)(KEY)) >> 3, (RESULT))
339#define ADDRESS_HASH_2(KEY, RESULT) INTEGER_HASH_2 (((unsigned long)(KEY)) >> 3, (RESULT))
340#define ADDRESS_COMPARE(X, Y, RESULT) INTEGER_COMPARE ((X), (Y), (RESULT))
341#define return_ADDRESS_HASH_1(KEY) return_INTEGER_HASH_1 (((unsigned long)(KEY)) >> 3)
342#define return_ADDRESS_HASH_2(KEY) return_INTEGER_HASH_2 (((unsigned long)(KEY)) >> 3)
343#define return_ADDRESS_COMPARE(X, Y) return_INTEGER_COMPARE ((X), (Y))
344
345#endif /* not _hash_h_ */
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