00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072 # include "lclintMacros.nf"
00073 # include "basic.h"
00074
00075
00076
00077
00078 # define NULLFACTOR 1
00079
00080 typedef Handle CharIndex;
00081
00082 typedef struct
00083 {
00084 lsymbol HashNext;
00085 CharIndex i;
00086 } StringEntry;
00087
00088 static void AllocCharSpace (unsigned p_newSize) ;
00089 static CharIndex AllocChar ( char *p_name) ;
00090 static void AllocEntrySpace (unsigned p_newSize) ;
00091 static lsymbol AllocEntry (char *p_name, long unsigned p_hashValue)
00092 ;
00093
00094 static lsymbol *hashArray = NULL;
00095
00096 static long unsigned MaxChar;
00097 static CharIndex FreeChar;
00098 static char *CharString;
00099
00100 static long unsigned MaxEntry;
00101 static lsymbol FreeEntry;
00102 static StringEntry *Entry;
00103
00104 lsymbol
00105 lsymbol_fromString (cstring s)
00106 {
00107 if (cstring_isUndefined (s))
00108 {
00109 return lsymbol_undefined;
00110 }
00111 else
00112 {
00113 return (lsymbol_fromChars (cstring_toCharsSafe (s)));
00114 }
00115 }
00116
00117 lsymbol
00118 lsymbol_fromChars ( char *name)
00119 {
00120 lsymbol ss;
00121 long unsigned hashValue;
00122 unsigned h = 0;
00123 char *p = name;
00124
00125 while (*p != '\0')
00126 {
00127 h = (h << 1) + (unsigned) (*p++);
00128 }
00129
00130 hashValue = h & HASHMASK;
00131
00132 if (hashArray == NULL)
00133 {
00134
00135 ss = AllocEntry (name, hashValue);
00136 }
00137 else
00138 {
00139 ss = hashArray[hashValue];
00140
00141 if (ss == lsymbol_undefined)
00142 {
00143
00144 ss = AllocEntry (name, hashValue);
00145 }
00146 else
00147 {
00148
00149
00150
00151
00152
00153 llassert (Entry != NULL);
00154 llassert (CharString != NULL);
00155
00156 while (strcmp (&CharString[Entry[ss].i], name) != 0)
00157 {
00158 if (lsymbol_undefined == (ss = Entry[ss].HashNext))
00159 {
00160 ss = AllocEntry (name, hashValue);
00161 break;
00162 }
00163 }
00164 }
00165 }
00166
00167 return ss;
00168 }
00169
00170 cstring lsymbol_toString (lsymbol ss)
00171 {
00172 return (cstring_fromChars (lsymbol_toChars (ss)));
00173 }
00174
00175 char *
00176 lsymbol_toCharsSafe (lsymbol ss)
00177 {
00178 char *ret = lsymbol_toChars (ss);
00179
00180 if (ret == NULL)
00181 {
00182 ret = mstring_create (0);
00183 }
00184
00185 return ret;
00186 }
00187
00188 char *lsymbol_toChars (lsymbol ss)
00189 {
00190 if (lsymbol_isDefined (ss))
00191 {
00192 if (ss >= FreeEntry)
00193 {
00194 llcontbug (message ("lsymbol_toChars: invalid lsymbol: %d", ss));
00195 return NULL;
00196 }
00197
00198 llassert (Entry != NULL);
00199 llassert (CharString != NULL);
00200
00201 return &CharString[Entry[ss].i];
00202 }
00203 else
00204 {
00205 return NULL;
00206 }
00207 }
00208
00209 static void
00210 AllocCharSpace (unsigned newSize)
00211 {
00212 llassert (newSize > MaxChar);
00213
00214 CharString = (char *) drealloc ((void *) CharString, newSize * sizeof (*CharString));
00215 MaxChar = newSize;
00216
00217 }
00218
00219 static CharIndex
00220 AllocChar ( char *name)
00221 {
00222 int namelength;
00223 CharIndex retVal;
00224 long unsigned size;
00225 CharIndex unused;
00226
00227 namelength = size_toInt (strlen (name));
00228 unused = FreeChar;
00229 size = MaxChar;
00230
00231 if ((unused + namelength + NULLFACTOR) > size)
00232 {
00233 if (size == 0)
00234 size = INITCHARSTRING;
00235 else
00236 size = (unsigned) (DELTACHARSTRING * size);
00237
00238 AllocCharSpace (size);
00239 }
00240
00241 llassert (CharString != NULL);
00242
00243 retVal = unused;
00244 strcpy (&CharString[unused], name);
00245 unused += namelength;
00246 CharString[unused] = '\0';
00247 unused += 1;
00248
00249 FreeChar = unused;
00250 return retVal;
00251 }
00252
00253 static void
00254 AllocEntrySpace (unsigned newSize)
00255 {
00256 llassert (newSize > MaxEntry);
00257
00258
00259
00260 Entry = (StringEntry *) drealloc ((void *) Entry, newSize * sizeof (*Entry));
00261
00262
00263 if (MaxEntry == 0) MaxEntry = 1;
00264
00265 FreeEntry = MaxEntry;
00266 MaxEntry = newSize;
00267
00268 }
00269
00270 static lsymbol AllocEntry (char *name, long unsigned hashValue)
00271 {
00272 lsymbol retVal;
00273 long unsigned size;
00274
00275 size = MaxEntry;
00276
00277 if ((retVal = FreeEntry) == size)
00278 {
00279 if (size == 0)
00280 {
00281 size = INITSTRINGENTRY;
00282 }
00283 else
00284 {
00285 size = (unsigned) (DELTASTRINGENTRY * size);
00286 }
00287
00288 AllocEntrySpace (size);
00289 retVal = FreeEntry;
00290 }
00291
00292 FreeEntry = retVal + 1;
00293
00294 llassert (hashArray != NULL);
00295 llassert (Entry != NULL);
00296
00297 Entry[retVal].HashNext = hashArray[hashValue];
00298 hashArray[hashValue] = retVal;
00299 Entry[retVal].i = AllocChar (name);
00300
00301 return retVal;
00302 }
00303
00304 void
00305 lsymbol_initMod (void)
00306
00307 {
00308 int i;
00309
00310 if (hashArray != NULL)
00311 {
00312 sfree (hashArray);
00313 }
00314
00315 hashArray = (lsymbol *) dmalloc (HASHSIZE * sizeof (*hashArray));
00316
00317 for (i = 0; i < HASHSIZE; i++)
00318 {
00319 hashArray[i] = lsymbol_undefined;
00320 }
00321
00322 MaxChar = 0;
00323 MaxEntry = 0;
00324
00325 FreeChar = 0;
00326 FreeEntry = 0;
00327
00328 CharString = (char *) 0;
00329 Entry = (StringEntry *) 0;
00330
00331 }
00332
00333
00334 void
00335 lsymbol_destroyMod (void)
00336
00337 {
00338 sfree (Entry);
00339 sfree (CharString);
00340 sfree (hashArray);
00341 }
00342
00343 void
00344 lsymbol_printStats (void)
00345 {
00346
00347 printf ("Number of lsymbols generated = %d\n", (int) FreeEntry);
00348 }
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360