bind - Removed version tag from contrib directory and updated README.DRAGONFLY.
[dragonfly.git] / contrib / bind / lib / isc / include / isc / util.h
1 /*
2  * Copyright (C) 2004-2007  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1998-2001  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and/or 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.
8  *
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.
16  */
17
18 /* $Id: util.h,v 1.30 2007/06/19 23:47:18 tbox Exp $ */
19
20 #ifndef ISC_UTIL_H
21 #define ISC_UTIL_H 1
22
23 /*! \file isc/util.h
24  * NOTE:
25  *
26  * This file is not to be included from any <isc/???.h> (or other) library
27  * files.
28  *
29  * \brief
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.
33  */
34
35 /***
36  *** General Macros.
37  ***/
38
39 /*%
40  * Use this to hide unused function arguments.
41  * \code
42  * int
43  * foo(char *bar)
44  * {
45  *      UNUSED(bar);
46  * }
47  * \endcode
48  */
49 #define UNUSED(x)      (void)(x)
50
51 #define ISC_MAX(a, b)  ((a) > (b) ? (a) : (b))
52 #define ISC_MIN(a, b)  ((a) < (b) ? (a) : (b))
53
54 /*%
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
60  * situation.
61  */
62 #define DE_CONST(konst, var) \
63         do { \
64                 union { const void *k; void *v; } _u; \
65                 _u.k = konst; \
66                 var = _u.v; \
67         } while (0)
68
69 /*%
70  * Use this in translation units that would otherwise be empty, to
71  * suppress compiler warnings.
72  */
73 #define EMPTY_TRANSLATION_UNIT static void isc__empty(void) { isc__empty(); }
74
75 /*%
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.
80  */
81
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. */
86 #else
87 #define ISC_UTIL_TRACE(a)
88 #endif
89
90 #include <isc/result.h>         /* Contractual promise. */
91
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__)); \
102         } while (0)
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__)); \
109         } while (0)
110 #define ISLOCKED(lp) (1)
111 #define DESTROYLOCK(lp) \
112         RUNTIME_CHECK(isc_mutex_destroy((lp)) == ISC_R_SUCCESS)
113
114
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); \
121         } while (0)
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); \
128         } while (0)
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"), \
133                                (cvp), \
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"), \
141                                (cvp), \
142                                isc_msgcat_get(isc_msgcat, ISC_MSGSET_UTIL, \
143                                               ISC_MSG_LOCKED, "LOCKED"), \
144                                (lp), __FILE__, __LINE__)); \
145         } while (0)
146
147 /*
148  * isc_condition_waituntil can return ISC_R_TIMEDOUT, so we
149  * don't RUNTIME_CHECK the result.
150  *
151  *  XXX Also, can't really debug this then...
152  */
153
154 #define WAITUNTIL(cvp, lp, tp) \
155         isc_condition_waituntil((cvp), (lp), (tp))
156
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__)); \
167         } while (0)
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); \
174         } while (0)
175
176 #define DESTROYMUTEXBLOCK(bp, n) \
177         RUNTIME_CHECK(isc_mutexblock_destroy((bp), (n)) == ISC_R_SUCCESS)
178
179 /*
180  * List Macros.
181  */
182 #include <isc/list.h>           /* Contractual promise. */
183
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)
201
202 /*
203  * Assertions
204  */
205 #include <isc/assertions.h>     /* Contractual promise. */
206
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)
215
216 /*
217  * Errors
218  */
219 #include <isc/error.h>          /* Contractual promise. */
220
221 /*% Unexpected Error */
222 #define UNEXPECTED_ERROR                isc_error_unexpected
223 /*% Fatal Error */
224 #define FATAL_ERROR                     isc_error_fatal
225 /*% Runtime Check */
226 #define RUNTIME_CHECK(cond)             ISC_ERROR_RUNTIMECHECK(cond)
227
228 /*%
229  * Time
230  */
231 #define TIME_NOW(tp)    RUNTIME_CHECK(isc_time_now((tp)) == ISC_R_SUCCESS)
232
233 #endif /* ISC_UTIL_H */