xref: /onnv-gate/usr/src/cmd/sgs/include/_string_table.h (revision 5892:b863dde33f1b)
15549Srie /*
25549Srie  * CDDL HEADER START
35549Srie  *
45549Srie  * The contents of this file are subject to the terms of the
55549Srie  * Common Development and Distribution License (the "License").
65549Srie  * You may not use this file except in compliance with the License.
75549Srie  *
85549Srie  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95549Srie  * or http://www.opensolaris.org/os/licensing.
105549Srie  * See the License for the specific language governing permissions
115549Srie  * and limitations under the License.
125549Srie  *
135549Srie  * When distributing Covered Code, include this CDDL HEADER in each
145549Srie  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155549Srie  * If applicable, add the following below this CDDL HEADER, with the
165549Srie  * fields enclosed by brackets "[]" replaced with your own identifying
175549Srie  * information: Portions Copyright [yyyy] [name of copyright owner]
185549Srie  *
195549Srie  * CDDL HEADER END
205549Srie  */
215549Srie /*
22*5892Sab196087  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
235549Srie  * Use is subject to license terms.
245549Srie  */
255549Srie 
265549Srie #ifndef	__STRING_TABLE_DOT_H
275549Srie #define	__STRING_TABLE_DOT_H
285549Srie 
295549Srie #pragma ident	"%Z%%M%	%I%	%E% SMI"
305549Srie 
315549Srie #include <sys/types.h>
325549Srie #include <sys/avl.h>
335549Srie #include <string_table.h>
345549Srie 
355549Srie #ifdef __cplusplus
365549Srie extern "C" {
375549Srie #endif
385549Srie 
395549Srie /*
405549Srie  * A string is represented in a string table using two values: length, and
415549Srie  * value.  Grouping all the strings of a given length together allows for
425549Srie  * efficient matching of tail strings, as each input string value is hashed.
435549Srie  * Each string table uses a 2-level AVL tree of AVL trees to represent this
445549Srie  * organization.
455549Srie  *
465549Srie  * The outer (main) AVL tree contains LenNode structures.  The search key for
475549Srie  * nodes on this main tree is the string length.  Each such node represents
485549Srie  * all strings of a given length, and all strings of that length are found
495549Srie  * within.
505549Srie  *
515549Srie  * The strings within each LenNode are maintained using a secondary AVL tree
525549Srie  * of StrNode structures.  The search key for this inner tree is the string
535549Srie  * itself.  The strings are maintained in lexical order.
545549Srie  */
555549Srie typedef struct {
565549Srie 	avl_node_t	sn_avlnode;	/* AVL book-keeping */
575549Srie 	const char	*sn_str;	/* string */
58*5892Sab196087 	size_t		sn_refcnt;	/* reference count */
595549Srie } StrNode;
605549Srie 
615549Srie typedef struct {
625549Srie 	avl_node_t	ln_avlnode;	/* AVL book-keeping */
635549Srie 	avl_tree_t	*ln_strtree;	/* AVL tree of associated strings */
64*5892Sab196087 	size_t		ln_strlen;	/* length of associated strings */
655549Srie } LenNode;
665549Srie 
675549Srie /*
685549Srie  * Define a master string data item.  Other strings may be suffixes of this
695549Srie  * string.  The final string table will consist of the master string values,
705549Srie  * laid end to end, with the other strings referencing their tails.
715549Srie  */
725549Srie typedef	struct str_master	Str_master;
735549Srie 
745549Srie struct str_master {
755549Srie 	const char	*sm_str;	/* pointer to master string */
765549Srie 	Str_master	*sm_next;	/* used for tracking master strings */
77*5892Sab196087 	size_t		sm_strlen;	/* length of master string */
785549Srie 	uint_t		sm_hashval;	/* hashval of master string */
79*5892Sab196087 	size_t		sm_stroff;	/* offset into destination strtab */
805549Srie };
815549Srie 
825549Srie /*
835549Srie  * Define a hash data item.  This item represents an individual string that has
845549Srie  * been input into the String hash table.  The string may either be a suffix of
855549Srie  * another string, or a master string.
865549Srie  */
875549Srie typedef	struct str_hash	Str_hash;
885549Srie 
895549Srie struct str_hash {
90*5892Sab196087 	size_t		hi_strlen;	/* string length */
91*5892Sab196087 	size_t		hi_refcnt;	/* number of references to str */
925549Srie 	uint_t		hi_hashval;	/* hash for string */
935549Srie 	Str_master	*hi_mstr;	/* pointer to master string */
945549Srie 	Str_hash	*hi_next;	/* next entry in hash bucket */
955549Srie };
965549Srie 
975549Srie /*
985549Srie  * Controlling data structure for a String Table.
995549Srie  */
1005549Srie struct str_tbl {
1015549Srie 	avl_tree_t	*st_lentree;		/* AVL tree of string lengths */
1025549Srie 	char		*st_strbuf;		/* string buffer */
1035549Srie 	Str_hash	**st_hashbcks;		/* hash buckets */
1045549Srie 	Str_master	*st_mstrlist;		/* list of all master strings */
105*5892Sab196087 	size_t		st_fullstrsize;		/* uncompressed table size */
106*5892Sab196087 	size_t		st_nextoff;		/* next available string */
107*5892Sab196087 	size_t		st_strsize;		/* compressed size */
108*5892Sab196087 	size_t		st_strcnt;		/* number of strings */
1095549Srie 	uint_t		st_hbckcnt;		/* number of buckets in */
1105549Srie 						/*    hashlist */
1115549Srie 	uint_t		st_flags;
1125549Srie };
1135549Srie 
1145549Srie #define	FLG_STTAB_COMPRESS	0x01		/* compressed string table */
1155549Srie #define	FLG_STTAB_COOKED	0x02		/* offset has been assigned */
1165549Srie 
1175549Srie /*
1185549Srie  * Starting value for use with string hashing functions inside of string_table.c
1195549Srie  */
1205549Srie #define	HASHSEED		5381
1215549Srie 
1225549Srie #ifdef __cplusplus
1235549Srie }
1245549Srie #endif
1255549Srie 
1265549Srie #endif /* __STRING_TABLE_DOT_H */
127