1 | /* $Id: strcache2.c 1904 2008-10-21 04:46:23Z bird $ */
|
---|
2 | /** @file
|
---|
3 | * strcache2 - New string cache.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 2008 knut st. osmundsen <bird-src-spam@anduin.net>
|
---|
8 | *
|
---|
9 | * This file is part of kBuild.
|
---|
10 | *
|
---|
11 | * kBuild is free software; you can redistribute it and/or modify
|
---|
12 | * it under the terms of the GNU General Public License as published by
|
---|
13 | * the Free Software Foundation; either version 2 of the License, or
|
---|
14 | * (at your option) any later version.
|
---|
15 | *
|
---|
16 | * kBuild is distributed in the hope that it will be useful,
|
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | * GNU General Public License for more details.
|
---|
20 | *
|
---|
21 | * You should have received a copy of the GNU General Public License
|
---|
22 | * along with kBuild; if not, write to the Free Software
|
---|
23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
24 | *
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*******************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *******************************************************************************/
|
---|
31 | #include "make.h"
|
---|
32 | #include "strcache2.h"
|
---|
33 |
|
---|
34 | #include <assert.h>
|
---|
35 |
|
---|
36 | #include "debug.h"
|
---|
37 |
|
---|
38 | #ifdef WINDOWS32
|
---|
39 | # include <io.h>
|
---|
40 | # include <process.h>
|
---|
41 | # include <Windows.h>
|
---|
42 | # define PARSE_IN_WORKER
|
---|
43 | #endif
|
---|
44 |
|
---|
45 | #ifdef __OS2__
|
---|
46 | # include <sys/fmutex.h>
|
---|
47 | #endif
|
---|
48 |
|
---|
49 | #ifdef HAVE_PTHREAD
|
---|
50 | # include <pthread.h>
|
---|
51 | #endif
|
---|
52 |
|
---|
53 |
|
---|
54 | /*******************************************************************************
|
---|
55 | * Defined Constants And Macros *
|
---|
56 | *******************************************************************************/
|
---|
57 | /* The default size of a memory segment (1MB). */
|
---|
58 | #define STRCACHE2_SEG_SIZE (1024U*1024U)
|
---|
59 | /* The default hash table shift (hash size give as a power of two). */
|
---|
60 | #define STRCACHE2_HASH_SHIFT 16
|
---|
61 |
|
---|
62 |
|
---|
63 | /*******************************************************************************
|
---|
64 | * Global Variables *
|
---|
65 | *******************************************************************************/
|
---|
66 | /* List of initialized string caches. */
|
---|
67 | static struct strcache2 *strcache_head;
|
---|
68 |
|
---|
69 |
|
---|
70 |
|
---|
71 | static struct strcache2_seg *
|
---|
72 | strcache2_new_seg (struct strcache2 *cache, unsigned int minlen)
|
---|
73 | {
|
---|
74 | struct strcache2_seg *seg;
|
---|
75 | size_t size;
|
---|
76 | size_t off;
|
---|
77 |
|
---|
78 | size = cache->def_seg_size;
|
---|
79 | if (size < (size_t)minlen + sizeof (struct strcache2_seg) + STRCACHE2_ENTRY_ALIGNMENT)
|
---|
80 | {
|
---|
81 | size = (size_t)minlen * 2;
|
---|
82 | size = (size + 0xfff) & ~(size_t)0xfff;
|
---|
83 | }
|
---|
84 |
|
---|
85 | seg = xmalloc (size);
|
---|
86 | seg->start = (char *)(seg + 1);
|
---|
87 | seg->size = size - sizeof (struct strcache2_seg);
|
---|
88 | off = (size_t)seg->start & (STRCACHE2_ENTRY_ALIGNMENT - 1);
|
---|
89 | if (off)
|
---|
90 | {
|
---|
91 | seg->start += off;
|
---|
92 | seg->size -= off;
|
---|
93 | }
|
---|
94 | assert (seg->size > minlen);
|
---|
95 | seg->cursor = seg->start;
|
---|
96 | seg->avail = seg->size;
|
---|
97 |
|
---|
98 | seg->next = cache->seg_head;
|
---|
99 | cache->seg_head = seg;
|
---|
100 |
|
---|
101 | return seg;
|
---|
102 | }
|
---|
103 |
|
---|
104 | MY_INLINE unsigned int
|
---|
105 | strcache2_case_sensitive_hash_1 (const char *str, unsigned int len)
|
---|
106 | {
|
---|
107 | unsigned int hash = 0;
|
---|
108 | if (MY_PREDICT_TRUE(len >= 2))
|
---|
109 | {
|
---|
110 | unsigned int ch0 = *str++;
|
---|
111 | hash = 0;
|
---|
112 | len--;
|
---|
113 | while (len >= 2)
|
---|
114 | {
|
---|
115 | unsigned int ch1 = *str++;
|
---|
116 | hash += ch0 << (ch1 & 0xf);
|
---|
117 |
|
---|
118 | ch0 = *str++;
|
---|
119 | hash += ch1 << (ch0 & 0xf);
|
---|
120 |
|
---|
121 | len -= 2;
|
---|
122 | }
|
---|
123 | if (len == 1)
|
---|
124 | {
|
---|
125 | unsigned ch1 = *str;
|
---|
126 | hash += ch0 << (ch1 & 0xf);
|
---|
127 |
|
---|
128 | hash += ch1;
|
---|
129 | }
|
---|
130 | else
|
---|
131 | hash += ch0;
|
---|
132 | }
|
---|
133 | else if (len)
|
---|
134 | {
|
---|
135 | hash = *str;
|
---|
136 | hash += hash << (hash & 0xf);
|
---|
137 | }
|
---|
138 | else
|
---|
139 | hash = 0;
|
---|
140 | return hash;
|
---|
141 | }
|
---|
142 |
|
---|
143 | MY_INLINE unsigned int
|
---|
144 | strcache2_case_sensitive_hash_2 (const char *str, unsigned int len)
|
---|
145 | {
|
---|
146 | unsigned int hash = 0;
|
---|
147 | if (MY_PREDICT_TRUE(len >= 2))
|
---|
148 | {
|
---|
149 | unsigned int ch0 = *str++;
|
---|
150 | hash = 0;
|
---|
151 | len--;
|
---|
152 | while (len >= 2)
|
---|
153 | {
|
---|
154 | unsigned int ch1 = *str++;
|
---|
155 | hash += ch0 << (ch1 & 0x7);
|
---|
156 |
|
---|
157 | ch0 = *str++;
|
---|
158 | hash += ch1 << (ch0 & 0x7);
|
---|
159 |
|
---|
160 | len -= 2;
|
---|
161 | }
|
---|
162 | if (len == 1)
|
---|
163 | {
|
---|
164 | unsigned ch1 = *str;
|
---|
165 | hash += ch0 << (ch1 & 0x7);
|
---|
166 |
|
---|
167 | hash += ch1;
|
---|
168 | }
|
---|
169 | else
|
---|
170 | hash += ch0;
|
---|
171 | }
|
---|
172 | else if (len)
|
---|
173 | {
|
---|
174 | hash = *str;
|
---|
175 | hash += hash << (hash & 0x7);
|
---|
176 | }
|
---|
177 | else
|
---|
178 | hash = 1;
|
---|
179 | hash |= 1;
|
---|
180 | return hash;
|
---|
181 | }
|
---|
182 |
|
---|
183 | MY_INLINE unsigned int
|
---|
184 | strcache2_case_insensitive_hash_1 (const char *str, unsigned int len)
|
---|
185 | {
|
---|
186 | unsigned int hash = 0;
|
---|
187 | if (MY_PREDICT_TRUE(len >= 2))
|
---|
188 | {
|
---|
189 | unsigned int ch0 = *str++;
|
---|
190 | ch0 = tolower (ch0);
|
---|
191 | hash = 0;
|
---|
192 | len--;
|
---|
193 | while (len >= 2)
|
---|
194 | {
|
---|
195 | unsigned int ch1 = *str++;
|
---|
196 | ch1 = tolower (ch1);
|
---|
197 | hash += ch0 << (ch1 & 0xf);
|
---|
198 |
|
---|
199 | ch0 = *str++;
|
---|
200 | ch0 = tolower (ch0);
|
---|
201 | hash += ch1 << (ch0 & 0xf);
|
---|
202 |
|
---|
203 | len -= 2;
|
---|
204 | }
|
---|
205 | if (len == 1)
|
---|
206 | {
|
---|
207 | unsigned ch1 = *str;
|
---|
208 | ch1 = tolower (ch1);
|
---|
209 | hash += ch0 << (ch1 & 0xf);
|
---|
210 |
|
---|
211 | hash += ch1;
|
---|
212 | }
|
---|
213 | else
|
---|
214 | hash += ch0;
|
---|
215 | }
|
---|
216 | else if (len)
|
---|
217 | {
|
---|
218 | hash = *str;
|
---|
219 | hash += hash << (hash & 0xf);
|
---|
220 | }
|
---|
221 | else
|
---|
222 | hash = 0;
|
---|
223 | return hash;
|
---|
224 | }
|
---|
225 |
|
---|
226 | MY_INLINE unsigned int
|
---|
227 | strcache2_case_insensitive_hash_2 (const char *str, unsigned int len)
|
---|
228 | {
|
---|
229 | unsigned int hash = 0;
|
---|
230 | if (MY_PREDICT_TRUE(len >= 2))
|
---|
231 | {
|
---|
232 | unsigned int ch0 = *str++;
|
---|
233 | ch0 = tolower (ch0);
|
---|
234 | hash = 0;
|
---|
235 | len--;
|
---|
236 | while (len >= 2)
|
---|
237 | {
|
---|
238 | unsigned int ch1 = *str++;
|
---|
239 | ch1 = tolower (ch1);
|
---|
240 | hash += ch0 << (ch1 & 0x7);
|
---|
241 |
|
---|
242 | ch0 = *str++;
|
---|
243 | ch0 = tolower (ch0);
|
---|
244 | hash += ch1 << (ch0 & 0x7);
|
---|
245 |
|
---|
246 | len -= 2;
|
---|
247 | }
|
---|
248 | if (len == 1)
|
---|
249 | {
|
---|
250 | unsigned ch1 = *str;
|
---|
251 | ch1 = tolower (ch1);
|
---|
252 | hash += ch0 << (ch1 & 0x7);
|
---|
253 |
|
---|
254 | hash += ch1;
|
---|
255 | }
|
---|
256 | else
|
---|
257 | hash += ch0;
|
---|
258 | }
|
---|
259 | else if (len)
|
---|
260 | {
|
---|
261 | hash = *str;
|
---|
262 | hash += hash << (hash & 0x7);
|
---|
263 | }
|
---|
264 | else
|
---|
265 | hash = 1;
|
---|
266 | hash |= 1;
|
---|
267 | return hash;
|
---|
268 | }
|
---|
269 |
|
---|
270 | #ifdef _MSC_VER
|
---|
271 | typedef unsigned int int32_t;
|
---|
272 | #else
|
---|
273 | # include <stdint.h>
|
---|
274 | #endif
|
---|
275 |
|
---|
276 | MY_INLINE int
|
---|
277 | strcache2_memcmp_inline_short (const char *xs, const char *ys, unsigned int length)
|
---|
278 | {
|
---|
279 | if (length <= 8)
|
---|
280 | {
|
---|
281 | /* short string compare - ~50% of the kBuild calls. */
|
---|
282 | assert ( !((size_t)ys & 3) );
|
---|
283 | if (!((size_t)xs & 3))
|
---|
284 | {
|
---|
285 | /* aligned */
|
---|
286 | int result;
|
---|
287 | switch (length)
|
---|
288 | {
|
---|
289 | default: /* memcmp for longer strings */
|
---|
290 | return memcmp (xs, ys, length);
|
---|
291 | case 8:
|
---|
292 | result = *(int32_t*)(xs + 4) - *(int32_t*)(ys + 4);
|
---|
293 | result |= *(int32_t*)xs - *(int32_t*)ys;
|
---|
294 | return result;
|
---|
295 | case 7:
|
---|
296 | result = xs[6] - ys[6];
|
---|
297 | result |= xs[5] - ys[5];
|
---|
298 | result |= xs[4] - ys[4];
|
---|
299 | result |= *(int32_t*)xs - *(int32_t*)ys;
|
---|
300 | return result;
|
---|
301 | case 6:
|
---|
302 | result = xs[5] - ys[5];
|
---|
303 | result |= xs[4] - ys[4];
|
---|
304 | result |= *(int32_t*)xs - *(int32_t*)ys;
|
---|
305 | return result;
|
---|
306 | case 5:
|
---|
307 | result = xs[4] - ys[4];
|
---|
308 | result |= *(int32_t*)xs - *(int32_t*)ys;
|
---|
309 | return result;
|
---|
310 | case 4:
|
---|
311 | return *(int32_t*)xs - *(int32_t*)ys;
|
---|
312 | case 3:
|
---|
313 | result = xs[2] - ys[2];
|
---|
314 | result |= xs[1] - ys[1];
|
---|
315 | result |= xs[0] - ys[0];
|
---|
316 | return result;
|
---|
317 | case 2:
|
---|
318 | result = xs[1] - ys[1];
|
---|
319 | result |= xs[0] - ys[0];
|
---|
320 | return result;
|
---|
321 | case 1:
|
---|
322 | return *xs - *ys;
|
---|
323 | case 0:
|
---|
324 | return 0;
|
---|
325 | }
|
---|
326 | }
|
---|
327 | else
|
---|
328 | {
|
---|
329 | /* unaligned */
|
---|
330 | int result = 0;
|
---|
331 | switch (length)
|
---|
332 | {
|
---|
333 | case 8: result |= xs[7] - ys[7];
|
---|
334 | case 7: result |= xs[6] - ys[6];
|
---|
335 | case 6: result |= xs[5] - ys[5];
|
---|
336 | case 5: result |= xs[4] - ys[4];
|
---|
337 | case 4: result |= xs[3] - ys[3];
|
---|
338 | case 3: result |= xs[2] - ys[2];
|
---|
339 | case 2: result |= xs[1] - ys[1];
|
---|
340 | case 1: result |= xs[0] - ys[0];
|
---|
341 | case 0:
|
---|
342 | return result;
|
---|
343 | }
|
---|
344 | }
|
---|
345 | }
|
---|
346 |
|
---|
347 | /* memcmp for longer strings */
|
---|
348 | return memcmp (xs, ys, length);
|
---|
349 | }
|
---|
350 |
|
---|
351 | MY_INLINE int
|
---|
352 | strcache2_memcmp_inlined (const char *xs, const char *ys, unsigned int length)
|
---|
353 | {
|
---|
354 | #ifndef ELECTRIC_HEAP
|
---|
355 | assert ( !((size_t)ys & 3) );
|
---|
356 | #endif
|
---|
357 | if (!((size_t)xs & 3))
|
---|
358 | {
|
---|
359 | int result;
|
---|
360 | /* aligned */
|
---|
361 | while (length >= 8)
|
---|
362 | {
|
---|
363 | result = *(int32_t*)xs - *(int32_t*)ys;
|
---|
364 | result |= *(int32_t*)(xs + 4) - *(int32_t*)(ys + 4);
|
---|
365 | if (MY_PREDICT_FALSE(result))
|
---|
366 | return result;
|
---|
367 | xs += 8;
|
---|
368 | ys += 8;
|
---|
369 | length -= 8;
|
---|
370 | }
|
---|
371 | switch (length)
|
---|
372 | {
|
---|
373 | case 7:
|
---|
374 | result = *(int32_t*)xs - *(int32_t*)ys;
|
---|
375 | result |= xs[6] - ys[6];
|
---|
376 | result |= xs[5] - ys[5];
|
---|
377 | result |= xs[4] - ys[4];
|
---|
378 | return result;
|
---|
379 | case 6:
|
---|
380 | result = *(int32_t*)xs - *(int32_t*)ys;
|
---|
381 | result |= xs[5] - ys[5];
|
---|
382 | result |= xs[4] - ys[4];
|
---|
383 | return result;
|
---|
384 | case 5:
|
---|
385 | result = *(int32_t*)xs - *(int32_t*)ys;
|
---|
386 | result |= xs[4] - ys[4];
|
---|
387 | return result;
|
---|
388 | case 4:
|
---|
389 | return *(int32_t*)xs - *(int32_t*)ys;
|
---|
390 | case 3:
|
---|
391 | result = xs[2] - ys[2];
|
---|
392 | result |= xs[1] - ys[1];
|
---|
393 | result |= xs[0] - ys[0];
|
---|
394 | return result;
|
---|
395 | case 2:
|
---|
396 | result = xs[1] - ys[1];
|
---|
397 | result |= xs[0] - ys[0];
|
---|
398 | return result;
|
---|
399 | case 1:
|
---|
400 | return *xs - *ys;
|
---|
401 | default:
|
---|
402 | case 0:
|
---|
403 | return 0;
|
---|
404 | }
|
---|
405 | }
|
---|
406 | else
|
---|
407 | {
|
---|
408 | /* unaligned */
|
---|
409 | int result;
|
---|
410 | while (length >= 8)
|
---|
411 | {
|
---|
412 | #if defined(__i386__) || defined(__x86_64__)
|
---|
413 | result = ( ((int32_t)xs[3] << 24)
|
---|
414 | | ((int32_t)xs[2] << 16)
|
---|
415 | | ((int32_t)xs[1] << 8)
|
---|
416 | | xs[0] )
|
---|
417 | - *(int32_t*)ys;
|
---|
418 | result |= ( ((int32_t)xs[7] << 24)
|
---|
419 | | ((int32_t)xs[6] << 16)
|
---|
420 | | ((int32_t)xs[5] << 8)
|
---|
421 | | xs[4] )
|
---|
422 | - *(int32_t*)(ys + 4);
|
---|
423 | #else
|
---|
424 | result = xs[3] - ys[3];
|
---|
425 | result |= xs[2] - ys[2];
|
---|
426 | result |= xs[1] - ys[1];
|
---|
427 | result |= xs[0] - ys[0];
|
---|
428 | result |= xs[7] - ys[7];
|
---|
429 | result |= xs[6] - ys[6];
|
---|
430 | result |= xs[5] - ys[5];
|
---|
431 | result |= xs[4] - ys[4];
|
---|
432 | #endif
|
---|
433 | if (MY_PREDICT_FALSE(result))
|
---|
434 | return result;
|
---|
435 | xs += 8;
|
---|
436 | ys += 8;
|
---|
437 | length -= 8;
|
---|
438 | }
|
---|
439 | result = 0;
|
---|
440 | switch (length)
|
---|
441 | {
|
---|
442 | case 7: result |= xs[6] - ys[6];
|
---|
443 | case 6: result |= xs[5] - ys[5];
|
---|
444 | case 5: result |= xs[4] - ys[4];
|
---|
445 | case 4: result |= xs[3] - ys[3];
|
---|
446 | case 3: result |= xs[2] - ys[2];
|
---|
447 | case 2: result |= xs[1] - ys[1];
|
---|
448 | case 1: result |= xs[0] - ys[0];
|
---|
449 | return result;
|
---|
450 | default:
|
---|
451 | case 0:
|
---|
452 | return 0;
|
---|
453 | }
|
---|
454 | }
|
---|
455 | }
|
---|
456 |
|
---|
457 | MY_INLINE int
|
---|
458 | strcache2_is_equal (struct strcache2 *cache, struct strcache2_entry const *entry,
|
---|
459 | const char *str, unsigned int length, unsigned int hash1)
|
---|
460 | {
|
---|
461 | assert (!cache->case_insensitive);
|
---|
462 |
|
---|
463 | /* the simple stuff first. */
|
---|
464 | if ( entry == NULL
|
---|
465 | || entry->hash1 != hash1
|
---|
466 | || entry->length != length)
|
---|
467 | return 0;
|
---|
468 |
|
---|
469 | #if 0
|
---|
470 | return memcmp (entry + 1, str, length) == 0;
|
---|
471 | #elif 1
|
---|
472 | return strcache2_memcmp_inlined ((const char *)(entry + 1), str, length) == 0;
|
---|
473 | #else
|
---|
474 | return strcache2_memcmp_inline_short ((const char *)(entry + 1), str, length) == 0;
|
---|
475 | #endif
|
---|
476 | }
|
---|
477 |
|
---|
478 | MY_INLINE int
|
---|
479 | strcache2_is_iequal (struct strcache2 *cache, struct strcache2_entry const *entry,
|
---|
480 | const char *str, unsigned int length, unsigned int hash1)
|
---|
481 | {
|
---|
482 | assert (cache->case_insensitive);
|
---|
483 |
|
---|
484 | /* the simple stuff first. */
|
---|
485 | if ( entry == NULL
|
---|
486 | || entry->hash1 != hash1
|
---|
487 | || entry->length != length)
|
---|
488 | return 0;
|
---|
489 |
|
---|
490 | #if defined(_MSC_VER) || defined(__OS2__)
|
---|
491 | return _memicmp (entry + 1, str, length) == 0;
|
---|
492 | #else
|
---|
493 | return strncasecmp ((const char *)(entry + 1), str, length) == 0;
|
---|
494 | #endif
|
---|
495 | }
|
---|
496 |
|
---|
497 | static void
|
---|
498 | strcache2_rehash (struct strcache2 *cache)
|
---|
499 | {
|
---|
500 | unsigned int src = cache->hash_size;
|
---|
501 | struct strcache2_entry **src_tab = cache->hash_tab;
|
---|
502 | struct strcache2_entry **dst_tab;
|
---|
503 | unsigned int hash_mask;
|
---|
504 |
|
---|
505 | /* Allocate a new hash table twice the size of the current. */
|
---|
506 | cache->hash_size <<= 1;
|
---|
507 | cache->hash_mask <<= 1;
|
---|
508 | cache->hash_mask |= 1;
|
---|
509 | cache->rehash_count <<= 1;
|
---|
510 | cache->hash_tab = dst_tab = (struct strcache2_entry **)
|
---|
511 | xmalloc (cache->hash_size * sizeof (struct strcache2_entry *));
|
---|
512 | memset (dst_tab, '\0', cache->hash_size * sizeof (struct strcache2_entry *));
|
---|
513 |
|
---|
514 | /* Copy the entries from the old to the new hash table. */
|
---|
515 | hash_mask = cache->hash_mask;
|
---|
516 | while (src-- > 0)
|
---|
517 | {
|
---|
518 | struct strcache2_entry *entry = src_tab[src];
|
---|
519 | if (entry)
|
---|
520 | {
|
---|
521 | unsigned int dst = entry->hash1 & hash_mask;
|
---|
522 | if (dst_tab[dst])
|
---|
523 | {
|
---|
524 | unsigned int hash2 = entry->hash2;
|
---|
525 | if (!hash2)
|
---|
526 | entry->hash2 = hash2 = cache->case_insensitive
|
---|
527 | ? strcache2_case_insensitive_hash_2 ((const char *)(entry + 1), entry->length)
|
---|
528 | : strcache2_case_sensitive_hash_2 ((const char *)(entry + 1), entry->length);
|
---|
529 | dst += hash2;
|
---|
530 | dst &= hash_mask;
|
---|
531 | while (dst_tab[dst])
|
---|
532 | {
|
---|
533 | dst += hash2;
|
---|
534 | dst &= hash_mask;
|
---|
535 | }
|
---|
536 | }
|
---|
537 |
|
---|
538 | dst_tab[dst] = entry;
|
---|
539 | }
|
---|
540 | }
|
---|
541 |
|
---|
542 | /* That's it, just free the old table and we're done. */
|
---|
543 | free (src_tab);
|
---|
544 | }
|
---|
545 |
|
---|
546 | /* Internal worker that enters a new string into the cache. */
|
---|
547 | static const char *
|
---|
548 | strcache2_enter_string (struct strcache2 *cache, unsigned int idx,
|
---|
549 | const char *str, unsigned int length,
|
---|
550 | unsigned int hash1, unsigned hash2)
|
---|
551 | {
|
---|
552 | struct strcache2_entry *entry;
|
---|
553 | struct strcache2_seg *seg;
|
---|
554 | unsigned int size;
|
---|
555 | char *str_copy;
|
---|
556 |
|
---|
557 | /* Allocate space for the string. */
|
---|
558 |
|
---|
559 | size = length + 1 + sizeof (struct strcache2_entry);
|
---|
560 | size = (size + STRCACHE2_ENTRY_ALIGNMENT - 1) & ~(STRCACHE2_ENTRY_ALIGNMENT - 1U);
|
---|
561 |
|
---|
562 | seg = cache->seg_head;
|
---|
563 | if (MY_PREDICT_FALSE(seg->avail < size))
|
---|
564 | {
|
---|
565 | do
|
---|
566 | seg = seg->next;
|
---|
567 | while (seg && seg->avail < size);
|
---|
568 | if (!seg)
|
---|
569 | seg = strcache2_new_seg (cache, size);
|
---|
570 | }
|
---|
571 |
|
---|
572 | entry = (struct strcache2_entry *) seg->cursor;
|
---|
573 | assert ((size_t)entry & (STRCACHE2_ENTRY_ALIGNMENT - 1));
|
---|
574 | seg->cursor += size;
|
---|
575 | seg->avail -= size;
|
---|
576 |
|
---|
577 | /* Setup the entry, copy the string and insert it into the hash table. */
|
---|
578 |
|
---|
579 | entry->user = NULL;
|
---|
580 | entry->length = length;
|
---|
581 | entry->hash1 = hash1;
|
---|
582 | entry->hash2 = hash2;
|
---|
583 | str_copy = (char *) memcpy (entry + 1, str, length);
|
---|
584 | str_copy[length] = '\0';
|
---|
585 |
|
---|
586 | cache->hash_tab[idx] = entry;
|
---|
587 | cache->count++;
|
---|
588 | if (cache->count >= cache->rehash_count)
|
---|
589 | strcache2_rehash (cache);
|
---|
590 |
|
---|
591 | return str_copy;
|
---|
592 | }
|
---|
593 |
|
---|
594 | /* The public add string interface. */
|
---|
595 | const char *
|
---|
596 | strcache2_add (struct strcache2 *cache, const char *str, unsigned int length)
|
---|
597 | {
|
---|
598 | struct strcache2_entry const *entry;
|
---|
599 | unsigned int hash2;
|
---|
600 | unsigned int hash1 = strcache2_case_sensitive_hash_1 (str, length);
|
---|
601 | unsigned int idx;
|
---|
602 |
|
---|
603 | assert (!cache->case_insensitive);
|
---|
604 |
|
---|
605 | cache->lookup_count++;
|
---|
606 |
|
---|
607 | /* Lookup the entry in the hash table, hoping for an
|
---|
608 | early match. */
|
---|
609 | idx = hash1 & cache->hash_mask;
|
---|
610 | entry = cache->hash_tab[idx];
|
---|
611 | if (strcache2_is_equal (cache, entry, str, length, hash1))
|
---|
612 | return (const char *)(entry + 1);
|
---|
613 | if (!entry)
|
---|
614 | hash2 = 0;
|
---|
615 | else
|
---|
616 | {
|
---|
617 | cache->collision_1st_count++;
|
---|
618 |
|
---|
619 | hash2 = strcache2_case_sensitive_hash_2 (str, length);
|
---|
620 | idx += hash2;
|
---|
621 | idx &= cache->hash_mask;
|
---|
622 | entry = cache->hash_tab[idx];
|
---|
623 | if (strcache2_is_equal (cache, entry, str, length, hash1))
|
---|
624 | return (const char *)(entry + 1);
|
---|
625 |
|
---|
626 | if (entry)
|
---|
627 | {
|
---|
628 | cache->collision_2nd_count++;
|
---|
629 | do
|
---|
630 | {
|
---|
631 | idx += hash2;
|
---|
632 | idx &= cache->hash_mask;
|
---|
633 | entry = cache->hash_tab[idx];
|
---|
634 | cache->collision_3rd_count++;
|
---|
635 | if (strcache2_is_equal (cache, entry, str, length, hash1))
|
---|
636 | return (const char *)(entry + 1);
|
---|
637 | }
|
---|
638 | while (entry);
|
---|
639 | }
|
---|
640 | }
|
---|
641 |
|
---|
642 | /* Not found, add it at IDX. */
|
---|
643 | return strcache2_enter_string (cache, idx, str, length, hash1, hash2);
|
---|
644 | }
|
---|
645 |
|
---|
646 | /* The public add string interface for prehashed strings.
|
---|
647 | Use strcache2_hash_str to calculate the hash of a string. */
|
---|
648 | const char *
|
---|
649 | strcache2_add_hashed (struct strcache2 *cache, const char *str, unsigned int length,
|
---|
650 | unsigned int hash1, unsigned int hash2)
|
---|
651 | {
|
---|
652 | struct strcache2_entry const *entry;
|
---|
653 | unsigned int idx;
|
---|
654 | #ifndef NDEBUG
|
---|
655 | unsigned correct_hash;
|
---|
656 |
|
---|
657 | assert (!cache->case_insensitive);
|
---|
658 | correct_hash = strcache2_case_sensitive_hash_1 (str, length);
|
---|
659 | MY_ASSERT_MSG (hash1 == correct_hash, ("%#x != %#x\n", hash1, correct_hash));
|
---|
660 | if (hash2)
|
---|
661 | {
|
---|
662 | correct_hash = strcache2_case_sensitive_hash_2 (str, length);
|
---|
663 | MY_ASSERT_MSG (hash2 == correct_hash, ("%#x != %#x\n", hash2, correct_hash));
|
---|
664 | }
|
---|
665 | #endif /* NDEBUG */
|
---|
666 |
|
---|
667 | cache->lookup_count++;
|
---|
668 |
|
---|
669 | /* Lookup the entry in the hash table, hoping for an
|
---|
670 | early match. */
|
---|
671 | idx = hash1 & cache->hash_mask;
|
---|
672 | entry = cache->hash_tab[idx];
|
---|
673 | if (strcache2_is_equal (cache, entry, str, length, hash1))
|
---|
674 | return (const char *)(entry + 1);
|
---|
675 | if (entry)
|
---|
676 | {
|
---|
677 | cache->collision_1st_count++;
|
---|
678 |
|
---|
679 | if (!hash2)
|
---|
680 | hash2 = cache->case_insensitive
|
---|
681 | ? strcache2_case_insensitive_hash_2 (str, length)
|
---|
682 | : strcache2_case_sensitive_hash_2 (str, length);
|
---|
683 | idx += hash2;
|
---|
684 | idx &= cache->hash_mask;
|
---|
685 | entry = cache->hash_tab[idx];
|
---|
686 | if (strcache2_is_equal (cache, entry, str, length, hash1))
|
---|
687 | return (const char *)(entry + 1);
|
---|
688 |
|
---|
689 | if (entry)
|
---|
690 | {
|
---|
691 | cache->collision_2nd_count++;
|
---|
692 | do
|
---|
693 | {
|
---|
694 | idx += hash2;
|
---|
695 | idx &= cache->hash_mask;
|
---|
696 | entry = cache->hash_tab[idx];
|
---|
697 | cache->collision_3rd_count++;
|
---|
698 | if (strcache2_is_equal (cache, entry, str, length, hash1))
|
---|
699 | return (const char *)(entry + 1);
|
---|
700 | }
|
---|
701 | while (entry);
|
---|
702 | }
|
---|
703 | }
|
---|
704 |
|
---|
705 | /* Not found, add it at IDX. */
|
---|
706 | return strcache2_enter_string (cache, idx, str, length, hash1, hash2);
|
---|
707 | }
|
---|
708 |
|
---|
709 | /* The public lookup (case sensitive) string interface. */
|
---|
710 | const char *
|
---|
711 | strcache2_lookup (struct strcache2 *cache, const char *str, unsigned int length)
|
---|
712 | {
|
---|
713 | struct strcache2_entry const *entry;
|
---|
714 | unsigned int hash2;
|
---|
715 | unsigned int hash1 = strcache2_case_sensitive_hash_1 (str, length);
|
---|
716 | unsigned int idx;
|
---|
717 |
|
---|
718 | assert (!cache->case_insensitive);
|
---|
719 |
|
---|
720 | cache->lookup_count++;
|
---|
721 |
|
---|
722 | /* Lookup the entry in the hash table, hoping for an
|
---|
723 | early match. */
|
---|
724 | idx = hash1 & cache->hash_mask;
|
---|
725 | entry = cache->hash_tab[idx];
|
---|
726 | if (strcache2_is_equal (cache, entry, str, length, hash1))
|
---|
727 | return (const char *)(entry + 1);
|
---|
728 | if (!entry)
|
---|
729 | hash2 = 0;
|
---|
730 | else
|
---|
731 | {
|
---|
732 | cache->collision_1st_count++;
|
---|
733 |
|
---|
734 | hash2 = strcache2_case_sensitive_hash_2 (str, length);
|
---|
735 | idx += hash2;
|
---|
736 | idx &= cache->hash_mask;
|
---|
737 | entry = cache->hash_tab[idx];
|
---|
738 | if (strcache2_is_equal (cache, entry, str, length, hash1))
|
---|
739 | return (const char *)(entry + 1);
|
---|
740 |
|
---|
741 | if (entry)
|
---|
742 | {
|
---|
743 | cache->collision_2nd_count++;
|
---|
744 | do
|
---|
745 | {
|
---|
746 | idx += hash2;
|
---|
747 | idx &= cache->hash_mask;
|
---|
748 | entry = cache->hash_tab[idx];
|
---|
749 | cache->collision_3rd_count++;
|
---|
750 | if (strcache2_is_equal (cache, entry, str, length, hash1))
|
---|
751 | return (const char *)(entry + 1);
|
---|
752 | }
|
---|
753 | while (entry);
|
---|
754 | }
|
---|
755 | }
|
---|
756 |
|
---|
757 | /* Not found. */
|
---|
758 | return NULL;
|
---|
759 | }
|
---|
760 |
|
---|
761 | #if defined(HAVE_CASE_INSENSITIVE_FS)
|
---|
762 |
|
---|
763 | /* The public add string interface for case insensitive strings. */
|
---|
764 | const char *
|
---|
765 | strcache2_iadd (struct strcache2 *cache, const char *str, unsigned int length)
|
---|
766 | {
|
---|
767 | struct strcache2_entry const *entry;
|
---|
768 | unsigned int hash2;
|
---|
769 | unsigned int hash1 = strcache2_case_insensitive_hash_1 (str, length);
|
---|
770 | unsigned int idx;
|
---|
771 |
|
---|
772 | assert (cache->case_insensitive);
|
---|
773 | cache->lookup_count++;
|
---|
774 |
|
---|
775 | /* Lookup the entry in the hash table, hoping for an
|
---|
776 | early match. */
|
---|
777 | idx = hash1 & cache->hash_mask;
|
---|
778 | entry = cache->hash_tab[idx];
|
---|
779 | if (strcache2_is_iequal (cache, entry, str, length, hash1))
|
---|
780 | return (const char *)(entry + 1);
|
---|
781 | if (!entry)
|
---|
782 | hash2 = 0;
|
---|
783 | else
|
---|
784 | {
|
---|
785 | cache->collision_1st_count++;
|
---|
786 |
|
---|
787 | hash2 = strcache2_case_insensitive_hash_2 (str, length);
|
---|
788 | idx += hash2;
|
---|
789 | idx &= cache->hash_mask;
|
---|
790 | entry = cache->hash_tab[idx];
|
---|
791 | if (strcache2_is_iequal (cache, entry, str, length, hash1))
|
---|
792 | return (const char *)(entry + 1);
|
---|
793 |
|
---|
794 | if (entry)
|
---|
795 | {
|
---|
796 | cache->collision_2nd_count++;
|
---|
797 | do
|
---|
798 | {
|
---|
799 | idx += hash2;
|
---|
800 | idx &= cache->hash_mask;
|
---|
801 | entry = cache->hash_tab[idx];
|
---|
802 | cache->collision_3rd_count++;
|
---|
803 | if (strcache2_is_iequal (cache, entry, str, length, hash1))
|
---|
804 | return (const char *)(entry + 1);
|
---|
805 | }
|
---|
806 | while (entry);
|
---|
807 | }
|
---|
808 | }
|
---|
809 |
|
---|
810 | /* Not found, add it at IDX. */
|
---|
811 | return strcache2_enter_string (cache, idx, str, length, hash1, hash2);
|
---|
812 | }
|
---|
813 |
|
---|
814 | /* The public add string interface for prehashed case insensitive strings.
|
---|
815 | Use strcache2_hash_istr to calculate the hash of a string. */
|
---|
816 | const char *
|
---|
817 | strcache2_iadd_hashed (struct strcache2 *cache, const char *str, unsigned int length,
|
---|
818 | unsigned int hash1, unsigned int hash2)
|
---|
819 | {
|
---|
820 | struct strcache2_entry const *entry;
|
---|
821 | unsigned int idx;
|
---|
822 | #ifndef NDEBUG
|
---|
823 | unsigned correct_hash;
|
---|
824 |
|
---|
825 | assert (cache->case_insensitive);
|
---|
826 | correct_hash = strcache2_case_insensitive_hash_1 (str, length);
|
---|
827 | MY_ASSERT_MSG (hash1 == correct_hash, ("%#x != %#x\n", hash1, correct_hash));
|
---|
828 | if (hash2)
|
---|
829 | {
|
---|
830 | correct_hash = strcache2_case_insensitive_hash_2 (str, length);
|
---|
831 | MY_ASSERT_MSG (hash2 == correct_hash, ("%#x != %#x\n", hash2, correct_hash));
|
---|
832 | }
|
---|
833 | #endif /* NDEBUG */
|
---|
834 |
|
---|
835 | cache->lookup_count++;
|
---|
836 |
|
---|
837 | /* Lookup the entry in the hash table, hoping for an
|
---|
838 | early match. */
|
---|
839 | idx = hash1 & cache->hash_mask;
|
---|
840 | entry = cache->hash_tab[idx];
|
---|
841 | if (strcache2_is_iequal (cache, entry, str, length, hash1))
|
---|
842 | return (const char *)(entry + 1);
|
---|
843 | if (entry)
|
---|
844 | {
|
---|
845 | cache->collision_1st_count++;
|
---|
846 |
|
---|
847 | if (!hash2)
|
---|
848 | hash2 = strcache2_case_insensitive_hash_2 (str, length);
|
---|
849 | idx += hash2;
|
---|
850 | idx &= cache->hash_mask;
|
---|
851 | entry = cache->hash_tab[idx];
|
---|
852 | if (strcache2_is_iequal (cache, entry, str, length, hash1))
|
---|
853 | return (const char *)(entry + 1);
|
---|
854 |
|
---|
855 | if (entry)
|
---|
856 | {
|
---|
857 | cache->collision_2nd_count++;
|
---|
858 | do
|
---|
859 | {
|
---|
860 | idx += hash2;
|
---|
861 | idx &= cache->hash_mask;
|
---|
862 | entry = cache->hash_tab[idx];
|
---|
863 | cache->collision_3rd_count++;
|
---|
864 | if (strcache2_is_iequal (cache, entry, str, length, hash1))
|
---|
865 | return (const char *)(entry + 1);
|
---|
866 | }
|
---|
867 | while (entry);
|
---|
868 | }
|
---|
869 | }
|
---|
870 |
|
---|
871 | /* Not found, add it at IDX. */
|
---|
872 | return strcache2_enter_string (cache, idx, str, length, hash1, hash2);
|
---|
873 | }
|
---|
874 |
|
---|
875 | /* The public lookup (case insensitive) string interface. */
|
---|
876 | const char *
|
---|
877 | strcache2_lookup (struct strcache2 *cache, const char *str, unsigned int length)
|
---|
878 | {
|
---|
879 | struct strcache2_entry const *entry;
|
---|
880 | unsigned int hash2;
|
---|
881 | unsigned int hash1 = strcache2_case_insensitive_hash_1 (str, length);
|
---|
882 | unsigned int idx;
|
---|
883 |
|
---|
884 | assert (cache->case_insensitive);
|
---|
885 |
|
---|
886 | cache->lookup_count++;
|
---|
887 |
|
---|
888 | /* Lookup the entry in the hash table, hoping for an
|
---|
889 | early match. */
|
---|
890 | idx = hash1 & cache->hash_mask;
|
---|
891 | entry = cache->hash_tab[idx];
|
---|
892 | if (strcache2_is_iequal (cache, entry, str, length, hash1))
|
---|
893 | return (const char *)(entry + 1);
|
---|
894 | if (!entry)
|
---|
895 | hash2 = 0;
|
---|
896 | else
|
---|
897 | {
|
---|
898 | cache->collision_1st_count++;
|
---|
899 |
|
---|
900 | hash2 = strcache2_case_insensitive_hash_2 (str, length);
|
---|
901 | idx += hash2;
|
---|
902 | idx &= cache->hash_mask;
|
---|
903 | entry = cache->hash_tab[idx];
|
---|
904 | if (strcache2_is_iequal (cache, entry, str, length, hash1))
|
---|
905 | return (const char *)(entry + 1);
|
---|
906 |
|
---|
907 | if (entry)
|
---|
908 | {
|
---|
909 | cache->collision_2nd_count++;
|
---|
910 | do
|
---|
911 | {
|
---|
912 | idx += hash2;
|
---|
913 | idx &= cache->hash_mask;
|
---|
914 | entry = cache->hash_tab[idx];
|
---|
915 | cache->collision_3rd_count++;
|
---|
916 | if (strcache2_is_iequal (cache, entry, str, length, hash1))
|
---|
917 | return (const char *)(entry + 1);
|
---|
918 | }
|
---|
919 | while (entry);
|
---|
920 | }
|
---|
921 | }
|
---|
922 |
|
---|
923 | /* Not found. */
|
---|
924 | return NULL;
|
---|
925 | }
|
---|
926 |
|
---|
927 | #endif /* HAVE_CASE_INSENSITIVE_FS */
|
---|
928 |
|
---|
929 | /* Is the given string cached? returns 1 if it is, 0 if it isn't. */
|
---|
930 | int
|
---|
931 | strcache2_is_cached (struct strcache2 *cache, const char *str)
|
---|
932 | {
|
---|
933 | /* Check mandatory alignment first. */
|
---|
934 | if (!((size_t)str & (sizeof (void *) - 1)))
|
---|
935 | {
|
---|
936 | /* Check the segment list and consider the question answered if the
|
---|
937 | string is within one of them. (Could check it more thoroughly...) */
|
---|
938 | struct strcache2_seg const *seg;
|
---|
939 | for (seg = cache->seg_head; seg; seg = seg->next)
|
---|
940 | if ((size_t)(str - seg->start) < seg->size)
|
---|
941 | return 1;
|
---|
942 | }
|
---|
943 |
|
---|
944 | return 0;
|
---|
945 | }
|
---|
946 |
|
---|
947 |
|
---|
948 | /* Verify the integrity of the specified string, returning 0 if OK. */
|
---|
949 | int
|
---|
950 | strcache2_verify_entry (struct strcache2 *cache, const char *str)
|
---|
951 | {
|
---|
952 | struct strcache2_entry const *entry;
|
---|
953 | unsigned hash;
|
---|
954 | const char *end;
|
---|
955 |
|
---|
956 | if ((size_t)str & (sizeof (void *) - 1))
|
---|
957 | {
|
---|
958 | fprintf (stderr,
|
---|
959 | "strcache2[%s]: missaligned string %p\nstring: %s\n",
|
---|
960 | cache->name, (void *)str, str);
|
---|
961 | return -1;
|
---|
962 | }
|
---|
963 |
|
---|
964 | entry = (struct strcache2_entry const *)str - 1;
|
---|
965 | end = memchr (str, '\0', entry->length);
|
---|
966 | if ((size_t)(end - str) == entry->length)
|
---|
967 | {
|
---|
968 | fprintf (stderr,
|
---|
969 | "strcache2[%s]: corrupt entry %p, length: %lu, expected %u;\nstring: %s\n",
|
---|
970 | cache->name, (void *)entry, (unsigned long)(end - str), entry->length, str);
|
---|
971 | return -1;
|
---|
972 | }
|
---|
973 |
|
---|
974 | hash = cache->case_insensitive
|
---|
975 | ? strcache2_case_insensitive_hash_1 (str, entry->length)
|
---|
976 | : strcache2_case_sensitive_hash_1 (str, entry->length);
|
---|
977 | if (hash != entry->hash1)
|
---|
978 | {
|
---|
979 | fprintf (stderr,
|
---|
980 | "strcache2[%s]: corrupt entry %p, hash#1: %x, expected %x;\nstring: %s\n",
|
---|
981 | cache->name, (void *)entry, hash, entry->hash1, str);
|
---|
982 | return -1;
|
---|
983 | }
|
---|
984 |
|
---|
985 | if (entry->hash2)
|
---|
986 | {
|
---|
987 | hash = cache->case_insensitive
|
---|
988 | ? strcache2_case_insensitive_hash_2 (str, entry->length)
|
---|
989 | : strcache2_case_sensitive_hash_2 (str, entry->length);
|
---|
990 | if (hash != entry->hash2)
|
---|
991 | {
|
---|
992 | fprintf (stderr,
|
---|
993 | "strcache2[%s]: corrupt entry %p, hash#2: %x, expected %x;\nstring: %s\n",
|
---|
994 | cache->name, (void *)entry, hash, entry->hash2, str);
|
---|
995 | return -1;
|
---|
996 | }
|
---|
997 | }
|
---|
998 |
|
---|
999 | return 0;
|
---|
1000 | }
|
---|
1001 |
|
---|
1002 | /* Fallback for calculating and returning the 2nd hash. */
|
---|
1003 | unsigned int
|
---|
1004 | strcache2_get_hash2_fallback (struct strcache2 *cache, const char *str)
|
---|
1005 | {
|
---|
1006 | struct strcache2_entry *entry = (struct strcache2_entry *) str - 1;
|
---|
1007 | unsigned hash2 = cache->case_insensitive
|
---|
1008 | ? strcache2_case_insensitive_hash_2 (str, entry->length)
|
---|
1009 | : strcache2_case_sensitive_hash_2 (str, entry->length);
|
---|
1010 | entry->hash2 = hash2;
|
---|
1011 | return hash2;
|
---|
1012 | }
|
---|
1013 |
|
---|
1014 | /* Calculates the case sensitive hash values of the string.
|
---|
1015 | The first hash is returned, the other is put at HASH2P. */
|
---|
1016 | unsigned int strcache2_hash_str (const char *str, unsigned int length, unsigned int *hash2p)
|
---|
1017 | {
|
---|
1018 | *hash2p = strcache2_case_sensitive_hash_2 (str, length);
|
---|
1019 | return strcache2_case_sensitive_hash_1 (str, length);
|
---|
1020 | }
|
---|
1021 |
|
---|
1022 | /* Calculates the case insensitive hash values of the string.
|
---|
1023 | The first hash is returned, the other is put at HASH2P. */
|
---|
1024 | unsigned int strcache2_hash_istr (const char *str, unsigned int length, unsigned int *hash2p)
|
---|
1025 | {
|
---|
1026 | *hash2p = strcache2_case_insensitive_hash_2 (str, length);
|
---|
1027 | return strcache2_case_insensitive_hash_1 (str, length);
|
---|
1028 | }
|
---|
1029 |
|
---|
1030 |
|
---|
1031 |
|
---|
1032 | /* Initalizes a new cache. */
|
---|
1033 | void
|
---|
1034 | strcache2_init (struct strcache2 *cache, const char *name, unsigned int size,
|
---|
1035 | unsigned int def_seg_size, int case_insensitive, int thread_safe)
|
---|
1036 | {
|
---|
1037 | unsigned hash_shift;
|
---|
1038 | assert (!thread_safe);
|
---|
1039 |
|
---|
1040 | /* calc the size as a power of two */
|
---|
1041 | if (!size)
|
---|
1042 | hash_shift = STRCACHE2_HASH_SHIFT;
|
---|
1043 | else
|
---|
1044 | {
|
---|
1045 | assert (size <= (~0U / 2 + 1));
|
---|
1046 | for (hash_shift = 8; (1U << hash_shift) < size; hash_shift++)
|
---|
1047 | /* nothing */;
|
---|
1048 | }
|
---|
1049 |
|
---|
1050 | /* adjust the default segment size */
|
---|
1051 | if (!def_seg_size)
|
---|
1052 | def_seg_size = STRCACHE2_SEG_SIZE;
|
---|
1053 | else if (def_seg_size < sizeof (struct strcache2_seg) * 10)
|
---|
1054 | def_seg_size = sizeof (struct strcache2_seg) * 10;
|
---|
1055 | else if ((def_seg_size & 0xfff) < 0xf00)
|
---|
1056 | def_seg_size = ((def_seg_size + 0xfff) & ~0xfffU);
|
---|
1057 |
|
---|
1058 |
|
---|
1059 | /* init the structure. */
|
---|
1060 | cache->case_insensitive = case_insensitive;
|
---|
1061 | cache->hash_mask = (1U << hash_shift) - 1U;
|
---|
1062 | cache->count = 0;
|
---|
1063 | cache->lookup_count = 0;
|
---|
1064 | cache->collision_1st_count = 0;
|
---|
1065 | cache->collision_2nd_count = 0;
|
---|
1066 | cache->collision_3rd_count = 0;
|
---|
1067 | cache->rehash_count = (1U << hash_shift) / 4 * 3; /* rehash at 75% */
|
---|
1068 | cache->init_size = 1U << hash_shift;
|
---|
1069 | cache->hash_size = 1U << hash_shift;
|
---|
1070 | cache->def_seg_size = def_seg_size;
|
---|
1071 | cache->lock = NULL;
|
---|
1072 | cache->name = name;
|
---|
1073 |
|
---|
1074 | /* allocate the hash table and first segment. */
|
---|
1075 | cache->hash_tab = (struct strcache2_entry **)
|
---|
1076 | xmalloc (cache->init_size * sizeof (struct strcache2_entry *));
|
---|
1077 | memset (cache->hash_tab, '\0', cache->init_size * sizeof (struct strcache2_entry *));
|
---|
1078 | strcache2_new_seg (cache, 0);
|
---|
1079 |
|
---|
1080 | /* link it */
|
---|
1081 | cache->next = strcache_head;
|
---|
1082 | strcache_head = cache;
|
---|
1083 | }
|
---|
1084 |
|
---|
1085 |
|
---|
1086 | /* Terminates a string cache, freeing all memory and other
|
---|
1087 | associated resources. */
|
---|
1088 | void
|
---|
1089 | strcache2_term (struct strcache2 *cache)
|
---|
1090 | {
|
---|
1091 | /* unlink it */
|
---|
1092 | if (strcache_head == cache)
|
---|
1093 | strcache_head = cache->next;
|
---|
1094 | else
|
---|
1095 | {
|
---|
1096 | struct strcache2 *prev = strcache_head;
|
---|
1097 | while (prev->next != cache)
|
---|
1098 | prev = prev->next;
|
---|
1099 | assert (prev);
|
---|
1100 | prev->next = cache->next;
|
---|
1101 | }
|
---|
1102 |
|
---|
1103 | /* free the memory segments */
|
---|
1104 | do
|
---|
1105 | {
|
---|
1106 | void *free_it = cache->seg_head;
|
---|
1107 | cache->seg_head = cache->seg_head->next;
|
---|
1108 | free (free_it);
|
---|
1109 | }
|
---|
1110 | while (cache->seg_head);
|
---|
1111 |
|
---|
1112 | /* free the hash and clear the structure. */
|
---|
1113 | free (cache->hash_tab);
|
---|
1114 | memset (cache, '\0', sizeof (struct strcache2));
|
---|
1115 | }
|
---|
1116 |
|
---|
1117 | /* Print statistics a string cache. */
|
---|
1118 | void
|
---|
1119 | strcache2_print_stats (struct strcache2 *cache, const char *prefix)
|
---|
1120 | {
|
---|
1121 | unsigned int seg_count = 0;
|
---|
1122 | unsigned long seg_total_bytes = 0;
|
---|
1123 | unsigned long seg_avg_bytes;
|
---|
1124 | unsigned long seg_avail_bytes = 0;
|
---|
1125 | unsigned long seg_max_bytes = 0;
|
---|
1126 | struct strcache2_seg *seg;
|
---|
1127 | unsigned long str_total_len = 0;
|
---|
1128 | unsigned int str_avg_len;
|
---|
1129 | unsigned int str_min_len = ~0U;
|
---|
1130 | unsigned int str_max_len = 0;
|
---|
1131 | unsigned int idx;
|
---|
1132 | unsigned int rehashes;
|
---|
1133 |
|
---|
1134 | printf (_("\n%s strcache2: %s\n"), prefix, cache->name);
|
---|
1135 |
|
---|
1136 | /* Segment statistics. */
|
---|
1137 | for (seg = cache->seg_head; seg; seg = seg->next)
|
---|
1138 | {
|
---|
1139 | seg_count++;
|
---|
1140 | seg_total_bytes += seg->size;
|
---|
1141 | seg_avail_bytes += seg->avail;
|
---|
1142 | if (seg->size > seg_max_bytes)
|
---|
1143 | seg_max_bytes = seg->size;
|
---|
1144 | }
|
---|
1145 | seg_avg_bytes = seg_total_bytes / seg_count;
|
---|
1146 | printf (_("%s %u segments: total = %lu / max = %lu / avg = %lu / def = %u avail = %lu\n"),
|
---|
1147 | prefix, seg_count, seg_total_bytes, seg_max_bytes, seg_avg_bytes,
|
---|
1148 | cache->def_seg_size, seg_avail_bytes);
|
---|
1149 |
|
---|
1150 | /* String statistics. */
|
---|
1151 | idx = cache->hash_size;
|
---|
1152 | while (idx-- > 0)
|
---|
1153 | {
|
---|
1154 | struct strcache2_entry const *entry = cache->hash_tab[idx];
|
---|
1155 | if (entry)
|
---|
1156 | {
|
---|
1157 | unsigned int length = entry->length;
|
---|
1158 | str_total_len += length;
|
---|
1159 | if (length > str_max_len)
|
---|
1160 | str_max_len = length;
|
---|
1161 | if (length < str_min_len)
|
---|
1162 | str_min_len = length;
|
---|
1163 | }
|
---|
1164 | }
|
---|
1165 | str_avg_len = cache->count ? str_total_len / cache->count : 0;
|
---|
1166 | printf (_("%s %u strings: total len = %lu / max = %u / avg = %u / min = %u\n"),
|
---|
1167 | prefix, cache->count, str_total_len, str_max_len, str_avg_len, str_min_len);
|
---|
1168 |
|
---|
1169 | /* Hash statistics. */
|
---|
1170 | idx = cache->init_size;
|
---|
1171 | rehashes = 0;
|
---|
1172 | while (idx < cache->hash_size)
|
---|
1173 | {
|
---|
1174 | idx *= 2;
|
---|
1175 | rehashes++;
|
---|
1176 | }
|
---|
1177 |
|
---|
1178 | printf (_("%s hash size = %u mask = %#x rehashed %u times lookups = %lu\n"),
|
---|
1179 | prefix, cache->hash_size, cache->hash_mask, rehashes, cache->lookup_count);
|
---|
1180 | printf (_("%s hash collisions 1st = %lu (%u%%) 2nd = %lu (%u%%) 3rd = %lu (%u%%)\n"),
|
---|
1181 | prefix,
|
---|
1182 | cache->collision_1st_count, (unsigned int)((float)cache->collision_1st_count * 100 / cache->lookup_count),
|
---|
1183 | cache->collision_2nd_count, (unsigned int)((float)cache->collision_2nd_count * 100 / cache->lookup_count),
|
---|
1184 | cache->collision_3rd_count, (unsigned int)((float)cache->collision_3rd_count * 100 / cache->lookup_count));
|
---|
1185 | }
|
---|
1186 |
|
---|
1187 | /* Print statistics for all string caches. */
|
---|
1188 | void
|
---|
1189 | strcache2_print_stats_all (const char *prefix)
|
---|
1190 | {
|
---|
1191 | struct strcache2 *cur;
|
---|
1192 | for (cur = strcache_head; cur; cur = cur->next)
|
---|
1193 | strcache2_print_stats (cur, prefix);
|
---|
1194 | }
|
---|
1195 |
|
---|