xref: /illumos-gate/usr/src/lib/libuutil/common/libuutil_impl.h (revision 54d02241ceb89d90c765e528622d88f84f65dab3)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * Copyright (c) 2015, Joyent, Inc. All rights reserved.
29  */
30 
31 #ifndef	_LIBUUTIL_IMPL_H
32 #define	_LIBUUTIL_IMPL_H
33 
34 #include <libuutil.h>
35 #include <pthread.h>
36 
37 #include <sys/avl_impl.h>
38 #include <sys/byteorder.h>
39 
40 #ifdef	__cplusplus
41 extern "C" {
42 #endif
43 
44 void uu_set_error(uint_t);
45 #pragma rarely_called(uu_set_error)
46 
47 struct uu_dprintf {
48 	char	*uud_name;
49 	uu_dprintf_severity_t uud_severity;
50 	uint_t	uud_flags;
51 };
52 
53 /*
54  * For debugging purposes, libuutil keeps around linked lists of all uu_lists
55  * and uu_avls, along with pointers to their parents.  These can cause false
56  * negatives when looking for memory leaks, so we encode the pointers by
57  * storing them with swapped endianness;  this is not perfect, but it's about
58  * the best we can do without wasting a lot of space.
59  */
60 #ifdef _LP64
61 #define	UU_PTR_ENCODE(ptr)		BSWAP_64((uintptr_t)(void *)(ptr))
62 #else
63 #define	UU_PTR_ENCODE(ptr)		BSWAP_32((uintptr_t)(void *)(ptr))
64 #endif
65 
66 #define	UU_PTR_DECODE(ptr)		((void *)UU_PTR_ENCODE(ptr))
67 
68 /*
69  * uu_list structures
70  */
71 typedef struct uu_list_node_impl {
72 	struct uu_list_node_impl *uln_next;
73 	struct uu_list_node_impl *uln_prev;
74 } uu_list_node_impl_t;
75 
76 struct uu_list_walk {
77 	uu_list_walk_t	*ulw_next;
78 	uu_list_walk_t	*ulw_prev;
79 
80 	uu_list_t	*ulw_list;
81 	int8_t		ulw_dir;
82 	uint8_t		ulw_robust;
83 	uu_list_node_impl_t *ulw_next_result;
84 };
85 
86 struct uu_list {
87 	uintptr_t	ul_next_enc;
88 	uintptr_t	ul_prev_enc;
89 
90 	uu_list_pool_t	*ul_pool;
91 	uintptr_t	ul_parent_enc;	/* encoded parent pointer */
92 	size_t		ul_offset;
93 	size_t		ul_numnodes;
94 	uint8_t		ul_debug;
95 	uint8_t		ul_sorted;
96 	uint8_t		ul_index;	/* mark for uu_list_index_ts */
97 
98 	uu_list_node_impl_t ul_null_node;
99 	uu_list_walk_t	ul_null_walk;	/* for robust walkers */
100 };
101 
102 #define	UU_LIST_PTR(ptr)		((uu_list_t *)UU_PTR_DECODE(ptr))
103 
104 #define	UU_LIST_POOL_MAXNAME	64
105 
106 struct uu_list_pool {
107 	uu_list_pool_t	*ulp_next;
108 	uu_list_pool_t	*ulp_prev;
109 
110 	char		ulp_name[UU_LIST_POOL_MAXNAME];
111 	size_t		ulp_nodeoffset;
112 	size_t		ulp_objsize;
113 	uu_compare_fn_t	*ulp_cmp;
114 	uint8_t		ulp_debug;
115 	uint8_t		ulp_last_index;
116 	pthread_mutex_t	ulp_lock;		/* protects null_list */
117 	uu_list_t	ulp_null_list;
118 };
119 
120 /*
121  * uu_avl structures
122  */
123 typedef struct avl_node		uu_avl_node_impl_t;
124 
125 struct uu_avl_walk {
126 	uu_avl_walk_t	*uaw_next;
127 	uu_avl_walk_t	*uaw_prev;
128 
129 	uu_avl_t	*uaw_avl;
130 	void		*uaw_next_result;
131 	int8_t		uaw_dir;
132 	uint8_t		uaw_robust;
133 };
134 
135 struct uu_avl {
136 	uintptr_t	ua_next_enc;
137 	uintptr_t	ua_prev_enc;
138 
139 	uu_avl_pool_t	*ua_pool;
140 	uintptr_t	ua_parent_enc;
141 	uint8_t		ua_debug;
142 	uint8_t		ua_index;	/* mark for uu_avl_index_ts */
143 
144 	struct avl_tree	ua_tree;
145 	uu_avl_walk_t	ua_null_walk;
146 };
147 
148 #define	UU_AVL_PTR(x)		((uu_avl_t *)UU_PTR_DECODE(x))
149 
150 #define	UU_AVL_POOL_MAXNAME	64
151 
152 struct uu_avl_pool {
153 	uu_avl_pool_t	*uap_next;
154 	uu_avl_pool_t	*uap_prev;
155 
156 	char		uap_name[UU_AVL_POOL_MAXNAME];
157 	size_t		uap_nodeoffset;
158 	size_t		uap_objsize;
159 	uu_compare_fn_t	*uap_cmp;
160 	uint8_t		uap_debug;
161 	uint8_t		uap_last_index;
162 	pthread_mutex_t	uap_lock;		/* protects null_avl */
163 	uu_avl_t	uap_null_avl;
164 };
165 
166 /*
167  * atfork() handlers
168  */
169 void uu_avl_lockup(void);
170 void uu_avl_release(void);
171 
172 void uu_list_lockup(void);
173 void uu_list_release(void);
174 
175 #ifdef	__cplusplus
176 }
177 #endif
178 
179 #endif	/* _LIBUUTIL_IMPL_H */
180