2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2003 University of Virginia,
4 ** Massachusetts Institute of Technology
6 ** This program is free software; you can redistribute it and/or modify it
7 ** under the terms of the GNU General Public License as published by the
8 ** Free Software Foundation; either version 2 of the License, or (at your
9 ** option) any later version.
11 ** This program is distributed in the hope that it will be useful, but
12 ** WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ** General Public License for more details.
16 ** The GNU General Public License is available from http://www.gnu.org/ or
17 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 ** MA 02111-1307, USA.
20 ** For information on splint: info@splint.org
21 ** To report a bug: splint-bug@splint.org
22 ** For more information: http://www.splint.org
27 ** A genericTable is a mapping from string keys to void * objects.
28 ** We sacrific type checking here for code reuse.
31 # include "splintMacros.nf"
33 # include "randomNumbers.h"
35 /*@constant null ghbucket ghbucket_undefined; @*/
36 # define ghbucket_undefined 0
38 static /*@nullwhentrue@*/ bool ghbucket_isNull (/*@null@*/ ghbucket h)
40 return (h == ghbucket_undefined);
44 ghentry_create (/*@keep@*/ cstring key, /*@keep@*/ void *val)
46 ghentry h = (ghentry) dmalloc (sizeof (*h));
50 llassert (val != NULL);
57 ghbucket_isEmpty (ghbucket h)
59 return (h == ghbucket_undefined || h->size == 0);
62 int genericTable_size (genericTable h)
64 if (genericTable_isDefined (h)) {
72 static /*@unused@*/ cstring
73 ghbucket_unparse (ghbucket h)
75 cstring s = cstring_undefined;
77 if (!ghbucket_isNull (h))
81 for (i = 0; i < h->size; i++)
83 s = message ("%q %s", s, h->entries[i]->key);
91 static ghbucket ghbucket_single (/*@keep@*/ ghentry e)
93 ghbucket h = (ghbucket) dmalloc (sizeof (*h));
96 h->nspace = GHBUCKET_BASESIZE - 1;
97 h->entries = (ghentry *) dmalloc (GHBUCKET_BASESIZE * sizeof (*h->entries));
98 /*@i23@*/ h->entries[0] = e;
104 ghbucket_grow (/*@notnull@*/ ghbucket h)
109 h->nspace += GHBUCKET_BASESIZE;
111 newentries = (ghentry *)
112 dmalloc ((h->size + GHBUCKET_BASESIZE) * sizeof (*newentries));
114 for (i = 0; i < h->size; i++)
116 newentries[i] = h->entries[i];
119 /*@i32@*/ sfree (h->entries);
120 h->entries = newentries;
123 static /*@null@*/ /*@exposed@*/ void *ghbucket_lookup (ghbucket p_h, cstring p_key);
126 ** bizarre duplicate behaviour
127 ** required for duplicate entries
131 ghbucket_add (/*@notnull@*/ ghbucket h, /*@only@*/ ghentry e)
133 void *exloc = ghbucket_lookup (h, e->key);
139 if (h->nspace == 0) {
143 h->entries[h->size] = e;
149 ghbucket_ncollisions (ghbucket h)
151 if (!ghbucket_isNull (h) && (h->size > 1))
152 return (h->size - 1);
157 /*@exposed@*/ /*@null@*/ void *
158 ghbucket_lookup (ghbucket h, cstring key)
160 if (!ghbucket_isNull (h))
164 for (i = 0; i < h->size; i++)
166 if (cstring_equal (h->entries[i]->key, key))
168 return h->entries[i]->val;
177 void ghbucket_free (/*@only@*/ ghbucket h)
179 if (!ghbucket_isNull (h))
181 /*@i32@*/ sfree (h->entries);
187 genericTable_free (/*@only@*/ genericTable h)
189 if (genericTable_isDefined (h))
193 for (i = 0; i < h->size; i++)
195 ghbucket_free (h->buckets[i]);
204 genericTable_countCollisions (genericTable h)
209 llassert (genericTable_isDefined (h));
211 for (i = 0; i < h->size; i++)
213 nc += ghbucket_ncollisions (h->buckets[i]);
221 genericTable_countEmpty (genericTable h)
226 llassert (genericTable_isDefined (h));
228 for (i = 0; i < h->size; i++)
230 if (ghbucket_isEmpty (h->buckets[i]))
240 ** hash function snarfed from quake/hash.c Hash_String
241 ** by Stephen Harrison
245 genericTable_hashValue (/*@notnull@*/ genericTable h, cstring key)
248 unsigned int hash_value = 0;
250 llassert (h->size != 0);
252 for (p = cstring_toCharsSafe (key); *p != '\0'; p++)
254 hash_value = (hash_value << 1) ^ g_randomNumbers[*p % 256];
257 return (hash_value % h->size);
260 static /*@exposed@*/ ghbucket
261 genericTable_hash (/*@notnull@*/ genericTable h, cstring key)
263 return h->buckets[genericTable_hashValue (h, key)];
267 /*@only@*/ genericTable
268 genericTable_create (int size)
271 genericTable h = (genericTable) dmalloc (sizeof (*h));
276 h->buckets = (ghbucket *) dmalloc (sizeof (*h->buckets) * size);
279 for (i = 0; i < size; i++)
281 h->buckets[i] = ghbucket_undefined;
289 static /*@unused@*/ void
290 genericTable_print (genericTable h)
294 if (genericTable_isDefined (h)) {
295 for (i = 0; i < h->size; i++)
297 ghbucket hb = h->buckets[i];
301 llmsg (message ("%d. %s\n", i, ghbucket_unparse (hb)));
305 llmsg (message ("size: %d, collisions: %d, empty: %d",
307 genericTable_countCollisions (h),
308 genericTable_countEmpty (h)));
310 llmsglit ("Empty hash table.");
317 genericTable_stats (genericTable h)
319 llassert (genericTable_isDefined (h));
320 return (message ("size: %d, collisions: %d, empty: %d\n",
321 h->size, genericTable_countCollisions (h),
322 genericTable_countEmpty (h)));
326 genericTable_addEntry (/*@notnull@*/ genericTable h, /*@keep@*/ ghentry e)
328 unsigned int hindex = genericTable_hashValue (h, e->key);
331 ** ghbucket hb = h->buckets[hindex];
332 ** instead reveals a bug I don't want to deal with right now!
337 if (ghbucket_isNull (h->buckets[hindex]))
339 h->buckets[hindex] = ghbucket_single (e);
343 /*@i23@*/ ghbucket_add (h->buckets[hindex], e);
348 genericTable_insert (genericTable h, cstring key, void *value)
354 llassert (genericTable_isDefined (h));
357 ** rehashing based (loosely) on code by Steve Harrison
360 if (h->nentries * 162 > h->size * 100)
363 int oldsize = h->size;
364 int newsize = 1 + ((oldsize * 26244) / 10000); /* 26244 = 162^2 */
365 ghbucket *oldbuckets = h->buckets;
367 DPRINTF (("Rehashing..."));
370 h->buckets = (ghbucket *) dmalloc (sizeof (*h->buckets) * newsize);
373 for (i = 0; i < newsize; i++)
375 h->buckets[i] = ghbucket_undefined;
379 for (i = 0; i < oldsize; i++)
381 ghbucket bucket = oldbuckets[i];
383 oldbuckets[i] = NULL;
385 if (!ghbucket_isNull (bucket))
389 for (j = 0; j < bucket->size; j++)
391 genericTable_addEntry (h, bucket->entries[j]);
394 sfree (bucket->entries); /* evans 2001-03-24: Splint caught this */
402 /* evans 2000-12-22: this was before the rehash! Lost an entry size! */
404 e = ghentry_create (key, value);
405 hindex = genericTable_hashValue (h, key);
406 hb = h->buckets[hindex];
408 if (ghbucket_isNull (hb))
410 /*@i23@*/ h->buckets[hindex] = ghbucket_single (e);
414 ghbucket_add (hb, e);
418 /*@null@*/ /*@exposed@*/ void *
419 genericTable_lookup (genericTable h, cstring key)
423 llassert (genericTable_isDefined (h));
425 hb = genericTable_hash (h, key);
426 res = ghbucket_lookup (hb, key);
428 /* if (res == NULL) { DPRINTF (("Lookup not found: %s", key)); } */
433 genericTable_update (genericTable h, cstring key, /*@only@*/ void *newval)
437 llassert (genericTable_isDefined (h));
439 hb = genericTable_hash (h, key);
441 if (!ghbucket_isNull (hb))
445 for (i = 0; i < hb->size; i++)
447 if (cstring_equal (hb->entries[i]->key, key))
449 llassert (newval != NULL);
450 hb->entries[i]->val = newval;
456 llbug (message ("genericTable_update: %s not found", key));
460 genericTable_remove (genericTable h, cstring key)
464 llassert (genericTable_isDefined (h));
465 hb = genericTable_hash (h, key);
467 if (!ghbucket_isNull (hb))
471 for (i = 0; i < hb->size; i++)
473 if (cstring_equal (hb->entries[i]->key, key))
475 if (i < hb->size - 1)
477 hb->entries[i] = hb->entries[hb->size - 1];
486 llbug (message ("genericTable_removeKey: %s not found", key));
489 bool genericTable_contains (genericTable h, cstring key)
491 return (genericTable_lookup (h, key) != NULL);