17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
238918dff3Sjwadams  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27*54d02241SBryan Cantrill /*
28*54d02241SBryan Cantrill  * Copyright (c) 2015, Joyent, Inc. All rights reserved.
29*54d02241SBryan Cantrill  */
30*54d02241SBryan Cantrill 
317c478bd9Sstevel@tonic-gate #ifndef	_LIBUUTIL_IMPL_H
327c478bd9Sstevel@tonic-gate #define	_LIBUUTIL_IMPL_H
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <libuutil.h>
357c478bd9Sstevel@tonic-gate #include <pthread.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <sys/avl_impl.h>
388918dff3Sjwadams #include <sys/byteorder.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
417c478bd9Sstevel@tonic-gate extern "C" {
427c478bd9Sstevel@tonic-gate #endif
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate void uu_set_error(uint_t);
457c478bd9Sstevel@tonic-gate #pragma rarely_called(uu_set_error)
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate struct uu_dprintf {
487c478bd9Sstevel@tonic-gate 	char	*uud_name;
497c478bd9Sstevel@tonic-gate 	uu_dprintf_severity_t uud_severity;
507c478bd9Sstevel@tonic-gate 	uint_t	uud_flags;
517c478bd9Sstevel@tonic-gate };
527c478bd9Sstevel@tonic-gate 
538918dff3Sjwadams /*
548918dff3Sjwadams  * For debugging purposes, libuutil keeps around linked lists of all uu_lists
558918dff3Sjwadams  * and uu_avls, along with pointers to their parents.  These can cause false
568918dff3Sjwadams  * negatives when looking for memory leaks, so we encode the pointers by
578918dff3Sjwadams  * storing them with swapped endianness;  this is not perfect, but it's about
588918dff3Sjwadams  * the best we can do without wasting a lot of space.
598918dff3Sjwadams  */
608918dff3Sjwadams #ifdef _LP64
618918dff3Sjwadams #define	UU_PTR_ENCODE(ptr)		BSWAP_64((uintptr_t)(void *)(ptr))
628918dff3Sjwadams #else
638918dff3Sjwadams #define	UU_PTR_ENCODE(ptr)		BSWAP_32((uintptr_t)(void *)(ptr))
648918dff3Sjwadams #endif
658918dff3Sjwadams 
668918dff3Sjwadams #define	UU_PTR_DECODE(ptr)		((void *)UU_PTR_ENCODE(ptr))
678918dff3Sjwadams 
687c478bd9Sstevel@tonic-gate /*
697c478bd9Sstevel@tonic-gate  * uu_list structures
707c478bd9Sstevel@tonic-gate  */
717c478bd9Sstevel@tonic-gate typedef struct uu_list_node_impl {
727c478bd9Sstevel@tonic-gate 	struct uu_list_node_impl *uln_next;
737c478bd9Sstevel@tonic-gate 	struct uu_list_node_impl *uln_prev;
747c478bd9Sstevel@tonic-gate } uu_list_node_impl_t;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate struct uu_list_walk {
777c478bd9Sstevel@tonic-gate 	uu_list_walk_t	*ulw_next;
787c478bd9Sstevel@tonic-gate 	uu_list_walk_t	*ulw_prev;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	uu_list_t	*ulw_list;
817c478bd9Sstevel@tonic-gate 	int8_t		ulw_dir;
827c478bd9Sstevel@tonic-gate 	uint8_t		ulw_robust;
837c478bd9Sstevel@tonic-gate 	uu_list_node_impl_t *ulw_next_result;
847c478bd9Sstevel@tonic-gate };
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate struct uu_list {
878918dff3Sjwadams 	uintptr_t	ul_next_enc;
888918dff3Sjwadams 	uintptr_t	ul_prev_enc;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	uu_list_pool_t	*ul_pool;
918918dff3Sjwadams 	uintptr_t	ul_parent_enc;	/* encoded parent pointer */
927c478bd9Sstevel@tonic-gate 	size_t		ul_offset;
937c478bd9Sstevel@tonic-gate 	size_t		ul_numnodes;
947c478bd9Sstevel@tonic-gate 	uint8_t		ul_debug;
957c478bd9Sstevel@tonic-gate 	uint8_t		ul_sorted;
967c478bd9Sstevel@tonic-gate 	uint8_t		ul_index;	/* mark for uu_list_index_ts */
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	uu_list_node_impl_t ul_null_node;
997c478bd9Sstevel@tonic-gate 	uu_list_walk_t	ul_null_walk;	/* for robust walkers */
1007c478bd9Sstevel@tonic-gate };
1017c478bd9Sstevel@tonic-gate 
1028918dff3Sjwadams #define	UU_LIST_PTR(ptr)		((uu_list_t *)UU_PTR_DECODE(ptr))
1038918dff3Sjwadams 
1047c478bd9Sstevel@tonic-gate #define	UU_LIST_POOL_MAXNAME	64
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate struct uu_list_pool {
1077c478bd9Sstevel@tonic-gate 	uu_list_pool_t	*ulp_next;
1087c478bd9Sstevel@tonic-gate 	uu_list_pool_t	*ulp_prev;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	char		ulp_name[UU_LIST_POOL_MAXNAME];
1117c478bd9Sstevel@tonic-gate 	size_t		ulp_nodeoffset;
1127c478bd9Sstevel@tonic-gate 	size_t		ulp_objsize;
1137c478bd9Sstevel@tonic-gate 	uu_compare_fn_t	*ulp_cmp;
1147c478bd9Sstevel@tonic-gate 	uint8_t		ulp_debug;
1157c478bd9Sstevel@tonic-gate 	uint8_t		ulp_last_index;
1167c478bd9Sstevel@tonic-gate 	pthread_mutex_t	ulp_lock;		/* protects null_list */
1177c478bd9Sstevel@tonic-gate 	uu_list_t	ulp_null_list;
1187c478bd9Sstevel@tonic-gate };
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate /*
1217c478bd9Sstevel@tonic-gate  * uu_avl structures
1227c478bd9Sstevel@tonic-gate  */
1237c478bd9Sstevel@tonic-gate typedef struct avl_node		uu_avl_node_impl_t;
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate struct uu_avl_walk {
1267c478bd9Sstevel@tonic-gate 	uu_avl_walk_t	*uaw_next;
1277c478bd9Sstevel@tonic-gate 	uu_avl_walk_t	*uaw_prev;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	uu_avl_t	*uaw_avl;
1307c478bd9Sstevel@tonic-gate 	void		*uaw_next_result;
1317c478bd9Sstevel@tonic-gate 	int8_t		uaw_dir;
1327c478bd9Sstevel@tonic-gate 	uint8_t		uaw_robust;
1337c478bd9Sstevel@tonic-gate };
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate struct uu_avl {
1368918dff3Sjwadams 	uintptr_t	ua_next_enc;
1378918dff3Sjwadams 	uintptr_t	ua_prev_enc;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	uu_avl_pool_t	*ua_pool;
1408918dff3Sjwadams 	uintptr_t	ua_parent_enc;
1417c478bd9Sstevel@tonic-gate 	uint8_t		ua_debug;
1427c478bd9Sstevel@tonic-gate 	uint8_t		ua_index;	/* mark for uu_avl_index_ts */
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	struct avl_tree	ua_tree;
1457c478bd9Sstevel@tonic-gate 	uu_avl_walk_t	ua_null_walk;
1467c478bd9Sstevel@tonic-gate };
1477c478bd9Sstevel@tonic-gate 
1488918dff3Sjwadams #define	UU_AVL_PTR(x)		((uu_avl_t *)UU_PTR_DECODE(x))
1498918dff3Sjwadams 
1507c478bd9Sstevel@tonic-gate #define	UU_AVL_POOL_MAXNAME	64
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate struct uu_avl_pool {
1537c478bd9Sstevel@tonic-gate 	uu_avl_pool_t	*uap_next;
1547c478bd9Sstevel@tonic-gate 	uu_avl_pool_t	*uap_prev;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	char		uap_name[UU_AVL_POOL_MAXNAME];
1577c478bd9Sstevel@tonic-gate 	size_t		uap_nodeoffset;
1587c478bd9Sstevel@tonic-gate 	size_t		uap_objsize;
1597c478bd9Sstevel@tonic-gate 	uu_compare_fn_t	*uap_cmp;
1607c478bd9Sstevel@tonic-gate 	uint8_t		uap_debug;
1617c478bd9Sstevel@tonic-gate 	uint8_t		uap_last_index;
1627c478bd9Sstevel@tonic-gate 	pthread_mutex_t	uap_lock;		/* protects null_avl */
1637c478bd9Sstevel@tonic-gate 	uu_avl_t	uap_null_avl;
1647c478bd9Sstevel@tonic-gate };
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate /*
1677c478bd9Sstevel@tonic-gate  * atfork() handlers
1687c478bd9Sstevel@tonic-gate  */
1697c478bd9Sstevel@tonic-gate void uu_avl_lockup(void);
1707c478bd9Sstevel@tonic-gate void uu_avl_release(void);
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate void uu_list_lockup(void);
1737c478bd9Sstevel@tonic-gate void uu_list_release(void);
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate #endif
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate #endif	/* _LIBUUTIL_IMPL_H */
180