Debian Codegraph @

generic/tclHash.c

raw
1 /*
2 * tclHash.c --
3 *
4 * Implementation of in-memory hash tables for Tcl and Tcl-based
5 * applications.
6 *
7 * Copyright (c) 1991-1993 The Regents of the University of California.
8 * Copyright (c) 1994 Sun Microsystems, Inc.
9 *
10 * See the file "license.terms" for information on usage and redistribution of
11 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
12 */
13
14 #include "tclInt.h"
15
16 /*
17 * Prevent macros from clashing with function definitions.
18 */
19
20 #undef Tcl_FindHashEntry
21 #undef Tcl_CreateHashEntry
22
23 /*
24 * When there are this many entries per bucket, on average, rebuild the hash
25 * table to make it larger.
26 */
27
28 #define REBUILD_MULTIPLIER 3
29
30 /*
31 * The following macro takes a preliminary integer hash value and produces an
32 * index into a hash tables bucket list. The idea is to make it so that
33 * preliminary values that are arbitrarily similar will end up in different
34 * buckets. The hash function was taken from a random-number generator.
35 */
36
37 #define RANDOM_INDEX(tablePtr, i) \
38 ((((i)*1103515245UL) >> (tablePtr)->downShift) & (tablePtr)->mask)
39
40 /*
41 * Prototypes for the array hash key methods.
42 */
43
44 static Tcl_HashEntry * AllocArrayEntry(Tcl_HashTable *tablePtr, void *keyPtr);
45 static int CompareArrayKeys(void *keyPtr, Tcl_HashEntry *hPtr);
46 static unsigned int HashArrayKey(Tcl_HashTable *tablePtr, void *keyPtr);
47
48 /*
49 * Prototypes for the one word hash key methods. Not actually declared because
50 * this is a critical path that is implemented in the core hash table access
51 * function.
52 */
53
54 #if 0
55 static Tcl_HashEntry * AllocOneWordEntry(Tcl_HashTable *tablePtr,
56 void *keyPtr);
57 static int CompareOneWordKeys(void *keyPtr, Tcl_HashEntry *hPtr);
58 static unsigned int HashOneWordKey(Tcl_HashTable *tablePtr, void *keyPtr);
59 #endif
60
61 /*
62 * Prototypes for the string hash key methods.
63 */
64
65 static Tcl_HashEntry * AllocStringEntry(Tcl_HashTable *tablePtr,
66 void *keyPtr);
67 static int CompareStringKeys(void *keyPtr, Tcl_HashEntry *hPtr);
68 static unsigned int HashStringKey(Tcl_HashTable *tablePtr, void *keyPtr);
69
70 /*
71 * Function prototypes for static functions in this file:
72 */
73
74 static Tcl_HashEntry * BogusFind(Tcl_HashTable *tablePtr, const char *key);
75 static Tcl_HashEntry * BogusCreate(Tcl_HashTable *tablePtr, const char *key,
76 int *newPtr);
77 static Tcl_HashEntry * CreateHashEntry(Tcl_HashTable *tablePtr, const char *key,
78 int *newPtr);
79 static Tcl_HashEntry * FindHashEntry(Tcl_HashTable *tablePtr, const char *key);
80 static void RebuildTable(Tcl_HashTable *tablePtr);
81
82 const Tcl_HashKeyType tclArrayHashKeyType = {
83 TCL_HASH_KEY_TYPE_VERSION, /* version */
84 TCL_HASH_KEY_RANDOMIZE_HASH, /* flags */
85 HashArrayKey, /* hashKeyProc */
86 CompareArrayKeys, /* compareKeysProc */
87 AllocArrayEntry, /* allocEntryProc */
88 NULL /* freeEntryProc */
89 };
90
91 const Tcl_HashKeyType tclOneWordHashKeyType = {
92 TCL_HASH_KEY_TYPE_VERSION, /* version */
93 0, /* flags */
94 NULL, /* HashOneWordKey, */ /* hashProc */
95 NULL, /* CompareOneWordKey, */ /* compareProc */
96 NULL, /* AllocOneWordKey, */ /* allocEntryProc */
97 NULL /* FreeOneWordKey, */ /* freeEntryProc */
98 };
99
100 const Tcl_HashKeyType tclStringHashKeyType = {
101 TCL_HASH_KEY_TYPE_VERSION, /* version */
102 0, /* flags */
103 HashStringKey, /* hashKeyProc */
104 CompareStringKeys, /* compareKeysProc */
105 AllocStringEntry, /* allocEntryProc */
106 NULL /* freeEntryProc */
107 };
108
109 /*
110 *----------------------------------------------------------------------
111 *
112 * Tcl_InitHashTable --
113 *
114 * Given storage for a hash table, set up the fields to prepare the hash
115 * table for use.
116 *
117 * Results:
118 * None.
119 *
120 * Side effects:
121 * TablePtr is now ready to be passed to Tcl_FindHashEntry and
122 * Tcl_CreateHashEntry.
123 *
124 *----------------------------------------------------------------------
125 */
126
127 void
128 Tcl_InitHashTable(
129 Tcl_HashTable *tablePtr,
130 /* Pointer to table record, which is supplied
131 * by the caller. */
132 int keyType) /* Type of keys to use in table:
133 * TCL_STRING_KEYS, TCL_ONE_WORD_KEYS, or an
134 * integer >= 2. */
135 {
136 /*
137 * Use a special value to inform the extended version that it must not
138 * access any of the new fields in the Tcl_HashTable. If an extension is
139 * rebuilt then any calls to this function will be redirected to the
140 * extended version by a macro.
141 */
142
143 Tcl_InitCustomHashTable(tablePtr, keyType, (const Tcl_HashKeyType *) -1);
144 }
145
146 /*
147 *----------------------------------------------------------------------
148 *
149 * Tcl_InitCustomHashTable --
150 *
151 * Given storage for a hash table, set up the fields to prepare the hash
152 * table for use. This is an extended version of Tcl_InitHashTable which
153 * supports user defined keys.
154 *
155 * Results:
156 * None.
157 *
158 * Side effects:
159 * TablePtr is now ready to be passed to Tcl_FindHashEntry and
160 * Tcl_CreateHashEntry.
161 *
162 *----------------------------------------------------------------------
163 */
164
165 void
166 Tcl_InitCustomHashTable(
167 Tcl_HashTable *tablePtr,
168 /* Pointer to table record, which is supplied
169 * by the caller. */
170 int keyType, /* Type of keys to use in table:
171 * TCL_STRING_KEYS, TCL_ONE_WORD_KEYS,
172 * TCL_CUSTOM_TYPE_KEYS, TCL_CUSTOM_PTR_KEYS,
173 * or an integer >= 2. */
174 const Tcl_HashKeyType *typePtr) /* Pointer to structure which defines the
175 * behaviour of this table. */
176 {
177 #if (TCL_SMALL_HASH_TABLE != 4)
178 Tcl_Panic("Tcl_InitCustomHashTable: TCL_SMALL_HASH_TABLE is %d, not 4",
179 TCL_SMALL_HASH_TABLE);
180 #endif
181
182 tablePtr->buckets = tablePtr->staticBuckets;
183 tablePtr->staticBuckets[0] = tablePtr->staticBuckets[1] = 0;
184 tablePtr->staticBuckets[2] = tablePtr->staticBuckets[3] = 0;
185 tablePtr->numBuckets = TCL_SMALL_HASH_TABLE;
186 tablePtr->numEntries = 0;
187 tablePtr->rebuildSize = TCL_SMALL_HASH_TABLE*REBUILD_MULTIPLIER;
188 tablePtr->downShift = 28;
189 tablePtr->mask = 3;
190 tablePtr->keyType = keyType;
191 tablePtr->findProc = FindHashEntry;
192 tablePtr->createProc = CreateHashEntry;
193
194 if (typePtr == NULL) {
195 /*
196 * The caller has been rebuilt so the hash table is an extended
197 * version.
198 */
199 } else if (typePtr != (Tcl_HashKeyType *) -1) {
200 /*
201 * The caller is requesting a customized hash table so it must be an
202 * extended version.
203 */
204
205 tablePtr->typePtr = typePtr;
206 } else {
207 /*
208 * The caller has not been rebuilt so the hash table is not extended.
209 */
210 }
211 }
212
213 /*
214 *----------------------------------------------------------------------
215 *
216 * Tcl_FindHashEntry --
217 *
218 * Given a hash table find the entry with a matching key.
219 *
220 * Results:
221 * The return value is a token for the matching entry in the hash table,
222 * or NULL if there was no matching entry.
223 *
224 * Side effects:
225 * None.
226 *
227 *----------------------------------------------------------------------
228 */
229
230 Tcl_HashEntry *
231 Tcl_FindHashEntry(
232 Tcl_HashTable *tablePtr, /* Table in which to lookup entry. */
233 const void *key) /* Key to use to find matching entry. */
234 {
235 return (*((tablePtr)->findProc))(tablePtr, key);
236 }
237
238 static Tcl_HashEntry *
239 FindHashEntry(
240 Tcl_HashTable *tablePtr, /* Table in which to lookup entry. */
241 const char *key) /* Key to use to find matching entry. */
242 {
243 return CreateHashEntry(tablePtr, key, NULL);
244 }
245
246
247 /*
248 *----------------------------------------------------------------------
249 *
250 * Tcl_CreateHashEntry --
251 *
252 * Given a hash table with string keys, and a string key, find the entry
253 * with a matching key. If there is no matching entry, then create a new
254 * entry that does match.
255 *
256 * Results:
257 * The return value is a pointer to the matching entry. If this is a
258 * newly-created entry, then *newPtr will be set to a non-zero value;
259 * otherwise *newPtr will be set to 0. If this is a new entry the value
260 * stored in the entry will initially be 0.
261 *
262 * Side effects:
263 * A new entry may be added to the hash table.
264 *
265 *----------------------------------------------------------------------
266 */
267
268 Tcl_HashEntry *
269 Tcl_CreateHashEntry(
270 Tcl_HashTable *tablePtr, /* Table in which to lookup entry. */
271 const void *key, /* Key to use to find or create matching
272 * entry. */
273 int *newPtr) /* Store info here telling whether a new entry
274 * was created. */
275 {
276 return (*((tablePtr)->createProc))(tablePtr, key, newPtr);
277 }
278
279 static Tcl_HashEntry *
280 CreateHashEntry(
281 Tcl_HashTable *tablePtr, /* Table in which to lookup entry. */
282 const char *key, /* Key to use to find or create matching
283 * entry. */
284 int *newPtr) /* Store info here telling whether a new entry
285 * was created. */
286 {
287 Tcl_HashEntry *hPtr;
288 const Tcl_HashKeyType *typePtr;
289 unsigned int hash;
290 int index;
291
292 if (tablePtr->keyType == TCL_STRING_KEYS) {
293 typePtr = &tclStringHashKeyType;
294 } else if (tablePtr->keyType == TCL_ONE_WORD_KEYS) {
295 typePtr = &tclOneWordHashKeyType;
296 } else if (tablePtr->keyType == TCL_CUSTOM_TYPE_KEYS
297 || tablePtr->keyType == TCL_CUSTOM_PTR_KEYS) {
298 typePtr = tablePtr->typePtr;
299 } else {
300 typePtr = &tclArrayHashKeyType;
301 }
302
303 if (typePtr->hashKeyProc) {
304 hash = typePtr->hashKeyProc(tablePtr, (void *) key);
305 if (typePtr->flags & TCL_HASH_KEY_RANDOMIZE_HASH) {
306 index = RANDOM_INDEX(tablePtr, hash);
307 } else {
308 index = hash & tablePtr->mask;
309 }
310 } else {
311 hash = PTR2UINT(key);
312 index = RANDOM_INDEX(tablePtr, hash);
313 }
314
315 /*
316 * Search all of the entries in the appropriate bucket.
317 */
318
319 if (typePtr->compareKeysProc) {
320 Tcl_CompareHashKeysProc *compareKeysProc = typePtr->compareKeysProc;
321 if (typePtr->flags & TCL_HASH_KEY_DIRECT_COMPARE) {
322 for (hPtr = tablePtr->buckets[index]; hPtr != NULL;
323 hPtr = hPtr->nextPtr) {
324 #if TCL_HASH_KEY_STORE_HASH
325 if (hash != PTR2UINT(hPtr->hash)) {
326 continue;
327 }
328 #endif
329 /* if keys pointers or values are equal */
330 if ((key == hPtr->key.oneWordValue)
331 || compareKeysProc((void *) key, hPtr)
332 ) {
333 if (newPtr) {
334 *newPtr = 0;
335 }
336 return hPtr;
337 }
338 }
339 } else { /* no direct compare - compare key addresses only */
340 for (hPtr = tablePtr->buckets[index]; hPtr != NULL;
341 hPtr = hPtr->nextPtr) {
342 #if TCL_HASH_KEY_STORE_HASH
343 if (hash != PTR2UINT(hPtr->hash)) {
344 continue;
345 }
346 #endif
347 /* if needle pointer equals content pointer or values equal */
348 if ((key == hPtr->key.string)
349 || compareKeysProc((void *) key, hPtr)
350 ) {
351 if (newPtr) {
352 *newPtr = 0;
353 }
354 return hPtr;
355 }
356 }
357 }
358 } else {
359 for (hPtr = tablePtr->buckets[index]; hPtr != NULL;
360 hPtr = hPtr->nextPtr) {
361 #if TCL_HASH_KEY_STORE_HASH
362 if (hash != PTR2UINT(hPtr->hash)) {
363 continue;
364 }
365 #endif
366 if (key == hPtr->key.oneWordValue) {
367 if (newPtr) {
368 *newPtr = 0;
369 }
370 return hPtr;
371 }
372 }
373 }
374
375 if (!newPtr) {
376 return NULL;
377 }
378
379 /*
380 * Entry not found. Add a new one to the bucket.
381 */
382
383 *newPtr = 1;
384 if (typePtr->allocEntryProc) {
385 hPtr = typePtr->allocEntryProc(tablePtr, (void *) key);
386 } else {
387 hPtr = ckalloc(sizeof(Tcl_HashEntry));
388 hPtr->key.oneWordValue = (char *) key;
389 hPtr->clientData = 0;
390 }
391
392 hPtr->tablePtr = tablePtr;
393 #if TCL_HASH_KEY_STORE_HASH
394 hPtr->hash = UINT2PTR(hash);
395 hPtr->nextPtr = tablePtr->buckets[index];
396 tablePtr->buckets[index] = hPtr;
397 #else
398 hPtr->bucketPtr = &tablePtr->buckets[index];
399 hPtr->nextPtr = *hPtr->bucketPtr;
400 *hPtr->bucketPtr = hPtr;
401 #endif
402 tablePtr->numEntries++;
403
404 /*
405 * If the table has exceeded a decent size, rebuild it with many more
406 * buckets.
407 */
408
409 if (tablePtr->numEntries >= tablePtr->rebuildSize) {
410 RebuildTable(tablePtr);
411 }
412 return hPtr;
413 }
414
415 /*
416 *----------------------------------------------------------------------
417 *
418 * Tcl_DeleteHashEntry --
419 *
420 * Remove a single entry from a hash table.
421 *
422 * Results:
423 * None.
424 *
425 * Side effects:
426 * The entry given by entryPtr is deleted from its table and should never
427 * again be used by the caller. It is up to the caller to free the
428 * clientData field of the entry, if that is relevant.
429 *
430 *----------------------------------------------------------------------
431 */
432
433 void
434 Tcl_DeleteHashEntry(
435 Tcl_HashEntry *entryPtr)
436 {
437 Tcl_HashEntry *prevPtr;
438 const Tcl_HashKeyType *typePtr;
439 Tcl_HashTable *tablePtr;
440 Tcl_HashEntry **bucketPtr;
441 #if TCL_HASH_KEY_STORE_HASH
442 int index;
443 #endif
444
445 tablePtr = entryPtr->tablePtr;
446
447 if (tablePtr->keyType == TCL_STRING_KEYS) {
448 typePtr = &tclStringHashKeyType;
449 } else if (tablePtr->keyType == TCL_ONE_WORD_KEYS) {
450 typePtr = &tclOneWordHashKeyType;
451 } else if (tablePtr->keyType == TCL_CUSTOM_TYPE_KEYS
452 || tablePtr->keyType == TCL_CUSTOM_PTR_KEYS) {
453 typePtr = tablePtr->typePtr;
454 } else {
455 typePtr = &tclArrayHashKeyType;
456 }
457
458 #if TCL_HASH_KEY_STORE_HASH
459 if (typePtr->hashKeyProc == NULL
460 || typePtr->flags & TCL_HASH_KEY_RANDOMIZE_HASH) {
461 index = RANDOM_INDEX(tablePtr, PTR2INT(entryPtr->hash));
462 } else {
463 index = PTR2UINT(entryPtr->hash) & tablePtr->mask;
464 }
465
466 bucketPtr = &tablePtr->buckets[index];
467 #else
468 bucketPtr = entryPtr->bucketPtr;
469 #endif
470
471 if (*bucketPtr == entryPtr) {
472 *bucketPtr = entryPtr->nextPtr;
473 } else {
474 for (prevPtr = *bucketPtr; ; prevPtr = prevPtr->nextPtr) {
475 if (prevPtr == NULL) {
476 Tcl_Panic("malformed bucket chain in Tcl_DeleteHashEntry");
477 }
478 if (prevPtr->nextPtr == entryPtr) {
479 prevPtr->nextPtr = entryPtr->nextPtr;
480 break;
481 }
482 }
483 }
484
485 tablePtr->numEntries--;
486 if (typePtr->freeEntryProc) {
487 typePtr->freeEntryProc(entryPtr);
488 } else {
489 ckfree(entryPtr);
490 }
491 }
492
493 /*
494 *----------------------------------------------------------------------
495 *
496 * Tcl_DeleteHashTable --
497 *
498 * Free up everything associated with a hash table except for the record
499 * for the table itself.
500 *
501 * Results:
502 * None.
503 *
504 * Side effects:
505 * The hash table is no longer useable.
506 *
507 *----------------------------------------------------------------------
508 */
509
510 void
511 Tcl_DeleteHashTable(
512 Tcl_HashTable *tablePtr) /* Table to delete. */
513 {
514 Tcl_HashEntry *hPtr, *nextPtr;
515 const Tcl_HashKeyType *typePtr;
516 int i;
517
518 if (tablePtr->keyType == TCL_STRING_KEYS) {
519 typePtr = &tclStringHashKeyType;
520 } else if (tablePtr->keyType == TCL_ONE_WORD_KEYS) {
521 typePtr = &tclOneWordHashKeyType;
522 } else if (tablePtr->keyType == TCL_CUSTOM_TYPE_KEYS
523 || tablePtr->keyType == TCL_CUSTOM_PTR_KEYS) {
524 typePtr = tablePtr->typePtr;
525 } else {
526 typePtr = &tclArrayHashKeyType;
527 }
528
529 /*
530 * Free up all the entries in the table.
531 */
532
533 for (i = 0; i < tablePtr->numBuckets; i++) {
534 hPtr = tablePtr->buckets[i];
535 while (hPtr != NULL) {
536 nextPtr = hPtr->nextPtr;
537 if (typePtr->freeEntryProc) {
538 typePtr->freeEntryProc(hPtr);
539 } else {
540 ckfree(hPtr);
541 }
542 hPtr = nextPtr;
543 }
544 }
545
546 /*
547 * Free up the bucket array, if it was dynamically allocated.
548 */
549
550 if (tablePtr->buckets != tablePtr->staticBuckets) {
551 if (typePtr->flags & TCL_HASH_KEY_SYSTEM_HASH) {
552 TclpSysFree((char *) tablePtr->buckets);
553 } else {
554 ckfree(tablePtr->buckets);
555 }
556 }
557
558 /*
559 * Arrange for panics if the table is used again without
560 * re-initialization.
561 */
562
563 tablePtr->findProc = BogusFind;
564 tablePtr->createProc = BogusCreate;
565 }
566
567 /*
568 *----------------------------------------------------------------------
569 *
570 * Tcl_FirstHashEntry --
571 *
572 * Locate the first entry in a hash table and set up a record that can be
573 * used to step through all the remaining entries of the table.
574 *
575 * Results:
576 * The return value is a pointer to the first entry in tablePtr, or NULL
577 * if tablePtr has no entries in it. The memory at *searchPtr is
578 * initialized so that subsequent calls to Tcl_NextHashEntry will return
579 * all of the entries in the table, one at a time.
580 *
581 * Side effects:
582 * None.
583 *
584 *----------------------------------------------------------------------
585 */
586
587 Tcl_HashEntry *
588 Tcl_FirstHashEntry(
589 Tcl_HashTable *tablePtr, /* Table to search. */
590 Tcl_HashSearch *searchPtr) /* Place to store information about progress
591 * through the table. */
592 {
593 searchPtr->tablePtr = tablePtr;
594 searchPtr->nextIndex = 0;
595 searchPtr->nextEntryPtr = NULL;
596 return Tcl_NextHashEntry(searchPtr);
597 }
598
599 /*
600 *----------------------------------------------------------------------
601 *
602 * Tcl_NextHashEntry --
603 *
604 * Once a hash table enumeration has been initiated by calling
605 * Tcl_FirstHashEntry, this function may be called to return successive
606 * elements of the table.
607 *
608 * Results:
609 * The return value is the next entry in the hash table being enumerated,
610 * or NULL if the end of the table is reached.
611 *
612 * Side effects:
613 * None.
614 *
615 *----------------------------------------------------------------------
616 */
617
618 Tcl_HashEntry *
619 Tcl_NextHashEntry(
620 Tcl_HashSearch *searchPtr)
621 /* Place to store information about progress
622 * through the table. Must have been
623 * initialized by calling
624 * Tcl_FirstHashEntry. */
625 {
626 Tcl_HashEntry *hPtr;
627 Tcl_HashTable *tablePtr = searchPtr->tablePtr;
628
629 while (searchPtr->nextEntryPtr == NULL) {
630 if (searchPtr->nextIndex >= tablePtr->numBuckets) {
631 return NULL;
632 }
633 searchPtr->nextEntryPtr =
634 tablePtr->buckets[searchPtr->nextIndex];
635 searchPtr->nextIndex++;
636 }
637 hPtr = searchPtr->nextEntryPtr;
638 searchPtr->nextEntryPtr = hPtr->nextPtr;
639 return hPtr;
640 }
641
642 /*
643 *----------------------------------------------------------------------
644 *
645 * Tcl_HashStats --
646 *
647 * Return statistics describing the layout of the hash table in its hash
648 * buckets.
649 *
650 * Results:
651 * The return value is a malloc-ed string containing information about
652 * tablePtr. It is the caller's responsibility to free this string.
653 *
654 * Side effects:
655 * None.
656 *
657 *----------------------------------------------------------------------
658 */
659
660 char *
661 Tcl_HashStats(
662 Tcl_HashTable *tablePtr) /* Table for which to produce stats. */
663 {
664 #define NUM_COUNTERS 10
665 int count[NUM_COUNTERS], overflow, i, j;
666 double average, tmp;
667 Tcl_HashEntry *hPtr;
668 char *result, *p;
669
670 /*
671 * Compute a histogram of bucket usage.
672 */
673
674 for (i = 0; i < NUM_COUNTERS; i++) {
675 count[i] = 0;
676 }
677 overflow = 0;
678 average = 0.0;
679 for (i = 0; i < tablePtr->numBuckets; i++) {
680 j = 0;
681 for (hPtr = tablePtr->buckets[i]; hPtr != NULL; hPtr = hPtr->nextPtr) {
682 j++;
683 }
684 if (j < NUM_COUNTERS) {
685 count[j]++;
686 } else {
687 overflow++;
688 }
689 tmp = j;
690 if (tablePtr->numEntries != 0) {
691 average += (tmp+1.0)*(tmp/tablePtr->numEntries)/2.0;
692 }
693 }
694
695 /*
696 * Print out the histogram and a few other pieces of information.
697 */
698
699 result = ckalloc((NUM_COUNTERS * 60) + 300);
700 snprintf(result, 60, "%d entries in table, %d buckets\n",
701 tablePtr->numEntries, tablePtr->numBuckets);
702 p = result + strlen(result);
703 for (i = 0; i < NUM_COUNTERS; i++) {
704 snprintf(p, 60, "number of buckets with %d entries: %d\n",
705 i, count[i]);
706 p += strlen(p);
707 }
708 snprintf(p, 60, "number of buckets with %d or more entries: %d\n",
709 NUM_COUNTERS, overflow);
710 p += strlen(p);
711 snprintf(p, 60, "average search distance for entry: %.1f", average);
712 return result;
713 }
714
715 /*
716 *----------------------------------------------------------------------
717 *
718 * AllocArrayEntry --
719 *
720 * Allocate space for a Tcl_HashEntry containing the array key.
721 *
722 * Results:
723 * The return value is a pointer to the created entry.
724 *
725 * Side effects:
726 * None.
727 *
728 *----------------------------------------------------------------------
729 */
730
731 static Tcl_HashEntry *
732 AllocArrayEntry(
733 Tcl_HashTable *tablePtr, /* Hash table. */
734 void *keyPtr) /* Key to store in the hash table entry. */
735 {
736 int *array = (int *) keyPtr;
737 int *iPtr1, *iPtr2;
738 Tcl_HashEntry *hPtr;
739 int count;
740 unsigned int size;
741
742 count = tablePtr->keyType;
743
744 size = TclOffset(Tcl_HashEntry, key) + count*sizeof(int);
745 if (size < sizeof(Tcl_HashEntry)) {
746 size = sizeof(Tcl_HashEntry);
747 }
748 hPtr = ckalloc(size);
749
750 for (iPtr1 = array, iPtr2 = hPtr->key.words;
751 count > 0; count--, iPtr1++, iPtr2++) {
752 *iPtr2 = *iPtr1;
753 }
754 hPtr->clientData = 0;
755
756 return hPtr;
757 }
758
759 /*
760 *----------------------------------------------------------------------
761 *
762 * CompareArrayKeys --
763 *
764 * Compares two array keys.
765 *
766 * Results:
767 * The return value is 0 if they are different and 1 if they are the
768 * same.
769 *
770 * Side effects:
771 * None.
772 *
773 *----------------------------------------------------------------------
774 */
775
776 static int
777 CompareArrayKeys(
778 void *keyPtr, /* New key to compare. */
779 Tcl_HashEntry *hPtr) /* Existing key to compare. */
780 {
781 const int *iPtr1 = (const int *) keyPtr;
782 const int *iPtr2 = (const int *) hPtr->key.words;
783 Tcl_HashTable *tablePtr = hPtr->tablePtr;
784 int count;
785
786 for (count = tablePtr->keyType; ; count--, iPtr1++, iPtr2++) {
787 if (count == 0) {
788 return 1;
789 }
790 if (*iPtr1 != *iPtr2) {
791 break;
792 }
793 }
794 return 0;
795 }
796
797 /*
798 *----------------------------------------------------------------------
799 *
800 * HashArrayKey --
801 *
802 * Compute a one-word summary of an array, which can be used to generate
803 * a hash index.
804 *
805 * Results:
806 * The return value is a one-word summary of the information in
807 * string.
808 *
809 * Side effects:
810 * None.
811 *
812 *----------------------------------------------------------------------
813 */
814
815 static unsigned int
816 HashArrayKey(
817 Tcl_HashTable *tablePtr, /* Hash table. */
818 void *keyPtr) /* Key from which to compute hash value. */
819 {
820 const int *array = (const int *) keyPtr;
821 unsigned int result;
822 int count;
823
824 for (result = 0, count = tablePtr->keyType; count > 0;
825 count--, array++) {
826 result += *array;
827 }
828 return result;
829 }
830
831 /*
832 *----------------------------------------------------------------------
833 *
834 * AllocStringEntry --
835 *
836 * Allocate space for a Tcl_HashEntry containing the string key.
837 *
838 * Results:
839 * The return value is a pointer to the created entry.
840 *
841 * Side effects:
842 * None.
843 *
844 *----------------------------------------------------------------------
845 */
846
847 static Tcl_HashEntry *
848 AllocStringEntry(
849 Tcl_HashTable *tablePtr, /* Hash table. */
850 void *keyPtr) /* Key to store in the hash table entry. */
851 {
852 const char *string = (const char *) keyPtr;
853 Tcl_HashEntry *hPtr;
854 unsigned int size, allocsize;
855
856 allocsize = size = strlen(string) + 1;
857 if (size < sizeof(hPtr->key)) {
858 allocsize = sizeof(hPtr->key);
859 }
860 hPtr = ckalloc(TclOffset(Tcl_HashEntry, key) + allocsize);
861 memset(hPtr, 0, TclOffset(Tcl_HashEntry, key) + allocsize);
862 memcpy(hPtr->key.string, string, size);
863 hPtr->clientData = 0;
864 return hPtr;
865 }
866
867 /*
868 *----------------------------------------------------------------------
869 *
870 * CompareStringKeys --
871 *
872 * Compares two string keys.
873 *
874 * Results:
875 * The return value is 0 if they are different and 1 if they are the
876 * same.
877 *
878 * Side effects:
879 * None.
880 *
881 *----------------------------------------------------------------------
882 */
883
884 static int
885 CompareStringKeys(
886 void *keyPtr, /* New key to compare. */
887 Tcl_HashEntry *hPtr) /* Existing key to compare. */
888 {
889 const char *p1 = (const char *) keyPtr;
890 const char *p2 = (const char *) hPtr->key.string;
891
892 return !strcmp(p1, p2);
893 }
894
895 /*
896 *----------------------------------------------------------------------
897 *
898 * HashStringKey --
899 *
900 * Compute a one-word summary of a text string, which can be used to
901 * generate a hash index.
902 *
903 * Results:
904 * The return value is a one-word summary of the information in string.
905 *
906 * Side effects:
907 * None.
908 *
909 *----------------------------------------------------------------------
910 */
911
912 static unsigned
913 HashStringKey(
914 Tcl_HashTable *tablePtr, /* Hash table. */
915 void *keyPtr) /* Key from which to compute hash value. */
916 {
917 const char *string = keyPtr;
918 unsigned int result;
919 char c;
920
921 /*
922 * I tried a zillion different hash functions and asked many other people
923 * for advice. Many people had their own favorite functions, all
924 * different, but no-one had much idea why they were good ones. I chose
925 * the one below (multiply by 9 and add new character) because of the
926 * following reasons:
927 *
928 * 1. Multiplying by 10 is perfect for keys that are decimal strings, and
929 * multiplying by 9 is just about as good.
930 * 2. Times-9 is (shift-left-3) plus (old). This means that each
931 * character's bits hang around in the low-order bits of the hash value
932 * for ever, plus they spread fairly rapidly up to the high-order bits
933 * to fill out the hash value. This seems works well both for decimal
934 * and non-decimal strings, but isn't strong against maliciously-chosen
935 * keys.
936 *
937 * Note that this function is very weak against malicious strings; it's
938 * very easy to generate multiple keys that have the same hashcode. On the
939 * other hand, that hardly ever actually occurs and this function *is*
940 * very cheap, even by comparison with industry-standard hashes like FNV.
941 * If real strength of hash is required though, use a custom hash based on
942 * Bob Jenkins's lookup3(), but be aware that it's significantly slower.
943 * Since Tcl command and namespace names are usually reasonably-named (the
944 * main use for string hashes in modern Tcl) speed is far more important
945 * than strength.
946 *
947 * See also HashString in tclLiteral.c.
948 * See also TclObjHashKey in tclObj.c.
949 *
950 * See [tcl-Feature Request #2958832]
951 */
952
953 if ((result = UCHAR(*string)) != 0) {
954 while ((c = *++string) != 0) {
955 result += (result << 3) + UCHAR(c);
956 }
957 }
958 return result;
959 }
960
961 /*
962 *----------------------------------------------------------------------
963 *
964 * BogusFind --
965 *
966 * This function is invoked when an Tcl_FindHashEntry is called on a
967 * table that has been deleted.
968 *
969 * Results:
970 * If Tcl_Panic returns (which it shouldn't) this function returns NULL.
971 *
972 * Side effects:
973 * Generates a panic.
974 *
975 *----------------------------------------------------------------------
976 */
977
978 /* ARGSUSED */
979 static Tcl_HashEntry *
980 BogusFind(
981 Tcl_HashTable *tablePtr, /* Table in which to lookup entry. */
982 const char *key) /* Key to use to find matching entry. */
983 {
984 Tcl_Panic("called %s on deleted table", "Tcl_FindHashEntry");
985 return NULL;
986 }
987
988 /*
989 *----------------------------------------------------------------------
990 *
991 * BogusCreate --
992 *
993 * This function is invoked when an Tcl_CreateHashEntry is called on a
994 * table that has been deleted.
995 *
996 * Results:
997 * If panic returns (which it shouldn't) this function returns NULL.
998 *
999 * Side effects:
1000 * Generates a panic.
1001 *
1002 *----------------------------------------------------------------------
1003 */
1004
1005 /* ARGSUSED */
1006 static Tcl_HashEntry *
1007 BogusCreate(
1008 Tcl_HashTable *tablePtr, /* Table in which to lookup entry. */
1009 const char *key, /* Key to use to find or create matching
1010 * entry. */
1011 int *newPtr) /* Store info here telling whether a new entry
1012 * was created. */
1013 {
1014 Tcl_Panic("called %s on deleted table", "Tcl_CreateHashEntry");
1015 return NULL;
1016 }
1017
1018 /*
1019 *----------------------------------------------------------------------
1020 *
1021 * RebuildTable --
1022 *
1023 * This function is invoked when the ratio of entries to hash buckets
1024 * becomes too large. It creates a new table with a larger bucket array
1025 * and moves all of the entries into the new table.
1026 *
1027 * Results:
1028 * None.
1029 *
1030 * Side effects:
1031 * Memory gets reallocated and entries get re-hashed to new buckets.
1032 *
1033 *----------------------------------------------------------------------
1034 */
1035
1036 static void
1037 RebuildTable(
1038 Tcl_HashTable *tablePtr) /* Table to enlarge. */
1039 {
1040 int count, index, oldSize = tablePtr->numBuckets;
1041 Tcl_HashEntry **oldBuckets = tablePtr->buckets;
1042 Tcl_HashEntry **oldChainPtr, **newChainPtr;
1043 Tcl_HashEntry *hPtr;
1044 const Tcl_HashKeyType *typePtr;
1045
1046 /* Avoid outgrowing capability of the memory allocators */
1047 if (oldSize > (int)(UINT_MAX / (4 * sizeof(Tcl_HashEntry *)))) {
1048 tablePtr->rebuildSize = INT_MAX;
1049 return;
1050 }
1051
1052 if (tablePtr->keyType == TCL_STRING_KEYS) {
1053 typePtr = &tclStringHashKeyType;
1054 } else if (tablePtr->keyType == TCL_ONE_WORD_KEYS) {
1055 typePtr = &tclOneWordHashKeyType;
1056 } else if (tablePtr->keyType == TCL_CUSTOM_TYPE_KEYS
1057 || tablePtr->keyType == TCL_CUSTOM_PTR_KEYS) {
1058 typePtr = tablePtr->typePtr;
1059 } else {
1060 typePtr = &tclArrayHashKeyType;
1061 }
1062
1063 /*
1064 * Allocate and initialize the new bucket array, and set up hashing
1065 * constants for new array size.
1066 */
1067
1068 tablePtr->numBuckets *= 4;
1069 if (typePtr->flags & TCL_HASH_KEY_SYSTEM_HASH) {
1070 tablePtr->buckets = (Tcl_HashEntry **) TclpSysAlloc((unsigned)
1071 (tablePtr->numBuckets * sizeof(Tcl_HashEntry *)), 0);
1072 } else {
1073 tablePtr->buckets =
1074 ckalloc(tablePtr->numBuckets * sizeof(Tcl_HashEntry *));
1075 }
1076 for (count = tablePtr->numBuckets, newChainPtr = tablePtr->buckets;
1077 count > 0; count--, newChainPtr++) {
1078 *newChainPtr = NULL;
1079 }
1080 tablePtr->rebuildSize *= 4;
1081 tablePtr->downShift -= 2;
1082 tablePtr->mask = (tablePtr->mask << 2) + 3;
1083
1084 /*
1085 * Rehash all of the existing entries into the new bucket array.
1086 */
1087
1088 for (oldChainPtr = oldBuckets; oldSize > 0; oldSize--, oldChainPtr++) {
1089 for (hPtr = *oldChainPtr; hPtr != NULL; hPtr = *oldChainPtr) {
1090 *oldChainPtr = hPtr->nextPtr;
1091 #if TCL_HASH_KEY_STORE_HASH
1092 if (typePtr->hashKeyProc == NULL
1093 || typePtr->flags & TCL_HASH_KEY_RANDOMIZE_HASH) {
1094 index = RANDOM_INDEX(tablePtr, PTR2INT(hPtr->hash));
1095 } else {
1096 index = PTR2UINT(hPtr->hash) & tablePtr->mask;
1097 }
1098 hPtr->nextPtr = tablePtr->buckets[index];
1099 tablePtr->buckets[index] = hPtr;
1100 #else
1101 void *key = Tcl_GetHashKey(tablePtr, hPtr);
1102
1103 if (typePtr->hashKeyProc) {
1104 unsigned int hash;
1105
1106 hash = typePtr->hashKeyProc(tablePtr, key);
1107 if (typePtr->flags & TCL_HASH_KEY_RANDOMIZE_HASH) {
1108 index = RANDOM_INDEX(tablePtr, hash);
1109 } else {
1110 index = hash & tablePtr->mask;
1111 }
1112 } else {
1113 index = RANDOM_INDEX(tablePtr, key);
1114 }
1115
1116 hPtr->bucketPtr = &tablePtr->buckets[index];
1117 hPtr->nextPtr = *hPtr->bucketPtr;
1118 *hPtr->bucketPtr = hPtr;
1119 #endif
1120 }
1121 }
1122
1123 /*
1124 * Free up the old bucket array, if it was dynamically allocated.
1125 */
1126
1127 if (oldBuckets != tablePtr->staticBuckets) {
1128 if (typePtr->flags & TCL_HASH_KEY_SYSTEM_HASH) {
1129 TclpSysFree((char *) oldBuckets);
1130 } else {
1131 ckfree(oldBuckets);
1132 }
1133 }
1134 }
1135
1136 /*
1137 * Local Variables:
1138 * mode: c
1139 * c-basic-offset: 4
1140 * fill-column: 78
1141 * End:
1142 */