2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2002 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: splint@cs.virginia.edu
21 ** To report a bug: splint-bug@cs.virginia.edu
22 ** For more information: http://www.splint.org
27 ** Since hsearch defined in <search.h> only allows one hash table,
28 ** I'll implement my own.
30 ** Try to find a decent hash function, etc. later...
32 ** cstringTable is from string -> unsigned
36 # include "splintMacros.nf"
38 # include "randomNumbers.h"
40 /*@constant null hbucket hbucket_undefined; @*/
41 # define hbucket_undefined 0
44 cstringTable_addEntry (/*@notnull@*/ cstringTable p_h, /*@only@*/ hentry p_e)
47 static /*@nullwhentrue@*/ bool hbucket_isNull (/*@null@*/ hbucket h)
49 return (h == hbucket_undefined);
52 static hentry hentry_create (/*@keep@*/ cstring key, int val)
54 hentry h = (hentry) dmalloc (sizeof (*h));
58 llassert (val != HBUCKET_DNE); /*@i523 way bogus! */
63 static void hentry_free (/*@only@*/ hentry h)
65 cstring_free (h->key);
70 hbucket_isEmpty (hbucket h)
72 return (h == hbucket_undefined || h->size == 0);
76 hbucket_unparse (hbucket h)
78 cstring s = cstring_undefined;
80 if (!hbucket_isNull (h))
84 for (i = 0; i < h->size; i++)
86 /*drl bee: si*/ s = message ("%q %s:%d", s, h->entries[i]->key, h->entries[i]->val);
94 hbucket_single (/*@only@*/ hentry e)
96 hbucket h = (hbucket) dmalloc (sizeof (*h));
99 h->nspace = HBUCKET_BASESIZE - 1;
100 h->entries = (hentry *) dmalloc (HBUCKET_BASESIZE * sizeof (*h->entries));
101 /*drl bee: dm*/ h->entries[0] = e;
107 hbucket_grow (/*@notnull@*/ hbucket h)
112 h->nspace += HBUCKET_BASESIZE;
114 newentries = (hentry *)
115 dmalloc ((h->size + HBUCKET_BASESIZE) * sizeof (*newentries));
117 for (i = 0; i < h->size; i++)
120 /*drl bee: si*/ newentries[i] = h->entries[i];
123 /*@i32@*/ sfree (h->entries);
124 h->entries = newentries;
127 static int hbucket_lookup (hbucket p_h, cstring p_key);
129 static bool hbucket_contains (hbucket p_h, cstring p_key) /*@*/ {
130 return (hbucket_lookup (p_h, p_key) != HBUCKET_DNE);
134 ** bizarre duplicate behaviour
135 ** required for duplicate entries
139 hbucket_add (/*@notnull@*/ hbucket h, /*@only@*/ hentry e)
141 int exloc = hbucket_lookup (h, e->key);
143 llassert (exloc == HBUCKET_DNE);
150 llassert (e->val != HBUCKET_DNE);
151 /*drl bee: si*/ h->entries[h->size] = e;
157 hbucket_ncollisions (hbucket h)
159 if (!hbucket_isNull (h) && (h->size > 1))
160 return (h->size - 1);
166 hbucket_lookup (hbucket h, cstring key)
168 if (!hbucket_isNull (h))
172 for (i = 0; i < h->size; i++)
174 /*drl bee: si*/ if (cstring_equal (h->entries[i]->key, key))
176 return h->entries[i]->val;
185 void hbucket_free (/*@only@*/ hbucket h)
187 if (!hbucket_isNull (h))
189 /*@i32@*/ sfree (h->entries);
195 cstringTable_free (/*@only@*/ cstringTable h)
199 llassert (cstringTable_isDefined (h));
201 for (i = 0; i < h->size; i++)
203 /*drl bee: si*/ hbucket_free (h->buckets[i]);
211 cstringTable_countCollisions (cstringTable h)
216 llassert (cstringTable_isDefined (h));
218 for (i = 0; i < h->size; i++)
220 /*drl bee: si*/ nc += hbucket_ncollisions (h->buckets[i]);
228 cstringTable_countEmpty (cstringTable h)
233 llassert (cstringTable_isDefined (h));
235 for (i = 0; i < h->size; i++)
237 /*drl bee: si*/ if (hbucket_isEmpty (h->buckets[i]))
247 ** hash function snarfed from quake/hash.c Hash_String
248 ** by Stephen Harrison
252 cstringTable_hashValue (/*@notnull@*/ cstringTable h, cstring key)
255 unsigned int hash_value = 0;
257 for (p = cstring_toCharsSafe (key); *p != '\0'; p++)
259 /*drl bee: nm*/ hash_value = (hash_value << 1) ^ g_randomNumbers[*p % 256];
262 return (hash_value % h->size);
265 static /*@exposed@*/ hbucket
266 cstringTable_hash (/*@notnull@*/ cstringTable h, cstring key)
268 return h->buckets[cstringTable_hashValue (h, key)];
272 /*@only@*/ cstringTable
273 cstringTable_create (int size)
276 cstringTable h = (cstringTable) dmalloc (sizeof (*h));
280 h->buckets = (hbucket *) dmalloc (sizeof (*h->buckets) * size);
283 for (i = 0; i < size; i++)
285 /*drl bee: dm*/ h->buckets[i] = hbucket_undefined;
291 cstring cstringTable_unparse (cstringTable h)
293 cstring res = cstring_newEmpty ();
296 if (cstringTable_isDefined (h))
298 for (i = 0; i < h->size; i++)
300 /*drl bee: si*/ hbucket hb = h->buckets[i];
304 res = message ("%q%d. %q\n", res, i, hbucket_unparse (hb));
308 res = message ("%qsize: %d, collisions: %d, empty: %d",
311 cstringTable_countCollisions (h),
312 cstringTable_countEmpty (h));
317 res = cstring_makeLiteral ("< empty cstring table >");
325 cstringTable_stats (cstringTable h)
327 llassert (cstringTable_isDefined (h));
328 return (message ("size: %d, collisions: %d, empty: %d\n",
329 h->size, cstringTable_countCollisions (h),
330 cstringTable_countEmpty (h)));
334 cstringTable_rehash (/*@notnull@*/ cstringTable h)
337 ** rehashing based (loosely) on code by Steve Harrison
341 int oldsize = h->size;
342 int newsize = 1 + ((oldsize * 26244) / 10000); /* 26244 = 162^2 */
343 hbucket *oldbuckets = h->buckets;
347 h->buckets = (hbucket *) dmalloc (sizeof (*h->buckets) * newsize);
350 for (i = 0; i < newsize; i++)
352 /*drl bee: dm*/ h->buckets[i] = hbucket_undefined;
356 for (i = 0; i < oldsize; i++)
358 /*drl bee: dm*/ hbucket bucket = oldbuckets[i];
360 /*drl bee: dm*/ oldbuckets[i] = NULL;
362 if (!hbucket_isNull (bucket))
366 for (j = 0; j < bucket->size; j++)
368 /*drl bee: si*/ cstringTable_addEntry (h, bucket->entries[j]);
372 ** evans 2001-03-24: new memory leak detected by Splint
373 ** after I fixed the checkCompletelyDestroyed.
376 sfree (bucket->entries);
385 cstringTable_addEntry (/*@notnull@*/ cstringTable h, /*@only@*/ hentry e)
387 unsigned int hindex = cstringTable_hashValue (h, e->key);
391 ** hbucket hb = h->buckets[hindex];
392 ** instead reveals a bug I don't want to deal with right now!
395 /*drl bee: si*/ if (hbucket_isNull (h->buckets[hindex]))
397 /*drl bee: si*/ h->buckets[hindex] = hbucket_single (e);
402 if (hbucket_contains (h->buckets[hindex], e->key)) {
405 ("cstringTable: Attempt to add duplicate entry: %s "
406 "[previous value %d, new value %d]",
407 e->key, cstringTable_lookup (h, e->key), e->val));
412 hbucket_add (h->buckets[hindex], e);
418 cstringTable_insert (cstringTable h, cstring key, int value)
424 llassert (cstringTable_isDefined (h));
428 if (h->nentries * 162 > h->size * 100)
430 cstringTable_rehash (h);
433 e = hentry_create (key, value);
434 hindex = cstringTable_hashValue (h, key);
436 /*drl bee: si*/ hb = h->buckets[hindex];
438 if (hbucket_isNull (hb))
440 /*drl bee: si*/ h->buckets[hindex] = hbucket_single (e);
444 llassert (!hbucket_contains (hb, e->key));
450 cstringTable_lookup (cstringTable h, cstring key)
453 llassert (cstringTable_isDefined (h));
455 hb = cstringTable_hash (h, key);
456 return (hbucket_lookup (hb, key));
460 cstringTable_update (cstringTable h, cstring key, int newval)
464 llassert (cstringTable_isDefined (h));
466 hb = cstringTable_hash (h, key);
468 if (!hbucket_isNull (hb))
472 for (i = 0; i < hb->size; i++)
474 /*drl bee: si*/ if (cstring_equal (hb->entries[i]->key, key))
476 /*drl bee: si*/ hb->entries[i]->val = newval;
482 llbug (message ("cstringTable_update: %s not found", key));
486 ** This is needed if oldkey is going to be released.
490 cstringTable_replaceKey (cstringTable h, cstring oldkey, /*@only@*/ cstring newkey)
493 llassert (cstringTable_isDefined (h));
495 hb = cstringTable_hash (h, oldkey);
496 llassert (cstring_equal (oldkey, newkey));
498 if (!hbucket_isNull (hb))
502 for (i = 0; i < hb->size; i++)
504 /*drl bee: si*/ if (cstring_equal (hb->entries[i]->key, oldkey))
506 /*drl bee: si*/ hb->entries[i]->key = newkey;
512 llbug (message ("cstringTable_replaceKey: %s not found", oldkey));
516 cstringTable_remove (cstringTable h, cstring key)
520 llassert (cstringTable_isDefined (h));
521 hb = cstringTable_hash (h, key);
523 if (!hbucket_isNull (hb))
527 for (i = 0; i < hb->size; i++)
529 /*drl bee: si*/ if (cstring_equal (hb->entries[i]->key, key))
531 if (i < hb->size - 1)
534 /*drl bee: si*/ hb->entries[i] = hb->entries[hb->size - 1];
543 llbug (message ("cstringTable_removeKey: %s not found", key));