1 /*
2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
7  *
8  * See the COPYRIGHT file distributed with this work for additional
9  * information regarding copyright ownership.
10  */
11 
12 /*! \file isc/assertions.h
13  */
14 
15 #pragma once
16 
17 #include <isc/attributes.h>
18 #include <isc/lang.h>
19 #include <isc/likely.h>
20 
21 ISC_LANG_BEGINDECLS
22 
23 /*% isc assertion type */
24 typedef enum {
25 	isc_assertiontype_require,
26 	isc_assertiontype_ensure,
27 	isc_assertiontype_insist,
28 	isc_assertiontype_invariant
29 } isc_assertiontype_t;
30 
31 typedef void (*isc_assertioncallback_t)(const char *, int, isc_assertiontype_t,
32 					const char *);
33 
34 /* coverity[+kill] */
35 ISC_NORETURN
36 void
37 isc_assertion_failed(const char *, int, isc_assertiontype_t, const char *);
38 
39 void isc_assertion_setcallback(isc_assertioncallback_t);
40 
41 const char *
42 isc_assertion_typetotext(isc_assertiontype_t type);
43 
44 #define ISC_REQUIRE(cond)                                                  \
45 	((void)(ISC_LIKELY(cond) ||                                        \
46 		((isc_assertion_failed)(__FILE__, __LINE__,                \
47 					isc_assertiontype_require, #cond), \
48 		 0)))
49 
50 #define ISC_ENSURE(cond)                                                  \
51 	((void)(ISC_LIKELY(cond) ||                                       \
52 		((isc_assertion_failed)(__FILE__, __LINE__,               \
53 					isc_assertiontype_ensure, #cond), \
54 		 0)))
55 
56 #define ISC_INSIST(cond)                                                  \
57 	((void)(ISC_LIKELY(cond) ||                                       \
58 		((isc_assertion_failed)(__FILE__, __LINE__,               \
59 					isc_assertiontype_insist, #cond), \
60 		 0)))
61 
62 #define ISC_INVARIANT(cond)                                                  \
63 	((void)(ISC_LIKELY(cond) ||                                          \
64 		((isc_assertion_failed)(__FILE__, __LINE__,                  \
65 					isc_assertiontype_invariant, #cond), \
66 		 0)))
67 
68 ISC_LANG_ENDDECLS
69