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