stable.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* stable.c -- string table module
*
* simple string table module. all read-only strings are entered in
* this table, allowing us to compare pointers rather than characters
* to see if two strings are equal.
*
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdio.h>
#include <strings.h>
#include "alloc.h"
#include "out.h"
#include "stats.h"
#include "stable.h"
#define MINPTR_ALIGN sizeof (char *) /* alignment boundary for pointers */
static char **Stable; /* the hash table */
static unsigned Stablesz;
static char *Stableblock;
static char *Stablenext;
static struct stats *Stablecount;
static struct stats *Blockcount;
struct chunklst {
char *chunkp;
};
struct chunklst *Stablechunks;
/*
* stable_init -- initialize the stable module
*
* hash table is sized according to sz. sz of zero means pick
* reasonable default size.
*/
void
stable_init(unsigned sz)
{
/* allocate hash table */
if (sz == 0)
else
}
void
stable_fini(void)
{
cp = Stablechunks;
}
Stablechunks = NULL;
}
static char *
stable_newchunk(void)
{
char *n;
n = MALLOC(CHUNK_SIZE);
bzero((void *)n, CHUNK_SIZE);
Stablechunks->chunkp = n;
return (n);
}
/*
*/
const char *
stable(const char *s)
{
unsigned slen = 0;
char **ptrp;
char *ptr;
char *eptr;
const char *sptr;
int collisions = 0;
if (Stablesz == 0)
slen++;
}
while (ptr) {
/* hash brought us to something, see if it is the string */
sptr = s;
;
return (ptr); /* found it */
/* strings didn't match, advance eptr to end of string */
while (*eptr)
eptr++;
eptr++; /* move past '\0' */
while ((unsigned)eptr % MINPTR_ALIGN)
eptr++;
/* pull in next pointer in bucket */
collisions++;
}
/* string wasn't in table, add it and point ptr to it */
/* need more room */
}
/* current chunk has room in it */
sptr = s;
while (*Stablenext++ = *sptr++)
;
while ((unsigned)Stablenext % MINPTR_ALIGN)
Stablenext++;
ptrp = (char **)(void *)Stablenext;
Stablenext += sizeof (char *);
/* just did an add, update stats */
if (collisions == 0)
else if (collisions == 1)
else if (collisions == 2)
else if (collisions == 3)
else
return (ptr);
}