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*6277Scy152378 * Common Development and Distribution License (the "License").
6*6277Scy152378 * 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 */
210Sstevel@tonic-gate /*
22*6277Scy152378 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate *
250Sstevel@tonic-gate * stable.c -- string table module
260Sstevel@tonic-gate *
270Sstevel@tonic-gate * simple string table module. all read-only strings are entered in
280Sstevel@tonic-gate * this table, allowing us to compare pointers rather than characters
290Sstevel@tonic-gate * to see if two strings are equal.
300Sstevel@tonic-gate *
310Sstevel@tonic-gate */
320Sstevel@tonic-gate
330Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
340Sstevel@tonic-gate
350Sstevel@tonic-gate #include <stdio.h>
360Sstevel@tonic-gate #include <strings.h>
370Sstevel@tonic-gate #include "alloc.h"
380Sstevel@tonic-gate #include "out.h"
390Sstevel@tonic-gate #include "stats.h"
400Sstevel@tonic-gate #include "stable.h"
410Sstevel@tonic-gate
420Sstevel@tonic-gate #define MINPTR_ALIGN sizeof (char *) /* alignment boundary for pointers */
430Sstevel@tonic-gate #define DEF_HASH_SIZE 11113 /* default hash table size */
440Sstevel@tonic-gate #define CHUNK_SIZE 8192 /* grab more memory with this chunk size */
450Sstevel@tonic-gate
460Sstevel@tonic-gate static char **Stable; /* the hash table */
470Sstevel@tonic-gate static unsigned Stablesz;
480Sstevel@tonic-gate static char *Stableblock;
490Sstevel@tonic-gate static char *Stablenext;
500Sstevel@tonic-gate
510Sstevel@tonic-gate static struct stats *Stablecount;
520Sstevel@tonic-gate static struct stats *Blockcount;
530Sstevel@tonic-gate static struct stats *Add0;
540Sstevel@tonic-gate static struct stats *Add1;
550Sstevel@tonic-gate static struct stats *Add2;
560Sstevel@tonic-gate static struct stats *Add3;
570Sstevel@tonic-gate static struct stats *Addn;
580Sstevel@tonic-gate
590Sstevel@tonic-gate struct chunklst {
600Sstevel@tonic-gate struct chunklst *next;
610Sstevel@tonic-gate char *chunkp;
620Sstevel@tonic-gate };
630Sstevel@tonic-gate
640Sstevel@tonic-gate struct chunklst *Stablechunks;
650Sstevel@tonic-gate
660Sstevel@tonic-gate /*
670Sstevel@tonic-gate * stable_init -- initialize the stable module
680Sstevel@tonic-gate *
690Sstevel@tonic-gate * hash table is sized according to sz. sz of zero means pick
700Sstevel@tonic-gate * reasonable default size.
710Sstevel@tonic-gate */
720Sstevel@tonic-gate
730Sstevel@tonic-gate void
stable_init(unsigned sz)740Sstevel@tonic-gate stable_init(unsigned sz)
750Sstevel@tonic-gate {
760Sstevel@tonic-gate /* allocate hash table */
770Sstevel@tonic-gate if (sz == 0)
780Sstevel@tonic-gate Stablesz = DEF_HASH_SIZE;
790Sstevel@tonic-gate else
800Sstevel@tonic-gate Stablesz = sz;
810Sstevel@tonic-gate
820Sstevel@tonic-gate Stable = MALLOC(Stablesz * sizeof (*Stable));
830Sstevel@tonic-gate bzero((void *)Stable, Stablesz * sizeof (*Stable));
840Sstevel@tonic-gate
850Sstevel@tonic-gate Stablecount = stats_new_counter("stable.size", "hash table size", 1);
860Sstevel@tonic-gate Blockcount = stats_new_counter("stable.blocks", "blocks allocated", 1);
870Sstevel@tonic-gate Add0 = stats_new_counter("stable.add0", "adds to empty buckets", 1);
880Sstevel@tonic-gate Add1 = stats_new_counter("stable.add1", "adds to 1-entry buckets", 1);
890Sstevel@tonic-gate Add2 = stats_new_counter("stable.add2", "adds to 2-entry buckets", 1);
900Sstevel@tonic-gate Add3 = stats_new_counter("stable.add3", "adds to 3-entry buckets", 1);
910Sstevel@tonic-gate Addn = stats_new_counter("stable.addn", "adds to n-entry buckets", 1);
920Sstevel@tonic-gate
930Sstevel@tonic-gate stats_counter_add(Stablecount, Stablesz);
940Sstevel@tonic-gate }
950Sstevel@tonic-gate
960Sstevel@tonic-gate void
stable_fini(void)970Sstevel@tonic-gate stable_fini(void)
980Sstevel@tonic-gate {
990Sstevel@tonic-gate struct chunklst *cp, *nc;
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate stats_delete(Stablecount);
1020Sstevel@tonic-gate stats_delete(Blockcount);
1030Sstevel@tonic-gate stats_delete(Add0);
1040Sstevel@tonic-gate stats_delete(Add1);
1050Sstevel@tonic-gate stats_delete(Add2);
1060Sstevel@tonic-gate stats_delete(Add3);
1070Sstevel@tonic-gate stats_delete(Addn);
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate FREE(Stable);
1100Sstevel@tonic-gate cp = Stablechunks;
1110Sstevel@tonic-gate nc = NULL;
1120Sstevel@tonic-gate while (cp != NULL) {
1130Sstevel@tonic-gate nc = cp->next;
1140Sstevel@tonic-gate FREE(cp->chunkp);
1150Sstevel@tonic-gate FREE(cp);
1160Sstevel@tonic-gate cp = nc;
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate Stablechunks = NULL;
1190Sstevel@tonic-gate }
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate static char *
stable_newchunk(void)1220Sstevel@tonic-gate stable_newchunk(void)
1230Sstevel@tonic-gate {
1240Sstevel@tonic-gate struct chunklst *save = Stablechunks;
1250Sstevel@tonic-gate char *n;
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate n = MALLOC(CHUNK_SIZE);
1280Sstevel@tonic-gate bzero((void *)n, CHUNK_SIZE);
1290Sstevel@tonic-gate stats_counter_bump(Blockcount);
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate Stablechunks = MALLOC(sizeof (struct chunklst));
1320Sstevel@tonic-gate Stablechunks->next = save;
1330Sstevel@tonic-gate Stablechunks->chunkp = n;
1340Sstevel@tonic-gate return (n);
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate
1370Sstevel@tonic-gate /*
1380Sstevel@tonic-gate * stable -- create/lookup a string table entry
1390Sstevel@tonic-gate */
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate const char *
stable(const char * s)1420Sstevel@tonic-gate stable(const char *s)
1430Sstevel@tonic-gate {
1440Sstevel@tonic-gate unsigned slen = 0;
1450Sstevel@tonic-gate unsigned hash = DEF_HASH_SIZE ^ ((unsigned)*s << 2);
1460Sstevel@tonic-gate char **ptrp;
1470Sstevel@tonic-gate char *ptr;
1480Sstevel@tonic-gate char *eptr;
1490Sstevel@tonic-gate const char *sptr;
1500Sstevel@tonic-gate int collisions = 0;
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate if (Stablesz == 0)
1530Sstevel@tonic-gate out(O_DIE, "internal error: Stablesz not set");
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate for (sptr = &s[1]; *sptr; sptr++) {
1560Sstevel@tonic-gate slen++;
1570Sstevel@tonic-gate hash ^= (((unsigned)*sptr) << (slen % 3)) +
158*6277Scy152378 ((unsigned)*(sptr - 1) << ((slen % 3 + 7)));
1590Sstevel@tonic-gate }
1600Sstevel@tonic-gate hash ^= slen;
1610Sstevel@tonic-gate if (slen > CHUNK_SIZE - sizeof (char *) - 1 - 4)
1620Sstevel@tonic-gate out(O_DIE, "too big for string table %.20s...", s);
1630Sstevel@tonic-gate hash %= Stablesz;
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate ptrp = &Stable[hash];
1660Sstevel@tonic-gate ptr = *ptrp;
1670Sstevel@tonic-gate while (ptr) {
1680Sstevel@tonic-gate /* hash brought us to something, see if it is the string */
1690Sstevel@tonic-gate sptr = s;
1700Sstevel@tonic-gate eptr = ptr;
1710Sstevel@tonic-gate while (*sptr && *eptr && *sptr++ == *eptr++)
1720Sstevel@tonic-gate ;
1730Sstevel@tonic-gate if (*sptr == '\0' && *eptr == '\0')
1740Sstevel@tonic-gate return (ptr); /* found it */
1750Sstevel@tonic-gate /* strings didn't match, advance eptr to end of string */
1760Sstevel@tonic-gate while (*eptr)
1770Sstevel@tonic-gate eptr++;
1780Sstevel@tonic-gate eptr++; /* move past '\0' */
179*6277Scy152378 while ((uintptr_t)eptr % MINPTR_ALIGN)
1800Sstevel@tonic-gate eptr++;
1810Sstevel@tonic-gate /* pull in next pointer in bucket */
1820Sstevel@tonic-gate ptrp = (char **)(void *)eptr;
1830Sstevel@tonic-gate ptr = *ptrp;
1840Sstevel@tonic-gate collisions++;
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate /* string wasn't in table, add it and point ptr to it */
1880Sstevel@tonic-gate if (Stablenext == NULL || (&Stableblock[CHUNK_SIZE] - Stablenext) <
189*6277Scy152378 (slen + sizeof (char *) + MINPTR_ALIGN + 4)) {
1900Sstevel@tonic-gate /* need more room */
1910Sstevel@tonic-gate Stablenext = Stableblock = stable_newchunk();
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate /* current chunk has room in it */
1940Sstevel@tonic-gate ptr = *ptrp = Stablenext;
1950Sstevel@tonic-gate sptr = s;
1960Sstevel@tonic-gate while (*Stablenext++ = *sptr++)
1970Sstevel@tonic-gate ;
198*6277Scy152378 while ((uintptr_t)Stablenext % MINPTR_ALIGN)
1990Sstevel@tonic-gate Stablenext++;
2000Sstevel@tonic-gate ptrp = (char **)(void *)Stablenext;
2010Sstevel@tonic-gate Stablenext += sizeof (char *);
2020Sstevel@tonic-gate *ptrp = NULL;
2030Sstevel@tonic-gate
2040Sstevel@tonic-gate /* just did an add, update stats */
2050Sstevel@tonic-gate if (collisions == 0)
2060Sstevel@tonic-gate stats_counter_bump(Add0);
2070Sstevel@tonic-gate else if (collisions == 1)
2080Sstevel@tonic-gate stats_counter_bump(Add1);
2090Sstevel@tonic-gate else if (collisions == 2)
2100Sstevel@tonic-gate stats_counter_bump(Add2);
2110Sstevel@tonic-gate else if (collisions == 3)
2120Sstevel@tonic-gate stats_counter_bump(Add3);
2130Sstevel@tonic-gate else
2140Sstevel@tonic-gate stats_counter_bump(Addn);
2150Sstevel@tonic-gate
2160Sstevel@tonic-gate return (ptr);
2170Sstevel@tonic-gate }
218