4 * Copyright (C) 1998-2001 Internet Software Consortium.
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
11 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
12 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
13 * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
15 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
16 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
17 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 /* Id: util.h,v 1.23 2001/11/30 01:59:38 gson Exp */
28 * This file is not to be included from any <isc/???.h> (or other) library
31 * Including this file puts several macros in your name space that are
32 * not protected (as all the other ISC functions/macros do) by prepending
33 * ISC_ or isc_ to the name.
41 * Use this to hide unused function arguments.
49 #define UNUSED(x) (void)(x)
51 #define ISC_MAX(a, b) ((a) > (b) ? (a) : (b))
52 #define ISC_MIN(a, b) ((a) < (b) ? (a) : (b))
55 * Use this to remove the const qualifier of a variable to assign it to
56 * a non-const variable or pass it as a non-const function argument ...
57 * but only when you are sure it won't then be changed!
58 * This is necessary to sometimes shut up some compilers
59 * (as with gcc -Wcast-qual) when there is just no other good way to avoid the
62 #define DE_CONST(konst, var) \
64 union { const void *k; void *v; } _u; \
70 * Use this in translation units that would otherwise be empty, to
71 * suppress compiler warnings.
73 #define EMPTY_TRANSLATION_UNIT static void isc__empty(void) { isc__empty(); }
76 * We use macros instead of calling the routines directly because
77 * the capital letters make the locking stand out.
79 * We RUNTIME_CHECK for success since in general there's no way
80 * for us to continue if they fail.
83 #ifdef ISC_UTIL_TRACEON
84 #define ISC_UTIL_TRACE(a) a
85 #include <stdio.h> /* Required for fprintf/stderr when tracing. */
86 #include <isc/msgs.h> /* Required for isc_msgcat when tracing. */
88 #define ISC_UTIL_TRACE(a)
91 #include <isc/result.h> /* Contractual promise. */
93 #define LOCK(lp) do { \
94 ISC_UTIL_TRACE(fprintf(stderr, "%s %p %s %d\n", \
95 isc_msgcat_get(isc_msgcat, ISC_MSGSET_UTIL, \
96 ISC_MSG_LOCKING, "LOCKING"), \
97 (lp), __FILE__, __LINE__)); \
98 RUNTIME_CHECK(isc_mutex_lock((lp)) == ISC_R_SUCCESS); \
99 ISC_UTIL_TRACE(fprintf(stderr, "%s %p %s %d\n", \
100 isc_msgcat_get(isc_msgcat, ISC_MSGSET_UTIL, \
101 ISC_MSG_LOCKED, "LOCKED"), \
102 (lp), __FILE__, __LINE__)); \
104 #define UNLOCK(lp) do { \
105 RUNTIME_CHECK(isc_mutex_unlock((lp)) == ISC_R_SUCCESS); \
106 ISC_UTIL_TRACE(fprintf(stderr, "%s %p %s %d\n", \
107 isc_msgcat_get(isc_msgcat, ISC_MSGSET_UTIL, \
108 ISC_MSG_UNLOCKED, "UNLOCKED"), \
109 (lp), __FILE__, __LINE__)); \
111 #define ISLOCKED(lp) (1)
112 #define DESTROYLOCK(lp) \
113 RUNTIME_CHECK(isc_mutex_destroy((lp)) == ISC_R_SUCCESS)
116 #define BROADCAST(cvp) do { \
117 ISC_UTIL_TRACE(fprintf(stderr, "%s %p %s %d\n", \
118 isc_msgcat_get(isc_msgcat, ISC_MSGSET_UTIL, \
119 ISC_MSG_BROADCAST, "BROADCAST"),\
120 (cvp), __FILE__, __LINE__)); \
121 RUNTIME_CHECK(isc_condition_broadcast((cvp)) == ISC_R_SUCCESS); \
123 #define SIGNAL(cvp) do { \
124 ISC_UTIL_TRACE(fprintf(stderr, "%s %p %s %d\n", \
125 isc_msgcat_get(isc_msgcat, ISC_MSGSET_UTIL, \
126 ISC_MSG_SIGNAL, "SIGNAL"), \
127 (cvp), __FILE__, __LINE__)); \
128 RUNTIME_CHECK(isc_condition_signal((cvp)) == ISC_R_SUCCESS); \
130 #define WAIT(cvp, lp) do { \
131 ISC_UTIL_TRACE(fprintf(stderr, "%s %p %s %p %s %d\n", \
132 isc_msgcat_get(isc_msgcat, ISC_MSGSET_UTIL, \
133 ISC_MSG_UTILWAIT, "WAIT"), \
135 isc_msgcat_get(isc_msgcat, ISC_MSGSET_UTIL, \
136 ISC_MSG_LOCK, "LOCK"), \
137 (lp), __FILE__, __LINE__)); \
138 RUNTIME_CHECK(isc_condition_wait((cvp), (lp)) == ISC_R_SUCCESS); \
139 ISC_UTIL_TRACE(fprintf(stderr, "%s %p %s %p %s %d\n", \
140 isc_msgcat_get(isc_msgcat, ISC_MSGSET_UTIL, \
141 ISC_MSG_WAITED, "WAITED"), \
143 isc_msgcat_get(isc_msgcat, ISC_MSGSET_UTIL, \
144 ISC_MSG_LOCKED, "LOCKED"), \
145 (lp), __FILE__, __LINE__)); \
149 * isc_condition_waituntil can return ISC_R_TIMEDOUT, so we
150 * don't RUNTIME_CHECK the result.
152 * XXX Also, can't really debug this then...
155 #define WAITUNTIL(cvp, lp, tp) \
156 isc_condition_waituntil((cvp), (lp), (tp))
158 #define RWLOCK(lp, t) do { \
159 ISC_UTIL_TRACE(fprintf(stderr, "%s %p, %d %s %d\n", \
160 isc_msgcat_get(isc_msgcat, ISC_MSGSET_UTIL, \
161 ISC_MSG_RWLOCK, "RWLOCK"), \
162 (lp), (t), __FILE__, __LINE__)); \
163 RUNTIME_CHECK(isc_rwlock_lock((lp), (t)) == ISC_R_SUCCESS); \
164 ISC_UTIL_TRACE(fprintf(stderr, "%s %p, %d %s %d\n", \
165 isc_msgcat_get(isc_msgcat, ISC_MSGSET_UTIL, \
166 ISC_MSG_RWLOCKED, "RWLOCKED"), \
167 (lp), (t), __FILE__, __LINE__)); \
169 #define RWUNLOCK(lp, t) do { \
170 ISC_UTIL_TRACE(fprintf(stderr, "%s %p, %d %s %d\n", \
171 isc_msgcat_get(isc_msgcat, ISC_MSGSET_UTIL, \
172 ISC_MSG_RWUNLOCK, "RWUNLOCK"), \
173 (lp), (t), __FILE__, __LINE__)); \
174 RUNTIME_CHECK(isc_rwlock_unlock((lp), (t)) == ISC_R_SUCCESS); \
177 #define DESTROYMUTEXBLOCK(bp, n) \
178 RUNTIME_CHECK(isc_mutexblock_destroy((bp), (n)) == ISC_R_SUCCESS)
183 #include <isc/list.h> /* Contractual promise. */
185 #define LIST(type) ISC_LIST(type)
186 #define INIT_LIST(type) ISC_LIST_INIT(type)
187 #define LINK(type) ISC_LINK(type)
188 #define INIT_LINK(elt, link) ISC_LINK_INIT(elt, link)
189 #define HEAD(list) ISC_LIST_HEAD(list)
190 #define TAIL(list) ISC_LIST_TAIL(list)
191 #define EMPTY(list) ISC_LIST_EMPTY(list)
192 #define PREV(elt, link) ISC_LIST_PREV(elt, link)
193 #define NEXT(elt, link) ISC_LIST_NEXT(elt, link)
194 #define APPEND(list, elt, link) ISC_LIST_APPEND(list, elt, link)
195 #define PREPEND(list, elt, link) ISC_LIST_PREPEND(list, elt, link)
196 #define UNLINK(list, elt, link) ISC_LIST_UNLINK(list, elt, link)
197 #define ENQUEUE(list, elt, link) ISC_LIST_APPEND(list, elt, link)
198 #define DEQUEUE(list, elt, link) ISC_LIST_UNLINK(list, elt, link)
199 #define INSERTBEFORE(li, b, e, ln) ISC_LIST_INSERTBEFORE(li, b, e, ln)
200 #define INSERTAFTER(li, a, e, ln) ISC_LIST_INSERTAFTER(li, a, e, ln)
201 #define APPENDLIST(list1, list2, link) ISC_LIST_APPENDLIST(list1, list2, link)
206 #include <isc/assertions.h> /* Contractual promise. */
208 #define REQUIRE(e) ISC_REQUIRE(e)
209 #define ENSURE(e) ISC_ENSURE(e)
210 #define INSIST(e) ISC_INSIST(e)
211 #define INVARIANT(e) ISC_INVARIANT(e)
216 #include <isc/error.h> /* Contractual promise. */
218 #define UNEXPECTED_ERROR isc_error_unexpected
219 #define FATAL_ERROR isc_error_fatal
220 #define RUNTIME_CHECK(cond) ISC_ERROR_RUNTIMECHECK(cond)
225 #define TIME_NOW(tp) RUNTIME_CHECK(isc_time_now((tp)) == ISC_R_SUCCESS)
227 #endif /* ISC_UTIL_H */