Path: blob/main/sys/contrib/openzfs/include/libuutil.h
48254 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* Common Development and Distribution License (the "License").6* You may not use this file except in compliance with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/21/*22* Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.23*/2425#ifndef _LIBUUTIL_H26#define _LIBUUTIL_H2728#include <sys/types.h>29#include <stdarg.h>30#include <stdio.h>3132#ifdef __cplusplus33extern "C" {34#endif3536/*37* Standard flags codes.38*/39#define UU_DEFAULT 04041/*42* Standard error codes.43*/44#define UU_ERROR_NONE 0 /* no error */45#define UU_ERROR_INVALID_ARGUMENT 1 /* invalid argument */46#define UU_ERROR_UNKNOWN_FLAG 2 /* passed flag invalid */47#define UU_ERROR_NO_MEMORY 3 /* out of memory */48#define UU_ERROR_CALLBACK_FAILED 4 /* callback-initiated error */49#define UU_ERROR_NOT_SUPPORTED 5 /* operation not supported */50#define UU_ERROR_EMPTY 6 /* no value provided */51#define UU_ERROR_UNDERFLOW 7 /* value is too small */52#define UU_ERROR_OVERFLOW 8 /* value is too value */53#define UU_ERROR_INVALID_CHAR 9 /* value contains unexpected char */54#define UU_ERROR_INVALID_DIGIT 10 /* value contains digit not in base */5556#define UU_ERROR_SYSTEM 99 /* underlying system error */57#define UU_ERROR_UNKNOWN 100 /* error status not known */5859/*60* Exit status profiles.61*/62#define UU_PROFILE_DEFAULT 063#define UU_PROFILE_LAUNCHER 16465/*66* Error reporting functions.67*/68uint32_t uu_error(void);69const char *uu_strerror(uint32_t);7071/*72* Identifier test flags and function.73*/74#define UU_NAME_DOMAIN 0x1 /* allow SUNW, or com.sun, prefix */75#define UU_NAME_PATH 0x2 /* allow '/'-delimited paths */7677int uu_check_name(const char *, uint_t);7879/*80* Convenience functions.81*/82#define UU_NELEM(a) (sizeof (a) / sizeof ((a)[0]))8384extern char *uu_msprintf(const char *format, ...)85__attribute__((format(printf, 1, 2)));86extern void *uu_zalloc(size_t);87extern char *uu_strdup(const char *);88extern void uu_free(void *);8990extern boolean_t uu_strcaseeq(const char *a, const char *b);91extern boolean_t uu_streq(const char *a, const char *b);92extern char *uu_strndup(const char *s, size_t n);93extern boolean_t uu_strbw(const char *a, const char *b);94extern void *uu_memdup(const void *buf, size_t sz);9596/*97* Comparison function type definition.98* Developers should be careful in their use of the _private argument. If you99* break interface guarantees, you get undefined behavior.100*/101typedef int uu_compare_fn_t(const void *__left, const void *__right,102void *__private);103104/*105* Walk variant flags.106* A data structure need not provide support for all variants and107* combinations. Refer to the appropriate documentation.108*/109#define UU_WALK_ROBUST 0x00000001 /* walk can survive removes */110#define UU_WALK_REVERSE 0x00000002 /* reverse walk order */111112#define UU_WALK_PREORDER 0x00000010 /* walk tree in pre-order */113#define UU_WALK_POSTORDER 0x00000020 /* walk tree in post-order */114115/*116* Walk callback function return codes.117*/118#define UU_WALK_ERROR -1119#define UU_WALK_NEXT 0120#define UU_WALK_DONE 1121122/*123* Walk callback function type definition.124*/125typedef int uu_walk_fn_t(void *_elem, void *_private);126127/*128* lists: opaque structures129*/130typedef struct uu_list_pool uu_list_pool_t;131typedef struct uu_list uu_list_t;132133typedef struct uu_list_node {134uintptr_t uln_opaque[2];135} uu_list_node_t;136137typedef struct uu_list_walk uu_list_walk_t;138139typedef uintptr_t uu_list_index_t;140141/*142* lists: interface143*144* basic usage:145* typedef struct foo {146* ...147* uu_list_node_t foo_node;148* ...149* } foo_t;150*151* static int152* foo_compare(void *l_arg, void *r_arg, void *private)153* {154* foo_t *l = l_arg;155* foo_t *r = r_arg;156*157* if (... l greater than r ...)158* return (1);159* if (... l less than r ...)160* return (-1);161* return (0);162* }163*164* ...165* // at initialization time166* foo_pool = uu_list_pool_create("foo_pool",167* sizeof (foo_t), offsetof(foo_t, foo_node), foo_compare,168* debugging? 0 : UU_AVL_POOL_DEBUG);169* ...170*/171uu_list_pool_t *uu_list_pool_create(const char *, size_t, size_t,172uu_compare_fn_t *, uint32_t);173#define UU_LIST_POOL_DEBUG 0x00000001174175void uu_list_pool_destroy(uu_list_pool_t *);176177/*178* usage:179*180* foo_t *a;181* a = malloc(sizeof (*a));182* uu_list_node_init(a, &a->foo_list, pool);183* ...184* uu_list_node_fini(a, &a->foo_list, pool);185* free(a);186*/187void uu_list_node_init(void *, uu_list_node_t *, uu_list_pool_t *);188void uu_list_node_fini(void *, uu_list_node_t *, uu_list_pool_t *);189190uu_list_t *uu_list_create(uu_list_pool_t *, void *_parent, uint32_t);191#define UU_LIST_DEBUG 0x00000001192#define UU_LIST_SORTED 0x00000002 /* list is sorted */193194void uu_list_destroy(uu_list_t *); /* list must be empty */195196size_t uu_list_numnodes(uu_list_t *);197198void *uu_list_first(uu_list_t *);199void *uu_list_last(uu_list_t *);200201void *uu_list_next(uu_list_t *, void *);202void *uu_list_prev(uu_list_t *, void *);203204int uu_list_walk(uu_list_t *, uu_walk_fn_t *, void *, uint32_t);205206uu_list_walk_t *uu_list_walk_start(uu_list_t *, uint32_t);207void *uu_list_walk_next(uu_list_walk_t *);208void uu_list_walk_end(uu_list_walk_t *);209210void *uu_list_find(uu_list_t *, void *, void *, uu_list_index_t *);211void uu_list_insert(uu_list_t *, void *, uu_list_index_t);212213void *uu_list_nearest_next(uu_list_t *, uu_list_index_t);214void *uu_list_nearest_prev(uu_list_t *, uu_list_index_t);215216void *uu_list_teardown(uu_list_t *, void **);217218void uu_list_remove(uu_list_t *, void *);219220/*221* lists: interfaces for non-sorted lists only222*/223int uu_list_insert_before(uu_list_t *, void *_target, void *_elem);224int uu_list_insert_after(uu_list_t *, void *_target, void *_elem);225226/*227* avl trees: opaque structures228*/229typedef struct uu_avl_pool uu_avl_pool_t;230typedef struct uu_avl uu_avl_t;231232typedef struct uu_avl_node {233#ifdef _LP64234uintptr_t uan_opaque[3];235#else236uintptr_t uan_opaque[4];237#endif238} uu_avl_node_t;239240typedef struct uu_avl_walk uu_avl_walk_t;241242typedef uintptr_t uu_avl_index_t;243244/*245* avl trees: interface246*247* basic usage:248* typedef struct foo {249* ...250* uu_avl_node_t foo_node;251* ...252* } foo_t;253*254* static int255* foo_compare(void *l_arg, void *r_arg, void *private)256* {257* foo_t *l = l_arg;258* foo_t *r = r_arg;259*260* if (... l greater than r ...)261* return (1);262* if (... l less than r ...)263* return (-1);264* return (0);265* }266*267* ...268* // at initialization time269* foo_pool = uu_avl_pool_create("foo_pool",270* sizeof (foo_t), offsetof(foo_t, foo_node), foo_compare,271* debugging? 0 : UU_AVL_POOL_DEBUG);272* ...273*/274uu_avl_pool_t *uu_avl_pool_create(const char *, size_t, size_t,275uu_compare_fn_t *, uint32_t);276#define UU_AVL_POOL_DEBUG 0x00000001277278void uu_avl_pool_destroy(uu_avl_pool_t *);279280/*281* usage:282*283* foo_t *a;284* a = malloc(sizeof (*a));285* uu_avl_node_init(a, &a->foo_avl, pool);286* ...287* uu_avl_node_fini(a, &a->foo_avl, pool);288* free(a);289*/290void uu_avl_node_init(void *, uu_avl_node_t *, uu_avl_pool_t *);291void uu_avl_node_fini(void *, uu_avl_node_t *, uu_avl_pool_t *);292293uu_avl_t *uu_avl_create(uu_avl_pool_t *, void *_parent, uint32_t);294#define UU_AVL_DEBUG 0x00000001295296void uu_avl_destroy(uu_avl_t *); /* list must be empty */297298size_t uu_avl_numnodes(uu_avl_t *);299300void *uu_avl_first(uu_avl_t *);301void *uu_avl_last(uu_avl_t *);302303void *uu_avl_next(uu_avl_t *, void *);304void *uu_avl_prev(uu_avl_t *, void *);305306int uu_avl_walk(uu_avl_t *, uu_walk_fn_t *, void *, uint32_t);307308uu_avl_walk_t *uu_avl_walk_start(uu_avl_t *, uint32_t);309void *uu_avl_walk_next(uu_avl_walk_t *);310void uu_avl_walk_end(uu_avl_walk_t *);311312void *uu_avl_find(uu_avl_t *, void *, void *, uu_avl_index_t *);313void uu_avl_insert(uu_avl_t *, void *, uu_avl_index_t);314315void *uu_avl_nearest_next(uu_avl_t *, uu_avl_index_t);316void *uu_avl_nearest_prev(uu_avl_t *, uu_avl_index_t);317318void *uu_avl_teardown(uu_avl_t *, void **);319320void uu_avl_remove(uu_avl_t *, void *);321322#ifdef __cplusplus323}324#endif325326#endif /* _LIBUUTIL_H */327328329