2 * Copyright (C) 2004, 2005 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1998-2001 Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: util.h,v 1.24.18.2 2005/04/29 00:17:04 marka Exp $ */
26 * This file is not to be included from any <isc/???.h> (or other) library
30 * Including this file puts several macros in your name space that are
31 * not protected (as all the other ISC functions/macros do) by prepending
32 * ISC_ or isc_ to the name.
40 * 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.
78 * We RUNTIME_CHECK for success since in general there's no way
79 * for us to continue if they fail.
82 #ifdef ISC_UTIL_TRACEON
83 #define ISC_UTIL_TRACE(a) a
84 #include <stdio.h> /* Required for fprintf/stderr when tracing. */
85 #include <isc/msgs.h> /* Required for isc_msgcat when tracing. */
87 #define ISC_UTIL_TRACE(a)
90 #include <isc/result.h> /* Contractual promise. */
92 #define LOCK(lp) do { \
93 ISC_UTIL_TRACE(fprintf(stderr, "%s %p %s %d\n", \
94 isc_msgcat_get(isc_msgcat, ISC_MSGSET_UTIL, \
95 ISC_MSG_LOCKING, "LOCKING"), \
96 (lp), __FILE__, __LINE__)); \
97 RUNTIME_CHECK(isc_mutex_lock((lp)) == ISC_R_SUCCESS); \
98 ISC_UTIL_TRACE(fprintf(stderr, "%s %p %s %d\n", \
99 isc_msgcat_get(isc_msgcat, ISC_MSGSET_UTIL, \
100 ISC_MSG_LOCKED, "LOCKED"), \
101 (lp), __FILE__, __LINE__)); \
103 #define UNLOCK(lp) do { \
104 RUNTIME_CHECK(isc_mutex_unlock((lp)) == ISC_R_SUCCESS); \
105 ISC_UTIL_TRACE(fprintf(stderr, "%s %p %s %d\n", \
106 isc_msgcat_get(isc_msgcat, ISC_MSGSET_UTIL, \
107 ISC_MSG_UNLOCKED, "UNLOCKED"), \
108 (lp), __FILE__, __LINE__)); \
110 #define ISLOCKED(lp) (1)
111 #define DESTROYLOCK(lp) \
112 RUNTIME_CHECK(isc_mutex_destroy((lp)) == ISC_R_SUCCESS)
115 #define BROADCAST(cvp) do { \
116 ISC_UTIL_TRACE(fprintf(stderr, "%s %p %s %d\n", \
117 isc_msgcat_get(isc_msgcat, ISC_MSGSET_UTIL, \
118 ISC_MSG_BROADCAST, "BROADCAST"),\
119 (cvp), __FILE__, __LINE__)); \
120 RUNTIME_CHECK(isc_condition_broadcast((cvp)) == ISC_R_SUCCESS); \
122 #define SIGNAL(cvp) do { \
123 ISC_UTIL_TRACE(fprintf(stderr, "%s %p %s %d\n", \
124 isc_msgcat_get(isc_msgcat, ISC_MSGSET_UTIL, \
125 ISC_MSG_SIGNAL, "SIGNAL"), \
126 (cvp), __FILE__, __LINE__)); \
127 RUNTIME_CHECK(isc_condition_signal((cvp)) == ISC_R_SUCCESS); \
129 #define WAIT(cvp, lp) do { \
130 ISC_UTIL_TRACE(fprintf(stderr, "%s %p %s %p %s %d\n", \
131 isc_msgcat_get(isc_msgcat, ISC_MSGSET_UTIL, \
132 ISC_MSG_UTILWAIT, "WAIT"), \
134 isc_msgcat_get(isc_msgcat, ISC_MSGSET_UTIL, \
135 ISC_MSG_LOCK, "LOCK"), \
136 (lp), __FILE__, __LINE__)); \
137 RUNTIME_CHECK(isc_condition_wait((cvp), (lp)) == ISC_R_SUCCESS); \
138 ISC_UTIL_TRACE(fprintf(stderr, "%s %p %s %p %s %d\n", \
139 isc_msgcat_get(isc_msgcat, ISC_MSGSET_UTIL, \
140 ISC_MSG_WAITED, "WAITED"), \
142 isc_msgcat_get(isc_msgcat, ISC_MSGSET_UTIL, \
143 ISC_MSG_LOCKED, "LOCKED"), \
144 (lp), __FILE__, __LINE__)); \
148 * isc_condition_waituntil can return ISC_R_TIMEDOUT, so we
149 * don't RUNTIME_CHECK the result.
151 * XXX Also, can't really debug this then...
154 #define WAITUNTIL(cvp, lp, tp) \
155 isc_condition_waituntil((cvp), (lp), (tp))
157 #define RWLOCK(lp, t) do { \
158 ISC_UTIL_TRACE(fprintf(stderr, "%s %p, %d %s %d\n", \
159 isc_msgcat_get(isc_msgcat, ISC_MSGSET_UTIL, \
160 ISC_MSG_RWLOCK, "RWLOCK"), \
161 (lp), (t), __FILE__, __LINE__)); \
162 RUNTIME_CHECK(isc_rwlock_lock((lp), (t)) == ISC_R_SUCCESS); \
163 ISC_UTIL_TRACE(fprintf(stderr, "%s %p, %d %s %d\n", \
164 isc_msgcat_get(isc_msgcat, ISC_MSGSET_UTIL, \
165 ISC_MSG_RWLOCKED, "RWLOCKED"), \
166 (lp), (t), __FILE__, __LINE__)); \
168 #define RWUNLOCK(lp, t) do { \
169 ISC_UTIL_TRACE(fprintf(stderr, "%s %p, %d %s %d\n", \
170 isc_msgcat_get(isc_msgcat, ISC_MSGSET_UTIL, \
171 ISC_MSG_RWUNLOCK, "RWUNLOCK"), \
172 (lp), (t), __FILE__, __LINE__)); \
173 RUNTIME_CHECK(isc_rwlock_unlock((lp), (t)) == ISC_R_SUCCESS); \
176 #define DESTROYMUTEXBLOCK(bp, n) \
177 RUNTIME_CHECK(isc_mutexblock_destroy((bp), (n)) == ISC_R_SUCCESS)
182 #include <isc/list.h> /* Contractual promise. */
184 #define LIST(type) ISC_LIST(type)
185 #define INIT_LIST(type) ISC_LIST_INIT(type)
186 #define LINK(type) ISC_LINK(type)
187 #define INIT_LINK(elt, link) ISC_LINK_INIT(elt, link)
188 #define HEAD(list) ISC_LIST_HEAD(list)
189 #define TAIL(list) ISC_LIST_TAIL(list)
190 #define EMPTY(list) ISC_LIST_EMPTY(list)
191 #define PREV(elt, link) ISC_LIST_PREV(elt, link)
192 #define NEXT(elt, link) ISC_LIST_NEXT(elt, link)
193 #define APPEND(list, elt, link) ISC_LIST_APPEND(list, elt, link)
194 #define PREPEND(list, elt, link) ISC_LIST_PREPEND(list, elt, link)
195 #define UNLINK(list, elt, link) ISC_LIST_UNLINK(list, elt, link)
196 #define ENQUEUE(list, elt, link) ISC_LIST_APPEND(list, elt, link)
197 #define DEQUEUE(list, elt, link) ISC_LIST_UNLINK(list, elt, link)
198 #define INSERTBEFORE(li, b, e, ln) ISC_LIST_INSERTBEFORE(li, b, e, ln)
199 #define INSERTAFTER(li, a, e, ln) ISC_LIST_INSERTAFTER(li, a, e, ln)
200 #define APPENDLIST(list1, list2, link) ISC_LIST_APPENDLIST(list1, list2, link)
205 #include <isc/assertions.h> /* Contractual promise. */
207 /*% Require Assertion */
208 #define REQUIRE(e) ISC_REQUIRE(e)
209 /*% Ensure Assertion */
210 #define ENSURE(e) ISC_ENSURE(e)
211 /*% Insist Assertion */
212 #define INSIST(e) ISC_INSIST(e)
213 /*% Invariant Assertion */
214 #define INVARIANT(e) ISC_INVARIANT(e)
219 #include <isc/error.h> /* Contractual promise. */
221 /*% Unexpected Error */
222 #define UNEXPECTED_ERROR isc_error_unexpected
224 #define FATAL_ERROR isc_error_fatal
226 #define RUNTIME_CHECK(cond) ISC_ERROR_RUNTIMECHECK(cond)
231 #define TIME_NOW(tp) RUNTIME_CHECK(isc_time_now((tp)) == ISC_R_SUCCESS)
233 #endif /* ISC_UTIL_H */