1 /*****************************************************************************/
9 /* (C) 2003 Ullrich von Bassewitz */
10 /* Römerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
36 /* A string pool is used to store identifiers and other strings. Each string
37 * stored in the pool has a unique id, which may be used to access the string
38 * in the pool. Identical strings are only stored once in the pool and have
39 * identical ids. This means that instead of comparing strings, just the
40 * string pool ids must be compared.
56 /*****************************************************************************/
58 /*****************************************************************************/
62 /* A string pool entry */
63 struct StringPoolEntry {
64 StringPoolEntry* Next; /* Pointer to next entry in hash chain */
65 unsigned Hash; /* Full hash value */
66 unsigned Id; /* The numeric string id */
67 unsigned Len; /* Length of the string (excluding terminator) */
68 char S[1]; /* The string itself */
73 /*****************************************************************************/
74 /* struct StringPoolEntry */
75 /*****************************************************************************/
79 static StringPoolEntry* NewStringPoolEntry (const char* S, unsigned Hash, unsigned Id)
80 /* Create a new string pool entry and return it. */
82 /* Get the length of the string */
83 unsigned Len = strlen (S);
86 StringPoolEntry* E = xmalloc (sizeof (StringPoolEntry) + Len);
88 /* Initialize the fields */
93 memcpy (E->S, S, Len+1);
95 /* Return the new entry */
101 /*****************************************************************************/
103 /*****************************************************************************/
107 StringPool* InitStringPool (StringPool* P)
108 /* Initialize a string pool */
112 /* Initialize the fields */
113 for (I = 0; I < sizeof (P->Tab) / sizeof (P->Tab[0]); ++I) {
116 P->Entries = EmptyCollection;
119 /* Return a pointer to the initialized pool */
125 void DoneStringPool (StringPool* P)
126 /* Free the data of a string pool (but not the data itself) */
130 /* Free all entries and clear the entry collection */
131 for (I = 0; I < CollCount (&P->Entries); ++I) {
132 xfree (CollAtUnchecked (&P->Entries, I));
134 CollDeleteAll (&P->Entries);
136 /* Clear the hash table */
137 for (I = 0; I < sizeof (P->Tab) / sizeof (P->Tab[0]); ++I) {
147 StringPool* NewStringPool (void)
148 /* Allocate, initialize and return a new string pool */
150 /* Allocate memory, initialize and return it */
151 return InitStringPool (xmalloc (sizeof (StringPool)));
156 void FreeStringPool (StringPool* P)
157 /* Free a string pool */
159 /* Free all entries */
162 /* Free the string pool itself */
168 const char* SP_Get (const StringPool* P, unsigned Index)
169 /* Return a string from the pool. Index must exist, otherwise FAIL is called. */
171 /* Get the collection entry */
172 const StringPoolEntry* E = CollConstAt (&P->Entries, Index);
174 /* Return the string from the entry */
180 unsigned SP_Add (StringPool* P, const char* S)
181 /* Add a string to the buffer and return the index. If the string does already
182 * exist in the pool, SP_Add will just return the index of the existing string.
185 /* Calculate the string hash */
186 unsigned Hash = HashStr (S);
188 /* Calculate the reduced string hash */
189 unsigned RHash = Hash % (sizeof (P->Tab)/sizeof (P->Tab[0]));
191 /* Search for an existing entry */
192 StringPoolEntry* E = P->Tab[RHash];
194 if (E->Hash == Hash && strcmp (E->S, S) == 0) {
195 /* Found, return the id of the existing string */
201 /* We didn't find the entry, so create a new one */
202 E = NewStringPoolEntry (S, Hash, CollCount (&P->Entries));
204 /* Insert the new entry into the entry collection */
205 CollAppend (&P->Entries, E);
207 /* Insert the new entry into the hash table */
208 E->Next = P->Tab[RHash];
211 /* Add up the string size (plus terminator) */
212 P->TotalSize += E->Len + 1;
214 /* Return the id of the entry */