xref: /illumos-gate/usr/src/lib/libuutil/common/libuutil_impl.h (revision fcdb3229a31dd4ff700c69238814e326aad49098)
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 
46 struct uu_dprintf {
47 	char	*uud_name;
48 	uu_dprintf_severity_t uud_severity;
49 	uint_t	uud_flags;
50 };
51 
52 /*
53  * For debugging purposes, libuutil keeps around linked lists of all uu_lists
54  * and uu_avls, along with pointers to their parents.  These can cause false
55  * negatives when looking for memory leaks, so we encode the pointers by
56  * storing them with swapped endianness;  this is not perfect, but it's about
57  * the best we can do without wasting a lot of space.
58  */
59 #ifdef _LP64
60 #define	UU_PTR_ENCODE(ptr)		BSWAP_64((uintptr_t)(void *)(ptr))
61 #else
62 #define	UU_PTR_ENCODE(ptr)		BSWAP_32((uintptr_t)(void *)(ptr))
63 #endif
64 
65 #define	UU_PTR_DECODE(ptr)		((void *)UU_PTR_ENCODE(ptr))
66 
67 /*
68  * uu_list structures
69  */
70 typedef struct uu_list_node_impl {
71 	struct uu_list_node_impl *uln_next;
72 	struct uu_list_node_impl *uln_prev;
73 } uu_list_node_impl_t;
74 
75 struct uu_list_walk {
76 	uu_list_walk_t	*ulw_next;
77 	uu_list_walk_t	*ulw_prev;
78 
79 	uu_list_t	*ulw_list;
80 	int8_t		ulw_dir;
81 	uint8_t		ulw_robust;
82 	uu_list_node_impl_t *ulw_next_result;
83 };
84 
85 struct uu_list {
86 	uintptr_t	ul_next_enc;
87 	uintptr_t	ul_prev_enc;
88 
89 	uu_list_pool_t	*ul_pool;
90 	uintptr_t	ul_parent_enc;	/* encoded parent pointer */
91 	size_t		ul_offset;
92 	size_t		ul_numnodes;
93 	uint8_t		ul_debug;
94 	uint8_t		ul_sorted;
95 	uint8_t		ul_index;	/* mark for uu_list_index_ts */
96 
97 	uu_list_node_impl_t ul_null_node;
98 	uu_list_walk_t	ul_null_walk;	/* for robust walkers */
99 };
100 
101 #define	UU_LIST_PTR(ptr)		((uu_list_t *)UU_PTR_DECODE(ptr))
102 
103 #define	UU_LIST_POOL_MAXNAME	64
104 
105 struct uu_list_pool {
106 	uu_list_pool_t	*ulp_next;
107 	uu_list_pool_t	*ulp_prev;
108 
109 	char		ulp_name[UU_LIST_POOL_MAXNAME];
110 	size_t		ulp_nodeoffset;
111 	size_t		ulp_objsize;
112 	uu_compare_fn_t	*ulp_cmp;
113 	uint8_t		ulp_debug;
114 	uint8_t		ulp_last_index;
115 	pthread_mutex_t	ulp_lock;		/* protects null_list */
116 	uu_list_t	ulp_null_list;
117 };
118 
119 /*
120  * uu_avl structures
121  */
122 typedef struct avl_node		uu_avl_node_impl_t;
123 
124 struct uu_avl_walk {
125 	uu_avl_walk_t	*uaw_next;
126 	uu_avl_walk_t	*uaw_prev;
127 
128 	uu_avl_t	*uaw_avl;
129 	void		*uaw_next_result;
130 	int8_t		uaw_dir;
131 	uint8_t		uaw_robust;
132 };
133 
134 struct uu_avl {
135 	uintptr_t	ua_next_enc;
136 	uintptr_t	ua_prev_enc;
137 
138 	uu_avl_pool_t	*ua_pool;
139 	uintptr_t	ua_parent_enc;
140 	uint8_t		ua_debug;
141 	uint8_t		ua_index;	/* mark for uu_avl_index_ts */
142 
143 	struct avl_tree	ua_tree;
144 	uu_avl_walk_t	ua_null_walk;
145 };
146 
147 #define	UU_AVL_PTR(x)		((uu_avl_t *)UU_PTR_DECODE(x))
148 
149 #define	UU_AVL_POOL_MAXNAME	64
150 
151 struct uu_avl_pool {
152 	uu_avl_pool_t	*uap_next;
153 	uu_avl_pool_t	*uap_prev;
154 
155 	char		uap_name[UU_AVL_POOL_MAXNAME];
156 	size_t		uap_nodeoffset;
157 	size_t		uap_objsize;
158 	uu_compare_fn_t	*uap_cmp;
159 	uint8_t		uap_debug;
160 	uint8_t		uap_last_index;
161 	pthread_mutex_t	uap_lock;		/* protects null_avl */
162 	uu_avl_t	uap_null_avl;
163 };
164 
165 /*
166  * atfork() handlers
167  */
168 void uu_avl_lockup(void);
169 void uu_avl_release(void);
170 
171 void uu_list_lockup(void);
172 void uu_list_release(void);
173 
174 #ifdef	__cplusplus
175 }
176 #endif
177 
178 #endif	/* _LIBUUTIL_IMPL_H */
179