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
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
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
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.
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"
77 /*@constant int NULLFACTOR; @*/
80 typedef Handle CharIndex
;
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
;
105 lsymbol_fromString (cstring s
)
107 if (cstring_isUndefined (s
))
109 return lsymbol_undefined
;
113 return (lsymbol_fromChars (cstring_toCharsSafe (s
)));
118 lsymbol_fromChars (/*@temp@*/ char *name
)
121 long unsigned hashValue
;
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
);
139 ss
= hashArray
[hashValue
]; /* start of hash chain */
141 if (ss
== lsymbol_undefined
)
143 /* hash not initialized */
144 ss
= AllocEntry (name
, hashValue
);
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
);
170 cstring
lsymbol_toString (lsymbol ss
)
172 return (cstring_fromChars (lsymbol_toChars (ss
)));
176 lsymbol_toCharsSafe (lsymbol ss
)
178 char *ret
= lsymbol_toChars (ss
);
182 ret
= mstring_create (0);
188 char *lsymbol_toChars (lsymbol ss
)
190 if (lsymbol_isDefined (ss
))
194 llcontbug (message ("lsymbol_toChars: invalid lsymbol: %d", ss
));
198 llassert (Entry
!= NULL
);
199 llassert (CharString
!= NULL
);
201 return &CharString
[Entry
[ss
].i
];
210 AllocCharSpace (unsigned newSize
)
212 llassert (newSize
> MaxChar
);
214 CharString
= (char *) drealloc ((void *) CharString
, newSize
* sizeof (*CharString
));
220 AllocChar (/*@unique@*/ char *name
)
227 namelength
= size_toInt (strlen (name
));
231 if ((unused
+ namelength
+ NULLFACTOR
) > size
)
234 size
= INITCHARSTRING
;
236 size
= (unsigned) (DELTACHARSTRING
* size
);
238 AllocCharSpace (size
);
241 llassert (CharString
!= NULL
);
244 strcpy (&CharString
[unused
], name
);
245 unused
+= namelength
;
246 CharString
[unused
] = '\0';
254 AllocEntrySpace (unsigned newSize
)
256 llassert (newSize
> MaxEntry
);
258 /* Casts mess up checking here. */
260 Entry
= (StringEntry
*) drealloc ((void *) Entry
, newSize
* sizeof (*Entry
));
263 if (MaxEntry
== 0) MaxEntry
= 1;
265 FreeEntry
= MaxEntry
;
270 static lsymbol
AllocEntry (char *name
, long unsigned hashValue
)
277 if ((retVal
= FreeEntry
) == size
)
281 size
= INITSTRINGENTRY
;
285 size
= (unsigned) (DELTASTRINGENTRY
* size
);
288 AllocEntrySpace (size
);
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
);
305 lsymbol_initMod (void)
306 /*@globals undef CharString, undef Entry; @*/
310 if (hashArray
!= NULL
)
315 hashArray
= (lsymbol
*) dmalloc (HASHSIZE
* sizeof (*hashArray
));
317 for (i
= 0; i
< HASHSIZE
; i
++)
319 hashArray
[i
] = lsymbol_undefined
;
328 CharString
= (char *) 0;
329 Entry
= (StringEntry
*) 0;
335 lsymbol_destroyMod (void)
336 /*@globals killed Entry, killed CharString, killed hashArray@*/
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