x86: Don't check XFD against /proc/cpuinfo
[glibc.git] / iconv / strtab.c
blobd6213a47398615cb429707683d21dd35801f91c9
1 /* C string table handling.
2 Copyright (C) 2000-2024 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <https://www.gnu.org/licenses/>. */
17 #ifdef HAVE_CONFIG_H
18 # include <config.h>
19 #endif
21 #include <assert.h>
22 #include <inttypes.h>
23 #include <stddef.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <sys/cdefs.h>
28 #include <sys/param.h>
31 struct Strent
33 const char *string;
34 size_t len;
35 struct Strent *next;
36 struct Strent *left;
37 struct Strent *right;
38 size_t offset;
39 char reverse[0];
43 struct memoryblock
45 struct memoryblock *next;
46 char memory[0];
50 struct Strtab
52 struct Strent *root;
53 struct memoryblock *memory;
54 char *backp;
55 size_t left;
56 size_t total;
58 struct Strent null;
62 /* Cache for the pagesize. We correct this value a bit so that `malloc'
63 is not allocating more than a page. */
64 static size_t ps;
67 #include <programs/xmalloc.h>
69 /* Prototypes for our functions that are used from iconvconfig.c. If
70 you change these, change also iconvconfig.c. */
71 /* Create new C string table object in memory. */
72 extern struct Strtab *strtabinit (void);
74 /* Free resources allocated for C string table ST. */
75 extern void strtabfree (struct Strtab *st);
77 /* Add string STR (length LEN is != 0) to C string table ST. */
78 extern struct Strent *strtabadd (struct Strtab *st, const char *str,
79 size_t len);
81 /* Finalize string table ST and store size in *SIZE and return a pointer. */
82 extern void *strtabfinalize (struct Strtab *st, size_t *size);
84 /* Get offset in string table for string associated with SE. */
85 extern size_t strtaboffset (struct Strent *se);
88 struct Strtab *
89 strtabinit (void)
91 struct Strtab *ret;
93 if (ps == 0)
95 ps = sysconf (_SC_PAGESIZE) - 2 * sizeof (void *);
96 assert (sizeof (struct memoryblock) < ps);
99 ret = (struct Strtab *) calloc (1, sizeof (struct Strtab));
100 if (ret != NULL)
102 ret->null.len = 1;
103 ret->null.string = "";
105 return ret;
109 static void
110 morememory (struct Strtab *st, size_t len)
112 struct memoryblock *newmem;
114 if (len < ps)
115 len = ps;
116 newmem = (struct memoryblock *) malloc (len);
117 if (newmem == NULL)
118 abort ();
120 newmem->next = st->memory;
121 st->memory = newmem;
122 st->backp = newmem->memory;
123 st->left = len - offsetof (struct memoryblock, memory);
127 void
128 strtabfree (struct Strtab *st)
130 struct memoryblock *mb = st->memory;
132 while (mb != NULL)
134 void *old = mb;
135 mb = mb->next;
136 free (old);
139 free (st);
143 static struct Strent *
144 newstring (struct Strtab *st, const char *str, size_t len)
146 struct Strent *newstr;
147 size_t align;
148 int i;
150 /* Compute the amount of padding needed to make the structure aligned. */
151 align = ((__alignof__ (struct Strent)
152 - (((uintptr_t) st->backp)
153 & (__alignof__ (struct Strent) - 1)))
154 & (__alignof__ (struct Strent) - 1));
156 /* Make sure there is enough room in the memory block. */
157 if (st->left < align + sizeof (struct Strent) + len)
159 morememory (st, sizeof (struct Strent) + len);
160 align = 0;
163 /* Create the reserved string. */
164 newstr = (struct Strent *) (st->backp + align);
165 newstr->string = str;
166 newstr->len = len;
167 newstr->next = NULL;
168 newstr->left = NULL;
169 newstr->right = NULL;
170 newstr->offset = 0;
171 for (i = len - 2; i >= 0; --i)
172 newstr->reverse[i] = str[len - 2 - i];
173 newstr->reverse[len - 1] = '\0';
174 st->backp += align + sizeof (struct Strent) + len;
175 st->left -= align + sizeof (struct Strent) + len;
177 return newstr;
181 /* XXX This function should definitely be rewritten to use a balancing
182 tree algorithm (AVL, red-black trees). For now a simple, correct
183 implementation is enough. */
184 static struct Strent **
185 searchstring (struct Strent **sep, struct Strent *newstr)
187 int cmpres;
189 /* More strings? */
190 if (*sep == NULL)
192 *sep = newstr;
193 return sep;
196 /* Compare the strings. */
197 cmpres = memcmp ((*sep)->reverse, newstr->reverse,
198 MIN ((*sep)->len, newstr->len) - 1);
199 if (cmpres == 0)
200 /* We found a matching string. */
201 return sep;
202 else if (cmpres > 0)
203 return searchstring (&(*sep)->left, newstr);
204 else
205 return searchstring (&(*sep)->right, newstr);
209 /* Add new string. The actual string is assumed to be permanent. */
210 struct Strent *
211 strtabadd (struct Strtab *st, const char *str, size_t len)
213 struct Strent *newstr;
214 struct Strent **sep;
216 /* Compute the string length if the caller doesn't know it. */
217 if (len == 0)
218 len = strlen (str) + 1;
220 /* Make sure all "" strings get offset 0. */
221 if (len == 1)
222 return &st->null;
224 /* Allocate memory for the new string and its associated information. */
225 newstr = newstring (st, str, len);
227 /* Search in the array for the place to insert the string. If there
228 is no string with matching prefix and no string with matching
229 leading substring, create a new entry. */
230 sep = searchstring (&st->root, newstr);
231 if (*sep != newstr)
233 /* This is not the same entry. This means we have a prefix match. */
234 if ((*sep)->len > newstr->len)
236 struct Strent *subs;
238 for (subs = (*sep)->next; subs; subs = subs->next)
239 if (subs->len == newstr->len)
241 /* We have an exact match with a substring. Free the memory
242 we allocated. */
243 st->left += st->backp - (char *) newstr;
244 st->backp = (char *) newstr;
246 return subs;
249 /* We have a new substring. This means we don't need the reverse
250 string of this entry anymore. */
251 st->backp -= newstr->len;
252 st->left += newstr->len;
254 newstr->next = (*sep)->next;
255 (*sep)->next = newstr;
257 else if ((*sep)->len != newstr->len)
259 /* When we get here it means that the string we are about to
260 add has a common prefix with a string we already have but
261 it is longer. In this case we have to put it first. */
262 st->total += newstr->len - (*sep)->len;
263 newstr->next = *sep;
264 newstr->left = (*sep)->left;
265 newstr->right = (*sep)->right;
266 *sep = newstr;
268 else
270 /* We have an exact match. Free the memory we allocated. */
271 st->left += st->backp - (char *) newstr;
272 st->backp = (char *) newstr;
274 newstr = *sep;
277 else
278 st->total += newstr->len;
280 return newstr;
284 static void
285 copystrings (struct Strent *nodep, char **freep, size_t *offsetp)
287 struct Strent *subs;
289 if (nodep->left != NULL)
290 copystrings (nodep->left, freep, offsetp);
292 /* Process the current node. */
293 nodep->offset = *offsetp;
294 *freep = (char *) mempcpy (*freep, nodep->string, nodep->len);
295 *offsetp += nodep->len;
297 for (subs = nodep->next; subs != NULL; subs = subs->next)
299 assert (subs->len < nodep->len);
300 subs->offset = nodep->offset + nodep->len - subs->len;
303 if (nodep->right != NULL)
304 copystrings (nodep->right, freep, offsetp);
308 void *
309 strtabfinalize (struct Strtab *st, size_t *size)
311 size_t copylen;
312 char *endp;
313 char *retval;
315 /* Fill in the information. */
316 endp = retval = (char *) xmalloc (st->total + 1);
318 /* Always put an empty string at the beginning so that a zero offset
319 can mean error. */
320 *endp++ = '\0';
322 /* Now run through the tree and add all the string while also updating
323 the offset members of the elfstrent records. */
324 copylen = 1;
325 copystrings (st->root, &endp, &copylen);
326 assert (copylen == st->total + 1);
327 assert (endp == retval + st->total + 1);
328 *size = copylen;
330 return retval;
334 size_t
335 strtaboffset (struct Strent *se)
337 return se->offset;