string_table.c revision cce0e03bb2d07f0fe27cabb93acae9c23655859f
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (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 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <_string_table.h>
#include <strings.h>
#include <sgs.h>
#include <stdio.h>
/*
* This file provides the interfaces to build a Str_tbl suitable for use by
* either the sgsmsg message system, or a standard ELF string table (SHT_STRTAB)
* as created by ld(1).
*
* There are two modes which can be used when constructing a string table:
*
* st_new(0)
* standard string table - no compression. This is the
* traditional, fast method.
*
* st_new(FLG_STTAB_COMPRESS)
* builds a compressed string table which both eliminates
* duplicate strings, and permits strings with common suffixes
* (atexit vs. exit) to overlap in the table. This provides space
* savings for many string tables. Although more work than the
* traditional method, the algorithms used are designed to scale
* and keep any overhead at a minimum.
*
* These string tables are built with a common interface in a two-pass manner.
* The first pass finds all of the strings required for the string-table and
* calculates the size required for the final string table.
*
* The second pass allocates the string table, populates the strings into the
* table and returns the offsets the strings have been assigned.
*
* The calling sequence to build and populate a string table is:
*
* st_new(); // initialize strtab
*
* st_insert(st1); // first pass of strings ...
* // calculates size required for
* // string table
*
* st_delstring(st?); // remove string previously
* // inserted
* st_insert(stN);
*
* st_getstrtab_sz(); // freezes strtab and computes
* // size of table.
*
* st_setstrbuf(); // associates a final destination
* // for the string table
*
* st_setstring(st1); // populate the string table
* ... // offsets are based off of second
* // pass through the string table
* st_setstring(stN);
*
* st_destroy(); // tear down string table
* // structures.
*
* String Suffix Compression Algorithm:
*
* Here's a quick high level overview of the Suffix String
* compression algorithm used. First - the heart of the algorithm
* is a Hash table list which represents a dictionary of all unique
* strings inserted into the string table. The hash function for
* this table is a standard string hash except that the hash starts
* at the last character in the string (&str[n - 1]) and works towards
* the first character in the function (&str[0]). As we compute the
* HASH value for a given string, we also compute the hash values
* for all of the possible suffix strings for that string.
*
* As we compute the hash - at each character see if the current
* suffix string for that hash is already present in the table. If
* it is, and the string is a master string. Then change that
* string to a suffix string of the new string being inserted.
*
* When the final hash value is found (hash for str[0...n]), check
* to see if it is in the hash table - if so increment the reference
* count for the string. If it is not yet in the table, insert a
* new hash table entry for a master string.
*
* The above method will find all suffixes of a given string given
* that the strings are inserted from shortest to longest. That is
* why this is a two phase method, we first collect all of the
* strings and store them based off of their length in an AVL tree.
* Once all of the strings have been submitted we then start the
* hash table build by traversing the AVL tree in order and
* inserting the strings from shortest to longest as described
* above.
*/
/* LINTLIBRARY */
static int
{
return (0);
return (1);
return (-1);
}
static int
{
int rc;
if (rc > 0)
return (1);
if (rc < 0)
return (-1);
return (0);
}
/*
* Return an initialized Str_tbl - returns NULL on failure.
*
* flags:
* FLG_STTAB_COMPRESS - build a compressed string table
*/
Str_tbl *
{
return (NULL);
/*
* Start with a leading '\0' - it's tradition.
*/
/*
* Do we compress this string table?
*/
return (stp);
return (NULL);
return (stp);
}
/*
* Insert a new string into the Str_tbl. There are two AVL trees used.
*
* . The first LenNode AVL tree maintains a tree of nodes based on string
* sizes.
* . Each LenNode maintains a StrNode AVL tree for each string. Large
* applications have been known to contribute thousands of strings of
* the same size. Should strings need to be removed (-z ignore), then
* the string AVL tree makes this removal efficient and scalable.
*/
int
{
/*
* String table can't have been cooked
*/
/*
* Null strings always point to the head of the string
* table - no reason to keep searching.
*/
return (0);
return (0);
/*
* From the controlling string table, determine which LenNode AVL node
* provides for this string length. If the node doesn't exist, insert
* a new node to represent this string length.
*/
return (-1);
return (0);
}
/*
* From the string length AVL node determine whether a StrNode AVL node
* provides this string. If the node doesn't exist, insert a new node
* to represent this string.
*/
return (-1);
}
return (0);
}
/*
* Remove a previously inserted string from the Str_tbl.
*/
int
{
/*
* String table can't have been cooked
*/
return (0);
/*
* Determine which LenNode AVL node provides for this string length.
*/
/*
* Reduce the reference count, and if zero remove the
* node.
*/
return (0);
}
}
/*
* No strings of this length, or no string itself - someone goofed.
*/
return (-1);
}
/*
* Tear down a String_Table structure.
*/
void
{
uint_t i;
/*
* cleanup the master strings
*/
if (pmstr)
}
if (pmstr)
if (stp->st_hashbcks) {
for (i = 0; i < stp->st_hbckcnt; i++) {
if (psthash)
}
if (psthash)
}
}
}
/*
* For a given string - copy it into the buffer associated with
* the string table - and return the offset it has been assigned.
*
* If a value of '-1' is returned - the string was not found in
* the Str_tbl.
*/
int
{
int i;
/*
* String table *must* have been previously cooked
*/
/*
* Null string always points to head of string table
*/
if (stlen == 0) {
*stoff = 0;
return (0);
}
stlen++; /* count for trailing '\0' */
/*
* Have we overflowed our assigned buffer?
*/
return (-1);
return (0);
}
/*
* Calculate reverse hash for string.
*/
for (i = stlen; i >= 0; i--) {
str[i]; /* h = ((h * 33) + c) */
}
const char *hstr;
continue;
break;
}
/*
* Did we find the string?
*/
if (sthash == 0)
return (-1);
/*
* Has this string been copied into the string table?
*/
/*
* Have we overflowed our assigned buffer?
*/
return (-1);
}
/*
* Calculate offset of (sub)string.
*/
return (0);
}
static int
{
int i;
Str_master *mstr = 0;
/*
* We use a classic 'Bernstein k=33' hash function. But
* instead of hashing from the start of the string to the
* end, we do it in reverse.
*
* This way - we are essentially building all of the
* suffix hashvalues as we go. We can check to see if
* any suffixes already exist in the tree as we generate
* the hash.
*/
for (i = len; i >= 0; i--) {
str[i]; /* h = ((h * 33) + c) */
const char *hstr;
continue;
continue;
if (i == 0) {
/*
* Entry already in table, increment refcnt and
* get out.
*/
return (0);
} else {
/*
* If this 'suffix' is presently a 'master
* string, then take over it's record.
*/
/*
* we should only do this once.
*/
}
}
}
}
/*
* Do we need a new master string, or can we take over
* one we already found in the table?
*/
if (mstr == 0) {
/*
* allocate a new master string
*/
return (-1);
} else {
/*
* We are taking over a existing master string, the string size
* only increments by the difference between the current string
* and the previous master.
*/
}
return (-1);
/*
* Insert string element into head of hash list
*/
return (0);
}
/*
* Return amount of space required for the string table.
*/
{
return (stp->st_fullstrsize);
}
void *cookie;
/*
* allocate a hash table about the size of # of
* strings input.
*/
if ((stp->st_hashbcks =
return (0);
/*
* We now walk all of the strings in the list, from shortest to
* longest, and insert them into the hashtable.
*/
/*
* Is it possible we have an empty string table, if so,
* the table still contains '\0', so return the size.
*/
return (stp->st_strsize);
}
return (0);
}
while (lnp) {
/*
* Walk the string lists and insert them into the hash
* list. Once a string is inserted we no longer need
* it's entry, so the string can be freed.
*/
return (0);
}
/*
* Now that the strings have been copied, walk the
* StrNode tree and free all the AVL nodes. Note,
* avl_destroy_nodes() beats avl_remove() as the
* latter balances the nodes as they are removed.
* We just want to tear the whole thing down fast.
*/
/*
* Move on to the next LenNode.
*/
}
/*
* Now that all of the strings have been freed, walk the
* LenNode tree and free all of the AVL nodes. Note,
* avl_destroy_nodes() beats avl_remove() as the latter
* balances the nodes as they are removed. We just want to
* tear the whole thing down fast.
*/
stp->st_lentree = 0;
}
return (stp->st_strsize);
}
/*
* Associate a buffer with a string table.
*/
const char *
{
}
int
{
return (-1);
} else {
return (-1);
}
#ifdef DEBUG
/*
* for debug builds - start with a stringtable filled in
* with '0xff'. This makes it very easy to find wholes
* which we failed to fill in - in the strtab.
*/
stbuf[0] = '\0';
#else
#endif
return (0);
}