1 /* C string table handling.
2 Copyright (C) 2000, 2001 Free Software Foundation, Inc.
3 Written by Ulrich Drepper <drepper@redhat.com>, 2000.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
28 #include <sys/param.h>
45 struct memoryblock *next;
53 struct memoryblock *memory;
62 /* Cache for the pagesize. We correct this value a bit so that `malloc'
63 is not allocating more than a page. */
67 extern void *xmalloc (size_t n) __attribute__ ((__malloc__));
75 ps = sysconf (_SC_PAGESIZE) - 2 * sizeof (void);
76 assert (sizeof (struct memoryblock) < ps);
79 return (struct Strtab *) calloc (1, sizeof (struct Strtab));
84 morememory (struct Strtab *st, size_t len)
86 struct memoryblock *newmem;
90 newmem = (struct memoryblock *) malloc (len);
94 newmem->next = st->memory;
96 st->backp = newmem->memory;
102 strtabfree (struct Strtab *st)
104 struct memoryblock *mb = st->memory;
117 static struct Strent *
118 newstring (struct Strtab *st, const char *str, size_t len)
120 struct Strent *newstr;
124 /* Compute the string length if the caller doesn't know it. */
126 len = strlen (str) + 1;
128 /* Compute the amount of padding needed to make the structure aligned. */
129 align = ((__alignof__ (struct Strent)
130 - (((uintptr_t) st->backp)
131 & (__alignof__ (struct Strent) - 1)))
132 & (__alignof__ (struct Strent) - 1));
134 /* Make sure there is enough room in the memory block. */
135 if (st->left < align + sizeof (struct Strent) + len)
137 morememory (st, sizeof (struct Strent) + len);
141 /* Create the reserved string. */
142 newstr = (struct Strent *) (st->backp + align);
143 newstr->string = str;
147 newstr->right = NULL;
149 for (i = len - 2; i >= 0; --i)
150 newstr->reverse[i] = str[len - 2 - i];
151 newstr->reverse[len - 1] = '\0';
152 st->backp += align + sizeof (struct Strent) + len;
153 st->left -= align + sizeof (struct Strent) + len;
159 /* XXX This function should definitely be rewritten to use a balancing
160 tree algorith (AVL, red-black trees). For now a simple, correct
161 implementation is enough. */
162 static struct Strent **
163 searchstring (struct Strent **sep, struct Strent *newstr)
174 /* Compare the strings. */
175 cmpres = memcmp ((*sep)->reverse, newstr->reverse,
176 MIN ((*sep)->len, newstr->len));
178 /* We found a matching string. */
181 return searchstring (&(*sep)->left, newstr);
183 return searchstring (&(*sep)->right, newstr);
187 /* Add new string. The actual string is assumed to be permanent. */
189 strtabadd (struct Strtab *st, const char *str, size_t len)
191 struct Strent *newstr;
194 /* Allocate memory for the new string and its associated information. */
195 newstr = newstring (st, str, len);
197 /* Search in the array for the place to insert the string. If there
198 is no string with matching prefix and no string with matching
199 leading substring, create a new entry. */
200 sep = searchstring (&st->root, newstr);
203 /* This is not the same entry. This means we have a prefix match. */
204 if ((*sep)->len > newstr->len)
206 /* We have a new substring. This means we don't need the reverse
207 string of this entry anymore. */
208 st->backp -= newstr->len;
209 st->left += newstr->len;
211 newstr->next = (*sep)->next;
212 (*sep)->next = newstr;
214 else if ((*sep)->len != newstr->len)
216 /* When we get here it means that the string we are about to
217 add has a common prefix with a string we already have but
218 it is longer. In this case we have to put it first. */
222 st->total += newstr->len - (*sep)->len;
226 /* We have an exact match. Free the memory we allocated. */
227 st->left += st->backp - (char *) newstr;
228 st->backp = (char *) newstr;
234 st->total += newstr->len;
241 copystrings (struct Strent *nodep, char **freep, size_t *offsetp)
245 if (nodep->left != NULL)
246 copystrings (nodep->left, freep, offsetp);
248 /* Process the current node. */
249 nodep->offset = *offsetp;
250 *freep = (char *) mempcpy (*freep, nodep->string, nodep->len);
251 *offsetp += nodep->len;
253 for (subs = nodep->next; subs != NULL; subs = subs->next)
255 assert (subs->len < nodep->len);
256 subs->offset = nodep->offset + nodep->len - subs->len;
259 if (nodep->right != NULL)
260 copystrings (nodep->right, freep, offsetp);
265 strtabfinalize (struct Strtab *st, size_t *size)
271 /* Fill in the information. */
272 endp = retval = (char *) xmalloc (st->total + 1);
274 /* Always put an empty string at the beginning so that a zero offset
278 /* Now run through the tree and add all the string while also updating
279 the offset members of the elfstrent records. */
281 copystrings (st->root, &endp, ©len);
282 assert (copylen == st->total + 1);
283 assert (endp = retval + st->total + 1);
291 strtaboffset (struct Strent *se)