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 ghentry_free (/*@only@*/ ghentry ghe)
59 cstring_free (ghe->key);
60 /* can't free val contents */
66 ghbucket_isEmpty (ghbucket h)
68 return (h == ghbucket_undefined || h->size == 0);
71 int genericTable_size (genericTable h)
73 if (genericTable_isDefined (h)) {
81 static /*@unused@*/ cstring
82 ghbucket_unparse (ghbucket h)
84 cstring s = cstring_undefined;
86 if (!ghbucket_isNull (h))
90 for (i = 0; i < h->size; i++)
92 s = message ("%q %s", s, h->entries[i]->key);
100 static ghbucket ghbucket_single (/*@keep@*/ ghentry e)
102 ghbucket h = (ghbucket) dmalloc (sizeof (*h));
105 h->nspace = GHBUCKET_BASESIZE - 1;
106 h->entries = (ghentry *) dmalloc (GHBUCKET_BASESIZE * sizeof (*h->entries));
113 ghbucket_grow (/*@notnull@*/ ghbucket h)
118 h->nspace += GHBUCKET_BASESIZE;
120 newentries = (ghentry *)
121 dmalloc ((h->size + GHBUCKET_BASESIZE) * sizeof (*newentries));
123 for (i = 0; i < h->size; i++)
125 newentries[i] = h->entries[i];
129 h->entries = newentries;
131 } /*@=compmempass*/ /* Spurious warnings reported for h->entries */
133 static /*@null@*/ /*@exposed@*/ void *ghbucket_lookup (ghbucket p_h, cstring p_key);
136 ** bizarre duplicate behaviour
137 ** required for duplicate entries
141 ghbucket_add (/*@notnull@*/ ghbucket h, /*@only@*/ ghentry e)
143 void *exloc = ghbucket_lookup (h, e->key);
149 if (h->nspace == 0) {
153 h->entries[h->size] = e;
159 ghbucket_ncollisions (ghbucket h)
161 if (!ghbucket_isNull (h) && (h->size > 1))
162 return (h->size - 1);
167 /*@exposed@*/ /*@null@*/ void *
168 ghbucket_lookup (ghbucket h, cstring key)
170 if (!ghbucket_isNull (h))
174 for (i = 0; i < h->size; i++)
176 if (cstring_equal (h->entries[i]->key, key))
178 return h->entries[i]->val;
187 void ghbucket_free (/*@only@*/ ghbucket h)
189 if (!ghbucket_isNull (h))
193 for (i = 0; i < h->size; i++)
195 ghentry_free (h->entries[i]);
204 genericTable_free (/*@only@*/ genericTable h)
206 if (genericTable_isDefined (h))
210 for (i = 0; i < h->size; i++)
212 ghbucket_free (h->buckets[i]);
221 genericTable_countCollisions (genericTable h)
226 llassert (genericTable_isDefined (h));
228 for (i = 0; i < h->size; i++)
230 nc += ghbucket_ncollisions (h->buckets[i]);
238 genericTable_countEmpty (genericTable h)
243 llassert (genericTable_isDefined (h));
245 for (i = 0; i < h->size; i++)
247 if (ghbucket_isEmpty (h->buckets[i]))
257 ** hash function snarfed from quake/hash.c Hash_String
258 ** by Stephen Harrison
262 genericTable_hashValue (/*@notnull@*/ genericTable h, cstring key)
265 unsigned int hash_value = 0;
267 llassert (h->size != 0);
269 for (p = cstring_toCharsSafe (key); *p != '\0'; p++)
271 hash_value = (hash_value << 1) ^ g_randomNumbers[*p % 256];
274 return (hash_value % h->size);
277 static /*@exposed@*/ ghbucket
278 genericTable_hash (/*@notnull@*/ genericTable h, cstring key)
280 return h->buckets[genericTable_hashValue (h, key)];
284 /*@only@*/ genericTable
285 genericTable_create (int size)
288 genericTable h = (genericTable) dmalloc (sizeof (*h));
293 h->buckets = (ghbucket *) dmalloc (sizeof (*h->buckets) * size);
296 for (i = 0; i < size; i++)
298 h->buckets[i] = ghbucket_undefined;
306 static /*@unused@*/ void
307 genericTable_print (genericTable h)
311 if (genericTable_isDefined (h)) {
312 for (i = 0; i < h->size; i++)
314 ghbucket hb = h->buckets[i];
318 llmsg (message ("%d. %s\n", i, ghbucket_unparse (hb)));
322 llmsg (message ("size: %d, collisions: %d, empty: %d",
324 genericTable_countCollisions (h),
325 genericTable_countEmpty (h)));
327 llmsglit ("Empty hash table.");
334 genericTable_stats (genericTable h)
336 llassert (genericTable_isDefined (h));
337 return (message ("size: %d, collisions: %d, empty: %d\n",
338 h->size, genericTable_countCollisions (h),
339 genericTable_countEmpty (h)));
343 genericTable_addEntry (/*@notnull@*/ genericTable h, /*@only@*/ ghentry e)
345 unsigned int hindex = genericTable_hashValue (h, e->key);
348 ** ghbucket hb = h->buckets[hindex];
349 ** instead reveals a bug I don't want to deal with right now!
354 if (ghbucket_isNull (h->buckets[hindex]))
356 h->buckets[hindex] = ghbucket_single (e);
360 ghbucket_add (h->buckets[hindex], e);
365 genericTable_insert (genericTable h, cstring key, void *value)
371 llassert (genericTable_isDefined (h));
374 ** rehashing based (loosely) on code by Steve Harrison
377 if (h->nentries * 162 > h->size * 100)
380 int oldsize = h->size;
381 int newsize = 1 + ((oldsize * 26244) / 10000); /* 26244 = 162^2 */
382 ghbucket *oldbuckets = h->buckets;
384 DPRINTF (("Rehashing..."));
387 h->buckets = (ghbucket *) dmalloc (sizeof (*h->buckets) * newsize);
390 for (i = 0; i < newsize; i++)
392 h->buckets[i] = ghbucket_undefined;
396 for (i = 0; i < oldsize; i++)
398 ghbucket bucket = oldbuckets[i];
400 oldbuckets[i] = NULL;
402 if (!ghbucket_isNull (bucket))
406 for (j = 0; j < bucket->size; j++)
408 genericTable_addEntry (h, bucket->entries[j]);
411 sfree (bucket->entries); /* evans 2001-03-24: Splint caught this */
419 /* evans 2000-12-22: this was before the rehash! Lost an entry size! */
421 e = ghentry_create (key, value);
422 hindex = genericTable_hashValue (h, key);
423 hb = h->buckets[hindex];
425 if (ghbucket_isNull (hb))
427 h->buckets[hindex] = ghbucket_single (e);
431 ghbucket_add (hb, e);
435 /*@null@*/ /*@exposed@*/ void *
436 genericTable_lookup (genericTable h, cstring key)
440 llassert (genericTable_isDefined (h));
442 hb = genericTable_hash (h, key);
443 res = ghbucket_lookup (hb, key);
445 /* if (res == NULL) { DPRINTF (("Lookup not found: %s", key)); } */
450 genericTable_update (genericTable h, cstring key, /*@only@*/ void *newval)
454 llassert (genericTable_isDefined (h));
456 hb = genericTable_hash (h, key);
458 if (!ghbucket_isNull (hb))
462 for (i = 0; i < hb->size; i++)
464 if (cstring_equal (hb->entries[i]->key, key))
466 llassert (newval != NULL);
467 hb->entries[i]->val = newval;
473 llbug (message ("genericTable_update: %s not found", key));
477 genericTable_remove (genericTable h, cstring key)
481 llassert (genericTable_isDefined (h));
482 hb = genericTable_hash (h, key);
484 if (!ghbucket_isNull (hb))
488 for (i = 0; i < hb->size; i++)
490 if (cstring_equal (hb->entries[i]->key, key))
492 if (i < hb->size - 1)
494 hb->entries[i] = hb->entries[hb->size - 1];
503 llbug (message ("genericTable_removeKey: %s not found", key));
506 bool genericTable_contains (genericTable h, cstring key)
508 return (genericTable_lookup (h, key) != NULL);