/contrib/ntp/include/isc/util.h

https://bitbucket.org/freebsd/freebsd-head/ · C++ Header · 225 lines · 123 code · 26 blank · 76 comment · 18 complexity · 9e13d4bf4c66b8a830097a6143dfd50f MD5 · raw file

  1. /*
  2. * Copyright (C) 1998-2001 Internet Software Consortium.
  3. *
  4. * Permission to use, copy, modify, and 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 INTERNET SOFTWARE CONSORTIUM
  9. * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  10. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  11. * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  13. * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  14. * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  15. * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /* $Id: util.h,v 1.23 2001/11/30 01:59:38 gson Exp $ */
  18. #ifndef ISC_UTIL_H
  19. #define ISC_UTIL_H 1
  20. /*
  21. * NOTE:
  22. *
  23. * This file is not to be included from any <isc/???.h> (or other) library
  24. * files.
  25. *
  26. * Including this file puts several macros in your name space that are
  27. * not protected (as all the other ISC functions/macros do) by prepending
  28. * ISC_ or isc_ to the name.
  29. */
  30. /***
  31. *** General Macros.
  32. ***/
  33. /*
  34. * Use this to hide unused function arguments.
  35. *
  36. * int
  37. * foo(char *bar)
  38. * {
  39. * UNUSED(bar);
  40. * }
  41. */
  42. #define UNUSED(x) (void)(x)
  43. #define ISC_MAX(a, b) ((a) > (b) ? (a) : (b))
  44. #define ISC_MIN(a, b) ((a) < (b) ? (a) : (b))
  45. /*
  46. * Use this to remove the const qualifier of a variable to assign it to
  47. * a non-const variable or pass it as a non-const function argument ...
  48. * but only when you are sure it won't then be changed!
  49. * This is necessary to sometimes shut up some compilers
  50. * (as with gcc -Wcast-qual) when there is just no other good way to avoid the
  51. * situation.
  52. */
  53. #define DE_CONST(konst, var) \
  54. do { \
  55. union { const void *k; void *v; } _u; \
  56. _u.k = konst; \
  57. var = _u.v; \
  58. } while (0)
  59. /*
  60. * Use this in translation units that would otherwise be empty, to
  61. * suppress compiler warnings.
  62. */
  63. #define EMPTY_TRANSLATION_UNIT static void isc__empty(void) { isc__empty(); }
  64. /*
  65. * We use macros instead of calling the routines directly because
  66. * the capital letters make the locking stand out.
  67. *
  68. * We RUNTIME_CHECK for success since in general there's no way
  69. * for us to continue if they fail.
  70. */
  71. #ifdef ISC_UTIL_TRACEON
  72. #define ISC_UTIL_TRACE(a) a
  73. #include <stdio.h> /* Required for fprintf/stderr when tracing. */
  74. #include <isc/msgs.h> /* Required for isc_msgcat when tracing. */
  75. #else
  76. #define ISC_UTIL_TRACE(a)
  77. #endif
  78. #include <isc/result.h> /* Contractual promise. */
  79. #define LOCK(lp) do { \
  80. ISC_UTIL_TRACE(fprintf(stderr, "%s %p %s %d\n", \
  81. isc_msgcat_get(isc_msgcat, ISC_MSGSET_UTIL, \
  82. ISC_MSG_LOCKING, "LOCKING"), \
  83. (lp), __FILE__, __LINE__)); \
  84. RUNTIME_CHECK(isc_mutex_lock((lp)) == ISC_R_SUCCESS); \
  85. ISC_UTIL_TRACE(fprintf(stderr, "%s %p %s %d\n", \
  86. isc_msgcat_get(isc_msgcat, ISC_MSGSET_UTIL, \
  87. ISC_MSG_LOCKED, "LOCKED"), \
  88. (lp), __FILE__, __LINE__)); \
  89. } while (0)
  90. #define UNLOCK(lp) do { \
  91. RUNTIME_CHECK(isc_mutex_unlock((lp)) == ISC_R_SUCCESS); \
  92. ISC_UTIL_TRACE(fprintf(stderr, "%s %p %s %d\n", \
  93. isc_msgcat_get(isc_msgcat, ISC_MSGSET_UTIL, \
  94. ISC_MSG_UNLOCKED, "UNLOCKED"), \
  95. (lp), __FILE__, __LINE__)); \
  96. } while (0)
  97. #define ISLOCKED(lp) (1)
  98. #define DESTROYLOCK(lp) \
  99. RUNTIME_CHECK(isc_mutex_destroy((lp)) == ISC_R_SUCCESS)
  100. #define BROADCAST(cvp) do { \
  101. ISC_UTIL_TRACE(fprintf(stderr, "%s %p %s %d\n", \
  102. isc_msgcat_get(isc_msgcat, ISC_MSGSET_UTIL, \
  103. ISC_MSG_BROADCAST, "BROADCAST"),\
  104. (cvp), __FILE__, __LINE__)); \
  105. RUNTIME_CHECK(isc_condition_broadcast((cvp)) == ISC_R_SUCCESS); \
  106. } while (0)
  107. #define SIGNAL(cvp) do { \
  108. ISC_UTIL_TRACE(fprintf(stderr, "%s %p %s %d\n", \
  109. isc_msgcat_get(isc_msgcat, ISC_MSGSET_UTIL, \
  110. ISC_MSG_SIGNAL, "SIGNAL"), \
  111. (cvp), __FILE__, __LINE__)); \
  112. RUNTIME_CHECK(isc_condition_signal((cvp)) == ISC_R_SUCCESS); \
  113. } while (0)
  114. #define WAIT(cvp, lp) do { \
  115. ISC_UTIL_TRACE(fprintf(stderr, "%s %p %s %p %s %d\n", \
  116. isc_msgcat_get(isc_msgcat, ISC_MSGSET_UTIL, \
  117. ISC_MSG_UTILWAIT, "WAIT"), \
  118. (cvp), \
  119. isc_msgcat_get(isc_msgcat, ISC_MSGSET_UTIL, \
  120. ISC_MSG_LOCK, "LOCK"), \
  121. (lp), __FILE__, __LINE__)); \
  122. RUNTIME_CHECK(isc_condition_wait((cvp), (lp)) == ISC_R_SUCCESS); \
  123. ISC_UTIL_TRACE(fprintf(stderr, "%s %p %s %p %s %d\n", \
  124. isc_msgcat_get(isc_msgcat, ISC_MSGSET_UTIL, \
  125. ISC_MSG_WAITED, "WAITED"), \
  126. (cvp), \
  127. isc_msgcat_get(isc_msgcat, ISC_MSGSET_UTIL, \
  128. ISC_MSG_LOCKED, "LOCKED"), \
  129. (lp), __FILE__, __LINE__)); \
  130. } while (0)
  131. /*
  132. * isc_condition_waituntil can return ISC_R_TIMEDOUT, so we
  133. * don't RUNTIME_CHECK the result.
  134. *
  135. * XXX Also, can't really debug this then...
  136. */
  137. #define WAITUNTIL(cvp, lp, tp) \
  138. isc_condition_waituntil((cvp), (lp), (tp))
  139. #define RWLOCK(lp, t) do { \
  140. ISC_UTIL_TRACE(fprintf(stderr, "%s %p, %d %s %d\n", \
  141. isc_msgcat_get(isc_msgcat, ISC_MSGSET_UTIL, \
  142. ISC_MSG_RWLOCK, "RWLOCK"), \
  143. (lp), (t), __FILE__, __LINE__)); \
  144. RUNTIME_CHECK(isc_rwlock_lock((lp), (t)) == ISC_R_SUCCESS); \
  145. ISC_UTIL_TRACE(fprintf(stderr, "%s %p, %d %s %d\n", \
  146. isc_msgcat_get(isc_msgcat, ISC_MSGSET_UTIL, \
  147. ISC_MSG_RWLOCKED, "RWLOCKED"), \
  148. (lp), (t), __FILE__, __LINE__)); \
  149. } while (0)
  150. #define RWUNLOCK(lp, t) do { \
  151. ISC_UTIL_TRACE(fprintf(stderr, "%s %p, %d %s %d\n", \
  152. isc_msgcat_get(isc_msgcat, ISC_MSGSET_UTIL, \
  153. ISC_MSG_RWUNLOCK, "RWUNLOCK"), \
  154. (lp), (t), __FILE__, __LINE__)); \
  155. RUNTIME_CHECK(isc_rwlock_unlock((lp), (t)) == ISC_R_SUCCESS); \
  156. } while (0)
  157. #define DESTROYMUTEXBLOCK(bp, n) \
  158. RUNTIME_CHECK(isc_mutexblock_destroy((bp), (n)) == ISC_R_SUCCESS)
  159. /*
  160. * List Macros.
  161. */
  162. #include <isc/list.h> /* Contractual promise. */
  163. #define LIST(type) ISC_LIST(type)
  164. #define INIT_LIST(type) ISC_LIST_INIT(type)
  165. #define LINK(type) ISC_LINK(type)
  166. #define INIT_LINK(elt, link) ISC_LINK_INIT(elt, link)
  167. #define HEAD(list) ISC_LIST_HEAD(list)
  168. #define TAIL(list) ISC_LIST_TAIL(list)
  169. #define EMPTY(list) ISC_LIST_EMPTY(list)
  170. #define PREV(elt, link) ISC_LIST_PREV(elt, link)
  171. #define NEXT(elt, link) ISC_LIST_NEXT(elt, link)
  172. #define APPEND(list, elt, link) ISC_LIST_APPEND(list, elt, link)
  173. #define PREPEND(list, elt, link) ISC_LIST_PREPEND(list, elt, link)
  174. #define UNLINK(list, elt, link) ISC_LIST_UNLINK(list, elt, link)
  175. #define ENQUEUE(list, elt, link) ISC_LIST_APPEND(list, elt, link)
  176. #define DEQUEUE(list, elt, link) ISC_LIST_UNLINK(list, elt, link)
  177. #define INSERTBEFORE(li, b, e, ln) ISC_LIST_INSERTBEFORE(li, b, e, ln)
  178. #define INSERTAFTER(li, a, e, ln) ISC_LIST_INSERTAFTER(li, a, e, ln)
  179. #define APPENDLIST(list1, list2, link) ISC_LIST_APPENDLIST(list1, list2, link)
  180. /*
  181. * Assertions
  182. */
  183. #include <isc/assertions.h> /* Contractual promise. */
  184. #define REQUIRE(e) ISC_REQUIRE(e)
  185. #define ENSURE(e) ISC_ENSURE(e)
  186. #define INSIST(e) ISC_INSIST(e)
  187. #define INVARIANT(e) ISC_INVARIANT(e)
  188. /*
  189. * Errors
  190. */
  191. #include <isc/error.h> /* Contractual promise. */
  192. #define UNEXPECTED_ERROR isc_error_unexpected
  193. #define FATAL_ERROR isc_error_fatal
  194. #define RUNTIME_CHECK(cond) ISC_ERROR_RUNTIMECHECK(cond)
  195. /*
  196. * Time
  197. */
  198. #define TIME_NOW(tp) RUNTIME_CHECK(isc_time_now((tp)) == ISC_R_SUCCESS)
  199. #endif /* ISC_UTIL_H */