xref: /openbsd/usr.bin/dig/lib/isc/include/isc/util.h (revision b9558d14)
1 /*
2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14  * PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 /* $Id: util.h,v 1.3 2020/02/25 05:00:43 jsg Exp $ */
18 
19 #ifndef ISC_UTIL_H
20 #define ISC_UTIL_H 1
21 
22 /*! \file isc/util.h
23  * NOTE:
24  *
25  * This file is not to be included from any <isc/???.h> (or other) library
26  * files.
27  *
28  * \brief
29  * Including this file puts several macros in your name space that are
30  * not protected (as all the other ISC functions/macros do) by prepending
31  * ISC_ or isc_ to the name.
32  */
33 
34 /***
35  *** General Macros.
36  ***/
37 
38 /*%
39  * Use this to hide unused function arguments.
40  * \code
41  * int
42  * foo(char *bar)
43  * {
44  *	UNUSED(bar);
45  * }
46  * \endcode
47  */
48 #define UNUSED(x)      (void)(x)
49 
50 /*%
51  * The opposite: silent warnings about stored values which are never read.
52  */
53 #define POST(x)        (void)(x)
54 
55 #define ISC_MAX(a, b)  ((a) > (b) ? (a) : (b))
56 #define ISC_MIN(a, b)  ((a) < (b) ? (a) : (b))
57 
58 #define ISC_CLAMP(v, x, y) ((v) < (x) ? (x) : ((v) > (y) ? (y) : (v)))
59 
60 /*%
61  * Use this to remove the const qualifier of a variable to assign it to
62  * a non-const variable or pass it as a non-const function argument ...
63  * but only when you are sure it won't then be changed!
64  * This is necessary to sometimes shut up some compilers
65  * (as with gcc -Wcast-qual) when there is just no other good way to avoid the
66  * situation.
67  */
68 #define DE_CONST(konst, var) \
69 	do { \
70 		union { const void *k; void *v; } _u; \
71 		_u.k = konst; \
72 		var = _u.v; \
73 	} while (0)
74 
75 /*%
76  * Use this in translation units that would otherwise be empty, to
77  * suppress compiler warnings.
78  */
79 #define EMPTY_TRANSLATION_UNIT static void isc__empty(void) { isc__empty(); }
80 
81 /*%
82  * We use macros instead of calling the routines directly because
83  * the capital letters make the locking stand out.
84  * We RUNTIME_CHECK for success since in general there's no way
85  * for us to continue if they fail.
86  */
87 
88 #include <isc/result.h>		/* Contractual promise. */
89 /*
90  * List Macros.
91  */
92 #include <isc/list.h>		/* Contractual promise. */
93 
94 #define LIST(type)			ISC_LIST(type)
95 #define INIT_LIST(type)			ISC_LIST_INIT(type)
96 #define LINK(type)			ISC_LINK(type)
97 #define INIT_LINK(elt, link)		ISC_LINK_INIT(elt, link)
98 #define HEAD(list)			ISC_LIST_HEAD(list)
99 #define TAIL(list)			ISC_LIST_TAIL(list)
100 #define EMPTY(list)			ISC_LIST_EMPTY(list)
101 #define PREV(elt, link)			ISC_LIST_PREV(elt, link)
102 #define NEXT(elt, link)			ISC_LIST_NEXT(elt, link)
103 #define APPEND(list, elt, link)		ISC_LIST_APPEND(list, elt, link)
104 #define PREPEND(list, elt, link)	ISC_LIST_PREPEND(list, elt, link)
105 #define UNLINK(list, elt, link)		ISC_LIST_UNLINK(list, elt, link)
106 #define ENQUEUE(list, elt, link)	ISC_LIST_APPEND(list, elt, link)
107 #define DEQUEUE(list, elt, link)	ISC_LIST_UNLINK(list, elt, link)
108 #define INSERTBEFORE(li, b, e, ln)	ISC_LIST_INSERTBEFORE(li, b, e, ln)
109 #define INSERTAFTER(li, a, e, ln)	ISC_LIST_INSERTAFTER(li, a, e, ln)
110 #define APPENDLIST(list1, list2, link)	ISC_LIST_APPENDLIST(list1, list2, link)
111 
112 /*%
113  * Performance
114  */
115 
116 /*
117  * Assertions
118  */
119 #include <isc/assertions.h>	/* Contractual promise. */
120 
121 /*% Require Assertion */
122 #define REQUIRE(e)			ISC_REQUIRE(e)
123 /*% Ensure Assertion */
124 #define ENSURE(e)			ISC_ENSURE(e)
125 /*% Insist Assertion */
126 #define INSIST(e)			ISC_INSIST(e)
127 /*% Invariant Assertion */
128 #define INVARIANT(e)			ISC_INVARIANT(e)
129 
130 /*
131  * Errors
132  */
133 #include <isc/error.h>		/* Contractual promise. */
134 
135 /*% Unexpected Error */
136 #define UNEXPECTED_ERROR		isc_error_unexpected
137 /*% Fatal Error */
138 #define FATAL_ERROR			isc_error_fatal
139 /*% Runtime Check */
140 #define RUNTIME_CHECK(cond)		ISC_ERROR_RUNTIMECHECK(cond)
141 
142 #endif /* ISC_UTIL_H */
143