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 */
64 StrPoolEntry* 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 StrPoolEntry */
75 /*****************************************************************************/
79 static StrPoolEntry* NewStrPoolEntry (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 StrPoolEntry* E = xmalloc (sizeof (StrPoolEntry) + Len);
88 /* Initialize the fields */
93 memcpy (E->S, S, Len+1);
95 /* Return the new entry */
101 /*****************************************************************************/
103 /*****************************************************************************/
107 StrPool* InitStrPool (StrPool* 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 DoneStrPool (StrPool* 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 StrPool* NewStrPool (void)
148 /* Allocate, initialize and return a new string pool */
150 /* Allocate memory, initialize and return it */
151 return InitStrPool (xmalloc (sizeof (StrPool)));
156 void FreeStrPool (StrPool* P)
157 /* Free a string pool */
159 /* Free all entries */
162 /* Free the string pool itself */
168 const char* SP_Get (const StrPool* P, unsigned Index)
169 /* Return a string from the pool. Index must exist, otherwise FAIL is called. */
171 /* Get the collection entry */
172 const StrPoolEntry* E = CollConstAt (&P->Entries, Index);
174 /* Return the string from the entry */
180 unsigned SP_Add (StrPool* 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 StrPoolEntry* 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 = NewStrPoolEntry (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 */
220 unsigned SP_AddBuf (StrPool* P, const void* Buffer, unsigned Size)
221 /* Add strings from a string buffer. Buffer must contain a list of zero
222 * terminated strings. These strings are added to the pool, starting with
223 * the current index. The number of strings added is returned.
224 * Beware: The function will do only loose range checking for the buffer
225 * limits, so a SEGV may occur if the last string in the buffer is not
226 * correctly terminated.
229 /* Cast the buffer pointer to something useful */
230 const char* Buf = Buffer;
232 /* Remember the current number of strings in the buffer. */
233 unsigned OldCount = SB_GetCount (P);
235 /* Add all strings from the buffer */
238 /* Add the next entry */
239 unsigned Id = SP_Add (P, Buf);
241 /* Get the entry from the id */
242 const StrPoolEntry* E = CollConstAt (&P->Entries, Id);
244 /* Skip this string */
249 /* Return the number of strings added */
250 return SB_GetCount (P) - OldCount;
255 void SP_Build (StrPool* P, const void* Buffer, unsigned Size)
256 /* Delete existing data and use the data from Buffer instead. */
258 /* Delete old data */
261 /* Add the buffer data */
262 SP_AddBuf (P, Buffer, Size);