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