xref: /titanic_50/usr/src/cmd/sgs/include/_string_table.h (revision cce0e03bb2d07f0fe27cabb93acae9c23655859f)
1a194faf8Srie /*
2a194faf8Srie  * CDDL HEADER START
3a194faf8Srie  *
4a194faf8Srie  * The contents of this file are subject to the terms of the
5a194faf8Srie  * Common Development and Distribution License (the "License").
6a194faf8Srie  * You may not use this file except in compliance with the License.
7a194faf8Srie  *
8a194faf8Srie  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9a194faf8Srie  * or http://www.opensolaris.org/os/licensing.
10a194faf8Srie  * See the License for the specific language governing permissions
11a194faf8Srie  * and limitations under the License.
12a194faf8Srie  *
13a194faf8Srie  * When distributing Covered Code, include this CDDL HEADER in each
14a194faf8Srie  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15a194faf8Srie  * If applicable, add the following below this CDDL HEADER, with the
16a194faf8Srie  * fields enclosed by brackets "[]" replaced with your own identifying
17a194faf8Srie  * information: Portions Copyright [yyyy] [name of copyright owner]
18a194faf8Srie  *
19a194faf8Srie  * CDDL HEADER END
20a194faf8Srie  */
21a194faf8Srie /*
22*cce0e03bSab196087  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23a194faf8Srie  * Use is subject to license terms.
24a194faf8Srie  */
25a194faf8Srie 
26a194faf8Srie #ifndef	__STRING_TABLE_DOT_H
27a194faf8Srie #define	__STRING_TABLE_DOT_H
28a194faf8Srie 
29a194faf8Srie #pragma ident	"%Z%%M%	%I%	%E% SMI"
30a194faf8Srie 
31a194faf8Srie #include <sys/types.h>
32a194faf8Srie #include <sys/avl.h>
33a194faf8Srie #include <string_table.h>
34a194faf8Srie 
35a194faf8Srie #ifdef __cplusplus
36a194faf8Srie extern "C" {
37a194faf8Srie #endif
38a194faf8Srie 
39a194faf8Srie /*
40a194faf8Srie  * A string is represented in a string table using two values: length, and
41a194faf8Srie  * value.  Grouping all the strings of a given length together allows for
42a194faf8Srie  * efficient matching of tail strings, as each input string value is hashed.
43a194faf8Srie  * Each string table uses a 2-level AVL tree of AVL trees to represent this
44a194faf8Srie  * organization.
45a194faf8Srie  *
46a194faf8Srie  * The outer (main) AVL tree contains LenNode structures.  The search key for
47a194faf8Srie  * nodes on this main tree is the string length.  Each such node represents
48a194faf8Srie  * all strings of a given length, and all strings of that length are found
49a194faf8Srie  * within.
50a194faf8Srie  *
51a194faf8Srie  * The strings within each LenNode are maintained using a secondary AVL tree
52a194faf8Srie  * of StrNode structures.  The search key for this inner tree is the string
53a194faf8Srie  * itself.  The strings are maintained in lexical order.
54a194faf8Srie  */
55a194faf8Srie typedef struct {
56a194faf8Srie 	avl_node_t	sn_avlnode;	/* AVL book-keeping */
57a194faf8Srie 	const char	*sn_str;	/* string */
58*cce0e03bSab196087 	size_t		sn_refcnt;	/* reference count */
59a194faf8Srie } StrNode;
60a194faf8Srie 
61a194faf8Srie typedef struct {
62a194faf8Srie 	avl_node_t	ln_avlnode;	/* AVL book-keeping */
63a194faf8Srie 	avl_tree_t	*ln_strtree;	/* AVL tree of associated strings */
64*cce0e03bSab196087 	size_t		ln_strlen;	/* length of associated strings */
65a194faf8Srie } LenNode;
66a194faf8Srie 
67a194faf8Srie /*
68a194faf8Srie  * Define a master string data item.  Other strings may be suffixes of this
69a194faf8Srie  * string.  The final string table will consist of the master string values,
70a194faf8Srie  * laid end to end, with the other strings referencing their tails.
71a194faf8Srie  */
72a194faf8Srie typedef	struct str_master	Str_master;
73a194faf8Srie 
74a194faf8Srie struct str_master {
75a194faf8Srie 	const char	*sm_str;	/* pointer to master string */
76a194faf8Srie 	Str_master	*sm_next;	/* used for tracking master strings */
77*cce0e03bSab196087 	size_t		sm_strlen;	/* length of master string */
78a194faf8Srie 	uint_t		sm_hashval;	/* hashval of master string */
79*cce0e03bSab196087 	size_t		sm_stroff;	/* offset into destination strtab */
80a194faf8Srie };
81a194faf8Srie 
82a194faf8Srie /*
83a194faf8Srie  * Define a hash data item.  This item represents an individual string that has
84a194faf8Srie  * been input into the String hash table.  The string may either be a suffix of
85a194faf8Srie  * another string, or a master string.
86a194faf8Srie  */
87a194faf8Srie typedef	struct str_hash	Str_hash;
88a194faf8Srie 
89a194faf8Srie struct str_hash {
90*cce0e03bSab196087 	size_t		hi_strlen;	/* string length */
91*cce0e03bSab196087 	size_t		hi_refcnt;	/* number of references to str */
92a194faf8Srie 	uint_t		hi_hashval;	/* hash for string */
93a194faf8Srie 	Str_master	*hi_mstr;	/* pointer to master string */
94a194faf8Srie 	Str_hash	*hi_next;	/* next entry in hash bucket */
95a194faf8Srie };
96a194faf8Srie 
97a194faf8Srie /*
98a194faf8Srie  * Controlling data structure for a String Table.
99a194faf8Srie  */
100a194faf8Srie struct str_tbl {
101a194faf8Srie 	avl_tree_t	*st_lentree;		/* AVL tree of string lengths */
102a194faf8Srie 	char		*st_strbuf;		/* string buffer */
103a194faf8Srie 	Str_hash	**st_hashbcks;		/* hash buckets */
104a194faf8Srie 	Str_master	*st_mstrlist;		/* list of all master strings */
105*cce0e03bSab196087 	size_t		st_fullstrsize;		/* uncompressed table size */
106*cce0e03bSab196087 	size_t		st_nextoff;		/* next available string */
107*cce0e03bSab196087 	size_t		st_strsize;		/* compressed size */
108*cce0e03bSab196087 	size_t		st_strcnt;		/* number of strings */
109a194faf8Srie 	uint_t		st_hbckcnt;		/* number of buckets in */
110a194faf8Srie 						/*    hashlist */
111a194faf8Srie 	uint_t		st_flags;
112a194faf8Srie };
113a194faf8Srie 
114a194faf8Srie #define	FLG_STTAB_COMPRESS	0x01		/* compressed string table */
115a194faf8Srie #define	FLG_STTAB_COOKED	0x02		/* offset has been assigned */
116a194faf8Srie 
117a194faf8Srie /*
118a194faf8Srie  * Starting value for use with string hashing functions inside of string_table.c
119a194faf8Srie  */
120a194faf8Srie #define	HASHSEED		5381
121a194faf8Srie 
122a194faf8Srie #ifdef __cplusplus
123a194faf8Srie }
124a194faf8Srie #endif
125a194faf8Srie 
126a194faf8Srie #endif /* __STRING_TABLE_DOT_H */
127