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