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 ** 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 (/*@only@*/ cstring key, int val)
54 hentry h = (hentry) dmalloc (sizeof (*h));
58 llassert (val != HBUCKET_DNE);
62 static void hentry_free (/*@only@*/ hentry h)
64 cstring_free (h->key);
69 hbucket_isEmpty (hbucket h)
71 return (h == hbucket_undefined || h->size == 0);
75 hbucket_unparse (hbucket h)
77 cstring s = cstring_undefined;
79 if (!hbucket_isNull (h))
83 for (i = 0; i < h->size; i++)
85 s = message ("%q %s:%d", s, h->entries[i]->key, h->entries[i]->val);
93 hbucket_single (/*@only@*/ hentry e)
95 hbucket h = (hbucket) dmalloc (sizeof (*h));
98 h->nspace = HBUCKET_BASESIZE - 1;
99 h->entries = (hentry *) dmalloc (HBUCKET_BASESIZE * sizeof (*h->entries));
106 hbucket_grow (/*@notnull@*/ hbucket h)
111 h->nspace += HBUCKET_BASESIZE;
113 newentries = (hentry *)
114 dmalloc ((h->size + HBUCKET_BASESIZE) * sizeof (*newentries));
116 for (i = 0; i < h->size; i++)
118 newentries[i] = h->entries[i];
122 h->entries = newentries;
124 } /*@=compmempass@*/ /* Spurious warnings reported - shouldn't need this */
126 static int hbucket_lookup (hbucket p_h, cstring p_key);
128 static bool hbucket_contains (hbucket p_h, cstring p_key) /*@*/ {
129 return (hbucket_lookup (p_h, p_key) != HBUCKET_DNE);
133 ** bizarre duplicate behaviour
134 ** required for duplicate entries
138 hbucket_add (/*@notnull@*/ hbucket h, /*@only@*/ hentry e)
140 int exloc = hbucket_lookup (h, e->key);
142 llassert (exloc == HBUCKET_DNE);
149 llassert (e->val != HBUCKET_DNE);
150 h->entries[h->size] = e;
156 hbucket_ncollisions (hbucket h)
158 if (!hbucket_isNull (h) && (h->size > 1))
159 return (h->size - 1);
165 hbucket_lookup (hbucket h, cstring key)
167 if (!hbucket_isNull (h))
171 for (i = 0; i < h->size; i++)
173 if (cstring_equal (h->entries[i]->key, key))
175 return h->entries[i]->val;
184 void hbucket_free (/*@only@*/ hbucket h)
186 if (!hbucket_isNull (h))
189 /* evans 2002-07-12: added this to free entries (splint warning had been suppressed) */
190 for (i = 0; i < h->size; i++)
192 hentry_free (h->entries[i]);
201 cstringTable_free (/*@only@*/ cstringTable h)
205 llassert (cstringTable_isDefined (h));
207 for (i = 0; i < h->size; i++)
209 hbucket_free (h->buckets[i]);
217 cstringTable_countCollisions (cstringTable h)
222 llassert (cstringTable_isDefined (h));
224 for (i = 0; i < h->size; i++)
226 nc += hbucket_ncollisions (h->buckets[i]);
234 cstringTable_countEmpty (cstringTable h)
239 llassert (cstringTable_isDefined (h));
241 for (i = 0; i < h->size; i++)
243 if (hbucket_isEmpty (h->buckets[i]))
253 ** hash function snarfed from quake/hash.c Hash_String
254 ** by Stephen Harrison
258 cstringTable_hashValue (/*@notnull@*/ cstringTable h, cstring key)
261 unsigned int hash_value = 0;
263 for (p = cstring_toCharsSafe (key); *p != '\0'; p++)
265 hash_value = (hash_value << 1) ^ g_randomNumbers[*p % 256];
268 return (hash_value % h->size);
271 static /*@exposed@*/ hbucket
272 cstringTable_hash (/*@notnull@*/ cstringTable h, cstring key)
274 return h->buckets[cstringTable_hashValue (h, key)];
278 /*@only@*/ cstringTable
279 cstringTable_create (unsigned long size)
282 cstringTable h = (cstringTable) dmalloc (sizeof (*h));
286 h->buckets = (hbucket *) dmalloc (sizeof (*h->buckets) * size);
289 for (i = 0; i < size; i++)
291 h->buckets[i] = hbucket_undefined;
297 cstring cstringTable_unparse (cstringTable h)
299 cstring res = cstring_newEmpty ();
302 if (cstringTable_isDefined (h))
304 for (i = 0; i < h->size; i++)
306 hbucket hb = h->buckets[i];
310 res = message ("%q%wl. %q\n", res, i, hbucket_unparse (hb));
314 res = message ("%qsize: %wl, collisions: %d, empty: %d",
317 cstringTable_countCollisions (h),
318 cstringTable_countEmpty (h));
323 res = cstring_makeLiteral ("< empty cstring table >");
331 cstringTable_stats (cstringTable h)
333 llassert (cstringTable_isDefined (h));
334 return (message ("size: %wl, collisions: %d, empty: %d\n",
335 h->size, cstringTable_countCollisions (h),
336 cstringTable_countEmpty (h)));
340 cstringTable_rehash (/*@notnull@*/ cstringTable h)
343 ** rehashing based (loosely) on code by Steve Harrison
347 /* Fix provided by Thomas Mertz (int -> unsigned long), 21 Apr 2004 */
348 unsigned long oldsize = h->size;
349 unsigned long newsize = 1 + ((oldsize * 26244) / 10000); /* 26244 = 162^2 */
350 hbucket *oldbuckets = h->buckets;
354 h->buckets = (hbucket *) dmalloc (sizeof (*h->buckets) * newsize);
357 for (i = 0; i < newsize; i++)
359 h->buckets[i] = hbucket_undefined;
363 for (i = 0; i < oldsize; i++)
365 hbucket bucket = oldbuckets[i];
367 oldbuckets[i] = NULL;
369 if (!hbucket_isNull (bucket))
373 for (j = 0; j < bucket->size; j++)
375 cstringTable_addEntry (h, bucket->entries[j]);
379 ** evans 2001-03-24: new memory leak detected by Splint
380 ** after I fixed the checkCompletelyDestroyed.
383 sfree (bucket->entries);
392 cstringTable_addEntry (/*@notnull@*/ cstringTable h, /*@only@*/ hentry e)
394 unsigned int hindex = cstringTable_hashValue (h, e->key);
398 ** hbucket hb = h->buckets[hindex];
399 ** instead reveals a bug I don't want to deal with right now!
402 if (hbucket_isNull (h->buckets[hindex]))
404 h->buckets[hindex] = hbucket_single (e);
409 if (hbucket_contains (h->buckets[hindex], e->key)) {
412 ("cstringTable: Attempt to add duplicate entry: %s "
413 "[previous value %d, new value %d]",
414 e->key, cstringTable_lookup (h, e->key), e->val));
419 hbucket_add (h->buckets[hindex], e);
425 cstringTable_insert (cstringTable h, cstring key, int value)
427 unsigned long hindex;
431 llassert (cstringTable_isDefined (h));
435 if (h->nentries * 162 > h->size * 100)
437 cstringTable_rehash (h);
440 hindex = cstringTable_hashValue (h, key);
441 e = hentry_create (key, value);
443 hb = h->buckets[hindex];
445 if (hbucket_isNull (hb))
447 h->buckets[hindex] = hbucket_single (e);
451 llassert (!hbucket_contains (hb, e->key));
457 cstringTable_lookup (cstringTable h, cstring key)
460 llassert (cstringTable_isDefined (h));
462 hb = cstringTable_hash (h, key);
463 return (hbucket_lookup (hb, key));
467 cstringTable_update (cstringTable h, cstring key, int newval)
471 llassert (cstringTable_isDefined (h));
473 hb = cstringTable_hash (h, key);
475 if (!hbucket_isNull (hb))
479 for (i = 0; i < hb->size; i++)
481 if (cstring_equal (hb->entries[i]->key, key))
483 hb->entries[i]->val = newval;
489 llbug (message ("cstringTable_update: %s not found", key));
493 ** This is needed if oldkey is going to be released.
497 cstringTable_replaceKey (cstringTable h, cstring oldkey, /*@only@*/ cstring newkey)
500 llassert (cstringTable_isDefined (h));
502 hb = cstringTable_hash (h, oldkey);
503 llassert (cstring_equal (oldkey, newkey));
505 if (!hbucket_isNull (hb))
509 for (i = 0; i < hb->size; i++)
511 if (cstring_equal (hb->entries[i]->key, oldkey))
513 hb->entries[i]->key = newkey;
519 llbug (message ("cstringTable_replaceKey: %s not found", oldkey));
523 cstringTable_remove (cstringTable h, cstring key)
527 llassert (cstringTable_isDefined (h));
528 hb = cstringTable_hash (h, key);
530 if (!hbucket_isNull (hb))
534 for (i = 0; i < hb->size; i++)
536 if (cstring_equal (hb->entries[i]->key, key))
538 if (i < hb->size - 1)
540 hb->entries[i] = hb->entries[hb->size - 1];
549 llbug (message ("cstringTable_removeKey: %s not found", key));