2 Copyright (C) 2000-2013 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 3, or (at your option) any
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; see the file COPYING3. If not see
16 <http://www.gnu.org/licenses/>.
18 In other words, you are welcome to use, share and improve this program.
19 You are forbidden to forbid anyone else to use, share and improve
20 what you give them. Help stamp out software-hoarding! */
26 /* The code below is a specialization of Vladimir Makarov's expandable
27 hash tables (see libiberty/hashtab.c). The abstraction penalty was
28 too high to continue using the generic form. This code knows
29 intrinsically how to calculate a hash value, and how to compare an
30 existing entry with a potential new one. */
32 static unsigned int calc_hash (const unsigned char *, size_t);
33 static void ht_expand (cpp_hash_table *);
34 static double approx_sqrt (double);
36 /* A deleted entry. */
37 #define DELETED ((hashnode) -1)
39 /* Calculate the hash of the string STR of length LEN. */
42 calc_hash (const unsigned char *str, size_t len)
48 r = HT_HASHSTEP (r, *str++);
50 return HT_HASHFINISH (r, len);
53 /* Initialize an identifier hashtable. */
56 ht_create (unsigned int order)
58 unsigned int nslots = 1 << order;
59 cpp_hash_table *table;
61 table = XCNEW (cpp_hash_table);
63 /* Strings need no alignment. */
64 _obstack_begin (&table->stack, 0, 0,
65 (void *(*) (long)) xmalloc,
66 (void (*) (void *)) free);
68 obstack_alignment_mask (&table->stack) = 0;
70 table->entries = XCNEWVEC (hashnode, nslots);
71 table->entries_owned = true;
72 table->nslots = nslots;
76 /* Frees all memory associated with a hash table. */
79 ht_destroy (cpp_hash_table *table)
81 obstack_free (&table->stack, NULL);
82 if (table->entries_owned)
83 free (table->entries);
87 /* Returns the hash entry for the a STR of length LEN. If that string
88 already exists in the table, returns the existing entry. If the
89 identifier hasn't been seen before, and INSERT is CPP_NO_INSERT,
90 returns NULL. Otherwise insert and returns a new entry. A new
91 string is allocated. */
93 ht_lookup (cpp_hash_table *table, const unsigned char *str, size_t len,
94 enum ht_lookup_option insert)
96 return ht_lookup_with_hash (table, str, len, calc_hash (str, len),
101 ht_lookup_with_hash (cpp_hash_table *table, const unsigned char *str,
102 size_t len, unsigned int hash,
103 enum ht_lookup_option insert)
107 unsigned int deleted_index = table->nslots;
111 sizemask = table->nslots - 1;
112 index = hash & sizemask;
115 node = table->entries[index];
120 deleted_index = index;
121 else if (node->hash_value == hash
122 && HT_LEN (node) == (unsigned int) len
123 && !memcmp (HT_STR (node), str, len))
126 /* hash2 must be odd, so we're guaranteed to visit every possible
127 location in the table during rehashing. */
128 hash2 = ((hash * 17) & sizemask) | 1;
133 index = (index + hash2) & sizemask;
134 node = table->entries[index];
140 if (deleted_index != table->nslots)
141 deleted_index = index;
143 else if (node->hash_value == hash
144 && HT_LEN (node) == (unsigned int) len
145 && !memcmp (HT_STR (node), str, len))
150 if (insert == HT_NO_INSERT)
153 /* We prefer to overwrite the first deleted slot we saw. */
154 if (deleted_index != table->nslots)
155 index = deleted_index;
157 node = (*table->alloc_node) (table);
158 table->entries[index] = node;
160 HT_LEN (node) = (unsigned int) len;
161 node->hash_value = hash;
163 if (table->alloc_subobject)
165 char *chars = (char *) table->alloc_subobject (len + 1);
166 memcpy (chars, str, len);
168 HT_STR (node) = (const unsigned char *) chars;
171 HT_STR (node) = (const unsigned char *) obstack_copy0 (&table->stack,
174 if (++table->nelements * 4 >= table->nslots * 3)
175 /* Must expand the string table. */
181 /* Double the size of a hash table, re-hashing existing entries. */
184 ht_expand (cpp_hash_table *table)
186 hashnode *nentries, *p, *limit;
187 unsigned int size, sizemask;
189 size = table->nslots * 2;
190 nentries = XCNEWVEC (hashnode, size);
194 limit = p + table->nslots;
196 if (*p && *p != DELETED)
198 unsigned int index, hash, hash2;
200 hash = (*p)->hash_value;
201 index = hash & sizemask;
205 hash2 = ((hash * 17) & sizemask) | 1;
208 index = (index + hash2) & sizemask;
210 while (nentries[index]);
212 nentries[index] = *p;
216 if (table->entries_owned)
217 free (table->entries);
218 table->entries_owned = true;
219 table->entries = nentries;
220 table->nslots = size;
223 /* For all nodes in TABLE, callback CB with parameters TABLE->PFILE,
226 ht_forall (cpp_hash_table *table, ht_cb cb, const void *v)
231 limit = p + table->nslots;
233 if (*p && *p != DELETED)
235 if ((*cb) (table->pfile, *p, v) == 0)
241 /* Like ht_forall, but a nonzero return from the callback means that
242 the entry should be removed from the table. */
244 ht_purge (cpp_hash_table *table, ht_cb cb, const void *v)
249 limit = p + table->nslots;
251 if (*p && *p != DELETED)
253 if ((*cb) (table->pfile, *p, v))
259 /* Restore the hash table. */
261 ht_load (cpp_hash_table *ht, hashnode *entries,
262 unsigned int nslots, unsigned int nelements,
265 if (ht->entries_owned)
267 ht->entries = entries;
269 ht->nelements = nelements;
270 ht->entries_owned = own;
273 /* Dump allocation statistics to stderr. */
276 ht_dump_statistics (cpp_hash_table *table)
278 size_t nelts, nids, overhead, headers;
279 size_t total_bytes, longest, deleted = 0;
280 double sum_of_squares, exp_len, exp_len2, exp2_len;
283 #define SCALE(x) ((unsigned long) ((x) < 1024*10 \
285 : ((x) < 1024*1024*10 \
287 : (x) / (1024*1024))))
288 #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
290 total_bytes = longest = sum_of_squares = nids = 0;
292 limit = p + table->nslots;
298 size_t n = HT_LEN (*p);
301 sum_of_squares += (double) n * n;
308 nelts = table->nelements;
309 overhead = obstack_memory_used (&table->stack) - total_bytes;
310 headers = table->nslots * sizeof (hashnode);
312 fprintf (stderr, "\nString pool\nentries\t\t%lu\n",
313 (unsigned long) nelts);
314 fprintf (stderr, "identifiers\t%lu (%.2f%%)\n",
315 (unsigned long) nids, nids * 100.0 / nelts);
316 fprintf (stderr, "slots\t\t%lu\n",
317 (unsigned long) table->nslots);
318 fprintf (stderr, "deleted\t\t%lu\n",
319 (unsigned long) deleted);
320 fprintf (stderr, "bytes\t\t%lu%c (%lu%c overhead)\n",
321 SCALE (total_bytes), LABEL (total_bytes),
322 SCALE (overhead), LABEL (overhead));
323 fprintf (stderr, "table size\t%lu%c\n",
324 SCALE (headers), LABEL (headers));
326 exp_len = (double)total_bytes / (double)nelts;
327 exp2_len = exp_len * exp_len;
328 exp_len2 = (double) sum_of_squares / (double) nelts;
330 fprintf (stderr, "coll/search\t%.4f\n",
331 (double) table->collisions / (double) table->searches);
332 fprintf (stderr, "ins/search\t%.4f\n",
333 (double) nelts / (double) table->searches);
334 fprintf (stderr, "avg. entry\t%.2f bytes (+/- %.2f)\n",
335 exp_len, approx_sqrt (exp_len2 - exp2_len));
336 fprintf (stderr, "longest entry\t%lu\n",
337 (unsigned long) longest);
342 /* Return the approximate positive square root of a number N. This is for
343 statistical reports, not code generation. */
345 approx_sqrt (double x)
357 d = (s * s - x) / (2 * s);