10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
56812Sraf * Common Development and Distribution License (the "License").
66812Sraf * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
216812Sraf
220Sstevel@tonic-gate /*
23*11411SSurya.Prakki@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
276812Sraf /* Copyright (c) 1988 AT&T */
286812Sraf /* All Rights Reserved */
290Sstevel@tonic-gate
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate * Compile time switches:
320Sstevel@tonic-gate *
330Sstevel@tonic-gate * MULT - use a multiplicative hashing function.
340Sstevel@tonic-gate * DIV - use the remainder mod table size as a hashing function.
350Sstevel@tonic-gate * CHAINED - use a linked list to resolve collisions.
360Sstevel@tonic-gate * OPEN - use open addressing to resolve collisions.
370Sstevel@tonic-gate * BRENT - use Brent's modification to improve the OPEN algorithm.
380Sstevel@tonic-gate * SORTUP - CHAINED list is sorted in increasing order.
390Sstevel@tonic-gate * SORTDOWN - CHAINED list is sorted in decreasing order.
400Sstevel@tonic-gate * START - CHAINED list with entries appended at front.
410Sstevel@tonic-gate * DRIVER - compile in a main program to drive the tests.
420Sstevel@tonic-gate * DEBUG - compile some debugging printout statements.
430Sstevel@tonic-gate * USCR - user supplied comparison routine.
440Sstevel@tonic-gate */
450Sstevel@tonic-gate
466812Sraf #pragma weak _hcreate = hcreate
476812Sraf #pragma weak _hdestroy = hdestroy
486812Sraf #pragma weak _hsearch = hsearch
490Sstevel@tonic-gate
506812Sraf #include "lint.h"
510Sstevel@tonic-gate #include <mtlib.h>
520Sstevel@tonic-gate #include <limits.h>
530Sstevel@tonic-gate #include <stdio.h>
540Sstevel@tonic-gate #include <stdlib.h>
550Sstevel@tonic-gate #include <string.h>
560Sstevel@tonic-gate #include <thread.h>
570Sstevel@tonic-gate #include <synch.h>
580Sstevel@tonic-gate #include <search.h>
590Sstevel@tonic-gate
600Sstevel@tonic-gate typedef char *POINTER;
610Sstevel@tonic-gate
620Sstevel@tonic-gate #define SUCCEED 0
630Sstevel@tonic-gate #define FAIL 1
640Sstevel@tonic-gate #define TRUE 1
650Sstevel@tonic-gate #define FALSE 0
660Sstevel@tonic-gate #define repeat for (;;)
670Sstevel@tonic-gate #define until(A) if (A) break;
680Sstevel@tonic-gate
690Sstevel@tonic-gate #ifdef OPEN
700Sstevel@tonic-gate #undef CHAINED
710Sstevel@tonic-gate #else
720Sstevel@tonic-gate #ifndef CHAINED
730Sstevel@tonic-gate #define OPEN
740Sstevel@tonic-gate #endif
750Sstevel@tonic-gate #endif
760Sstevel@tonic-gate
770Sstevel@tonic-gate #ifdef MULT
780Sstevel@tonic-gate #undef DIV
790Sstevel@tonic-gate #else
800Sstevel@tonic-gate #ifndef DIV
810Sstevel@tonic-gate #define MULT
820Sstevel@tonic-gate #endif
830Sstevel@tonic-gate #endif
840Sstevel@tonic-gate
850Sstevel@tonic-gate #ifdef START
860Sstevel@tonic-gate #undef SORTUP
870Sstevel@tonic-gate #undef SORTDOWN
880Sstevel@tonic-gate #else
890Sstevel@tonic-gate #ifdef SORTUP
900Sstevel@tonic-gate #undef SORTDOWN
910Sstevel@tonic-gate #endif
920Sstevel@tonic-gate #endif
930Sstevel@tonic-gate
940Sstevel@tonic-gate #ifdef USCR
950Sstevel@tonic-gate #define COMPARE(A, B) (* hcompar)((A), (B))
960Sstevel@tonic-gate extern int (* hcompar)();
970Sstevel@tonic-gate #else
980Sstevel@tonic-gate #define COMPARE(A, B) strcmp((A), (B))
990Sstevel@tonic-gate #endif
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate #ifdef MULT
1020Sstevel@tonic-gate #define SHIFT ((CHAR_BIT * sizeof (int)) - m) /* Shift factor */
1030Sstevel@tonic-gate #define FACTOR 035761254233 /* Magic multiplication factor */
1040Sstevel@tonic-gate #define HASH hashm /* Multiplicative hash function */
1050Sstevel@tonic-gate #define HASH2 hash2m /* Secondary hash function */
1060Sstevel@tonic-gate static unsigned int hashm(POINTER);
1070Sstevel@tonic-gate static unsigned int hash2m(POINTER);
1080Sstevel@tonic-gate #else
1090Sstevel@tonic-gate #ifdef DIV
1100Sstevel@tonic-gate #define HASH hashd /* Division hashing routine */
1110Sstevel@tonic-gate #define HASH2(A) 1 /* Secondary hash function */
1120Sstevel@tonic-gate static unsigned int hashd();
1130Sstevel@tonic-gate #endif
1140Sstevel@tonic-gate #endif
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate #ifdef CHAINED
1170Sstevel@tonic-gate typedef struct node { /* Part of the linked list of entries */
1180Sstevel@tonic-gate ENTRY item;
1190Sstevel@tonic-gate struct node *next;
1200Sstevel@tonic-gate } NODE;
1210Sstevel@tonic-gate typedef NODE *TABELEM;
1220Sstevel@tonic-gate static NODE **table; /* The address of the hash table */
1230Sstevel@tonic-gate static ENTRY *build();
1240Sstevel@tonic-gate #else
1250Sstevel@tonic-gate #ifdef OPEN
1260Sstevel@tonic-gate typedef ENTRY TABELEM; /* What the table contains (TABle ELEMents) */
1270Sstevel@tonic-gate static TABELEM *table; /* The address of the hash table */
1280Sstevel@tonic-gate static unsigned int count = 0; /* Number of entries in hash table */
1290Sstevel@tonic-gate #endif
1300Sstevel@tonic-gate #endif
1310Sstevel@tonic-gate
1320Sstevel@tonic-gate static unsigned int length; /* Size of the hash table */
1330Sstevel@tonic-gate static unsigned int m; /* Log base 2 of length */
1340Sstevel@tonic-gate static unsigned int prcnt; /* Number of probes this item */
1350Sstevel@tonic-gate static mutex_t table_lock = DEFAULTMUTEX;
1360Sstevel@tonic-gate #define RETURN(n) { lmutex_unlock(&table_lock); return (n); }
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate /*
1390Sstevel@tonic-gate * forward declarations
1400Sstevel@tonic-gate */
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate static unsigned int crunch(POINTER);
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate #ifdef DRIVER
1450Sstevel@tonic-gate static void hdump();
1460Sstevel@tonic-gate
main()1470Sstevel@tonic-gate main()
1480Sstevel@tonic-gate {
1490Sstevel@tonic-gate char line[80]; /* Room for the input line */
1500Sstevel@tonic-gate int i = 0; /* Data generator */
1510Sstevel@tonic-gate ENTRY *res; /* Result of hsearch */
1520Sstevel@tonic-gate ENTRY *new; /* Test entry */
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate start:
1550Sstevel@tonic-gate if (hcreate(5))
1560Sstevel@tonic-gate printf("Length = %u, m = %u\n", length, m);
1570Sstevel@tonic-gate else {
1580Sstevel@tonic-gate fprintf(stderr, "Out of core\n");
1590Sstevel@tonic-gate exit(FAIL);
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate repeat {
1620Sstevel@tonic-gate hdump();
1630Sstevel@tonic-gate printf("Enter a probe: ");
1640Sstevel@tonic-gate until(EOF == scanf("%s", line) || strcmp(line, "quit") == 0);
1650Sstevel@tonic-gate #ifdef DEBUG
1660Sstevel@tonic-gate printf("%s, ", line);
1670Sstevel@tonic-gate printf("division: %d, ", hashd(line));
1680Sstevel@tonic-gate printf("multiplication: %d\n", hashm(line));
1690Sstevel@tonic-gate #endif
1700Sstevel@tonic-gate new = (ENTRY *) malloc(sizeof (ENTRY));
1710Sstevel@tonic-gate if (new == NULL) {
1720Sstevel@tonic-gate fprintf(stderr, "Out of core \n");
1730Sstevel@tonic-gate exit(FAIL);
1746812Sraf } else {
1756812Sraf new->key = malloc((unsigned)strlen(line) + 1);
1766812Sraf if (new->key == NULL) {
1776812Sraf fprintf(stderr, "Out of core \n");
1786812Sraf exit(FAIL);
1796812Sraf }
180*11411SSurya.Prakki@Sun.COM (void) strcpy(new->key, line);
1816812Sraf new->data = malloc(sizeof (int));
1826812Sraf if (new->data == NULL) {
1836812Sraf fprintf(stderr, "Out of core \n");
1846812Sraf exit(FAIL);
1856812Sraf }
1866812Sraf *new->data = i++;
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate res = hsearch(*new, ENTER);
1890Sstevel@tonic-gate printf("The number of probes required was %d\n", prcnt);
1900Sstevel@tonic-gate if (res == (ENTRY *) 0)
1916812Sraf printf("Table is full\n");
1920Sstevel@tonic-gate else {
1936812Sraf printf("Success: ");
1946812Sraf printf("Key = %s, Value = %d\n", res->key, *res->data);
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate }
1970Sstevel@tonic-gate printf("Do you wish to start another hash table (yes/no?)");
1980Sstevel@tonic-gate if (EOF == scanf("%s", line) || strcmp(line, "no") == 0)
1990Sstevel@tonic-gate exit(SUCCEED);
2000Sstevel@tonic-gate hdestroy();
2010Sstevel@tonic-gate goto start;
2020Sstevel@tonic-gate }
2030Sstevel@tonic-gate #endif
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate int
hcreate(size_t size)2060Sstevel@tonic-gate hcreate(size_t size) /* Create a hash table no smaller than size */
2070Sstevel@tonic-gate /* Minimum "size" for hash table */
2080Sstevel@tonic-gate {
2090Sstevel@tonic-gate size_t unsize; /* Holds the shifted size */
2100Sstevel@tonic-gate TABELEM *local_table;
2110Sstevel@tonic-gate TABELEM *old_table;
2120Sstevel@tonic-gate unsigned int local_length;
2130Sstevel@tonic-gate unsigned int local_m;
2140Sstevel@tonic-gate
2150Sstevel@tonic-gate if (size == 0)
2160Sstevel@tonic-gate return (FALSE);
2170Sstevel@tonic-gate
2180Sstevel@tonic-gate unsize = size; /* +1 for empty table slot; -1 for ceiling */
2190Sstevel@tonic-gate local_length = 1; /* Maximum entries in table */
2200Sstevel@tonic-gate local_m = 0; /* Log2 length */
2210Sstevel@tonic-gate while (unsize) {
2220Sstevel@tonic-gate unsize >>= 1;
2230Sstevel@tonic-gate local_length <<= 1;
2240Sstevel@tonic-gate local_m++;
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate local_table = (TABELEM *) calloc(local_length, sizeof (TABELEM));
2280Sstevel@tonic-gate
2290Sstevel@tonic-gate lmutex_lock(&table_lock);
2300Sstevel@tonic-gate old_table = table;
2310Sstevel@tonic-gate table = local_table;
2320Sstevel@tonic-gate length = local_length;
2330Sstevel@tonic-gate m = local_m;
2340Sstevel@tonic-gate lmutex_unlock(&table_lock);
2350Sstevel@tonic-gate if (old_table != NULL)
2360Sstevel@tonic-gate free(old_table);
2370Sstevel@tonic-gate return (local_table != NULL);
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate
2400Sstevel@tonic-gate void
hdestroy(void)2410Sstevel@tonic-gate hdestroy(void) /* Reset the module to its initial state */
2420Sstevel@tonic-gate {
2430Sstevel@tonic-gate POINTER local_table;
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate lmutex_lock(&table_lock);
2460Sstevel@tonic-gate #ifdef CHAINED
2470Sstevel@tonic-gate int i;
2480Sstevel@tonic-gate NODE *p, *oldp;
2490Sstevel@tonic-gate for (i = 0; i < length; i++) {
2500Sstevel@tonic-gate if (table[i] != (NODE *)NULL) {
2510Sstevel@tonic-gate p = table[i];
2520Sstevel@tonic-gate while (p != (NODE *)NULL) {
2530Sstevel@tonic-gate oldp = p;
2540Sstevel@tonic-gate p = p -> next;
2550Sstevel@tonic-gate /*
2560Sstevel@tonic-gate * This is a locking vs malloc() violation.
2570Sstevel@tonic-gate * Fortunately, it is not actually compiled.
2580Sstevel@tonic-gate */
2590Sstevel@tonic-gate free(oldp);
2600Sstevel@tonic-gate }
2610Sstevel@tonic-gate }
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate #endif
2640Sstevel@tonic-gate local_table = (POINTER)table;
2650Sstevel@tonic-gate table = 0;
2660Sstevel@tonic-gate #ifdef OPEN
2670Sstevel@tonic-gate count = 0;
2680Sstevel@tonic-gate #endif
2690Sstevel@tonic-gate lmutex_unlock(&table_lock);
2700Sstevel@tonic-gate free(local_table);
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate
2730Sstevel@tonic-gate #ifdef OPEN
2740Sstevel@tonic-gate /*
2750Sstevel@tonic-gate * Hash search of a fixed-capacity table. Open addressing used to
2760Sstevel@tonic-gate * resolve collisions. Algorithm modified from Knuth, Volume 3,
2770Sstevel@tonic-gate * section 6.4, algorithm D. Labels flag corresponding actions.
2780Sstevel@tonic-gate */
2790Sstevel@tonic-gate
2800Sstevel@tonic-gate /* Find or insert the item into the table */
2810Sstevel@tonic-gate ENTRY
hsearch(ENTRY item,ACTION action)2820Sstevel@tonic-gate *hsearch(ENTRY item, ACTION action)
2830Sstevel@tonic-gate /* "item" to be inserted or found */
2840Sstevel@tonic-gate /* action: FIND or ENTER */
2850Sstevel@tonic-gate {
2860Sstevel@tonic-gate unsigned int i; /* Insertion index */
2870Sstevel@tonic-gate unsigned int c; /* Secondary probe displacement */
2880Sstevel@tonic-gate
2890Sstevel@tonic-gate lmutex_lock(&table_lock);
2900Sstevel@tonic-gate prcnt = 1;
2910Sstevel@tonic-gate
2920Sstevel@tonic-gate /* D1: */
2930Sstevel@tonic-gate i = HASH(item.key); /* Primary hash on key */
2940Sstevel@tonic-gate #ifdef DEBUG
2950Sstevel@tonic-gate if (action == ENTER)
2960Sstevel@tonic-gate printf("hash = %o\n", i);
2970Sstevel@tonic-gate #endif
2980Sstevel@tonic-gate
2990Sstevel@tonic-gate /* D2: */
3000Sstevel@tonic-gate if (table[i].key == NULL) /* Empty slot? */
3010Sstevel@tonic-gate goto D6;
3020Sstevel@tonic-gate else if (COMPARE(table[i].key, item.key) == 0) /* Match? */
3030Sstevel@tonic-gate RETURN(&table[i]);
3040Sstevel@tonic-gate
3050Sstevel@tonic-gate /* D3: */
3060Sstevel@tonic-gate c = HASH2(item.key); /* No match => compute secondary hash */
3070Sstevel@tonic-gate #ifdef DEBUG
3080Sstevel@tonic-gate if (action == ENTER)
3090Sstevel@tonic-gate printf("hash2 = %o\n", c);
3100Sstevel@tonic-gate #endif
3110Sstevel@tonic-gate
3120Sstevel@tonic-gate D4:
3130Sstevel@tonic-gate i = (i + c) % length; /* Advance to next slot */
3140Sstevel@tonic-gate prcnt++;
3150Sstevel@tonic-gate
3160Sstevel@tonic-gate /* D5: */
3170Sstevel@tonic-gate if (table[i].key == NULL) /* Empty slot? */
3180Sstevel@tonic-gate goto D6;
3190Sstevel@tonic-gate else if (COMPARE(table[i].key, item.key) == 0) /* Match? */
3200Sstevel@tonic-gate RETURN(&table[i])
3210Sstevel@tonic-gate else
3220Sstevel@tonic-gate goto D4;
3230Sstevel@tonic-gate
3240Sstevel@tonic-gate D6: if (action == FIND) /* Insert if requested */
3250Sstevel@tonic-gate RETURN((ENTRY *) NULL);
3260Sstevel@tonic-gate if (count == (length - 1)) /* Table full? */
3270Sstevel@tonic-gate RETURN((ENTRY *) 0);
3280Sstevel@tonic-gate
3290Sstevel@tonic-gate #ifdef BRENT
3300Sstevel@tonic-gate /*
3310Sstevel@tonic-gate * Brent's variation of the open addressing algorithm. Do extra
3320Sstevel@tonic-gate * work during insertion to speed retrieval. May require switching
3330Sstevel@tonic-gate * of previously placed items. Adapted from Knuth, Volume 3, section
3340Sstevel@tonic-gate * 4.6 and Brent's article in CACM, volume 10, #2, February 1973.
3350Sstevel@tonic-gate */
3360Sstevel@tonic-gate
3370Sstevel@tonic-gate {
3380Sstevel@tonic-gate unsigned int p0 = HASH(item.key); /* First probe index */
3390Sstevel@tonic-gate unsigned int c0 = HASH2(item.key); /* Main branch increment */
3400Sstevel@tonic-gate unsigned int r = prcnt - 1; /* Current minimum distance */
3410Sstevel@tonic-gate unsigned int j; /* Counts along main branch */
3420Sstevel@tonic-gate unsigned int k; /* Counts along secondary branch */
3430Sstevel@tonic-gate unsigned int curj; /* Current best main branch site */
3440Sstevel@tonic-gate unsigned int curpos; /* Current best table index */
3450Sstevel@tonic-gate unsigned int pj; /* Main branch indices */
3460Sstevel@tonic-gate unsigned int cj; /* Secondary branch increment distance */
3470Sstevel@tonic-gate unsigned int pjk; /* Secondary branch probe indices */
3480Sstevel@tonic-gate
3490Sstevel@tonic-gate if (prcnt >= 3) {
3500Sstevel@tonic-gate for (j = 0; j < prcnt; j++) { /* Count along main branch */
3510Sstevel@tonic-gate pj = (p0 + j * c0) % length; /* New main branch index */
3520Sstevel@tonic-gate cj = HASH2(table[pj].key); /* Secondary branch incr. */
3530Sstevel@tonic-gate for (k = 1; j+k <= r; k++) {
3540Sstevel@tonic-gate /* Count on secondary branch */
3550Sstevel@tonic-gate pjk = (pj + k * cj) % length;
3560Sstevel@tonic-gate /* Secondary probe */
3570Sstevel@tonic-gate if (table[pjk].key == NULL) {
3580Sstevel@tonic-gate /* Improvement found */
3590Sstevel@tonic-gate r = j + k; /* Decrement upper bound */
3600Sstevel@tonic-gate curj = pj; /* Save main probe index */
3610Sstevel@tonic-gate curpos = pjk;
3620Sstevel@tonic-gate /* Save secondeary index */
3630Sstevel@tonic-gate }
3640Sstevel@tonic-gate }
3650Sstevel@tonic-gate }
3660Sstevel@tonic-gate if (r != prcnt - 1) { /* If an improvement occurred */
3670Sstevel@tonic-gate table[curpos] = table[curj]; /* Old key to new site */
3680Sstevel@tonic-gate #ifdef DEBUG
3690Sstevel@tonic-gate printf("Switch curpos = %o, curj = %o, oldi = %o\n",
3700Sstevel@tonic-gate curj, curpos, i);
3710Sstevel@tonic-gate #endif
3720Sstevel@tonic-gate i = curj;
3730Sstevel@tonic-gate }
3740Sstevel@tonic-gate }
3750Sstevel@tonic-gate }
3760Sstevel@tonic-gate #endif
3770Sstevel@tonic-gate count++; /* Increment table occupancy count */
3780Sstevel@tonic-gate table[i] = item; /* Save item */
3790Sstevel@tonic-gate
3800Sstevel@tonic-gate lmutex_unlock(&table_lock);
3810Sstevel@tonic-gate return (&table[i]); /* Address of item is returned */
3820Sstevel@tonic-gate }
3830Sstevel@tonic-gate #endif
3840Sstevel@tonic-gate
3850Sstevel@tonic-gate #ifdef USCR
3860Sstevel@tonic-gate #ifdef DRIVER
3870Sstevel@tonic-gate static int
compare(a,b)3880Sstevel@tonic-gate compare(a, b)
3890Sstevel@tonic-gate POINTER a;
3900Sstevel@tonic-gate POINTER b;
3910Sstevel@tonic-gate {
3920Sstevel@tonic-gate return (strcmp(a, b));
3930Sstevel@tonic-gate }
3940Sstevel@tonic-gate
3950Sstevel@tonic-gate int (* hcompar)() = compare;
3960Sstevel@tonic-gate #endif
3970Sstevel@tonic-gate #endif
3980Sstevel@tonic-gate
3990Sstevel@tonic-gate #ifdef CHAINED
4000Sstevel@tonic-gate #ifdef SORTUP
4010Sstevel@tonic-gate #define STRCMP(A, B) (COMPARE((A), (B)) > 0)
4020Sstevel@tonic-gate #else
4030Sstevel@tonic-gate #ifdef SORTDOWN
4040Sstevel@tonic-gate #define STRCMP(A, B) (COMPARE((A), (B)) < 0)
4050Sstevel@tonic-gate #else
4060Sstevel@tonic-gate #define STRCMP(A, B) (COMPARE((A), (B)) != 0)
4070Sstevel@tonic-gate #endif
4080Sstevel@tonic-gate #endif
4090Sstevel@tonic-gate
4100Sstevel@tonic-gate ENTRY
hsearch(item,action)4110Sstevel@tonic-gate *hsearch(item, action) /* Chained search with sorted lists */
4120Sstevel@tonic-gate ENTRY item; /* Item to be inserted or found */
4130Sstevel@tonic-gate ACTION action; /* FIND or ENTER */
4140Sstevel@tonic-gate {
4150Sstevel@tonic-gate NODE *p; /* Searches through the linked list */
4160Sstevel@tonic-gate NODE **q; /* Where to store the pointer to a new NODE */
4170Sstevel@tonic-gate unsigned int i; /* Result of hash */
4180Sstevel@tonic-gate int res; /* Result of string comparison */
4190Sstevel@tonic-gate
4200Sstevel@tonic-gate lmutex_lock(&table_lock);
4210Sstevel@tonic-gate prcnt = 1;
4220Sstevel@tonic-gate
4230Sstevel@tonic-gate i = HASH(item.key); /* Table[i] contains list head */
4240Sstevel@tonic-gate
4250Sstevel@tonic-gate if (table[i] == (NODE*)NULL) { /* List has not yet been begun */
4260Sstevel@tonic-gate if (action == FIND)
4270Sstevel@tonic-gate RETURN((ENTRY *) NULL);
4280Sstevel@tonic-gate else
4290Sstevel@tonic-gate RETURN(build(&table[i], (NODE *) NULL, item));
4300Sstevel@tonic-gate } else { /* List is not empty */
4310Sstevel@tonic-gate q = &table[i];
4320Sstevel@tonic-gate p = table[i];
4330Sstevel@tonic-gate while (p != NULL && (res = STRCMP(item.key, p->item.key))) {
4340Sstevel@tonic-gate prcnt++;
4350Sstevel@tonic-gate q = &(p->next);
4360Sstevel@tonic-gate p = p->next;
4370Sstevel@tonic-gate }
4380Sstevel@tonic-gate
4390Sstevel@tonic-gate if (p != NULL && res == 0) /* Item has been found */
4400Sstevel@tonic-gate RETURN(&(p->item));
4410Sstevel@tonic-gate else { /* Item is not yet on list */
4420Sstevel@tonic-gate if (action == FIND)
4430Sstevel@tonic-gate RETURN((ENTRY *) NULL);
4440Sstevel@tonic-gate else
4450Sstevel@tonic-gate #ifdef START
4460Sstevel@tonic-gate RETURN(build(&table[i], table[i], item));
4470Sstevel@tonic-gate #else
4480Sstevel@tonic-gate RETURN(build(q, p, item));
4490Sstevel@tonic-gate #endif
4500Sstevel@tonic-gate }
4510Sstevel@tonic-gate }
4520Sstevel@tonic-gate }
4530Sstevel@tonic-gate
4540Sstevel@tonic-gate static ENTRY
build(last,next,item)4550Sstevel@tonic-gate *build(last, next, item)
4560Sstevel@tonic-gate NODE **last; /* Where to store in last list item */
4570Sstevel@tonic-gate NODE *next; /* Link to next list item */
4580Sstevel@tonic-gate ENTRY item; /* Item to be kept in node */
4590Sstevel@tonic-gate {
4600Sstevel@tonic-gate /*
4610Sstevel@tonic-gate * This is a locking vs malloc() violation.
4620Sstevel@tonic-gate * Fortunately, it is not actually compiled.
4630Sstevel@tonic-gate */
4640Sstevel@tonic-gate NODE *p = (NODE *) malloc(sizeof (NODE));
4650Sstevel@tonic-gate
4660Sstevel@tonic-gate if (p != NULL) {
4670Sstevel@tonic-gate p->item = item;
4680Sstevel@tonic-gate *last = p;
4690Sstevel@tonic-gate p->next = next;
4700Sstevel@tonic-gate return (&(p->item));
4710Sstevel@tonic-gate } else
4720Sstevel@tonic-gate return (NULL);
4730Sstevel@tonic-gate }
4740Sstevel@tonic-gate #endif
4750Sstevel@tonic-gate
4760Sstevel@tonic-gate #ifdef DIV
4770Sstevel@tonic-gate static unsigned int
hashd(key)4780Sstevel@tonic-gate hashd(key) /* Division hashing scheme */
4790Sstevel@tonic-gate POINTER key; /* Key to be hashed */
4800Sstevel@tonic-gate {
4810Sstevel@tonic-gate return (crunch(key) % length);
4820Sstevel@tonic-gate }
4830Sstevel@tonic-gate #else
4840Sstevel@tonic-gate #ifdef MULT
4850Sstevel@tonic-gate /*
4860Sstevel@tonic-gate * NOTE: The following algorithm only works on machines where
4870Sstevel@tonic-gate * the results of multiplying two integers is the least
4880Sstevel@tonic-gate * significant part of the double word integer required to hold
4890Sstevel@tonic-gate * the result. It is adapted from Knuth, Volume 3, section 6.4.
4900Sstevel@tonic-gate */
4910Sstevel@tonic-gate
4920Sstevel@tonic-gate static unsigned int
hashm(POINTER key)4930Sstevel@tonic-gate hashm(POINTER key) /* Multiplication hashing scheme */
4940Sstevel@tonic-gate /* "key" to be hashed */
4950Sstevel@tonic-gate {
4960Sstevel@tonic-gate return ((int)(((unsigned)(crunch(key) * FACTOR)) >> SHIFT));
4970Sstevel@tonic-gate }
4980Sstevel@tonic-gate
4990Sstevel@tonic-gate /*
5000Sstevel@tonic-gate * Secondary hashing, for use with multiplicitive hashing scheme.
5010Sstevel@tonic-gate * Adapted from Knuth, Volume 3, section 6.4.
5020Sstevel@tonic-gate */
5030Sstevel@tonic-gate
5040Sstevel@tonic-gate static unsigned int
hash2m(POINTER key)5050Sstevel@tonic-gate hash2m(POINTER key) /* Secondary hashing routine */
5060Sstevel@tonic-gate /* "key" is the string to be hashed */
5070Sstevel@tonic-gate {
5080Sstevel@tonic-gate return (((unsigned int)((crunch(key) * FACTOR) << m) >> SHIFT) | 1);
5090Sstevel@tonic-gate }
5100Sstevel@tonic-gate #endif
5110Sstevel@tonic-gate #endif
5120Sstevel@tonic-gate
5130Sstevel@tonic-gate /* PJ Weinberger's hash function */
5140Sstevel@tonic-gate static unsigned int
crunch(POINTER key)5150Sstevel@tonic-gate crunch(POINTER key) /* Convert multicharacter key to unsigned int */
5160Sstevel@tonic-gate {
5170Sstevel@tonic-gate unsigned int h = 0;
5180Sstevel@tonic-gate unsigned int g;
5190Sstevel@tonic-gate unsigned char *p = (unsigned char *)key;
5200Sstevel@tonic-gate
5210Sstevel@tonic-gate for (; *p; p++) {
5220Sstevel@tonic-gate h = (h << 4) + *p;
5230Sstevel@tonic-gate g = h & 0xf0000000;
5240Sstevel@tonic-gate if (g != 0) {
5250Sstevel@tonic-gate h ^= (g >> 24);
5260Sstevel@tonic-gate h ^= g;
5270Sstevel@tonic-gate }
5280Sstevel@tonic-gate }
5290Sstevel@tonic-gate return (h);
5300Sstevel@tonic-gate }
5310Sstevel@tonic-gate
5320Sstevel@tonic-gate #ifdef DRIVER
5330Sstevel@tonic-gate static void
hdump()5340Sstevel@tonic-gate hdump() /* Dumps loc, data, probe count, key */
5350Sstevel@tonic-gate {
5360Sstevel@tonic-gate unsigned int i; /* Counts table slots */
5370Sstevel@tonic-gate #ifdef OPEN
5380Sstevel@tonic-gate unsigned int sum = 0; /* Counts probes */
5390Sstevel@tonic-gate #else
5400Sstevel@tonic-gate #ifdef CHAINED
5410Sstevel@tonic-gate NODE *a; /* Current Node on list */
5420Sstevel@tonic-gate #endif
5430Sstevel@tonic-gate #endif
5440Sstevel@tonic-gate
5450Sstevel@tonic-gate for (i = 0; i < length; i++)
5460Sstevel@tonic-gate #ifdef OPEN
5470Sstevel@tonic-gate if (table[i].key == NULL)
5480Sstevel@tonic-gate printf("%o.\t-,\t-,\t(NULL)\n", i);
5490Sstevel@tonic-gate else {
5500Sstevel@tonic-gate unsigned int oldpr = prcnt;
5510Sstevel@tonic-gate /* Save current probe count */
5520Sstevel@tonic-gate
5530Sstevel@tonic-gate hsearch(table[i], FIND);
5540Sstevel@tonic-gate sum += prcnt;
5550Sstevel@tonic-gate printf("%o.\t%d,\t%d,\t%s\n", i,
5566812Sraf *table[i].data, prcnt, table[i].key);
5570Sstevel@tonic-gate prcnt = oldpr;
5580Sstevel@tonic-gate }
5590Sstevel@tonic-gate printf("Total probes = %d\n", sum);
5600Sstevel@tonic-gate #else
5610Sstevel@tonic-gate #ifdef CHAINED
5620Sstevel@tonic-gate if (table[i] == NULL)
5636812Sraf printf("%o.\t-,\t-,\t(NULL)\n", i);
5640Sstevel@tonic-gate else {
5656812Sraf printf("%o.", i);
5666812Sraf for (a = table[i]; a != NULL; a = a->next)
5676812Sraf printf("\t%d,\t%#0.4x,\t%s\n",
5686812Sraf *a->item.data, a, a->item.key);
5690Sstevel@tonic-gate }
5700Sstevel@tonic-gate #endif
5710Sstevel@tonic-gate #endif
5720Sstevel@tonic-gate }
5730Sstevel@tonic-gate #endif
574