source: trunk/include/k/kHlpAssert.h@ 55

Last change on this file since 55 was 46, checked in by bird, 13 years ago

Fix for solaris 11 / gcc 4.5.2 which is using the solaris as instead of gnu as and thus has trouble understanding int3.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 8.9 KB
Line 
1/* $Id: kHlpAssert.h 46 2012-03-17 01:39:33Z bird $ */
2/** @file
3 * kHlpAssert - Assertion Macros.
4 */
5
6/*
7 * Copyright (c) 2006-2007 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net>
8 *
9 * Permission is hereby granted, free of charge, to any person
10 * obtaining a copy of this software and associated documentation
11 * files (the "Software"), to deal in the Software without
12 * restriction, including without limitation the rights to use,
13 * copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following
16 * conditions:
17 *
18 * The above copyright notice and this permission notice shall be
19 * included in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
29 */
30
31#ifndef ___kHlpAssert_h___
32#define ___kHlpAssert_h___
33
34#include <k/kHlpDefs.h>
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
40/** @defgroup grp_kHlpAssert - Assertion Macros
41 * @addtogroup grp_kHlp
42 * @{ */
43
44/** @def K_STRICT
45 * Assertions are enabled when K_STRICT is \#defined. */
46
47/** @def kHlpAssertBreakpoint
48 * Emits a breakpoint instruction or somehow triggers a debugger breakpoint.
49 */
50#ifdef _MSC_VER
51# define kHlpAssertBreakpoint() do { __debugbreak(); } while (0)
52#elif defined(__GNUC__) && K_OS == K_OS_SOLARIS && (K_ARCH == K_ARCH_AMD64 || K_ARCH == K_ARCH_X86_32)
53# define kHlpAssertBreakpoint() do { __asm__ __volatile__ ("int $3"); } while (0)
54#elif defined(__GNUC__) && (K_ARCH == K_ARCH_AMD64 || K_ARCH == K_ARCH_X86_32 || K_ARCH == K_ARCH_X86_16)
55# define kHlpAssertBreakpoint() do { __asm__ __volatile__ ("int3"); } while (0)
56#else
57# error "Port Me"
58#endif
59
60#ifdef K_STRICT
61
62# define kHlpAssert(expr) \
63 do { \
64 if (!(expr)) \
65 { \
66 kHlpAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
67 kHlpAssertBreakpoint(); \
68 } \
69 } while (0)
70
71# define kHlpAssertStmt(expr, stmt) \
72 do { \
73 if (!(expr)) \
74 { \
75 kHlpAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
76 kHlpAssertBreakpoint(); \
77 stmt; \
78 } \
79 } while (0)
80
81# define kHlpAssertReturn(expr, rcRet) \
82 do { \
83 if (!(expr)) \
84 { \
85 kHlpAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
86 kHlpAssertBreakpoint(); \
87 return (rcRet); \
88 } \
89 } while (0)
90
91# define kHlpAssertStmtReturn(expr, stmt, rcRet) \
92 do { \
93 if (!(expr)) \
94 { \
95 kHlpAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
96 kHlpAssertBreakpoint(); \
97 stmt; \
98 return (rcRet); \
99 } \
100 } while (0)
101
102# define kHlpAssertReturnVoid(expr) \
103 do { \
104 if (!(expr)) \
105 { \
106 kHlpAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
107 kHlpAssertBreakpoint(); \
108 return; \
109 } \
110 } while (0)
111
112# define kHlpAssertStmtReturnVoid(expr, stmt) \
113 do { \
114 if (!(expr)) \
115 { \
116 kHlpAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
117 kHlpAssertBreakpoint(); \
118 stmt; \
119 return; \
120 } \
121 } while (0)
122
123# define kHlpAssertMsg(expr, msg) \
124 do { \
125 if (!(expr)) \
126 { \
127 kHlpAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
128 kHlpAssertMsg2 msg; \
129 kHlpAssertBreakpoint(); \
130 } \
131 } while (0)
132
133# define kHlpAssertMsgStmt(expr, msg, stmt) \
134 do { \
135 if (!(expr)) \
136 { \
137 kHlpAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
138 kHlpAssertMsg2 msg; \
139 kHlpAssertBreakpoint(); \
140 stmt; \
141 } \
142 } while (0)
143
144# define kHlpAssertMsgReturn(expr, msg, rcRet) \
145 do { \
146 if (!(expr)) \
147 { \
148 kHlpAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
149 kHlpAssertMsg2 msg; \
150 kHlpAssertBreakpoint(); \
151 return (rcRet); \
152 } \
153 } while (0)
154
155# define kHlpAssertMsgStmtReturn(expr, msg, stmt, rcRet) \
156 do { \
157 if (!(expr)) \
158 { \
159 kHlpAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
160 kHlpAssertMsg2 msg; \
161 kHlpAssertBreakpoint(); \
162 stmt; \
163 return (rcRet); \
164 } \
165 } while (0)
166
167# define kHlpAssertMsgReturnVoid(expr, msg) \
168 do { \
169 if (!(expr)) \
170 { \
171 kHlpAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
172 kHlpAssertMsg2 msg; \
173 kHlpAssertBreakpoint(); \
174 return; \
175 } \
176 } while (0)
177
178# define kHlpAssertMsgStmtReturnVoid(expr, msg, stmt) \
179 do { \
180 if (!(expr)) \
181 { \
182 kHlpAssertMsg1(#expr, __FILE__, __LINE__, __FUNCTION__); \
183 kHlpAssertMsg2 msg; \
184 kHlpAssertBreakpoint(); \
185 stmt; \
186 return; \
187 } \
188 } while (0)
189
190#else /* !K_STRICT */
191# define kHlpAssert(expr) do { } while (0)
192# define kHlpAssertStmt(expr, stmt) do { if (!(expr)) { stmt; } } while (0)
193# define kHlpAssertReturn(expr, rcRet) do { if (!(expr)) return (rcRet); } while (0)
194# define kHlpAssertStmtReturn(expr, stmt, rcRet) do { if (!(expr)) { stmt; return (rcRet); } } while (0)
195# define kHlpAssertReturnVoid(expr) do { if (!(expr)) return; } while (0)
196# define kHlpAssertStmtReturnVoid(expr, stmt) do { if (!(expr)) { stmt; return; } } while (0)
197# define kHlpAssertMsg(expr, msg) do { } while (0)
198# define kHlpAssertMsgStmt(expr, msg, stmt) do { if (!(expr)) { stmt; } } while (0)
199# define kHlpAssertMsgReturn(expr, msg, rcRet) do { if (!(expr)) return (rcRet); } while (0)
200# define kHlpAssertMsgStmtReturn(expr, msg, stmt, rcRet) do { if (!(expr)) { stmt; return (rcRet); } } while (0)
201# define kHlpAssertMsgReturnVoid(expr, msg) do { if (!(expr)) return; } while (0)
202# define kHlpAssertMsgStmtReturnVoid(expr, msg, stmt) do { if (!(expr)) { stmt; return; } } while (0)
203#endif /* !K_STRICT */
204
205#define kHlpAssertPtr(ptr) kHlpAssertMsg(K_VALID_PTR(ptr), ("%s = %p\n", #ptr, (ptr)))
206#define kHlpAssertPtrReturn(ptr, rcRet) kHlpAssertMsgReturn(K_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)), (rcRet))
207#define kHlpAssertPtrReturn(ptr, rcRet) kHlpAssertMsgReturn(K_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)), (rcRet))
208#define kHlpAssertPtrReturnVoid(ptr) kHlpAssertMsgReturnVoid(K_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)))
209#define kHlpAssertPtrNull(ptr) kHlpAssertMsg(!(ptr) || K_VALID_PTR(ptr), ("%s = %p\n", #ptr, (ptr)))
210#define kHlpAssertPtrNullReturn(ptr, rcRet) kHlpAssertMsgReturn(!(ptr) || K_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)), (rcRet))
211#define kHlpAssertPtrNullReturnVoid(ptr) kHlpAssertMsgReturnVoid(!(ptr) || K_VALID_PTR(ptr), ("%s = %p -> %d\n", #ptr, (ptr), (rcRet)))
212#define kHlpAssertRC(rc) kHlpAssertMsg((rc) == 0, ("%s = %d\n", #rc, (rc)))
213#define kHlpAssertRCReturn(rc, rcRet) kHlpAssertMsgReturn((rc) == 0, ("%s = %d -> %d\n", #rc, (rc), (rcRet)), (rcRet))
214#define kHlpAssertRCReturnVoid(rc) kHlpAssertMsgReturnVoid((rc) == 0, ("%s = %d -> %d\n", #rc, (rc), (rcRet)))
215#define kHlpAssertFailed() kHlpAssert(0)
216#define kHlpAssertFailedReturn(rcRet) kHlpAssertReturn(0, (rcRet))
217#define kHlpAssertFailedReturnVoid() kHlpAssertReturnVoid(0)
218#define kHlpAssertMsgFailed(msg) kHlpAssertMsg(0, msg)
219#define kHlpAssertMsgFailedReturn(msg, rcRet) kHlpAssertMsgReturn(0, msg, (rcRet))
220#define kHlpAssertMsgFailedReturnVoid(msg) kHlpAssertMsgReturnVoid(0, msg))
221
222/**
223 * Helper function that displays the first part of the assertion message.
224 *
225 * @param pszExpr The expression.
226 * @param pszFile The file name.
227 * @param iLine The line number is the file.
228 * @param pszFunction The function name.
229 * @internal
230 */
231KHLP_DECL(void) kHlpAssertMsg1(const char *pszExpr, const char *pszFile, unsigned iLine, const char *pszFunction);
232
233/**
234 * Helper function that displays custom assert message.
235 *
236 * @param pszFormat Format string that get passed to vprintf.
237 * @param ... Format arguments.
238 * @internal
239 */
240KHLP_DECL(void) kHlpAssertMsg2(const char *pszFormat, ...);
241
242
243/** @} */
244
245#ifdef __cplusplus
246}
247#endif
248
249#endif
Note: See TracBrowser for help on using the repository browser.