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