xref: /illumos-gate/usr/src/cmd/ldmad/mdesc_mutable.h (revision 27954b0d964ffcb749cf19296906e7fecdf3da1b)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef	_MDESC_MUTABLE_H_
28 #define	_MDESC_MUTABLE_H_
29 
30 #ifdef DEBUG
31 #include <assert.h>
32 #endif
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 #ifdef DEBUG
39 
40 #define	ASSERT(_s)	assert(_s)
41 
42 #else	/* DEBUG */
43 
44 #define	ASSERT(_s)
45 
46 #endif	/* DEBUG */
47 
48 #define	MD_ALIGNMENT_SIZE	0x10
49 #define	MD_OFFSET_UNDEF		(uint32_t)-1
50 /*
51  * List management macros for mutable MD structure
52  */
53 #define	CHAIN(_type, _chain)						\
54 	struct {							\
55 		_type	*startp;					\
56 		_type	*endp;						\
57 		int	count;						\
58 	} _chain
59 
60 #define	CHAIN_ITER(_chain, _itv)					\
61 	for ((_itv) = CHAIN_START(_chain); (_itv) != NULL;		\
62 		(_itv) = (_itv)->nextp)
63 
64 #define	CHAIN_START(_name)	((_name).startp)
65 #define	CHAIN_LENGTH(_name)	((_name).count)
66 
67 /*
68  * Add node _nodep to the end of _chain via the required 'nextp' element.
69  */
70 #define	CHAIN_ADD(_chain, _nodep)					\
71 	do {								\
72 		if ((_chain).startp == NULL) {				\
73 			(_chain).startp = (_nodep);			\
74 		} else {						\
75 			(_chain).endp->nextp = (_nodep);		\
76 		}							\
77 		(_chain).endp = (_nodep);				\
78 		(_nodep)->nextp = NULL;					\
79 		(_chain).count++;					\
80 	NOTE(CONSTCOND) } while (0)
81 
82 /*
83  * Internal definitions.
84  */
85 
86 typedef struct md_string md_string_t;
87 typedef struct md_data_block md_data_block_t;
88 typedef struct md_prop md_prop_t;
89 typedef struct md_node md_node_t;
90 typedef struct mmd mmd_t;
91 
92 struct md_string {
93 	md_string_t	*nextp;
94 	char		*strp;
95 	int		size;	/* strlen()+1 */
96 	uint32_t	hash;
97 	int		ref_cnt;
98 	uint32_t	build_offset;
99 };
100 
101 struct md_data_block {
102 	md_data_block_t *nextp;
103 	uint8_t		*datap;
104 	uint32_t	size;
105 	uint32_t	hash;
106 	int		ref_cnt;
107 	uint32_t	build_offset;
108 };
109 
110 struct md_prop {
111 	uint8_t		type;
112 	md_string_t	*sp;
113 	union {
114 		uint64_t	value;
115 		struct {
116 			boolean_t	is_ptr;
117 			union {
118 				uint64_t	index;
119 				md_node_t	*nodep;
120 			} val;
121 		} arc;
122 		md_data_block_t *dbp;
123 	} d;
124 	md_prop_t	*nextp;
125 };
126 
127 struct md_node {
128 	md_string_t	*typep;
129 	CHAIN(md_prop_t, prop_list);
130 	md_node_t	*nextp;
131 	int		build_index;	/* for building a binary md & cloning */
132 	int		next_index;	/* for building a binary md */
133 	char		seen;		/* seen flag (md_scan_dag/md_scour) */
134 	char		deleted;	/* pending deletion flag */
135 };
136 
137 struct mmd {
138 	CHAIN(md_node_t, node_list);
139 	CHAIN(md_string_t, string_list);
140 	CHAIN(md_data_block_t, data_block_list);
141 };
142 
143 md_node_t *md_new_node(mmd_t *mdp, char *sp);
144 int md_add_value_property(mmd_t *mdp,
145     md_node_t *nodep, char *sp, uint64_t value);
146 int md_add_string_property(mmd_t *mdp, md_node_t *nodep, char *sp, char *bufp);
147 int md_add_data_property(mmd_t *mdp, md_node_t *nodep, char *sp, int len,
148     uint8_t *bufp);
149 int md_gen_bin(mmd_t *mdp, uint8_t **bufpp);
150 md_node_t *md_link_new_node(mmd_t *mdp, char *nodenamep, md_node_t *parentnodep,
151     char *linktonewp, char *linkbackp);
152 mmd_t *md_new_md(void);
153 void md_free_node(mmd_t *mdp, md_node_t *nodep);
154 void md_destroy(mmd_t *);
155 
156 #ifdef __cplusplus
157 }
158 #endif
159 
160 #endif	/* _MDESC_MUTABLE_H_ */
161