Moved OS dependent constants from constants.h to osd.h
[splint-patched.git] / src / lsymbol.c
blob2d1fe015aa92142238a83c453e9d192c7c3541f8
1 /*
2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2003 University of Virginia,
4 ** Massachusetts Institute of Technology
5 **
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.
10 **
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.
15 **
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
25 ** lsymbol.c
27 ** String manager
29 ** This module implements an abstraction for efficiently managing
30 ** string comparisons. It alloctes and manages a string context,
31 ** which consists of three major data structures:
32 ** - a StringEntry table
33 ** - a character-string table
34 ** - a hash table
36 ** A StringEntry table is made of of StringEntries. StringEntries
37 ** are linked in hash-chains to support fast lookup. Every
38 ** allocated StringEntry corresponds to a unique character-string
39 ** in the character-string table. StringEntries can be referenced
40 ** via Symbol values.
42 ** A character-string table is composed of character-strings. A
43 ** character-string is a variable length byte array that is always
44 ** null-terminated ('\0').
46 ** A hash table manages the start of each hash-list of StringEntries.
47 ** StringEntries are entered into the hash-list by hashing on its
48 ** character-string representation.
50 ** This module provides routines for retrieving a unique Symbol for a
51 ** given character-string, and returning the character-string given its
52 ** corresponding Symbol. An item is allocated in both tables whenever a
53 ** new character-string is encountered, otherwise the Symbol for the
54 ** character-string is found and returned.
56 ** AUTHORS:
58 ** Shota Aki
60 ** MODIFICATION HISTORY:
62 ** {0} Aki at Digital -- 89.08.07 -- original
63 ** {1} Aki at Digital -- 89.11.13 -- context switchable
64 ** {2} Aki at Digital -- 89.11.28 -- removed use of TABLES interface
65 ** {3} Aki at Digital -- 89.11.29 -- moved to RC
66 ** {4} Aki at Digital -- 90.04.10 -- support primary-context
67 ** {5} McKeeman at Digital -- 90.05.08 -- C to Larch SL
68 ** {6} Wild at Digital -- 91.06.26 -- Update copyright notice.
69 ** {n} Who at Where -- yy.mm.dd -- what
72 # include "splintMacros.nf"
73 # include "basic.h"
75 /*@+ignorequals@*/
77 /*@constant int NULLFACTOR; @*/
78 # define NULLFACTOR 1
80 typedef Handle CharIndex;
82 typedef struct
84 lsymbol HashNext;
85 CharIndex i;
86 } StringEntry;
88 static void AllocCharSpace (unsigned p_newSize) /*@modifies internalState@*/ ;
89 static CharIndex AllocChar (/*@unique@*/ char *p_name) /*@modifies internalState@*/ ;
90 static void AllocEntrySpace (unsigned p_newSize) /*@modifies internalState@*/ ;
91 static lsymbol AllocEntry (char *p_name, long unsigned p_hashValue)
92 /*@modifies internalState@*/ ;
94 static /*@only@*/ /*@null@*/ lsymbol *hashArray = NULL;
96 static long unsigned MaxChar;
97 static CharIndex FreeChar;
98 static /*@only@*/ /*@null@*/ char *CharString;
100 static long unsigned MaxEntry;
101 static lsymbol FreeEntry;
102 static /*@only@*/ /*@null@*/ StringEntry *Entry;
104 lsymbol
105 lsymbol_fromString (cstring s)
107 if (cstring_isUndefined (s))
109 return lsymbol_undefined;
111 else
113 return (lsymbol_fromChars (cstring_toCharsSafe (s)));
117 lsymbol
118 lsymbol_fromChars (/*@temp@*/ char *name)
120 lsymbol ss;
121 long unsigned hashValue;
122 unsigned h = 0;
123 char *p = name;
125 while (*p != '\0')
127 h = (h << 1) + (unsigned) (*p++);
130 hashValue = h & HASHMASK;
132 if (hashArray == NULL) /* evs - was MaxIndex == 0 */
134 /* nothing initialized */
135 ss = AllocEntry (name, hashValue);
137 else
139 ss = hashArray[hashValue]; /* start of hash chain */
141 if (ss == lsymbol_undefined)
143 /* hash not initialized */
144 ss = AllocEntry (name, hashValue);
146 else
149 * Traverse hash-chain. Loop terminates when
150 * a match is found or end of chain is encountered.
153 llassert (Entry != NULL);
154 llassert (CharString != NULL);
156 while (strcmp (&CharString[Entry[ss].i], name) != 0)
158 if (lsymbol_undefined == (ss = Entry[ss].HashNext))
160 ss = AllocEntry (name, hashValue);
161 break;
167 return ss;
170 cstring lsymbol_toString (lsymbol ss)
172 return (cstring_fromChars (lsymbol_toChars (ss)));
175 char *
176 lsymbol_toCharsSafe (lsymbol ss)
178 char *ret = lsymbol_toChars (ss);
180 if (ret == NULL)
182 ret = mstring_create (0);
185 return ret;
188 char *lsymbol_toChars (lsymbol ss)
190 if (lsymbol_isDefined (ss))
192 if (ss >= FreeEntry)
194 llcontbug (message ("lsymbol_toChars: invalid lsymbol: %d", ss));
195 return NULL;
198 llassert (Entry != NULL);
199 llassert (CharString != NULL);
201 return &CharString[Entry[ss].i];
203 else
205 return NULL;
209 static void
210 AllocCharSpace (unsigned newSize)
212 llassert (newSize > MaxChar);
214 CharString = (char *) drealloc ((void *) CharString, newSize * sizeof (*CharString));
215 MaxChar = newSize;
216 /*@-compdef@*/
217 } /*@=compdef@*/
219 static CharIndex
220 AllocChar (/*@unique@*/ char *name)
222 int namelength;
223 CharIndex retVal;
224 long unsigned size;
225 CharIndex unused;
227 namelength = size_toInt (strlen (name));
228 unused = FreeChar;
229 size = MaxChar;
231 if ((unused + namelength + NULLFACTOR) > size)
233 if (size == 0)
234 size = INITCHARSTRING;
235 else
236 size = (unsigned) (DELTACHARSTRING * size);
238 AllocCharSpace (size);
241 llassert (CharString != NULL);
243 retVal = unused;
244 strcpy (&CharString[unused], name);
245 unused += namelength;
246 CharString[unused] = '\0';
247 unused += 1;
249 FreeChar = unused;
250 return retVal;
253 static void
254 AllocEntrySpace (unsigned newSize)
256 llassert (newSize > MaxEntry);
258 /* Casts mess up checking here. */
259 /*@-mustfree@*/
260 Entry = (StringEntry *) drealloc ((void *) Entry, newSize * sizeof (*Entry));
261 /*@=mustfree@*/
263 if (MaxEntry == 0) MaxEntry = 1;
265 FreeEntry = MaxEntry;
266 MaxEntry = newSize;
267 /*@-compdef@*/
268 } /*@=compdef@*/
270 static lsymbol AllocEntry (char *name, long unsigned hashValue)
272 lsymbol retVal;
273 long unsigned size;
275 size = MaxEntry;
277 if ((retVal = FreeEntry) == size)
279 if (size == 0)
281 size = INITSTRINGENTRY;
283 else
285 size = (unsigned) (DELTASTRINGENTRY * size);
288 AllocEntrySpace (size);
289 retVal = FreeEntry;
292 FreeEntry = retVal + 1;
294 llassert (hashArray != NULL);
295 llassert (Entry != NULL);
297 Entry[retVal].HashNext = hashArray[hashValue];
298 hashArray[hashValue] = retVal;
299 Entry[retVal].i = AllocChar (name);
301 return retVal;
304 void
305 lsymbol_initMod (void)
306 /*@globals undef CharString, undef Entry; @*/
308 int i;
310 if (hashArray != NULL)
312 sfree (hashArray);
315 hashArray = (lsymbol *) dmalloc (HASHSIZE * sizeof (*hashArray));
317 for (i = 0; i < HASHSIZE; i++)
319 hashArray[i] = lsymbol_undefined;
322 MaxChar = 0;
323 MaxEntry = 0;
325 FreeChar = 0;
326 FreeEntry = 0;
328 CharString = (char *) 0;
329 Entry = (StringEntry *) 0;
330 /*@-compdef@*/
332 /*@=compdef@*/
334 void
335 lsymbol_destroyMod (void)
336 /*@globals killed Entry, killed CharString, killed hashArray@*/
338 sfree (Entry);
339 sfree (CharString);
340 sfree (hashArray);
343 void
344 lsymbol_printStats (void)
346 /* only for debugging */
347 printf ("Number of lsymbols generated = %d\n", (int) FreeEntry);
351 ** note lsymbol_setbool, etc. defined in abstract.c