1 |
786 |
skrzyp |
#ifndef CYGONCE_INFRA_CYG_ASS_H
|
2 |
|
|
#define CYGONCE_INFRA_CYG_ASS_H
|
3 |
|
|
|
4 |
|
|
//==========================================================================
|
5 |
|
|
//
|
6 |
|
|
// assert.h
|
7 |
|
|
//
|
8 |
|
|
// Macros and prototypes for the assert system
|
9 |
|
|
//
|
10 |
|
|
//==========================================================================
|
11 |
|
|
// ####ECOSGPLCOPYRIGHTBEGIN####
|
12 |
|
|
// -------------------------------------------
|
13 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
14 |
|
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
|
15 |
|
|
//
|
16 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
17 |
|
|
// the terms of the GNU General Public License as published by the Free
|
18 |
|
|
// Software Foundation; either version 2 or (at your option) any later
|
19 |
|
|
// version.
|
20 |
|
|
//
|
21 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT
|
22 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
23 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
24 |
|
|
// for more details.
|
25 |
|
|
//
|
26 |
|
|
// You should have received a copy of the GNU General Public License
|
27 |
|
|
// along with eCos; if not, write to the Free Software Foundation, Inc.,
|
28 |
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
29 |
|
|
//
|
30 |
|
|
// As a special exception, if other files instantiate templates or use
|
31 |
|
|
// macros or inline functions from this file, or you compile this file
|
32 |
|
|
// and link it with other works to produce a work based on this file,
|
33 |
|
|
// this file does not by itself cause the resulting work to be covered by
|
34 |
|
|
// the GNU General Public License. However the source code for this file
|
35 |
|
|
// must still be made available in accordance with section (3) of the GNU
|
36 |
|
|
// General Public License v2.
|
37 |
|
|
//
|
38 |
|
|
// This exception does not invalidate any other reasons why a work based
|
39 |
|
|
// on this file might be covered by the GNU General Public License.
|
40 |
|
|
// -------------------------------------------
|
41 |
|
|
// ####ECOSGPLCOPYRIGHTEND####
|
42 |
|
|
//==========================================================================
|
43 |
|
|
//#####DESCRIPTIONBEGIN####
|
44 |
|
|
//
|
45 |
|
|
// Author(s): nickg from an original by hmt
|
46 |
|
|
// Contributors: nickg
|
47 |
|
|
// Date: 1997-09-08
|
48 |
|
|
// Purpose: Use asserts to avoid writing duff code.
|
49 |
|
|
// Description: Runtime tests that compile to nothing in
|
50 |
|
|
// release versions of the code, to allow
|
51 |
|
|
// as-you-go testing of alternate builds.
|
52 |
|
|
// Usage: #include <cyg/infra/cyg_ass.h>
|
53 |
|
|
// ...
|
54 |
|
|
// CYG_ASSERT( pcount > 0, "Number of probes should be > 0!" );
|
55 |
|
|
//
|
56 |
|
|
// which can result, for example, in a message of the form:
|
57 |
|
|
// ASSERT FAILED: probemgr.cxx:1340, scan_probes() :
|
58 |
|
|
// number of probes should be > 0!
|
59 |
|
|
// if the boolean "pcount > 0" is false.
|
60 |
|
|
//
|
61 |
|
|
//####DESCRIPTIONEND####
|
62 |
|
|
//
|
63 |
|
|
//==========================================================================
|
64 |
|
|
|
65 |
|
|
#include <pkgconf/infra.h>
|
66 |
|
|
|
67 |
|
|
#include <cyg/infra/cyg_type.h> // for CYGBLD_ATTRIB_NORET
|
68 |
|
|
|
69 |
|
|
// -------------------------------------------------------------------------
|
70 |
|
|
// If we do not have a function name macro, define it ourselves
|
71 |
|
|
|
72 |
|
|
#ifndef CYGDBG_INFRA_DEBUG_FUNCTION_PSEUDOMACRO
|
73 |
|
|
// __PRETTY_FUNCTION__ does not work
|
74 |
|
|
# ifndef __PRETTY_FUNCTION__ // And it is not already defined
|
75 |
|
|
# define __PRETTY_FUNCTION__ NULL
|
76 |
|
|
# endif
|
77 |
|
|
#endif
|
78 |
|
|
|
79 |
|
|
// -------------------------------------------------------------------------
|
80 |
|
|
// This is executed to deal with failure - breakpoint it first!
|
81 |
|
|
// It is declared as a weak symbol to allow user code to override the
|
82 |
|
|
// definition.
|
83 |
|
|
|
84 |
|
|
externC void
|
85 |
|
|
cyg_assert_fail( const char* /* psz_func */, const char* /* psz_file */,
|
86 |
|
|
cyg_uint32 /* linenum */, const char* /* psz_msg */ ) __THROW
|
87 |
|
|
CYGBLD_ATTRIB_NORET CYGBLD_ATTRIB_WEAK;
|
88 |
|
|
|
89 |
|
|
externC void
|
90 |
|
|
cyg_assert_msg( const char *psz_func, const char *psz_file,
|
91 |
|
|
cyg_uint32 linenum, const char *psz_msg ) __THROW;
|
92 |
|
|
|
93 |
|
|
// -------------------------------------------------------------------------
|
94 |
|
|
|
95 |
|
|
#ifdef CYGDBG_USE_ASSERTS
|
96 |
|
|
|
97 |
|
|
// -------------------------------------------------------------------------
|
98 |
|
|
// We define macros and appropriate prototypes for the assert/fail
|
99 |
|
|
// system. These are:
|
100 |
|
|
// CYG_FAIL - unconditional panic
|
101 |
|
|
// CYG_ASSERT - panic if boolean expression is false
|
102 |
|
|
// CYG_ASSERTC - compact version of CYG_ASSERT
|
103 |
|
|
|
104 |
|
|
# ifdef CYGDBG_INFRA_DEBUG_ASSERT_MESSAGE
|
105 |
|
|
# define CYG_ASSERT_DOCALL( _msg_ ) \
|
106 |
|
|
CYG_MACRO_START \
|
107 |
|
|
/* Make sure we always get a pretty-printed message */ \
|
108 |
|
|
cyg_assert_msg( __PRETTY_FUNCTION__, __FILE__, __LINE__, _msg_ ); \
|
109 |
|
|
cyg_assert_fail( __PRETTY_FUNCTION__, __FILE__, __LINE__, _msg_ );\
|
110 |
|
|
CYG_MACRO_END
|
111 |
|
|
# else
|
112 |
|
|
# define CYG_ASSERT_DOCALL( _msg_ ) \
|
113 |
|
|
CYG_MACRO_START \
|
114 |
|
|
const char* _tmp1_ = _msg_; \
|
115 |
|
|
_tmp1_ = _tmp1_; \
|
116 |
|
|
cyg_assert_fail( __PRETTY_FUNCTION__, __FILE__, __LINE__, NULL ); \
|
117 |
|
|
CYG_MACRO_END
|
118 |
|
|
# endif
|
119 |
|
|
|
120 |
|
|
// unconditional failure; use like panic(), coredump() &c.
|
121 |
|
|
# define CYG_FAIL( _msg_ ) \
|
122 |
|
|
CYG_MACRO_START \
|
123 |
|
|
CYG_ASSERT_DOCALL( _msg_ ); \
|
124 |
|
|
CYG_MACRO_END
|
125 |
|
|
|
126 |
|
|
// conditioned assert; if the condition is false, fail.
|
127 |
|
|
# define CYG_ASSERT( _bool_, _msg_ ) \
|
128 |
|
|
CYG_MACRO_START \
|
129 |
|
|
if ( ! ( _bool_ ) ) \
|
130 |
|
|
CYG_ASSERT_DOCALL( _msg_ ); \
|
131 |
|
|
CYG_MACRO_END
|
132 |
|
|
|
133 |
|
|
# define CYG_ASSERTC( _bool_ ) \
|
134 |
|
|
CYG_MACRO_START \
|
135 |
|
|
if ( ! ( _bool_ ) ) \
|
136 |
|
|
CYG_ASSERT_DOCALL( #_bool_ );\
|
137 |
|
|
CYG_MACRO_END
|
138 |
|
|
|
139 |
|
|
#else // ! CYGDBG_USE_ASSERTS
|
140 |
|
|
|
141 |
|
|
// -------------------------------------------------------------------------
|
142 |
|
|
// No asserts: we define empty statements for assert & fail.
|
143 |
|
|
|
144 |
|
|
# define CYG_FAIL( _msg_ ) CYG_EMPTY_STATEMENT
|
145 |
|
|
# define CYG_ASSERT( _bool_, _msg_ ) CYG_EMPTY_STATEMENT
|
146 |
|
|
# define CYG_ASSERTC( _bool_ ) CYG_EMPTY_STATEMENT
|
147 |
|
|
|
148 |
|
|
#endif // ! CYGDBG_USE_ASSERTS
|
149 |
|
|
|
150 |
|
|
// -------------------------------------------------------------------------
|
151 |
|
|
// Pointer integrity checks.
|
152 |
|
|
// These check not only for NULL pointer, but can also check for pointers
|
153 |
|
|
// that are outside to defined memory areas of the platform or executable.
|
154 |
|
|
// We differentiate between data and function pointers, so that we can cope
|
155 |
|
|
// with different formats, and so we can check them against different memory
|
156 |
|
|
// regions.
|
157 |
|
|
|
158 |
|
|
externC cyg_bool cyg_check_data_ptr(const void *ptr);
|
159 |
|
|
externC cyg_bool cyg_check_func_ptr(void (*ptr)(void));
|
160 |
|
|
|
161 |
|
|
#ifdef CYGDBG_USE_ASSERTS
|
162 |
|
|
|
163 |
|
|
# define CYG_CHECK_DATA_PTR( _ptr_, _msg_ ) \
|
164 |
|
|
CYG_MACRO_START \
|
165 |
|
|
if( !cyg_check_data_ptr((const void *)(_ptr_))) \
|
166 |
|
|
CYG_ASSERT_DOCALL( _msg_ ); \
|
167 |
|
|
CYG_MACRO_END
|
168 |
|
|
|
169 |
|
|
# define CYG_CHECK_FUNC_PTR( _ptr_, _msg_ ) \
|
170 |
|
|
CYG_MACRO_START \
|
171 |
|
|
if( !cyg_check_func_ptr((void (*)(void))(_ptr_))) \
|
172 |
|
|
CYG_ASSERT_DOCALL( _msg_ ); \
|
173 |
|
|
CYG_MACRO_END
|
174 |
|
|
|
175 |
|
|
# define CYG_CHECK_DATA_PTRC( _ptr_ ) \
|
176 |
|
|
CYG_MACRO_START \
|
177 |
|
|
if ( !cyg_check_data_ptr((const void *)(_ptr_))) \
|
178 |
|
|
CYG_ASSERT_DOCALL("data pointer (" #_ptr_ ") is valid");\
|
179 |
|
|
CYG_MACRO_END
|
180 |
|
|
|
181 |
|
|
# define CYG_CHECK_FUNC_PTRC( _ptr_ ) \
|
182 |
|
|
CYG_MACRO_START \
|
183 |
|
|
if ( !cyg_check_func_ptr((void (*)(void))(_ptr_))) \
|
184 |
|
|
CYG_ASSERT_DOCALL("function pointer (" #_ptr_ ") is valid"); \
|
185 |
|
|
CYG_MACRO_END
|
186 |
|
|
|
187 |
|
|
#else // CYGDBG_USE_ASSERTS
|
188 |
|
|
|
189 |
|
|
# define CYG_CHECK_DATA_PTR( _ptr_, _msg_ ) CYG_EMPTY_STATEMENT
|
190 |
|
|
# define CYG_CHECK_FUNC_PTR( _ptr_, _msg_ ) CYG_EMPTY_STATEMENT
|
191 |
|
|
# define CYG_CHECK_DATA_PTRC( _ptr_ ) CYG_EMPTY_STATEMENT
|
192 |
|
|
# define CYG_CHECK_FUNC_PTRC( _ptr_ ) CYG_EMPTY_STATEMENT
|
193 |
|
|
|
194 |
|
|
#endif // CYGDBG_USE_ASSERTS
|
195 |
|
|
|
196 |
|
|
// -------------------------------------------------------------------------
|
197 |
|
|
// Unconditional definitions:
|
198 |
|
|
|
199 |
|
|
// Check an object for validity by calling its own checker.
|
200 |
|
|
// Usage:
|
201 |
|
|
// ClassThing *p = &classobject;
|
202 |
|
|
// CYG_ASSERTCLASS( p, "Object at p is broken!" );
|
203 |
|
|
|
204 |
|
|
// this enum gives some options as to how keenly to test; avoids cluttering
|
205 |
|
|
// the member function declaration if the implementor wants to do more
|
206 |
|
|
// zealous tests themselves.
|
207 |
|
|
|
208 |
|
|
enum cyg_assert_class_zeal {
|
209 |
|
|
cyg_system_test = -1,
|
210 |
|
|
cyg_none = 0,
|
211 |
|
|
cyg_trivial,
|
212 |
|
|
cyg_quick,
|
213 |
|
|
cyg_thorough,
|
214 |
|
|
cyg_extreme
|
215 |
|
|
};
|
216 |
|
|
|
217 |
|
|
// -------------------------------------------------------------------------
|
218 |
|
|
// Define macros for checking classes:
|
219 |
|
|
//
|
220 |
|
|
// CYG_ASSERT_CLASS - do proforma check on a class pointer
|
221 |
|
|
// CYG_ASSERT_CLASSO - do proforma check on a class object
|
222 |
|
|
// CYG_ASSERT_ZERO_OR_CLASS- a class pointer is NULL or valid
|
223 |
|
|
// CYG_ASSERT_THIS - "this" is valid
|
224 |
|
|
// + 3 compact variants and two aliases for backwards compatibility.
|
225 |
|
|
//
|
226 |
|
|
// All of these end up going via CYG_ASSERT(), which will be an empty
|
227 |
|
|
// statement if CYGDBG_USE_ASSERTS is disabled. There is no need to
|
228 |
|
|
// test CYGDBG_USE_ASSERTS again here.
|
229 |
|
|
//
|
230 |
|
|
// The idiom required is that a class have a member function called
|
231 |
|
|
// "bool check_this( cyg_assert_class_zeal ) const" that returns true
|
232 |
|
|
// iff the object is OK. This need not be conditionally compiled against
|
233 |
|
|
// CYGDBG_USE_ASSERTS but it can be if only this macro is used to
|
234 |
|
|
// invoke it. Alternatively it can be invoked by hand with other
|
235 |
|
|
// choices from the above enum.
|
236 |
|
|
|
237 |
|
|
// Assert the checker function of an object by pointer, or in hand.
|
238 |
|
|
|
239 |
|
|
#ifdef __cplusplus
|
240 |
|
|
|
241 |
|
|
# ifndef CYG_ASSERT_CLASS_ZEAL
|
242 |
|
|
# define CYG_ASSERT_CLASS_ZEAL (cyg_quick) // can be redefined locally
|
243 |
|
|
# endif
|
244 |
|
|
|
245 |
|
|
# define CYG_ASSERT_CLASS( _pobj_, _msg_ ) \
|
246 |
|
|
CYG_ASSERT( ((0 != (_pobj_)) && \
|
247 |
|
|
(_pobj_)->check_this( CYG_ASSERT_CLASS_ZEAL )), _msg_ )
|
248 |
|
|
|
249 |
|
|
# define CYG_ASSERTCLASS( _pobj_,_msg_) \
|
250 |
|
|
CYG_ASSERT_CLASS( (_pobj_), _msg_ )
|
251 |
|
|
|
252 |
|
|
# define CYG_ASSERT_CLASSO( _obj_, _msg_ ) \
|
253 |
|
|
CYG_ASSERT( (_obj_).check_this( CYG_ASSERT_CLASS_ZEAL ), _msg_ )
|
254 |
|
|
|
255 |
|
|
# define CYG_ASSERTCLASSO( _obj_, _msg_ ) \
|
256 |
|
|
CYG_ASSERT_CLASSO( (_obj_), _msg_ )
|
257 |
|
|
|
258 |
|
|
# define CYG_ASSERT_ZERO_OR_CLASS( _pobj_, _msg_ ) \
|
259 |
|
|
CYG_ASSERT( ((0 == (_pobj_)) || \
|
260 |
|
|
(_pobj_)->check_this( CYG_ASSERT_CLASS_ZEAL )), _msg_ )
|
261 |
|
|
|
262 |
|
|
# define CYG_ASSERT_THIS( _msg_ ) \
|
263 |
|
|
CYG_ASSERT( this->check_this( CYG_ASSERT_CLASS_ZEAL ), _msg_ )
|
264 |
|
|
|
265 |
|
|
# define CYG_ASSERT_CLASSC( _pobj_ ) \
|
266 |
|
|
CYG_ASSERT_CLASS( (_pobj_), "class pointer (" #_pobj_ ") is valid" )
|
267 |
|
|
|
268 |
|
|
# define CYG_ASSERT_CLASSOC( _obj_ ) \
|
269 |
|
|
CYG_ASSERT_CLASSO( (_obj_), "object (" #_obj_ ") is valid" )
|
270 |
|
|
|
271 |
|
|
# define CYG_ASSERT_ZERO_OR_CLASSC( _pobj_ ) \
|
272 |
|
|
CYG_ASSERT_ZERO_OR_CLASS((_pobj_), \
|
273 |
|
|
"class pointer (" #_pobj_ ") is zero or valid")
|
274 |
|
|
|
275 |
|
|
# define CYG_ASSERT_THISC( ) \
|
276 |
|
|
CYG_ASSERT_THIS( "\"this\" pointer is valid" )
|
277 |
|
|
|
278 |
|
|
#define CYGDBG_DEFINE_CHECK_THIS \
|
279 |
|
|
cyg_bool check_this( cyg_assert_class_zeal zeal ) const;
|
280 |
|
|
|
281 |
|
|
#endif // __cplusplus
|
282 |
|
|
|
283 |
|
|
// -------------------------------------------------------------------------
|
284 |
|
|
// Some alternative names for basic assertions that we can disable
|
285 |
|
|
// individually.
|
286 |
|
|
//
|
287 |
|
|
// CYG_PRECONDITION - argument checking etc
|
288 |
|
|
// CYG_POSTCONDITION - results etc
|
289 |
|
|
// CYG_LOOP_INVARIANT - for putting in loops
|
290 |
|
|
//
|
291 |
|
|
// C++ programmers have class-related variants of all of these.
|
292 |
|
|
|
293 |
|
|
#ifdef CYGDBG_INFRA_DEBUG_PRECONDITIONS
|
294 |
|
|
# define CYG_PRECONDITION( _bool_ , _msg_ ) CYG_ASSERT( _bool_, _msg_ )
|
295 |
|
|
# define CYG_PRECONDITIONC( _bool_ ) \
|
296 |
|
|
CYG_ASSERT( _bool_, "precondition " #_bool_)
|
297 |
|
|
#else
|
298 |
|
|
# define CYG_PRECONDITION( _bool_ , _msg_ ) CYG_EMPTY_STATEMENT
|
299 |
|
|
# define CYG_PRECONDITIONC( _bool_ ) CYG_EMPTY_STATEMENT
|
300 |
|
|
#endif
|
301 |
|
|
|
302 |
|
|
#ifdef CYGDBG_INFRA_DEBUG_POSTCONDITIONS
|
303 |
|
|
# define CYG_POSTCONDITION( _bool_ , _msg_ ) CYG_ASSERT( _bool_, _msg_ )
|
304 |
|
|
# define CYG_POSTCONDITIONC( _bool_ ) \
|
305 |
|
|
CYG_ASSERT( _bool_, "postcondition " #_bool_)
|
306 |
|
|
#else
|
307 |
|
|
# define CYG_POSTCONDITION( _bool_ , _msg_ ) CYG_EMPTY_STATEMENT
|
308 |
|
|
# define CYG_POSTCONDITIONC( _bool_ ) CYG_EMPTY_STATEMENT
|
309 |
|
|
#endif
|
310 |
|
|
|
311 |
|
|
#ifdef CYGDBG_INFRA_DEBUG_LOOP_INVARIANTS
|
312 |
|
|
# define CYG_LOOP_INVARIANT( _bool_ , _msg_ ) CYG_ASSERT( _bool_, _msg_ )
|
313 |
|
|
# define CYG_LOOP_INVARIANTC( _bool_ ) \
|
314 |
|
|
CYG_ASSERT( _bool_, "loop invariant " #_bool_ )
|
315 |
|
|
#else
|
316 |
|
|
# define CYG_LOOP_INVARIANT( _bool_ , _msg_ ) CYG_EMPTY_STATEMENT
|
317 |
|
|
# define CYG_LOOP_INVARIANTC( _bool_ ) CYG_EMPTY_STATEMENT
|
318 |
|
|
#endif
|
319 |
|
|
|
320 |
|
|
#ifdef __cplusplus
|
321 |
|
|
|
322 |
|
|
// All variants of _CLASS
|
323 |
|
|
# define CYG_PRECONDITION_CLASS( _pobj_, _msg_ ) \
|
324 |
|
|
CYG_PRECONDITION( ((0 != (_pobj_)) && \
|
325 |
|
|
(_pobj_)->check_this(CYG_ASSERT_CLASS_ZEAL)), _msg_)
|
326 |
|
|
|
327 |
|
|
# define CYG_PRECONDITION_CLASSC( _pobj_ ) \
|
328 |
|
|
CYG_PRECONDITION_CLASS( (_pobj_), \
|
329 |
|
|
"precondition, class pointer (" #_pobj_ ") is valid" )
|
330 |
|
|
|
331 |
|
|
# define CYG_POSTCONDITION_CLASS( _pobj_, _msg_ ) \
|
332 |
|
|
CYG_POSTCONDITION( ((0 != (_pobj_)) && \
|
333 |
|
|
(_pobj_)->check_this(CYG_ASSERT_CLASS_ZEAL)), _msg_)
|
334 |
|
|
|
335 |
|
|
# define CYG_POSTCONDITION_CLASSC( _pobj_ ) \
|
336 |
|
|
CYG_POSTCONDITION_CLASS( (_pobj_), \
|
337 |
|
|
"postcondition, class pointer (" #_pobj_ ") is valid" )
|
338 |
|
|
|
339 |
|
|
# define CYG_LOOP_INVARIANT_CLASS( _pobj_, _msg_) \
|
340 |
|
|
CYG_LOOP_INVARIANT( ((0 != (_pobj_)) && \
|
341 |
|
|
(_pobj_)->check_this(CYG_ASSERT_CLASS_ZEAL)), _msg_)
|
342 |
|
|
|
343 |
|
|
# define CYG_LOOP_INVARIANT_CLASSC( _pobj_ ) \
|
344 |
|
|
CYG_LOOP_INVARIANT_CLASS( (_pobj_), \
|
345 |
|
|
"loop invariant, class pointer (" #_pobj_ ") is valid" )
|
346 |
|
|
|
347 |
|
|
// All variants of _CLASSO
|
348 |
|
|
# define CYG_PRECONDITION_CLASSO( _obj_, _msg_ ) \
|
349 |
|
|
CYG_PRECONDITION( (_obj_).check_this(CYG_ASSERT_CLASS_ZEAL), _msg_)
|
350 |
|
|
|
351 |
|
|
# define CYG_PRECONDITION_CLASSOC( _obj_ ) \
|
352 |
|
|
CYG_PRECONDITION_CLASSO( (_obj_), \
|
353 |
|
|
"precondition, object (" #_obj_ ") is valid" )
|
354 |
|
|
|
355 |
|
|
# define CYG_POSTCONDITION_CLASSO( _obj_, _msg_ ) \
|
356 |
|
|
CYG_POSTCONDITION( (_obj_).check_this(CYG_ASSERT_CLASS_ZEAL), _msg_)
|
357 |
|
|
|
358 |
|
|
# define CYG_POSTCONDITION_CLASSOC( _obj_ ) \
|
359 |
|
|
CYG_POSTCONDITION_CLASSO( (_obj_), \
|
360 |
|
|
"postcondition, object (" #_obj_ ") is valid" )
|
361 |
|
|
|
362 |
|
|
# define CYG_LOOP_INVARIANT_CLASSO( _obj_, _msg_) \
|
363 |
|
|
CYG_LOOP_INVARIANT( (_obj_).check_this(CYG_ASSERT_CLASS_ZEAL), _msg_)
|
364 |
|
|
|
365 |
|
|
# define CYG_LOOP_INVARIANT_CLASSOC( _obj_ ) \
|
366 |
|
|
CYG_LOOP_INVARIANT_CLASSO( (_obj_), \
|
367 |
|
|
"loop invariant, object (" #_obj_ ") is valid" )
|
368 |
|
|
|
369 |
|
|
// All variants of _ZERO_OR_CLASS
|
370 |
|
|
# define CYG_PRECONDITION_ZERO_OR_CLASS( _pobj_, _msg_ ) \
|
371 |
|
|
CYG_PRECONDITION( ((0 == (_pobj_)) || \
|
372 |
|
|
(_pobj_)->check_this(CYG_ASSERT_CLASS_ZEAL)), _msg_)
|
373 |
|
|
|
374 |
|
|
# define CYG_PRECONDITION_ZERO_OR_CLASSC( _pobj_ ) \
|
375 |
|
|
CYG_PRECONDITION_ZERO_OR_CLASS( (_pobj_), \
|
376 |
|
|
"precondition, class pointer (" #_pobj_ ") is zero or valid" )
|
377 |
|
|
|
378 |
|
|
# define CYG_POSTCONDITION_ZERO_OR_CLASS( _pobj_, _msg_ ) \
|
379 |
|
|
CYG_POSTCONDITION( ((0 == (_pobj_)) || \
|
380 |
|
|
(_pobj_)->check_this(CYG_ASSERT_CLASS_ZEAL)), _msg_)
|
381 |
|
|
|
382 |
|
|
# define CYG_POSTCONDITION_ZERO_OR_CLASSC( _pobj_ ) \
|
383 |
|
|
CYG_POSTCONDITION_ZERO_OR_CLASS( (_pobj_), \
|
384 |
|
|
"postcondition, class pointer (" #_pobj_ ") is zero or valid" )
|
385 |
|
|
|
386 |
|
|
# define CYG_LOOP_INVARIANT_ZERO_OR_CLASS( _pobj_, _msg_) \
|
387 |
|
|
CYG_LOOP_INVARIANT( ((0 == (_pobj_)) || \
|
388 |
|
|
(_pobj_)->check_this(CYG_ASSERT_CLASS_ZEAL)), _msg_)
|
389 |
|
|
|
390 |
|
|
# define CYG_LOOP_INVARIANT_ZERO_OR_CLASSC( _pobj_ ) \
|
391 |
|
|
CYG_LOOP_INVARIANT_ZERO_OR_CLASS( (_pobj_), \
|
392 |
|
|
"loop invariant, class pointer (" #_pobj_ ") is zero or valid" )
|
393 |
|
|
|
394 |
|
|
// All variants of _THIS
|
395 |
|
|
# define CYG_PRECONDITION_THIS( _msg_ ) \
|
396 |
|
|
CYG_PRECONDITION( this->check_this(CYG_ASSERT_CLASS_ZEAL), _msg_)
|
397 |
|
|
|
398 |
|
|
# define CYG_PRECONDITION_THISC() \
|
399 |
|
|
CYG_PRECONDITION_THIS( "precondition, \"this\" is valid" )
|
400 |
|
|
|
401 |
|
|
# define CYG_POSTCONDITION_THIS( _msg_ ) \
|
402 |
|
|
CYG_POSTCONDITION( this->check_this(CYG_ASSERT_CLASS_ZEAL), _msg_)
|
403 |
|
|
|
404 |
|
|
# define CYG_POSTCONDITION_THISC() \
|
405 |
|
|
CYG_POSTCONDITION_THIS( "postcondition, \"this\" is valid" )
|
406 |
|
|
|
407 |
|
|
# define CYG_LOOP_INVARIANT_THIS( _msg_) \
|
408 |
|
|
CYG_LOOP_INVARIANT( this->check_this(CYG_ASSERT_CLASS_ZEAL), _msg_)
|
409 |
|
|
|
410 |
|
|
# define CYG_LOOP_INVARIANT_THISC() \
|
411 |
|
|
CYG_LOOP_INVARIANT_THIS( "loop invariant, \"this\" is valid" )
|
412 |
|
|
|
413 |
|
|
#endif // __cplusplus
|
414 |
|
|
|
415 |
|
|
// -------------------------------------------------------------------------
|
416 |
|
|
// Invariants. These are a bit more interesting. The ordinary invariants
|
417 |
|
|
// take an arbitrary boolean expression, and C++ does not provide any way
|
418 |
|
|
// of evaluating this expression automatically on entry and exit - any
|
419 |
|
|
// attempt to use local objects leads to trying to evaluate the expression
|
420 |
|
|
// when it is not in scope. This problem does not arise with objects.
|
421 |
|
|
//
|
422 |
|
|
// For C++ objects it is possible to do a bit better. A template can be
|
423 |
|
|
// used to create a local object whose constructor will validate the
|
424 |
|
|
// target object and whose destructor will validate the target object
|
425 |
|
|
// again. Unfortunately it is necessary to pass the type as well as
|
426 |
|
|
// the object: typeof() is a gcc extension, and RTTI's typeid facility
|
427 |
|
|
// would provide the derived class and not what we actually want.
|
428 |
|
|
|
429 |
|
|
#ifdef CYGDBG_INFRA_DEBUG_INVARIANTS
|
430 |
|
|
|
431 |
|
|
# define CYG_INVARIANT( _bool_, _msg_ ) \
|
432 |
|
|
CYG_MACRO_START \
|
433 |
|
|
if ( ! ( _bool_ ) ) \
|
434 |
|
|
CYG_ASSERT_DOCALL( _msg_ ); \
|
435 |
|
|
CYG_MACRO_END
|
436 |
|
|
|
437 |
|
|
# define CYG_INVARIANTC( _bool_ ) \
|
438 |
|
|
CYG_MACRO_START \
|
439 |
|
|
if ( ! ( _bool_ ) ) \
|
440 |
|
|
CYG_ASSERT_DOCALL( "invariant (" #_bool_ ")" ); \
|
441 |
|
|
CYG_MACRO_END
|
442 |
|
|
|
443 |
|
|
# ifdef __cplusplus
|
444 |
|
|
// NOTE: if the compiler does not manage to inline the appropriate
|
445 |
|
|
// template functions then the impact on code size and performance becomes
|
446 |
|
|
// rather large. But there are significant performance overheads anyway
|
447 |
|
|
// simply because of the call to check_this()...
|
448 |
|
|
//
|
449 |
|
|
template<class X> class __CygInvariantObject {
|
450 |
|
|
|
451 |
|
|
const X* rep;
|
452 |
|
|
|
453 |
|
|
private:
|
454 |
|
|
// Prevent access to the default constructors.
|
455 |
|
|
__CygInvariantObject() { }
|
456 |
|
|
__CygInvariantObject( const __CygInvariantObject& arg ) { }
|
457 |
|
|
__CygInvariantObject & operator=( const __CygInvariantObject & arg) { return *this; }
|
458 |
|
|
|
459 |
|
|
public:
|
460 |
|
|
__CygInvariantObject( X* arg, const char* msg ) : rep(arg) {
|
461 |
|
|
if ( !rep->check_this( CYG_ASSERT_CLASS_ZEAL ) )
|
462 |
|
|
CYG_ASSERT_DOCALL( msg );
|
463 |
|
|
}
|
464 |
|
|
__CygInvariantObject( X& arg, const char* msg ) : rep(&arg) {
|
465 |
|
|
if ( !rep->check_this( CYG_ASSERT_CLASS_ZEAL ) )
|
466 |
|
|
CYG_ASSERT_DOCALL( msg );
|
467 |
|
|
}
|
468 |
|
|
__CygInvariantObject( const X* arg, const char* msg ) : rep(arg) {
|
469 |
|
|
if ( !rep->check_this( CYG_ASSERT_CLASS_ZEAL ) )
|
470 |
|
|
CYG_ASSERT_DOCALL( msg );
|
471 |
|
|
}
|
472 |
|
|
__CygInvariantObject( const X& arg, const char* msg ) : rep(&arg) {
|
473 |
|
|
if ( !rep->check_this( CYG_ASSERT_CLASS_ZEAL ) )
|
474 |
|
|
CYG_ASSERT_DOCALL( msg );
|
475 |
|
|
}
|
476 |
|
|
~__CygInvariantObject( ) {
|
477 |
|
|
if ( !rep->check_this( CYG_ASSERT_CLASS_ZEAL ) )
|
478 |
|
|
CYG_ASSERT_DOCALL( "invariant, object valid on exit" );
|
479 |
|
|
rep = 0;
|
480 |
|
|
};
|
481 |
|
|
};
|
482 |
|
|
|
483 |
|
|
//
|
484 |
|
|
// These macros provide sensible concatenation facilities at
|
485 |
|
|
// the C preprocessor level, getting around complications in the
|
486 |
|
|
// macro expansion rules related to __LINE__ and __FILE__.
|
487 |
|
|
|
488 |
|
|
# define __CYG_INVARIANT_CLASSNAME_AUX( a, b) a ## b
|
489 |
|
|
# define __CYG_INVARIANT_CLASSNAME( a, b ) \
|
490 |
|
|
__CYG_INVARIANT_CLASSNAME_AUX( a, b )
|
491 |
|
|
|
492 |
|
|
|
493 |
|
|
// These macro definitions do not use CYG_MACRO_START because
|
494 |
|
|
// I do not want the scope of the local objects to get confused.
|
495 |
|
|
//
|
496 |
|
|
// The first line of the macro expansion specifies the type of
|
497 |
|
|
// the local object being created. The second line invents a
|
498 |
|
|
// name for this object. The third line provides command-line
|
499 |
|
|
// arguments.
|
500 |
|
|
|
501 |
|
|
# define CYG_INVARIANT_CLASS( _type_, _pobj_, _msg_ ) \
|
502 |
|
|
__CygInvariantObject<_type_> \
|
503 |
|
|
__CYG_INVARIANT_CLASSNAME( __invariant_class_, __LINE__ ) \
|
504 |
|
|
( _pobj_, _msg_ )
|
505 |
|
|
|
506 |
|
|
# define CYG_INVARIANT_CLASSC( _type_, _pobj_ ) \
|
507 |
|
|
__CygInvariantObject<_type_> \
|
508 |
|
|
__CYG_INVARIANT_CLASSNAME( __invariant_class_, __LINE__ ) \
|
509 |
|
|
( _pobj_, "invariant, class pointer (" #_pobj_ ") is valid" )
|
510 |
|
|
|
511 |
|
|
# define CYG_INVARIANT_CLASSO( _type_, _obj_, _msg_ ) \
|
512 |
|
|
__CygInvariantObject<_type_> \
|
513 |
|
|
__CYG_INVARIANT_CLASSNAME( __invariant_class_, __LINE__ ) \
|
514 |
|
|
( _obj_, _msg_ )
|
515 |
|
|
|
516 |
|
|
# define CYG_INVARIANT_CLASSOC( _type_, _obj_ ) \
|
517 |
|
|
__CygInvariantObject<_type_> \
|
518 |
|
|
__CYG_INVARIANT_CLASSNAME( __invariant_class_, __LINE__ ) \
|
519 |
|
|
( _obj_, "invariant, object (" #_obj_ ") is valid" )
|
520 |
|
|
|
521 |
|
|
# define CYG_INVARIANT_THIS( _type_, _msg_ ) \
|
522 |
|
|
__CygInvariantObject<_type_> \
|
523 |
|
|
__CYG_INVARIANT_CLASSNAME( __invariant_class_, __LINE__ ) \
|
524 |
|
|
( this, _msg_ )
|
525 |
|
|
|
526 |
|
|
# define CYG_INVARIANT_THISC( _type_ ) \
|
527 |
|
|
__CygInvariantObject<_type_> \
|
528 |
|
|
__CYG_INVARIANT_CLASSNAME( __invariant_class_, __LINE__ ) \
|
529 |
|
|
( this, "invariant, \"this\" is valid" )
|
530 |
|
|
|
531 |
|
|
# endif // __cplusplus
|
532 |
|
|
|
533 |
|
|
#else // !CYGDBG_INFRA_DEBUG_INVARIANTS
|
534 |
|
|
|
535 |
|
|
# define CYG_INVARIANT( _bool_, _msg_ ) CYG_EMPTY_STATEMENT
|
536 |
|
|
# define CYG_INVARIANTC( _bool_ ) CYG_EMPTY_STATEMENT
|
537 |
|
|
|
538 |
|
|
# ifdef __cplusplus
|
539 |
|
|
|
540 |
|
|
# define CYG_INVARIANT_CLASS( _type_, _pobj_, _msg_ )
|
541 |
|
|
# define CYG_INVARIANT_CLASSC( _type_, _pobj_ )
|
542 |
|
|
# define CYG_INVARIANT_CLASSO( _type_, _obj_, _msg_ )
|
543 |
|
|
# define CYG_INVARIANT_CLASSOC( _type_, _obj_ )
|
544 |
|
|
# define CYG_INVARIANT_THIS( _type_, _msg_ )
|
545 |
|
|
# define CYG_INVARIANT_THISC( _type_ )
|
546 |
|
|
|
547 |
|
|
# endif
|
548 |
|
|
|
549 |
|
|
#endif // CYGDBG_INFRA_DEBUG_INVARIANTS
|
550 |
|
|
|
551 |
|
|
// -------------------------------------------------------------------------
|
552 |
|
|
// Compile time failure; like #error but in a macro so we can use it in
|
553 |
|
|
// other definitions.
|
554 |
|
|
//
|
555 |
|
|
// Usage:
|
556 |
|
|
// #define new CYG_COMPILETIMEFAIL( "Do NOT use new!")
|
557 |
|
|
|
558 |
|
|
#define CYG_COMPILETIMEFAIL( _msg_ ) !!!-- _msg_ --!!!
|
559 |
|
|
|
560 |
|
|
|
561 |
|
|
// -------------------------------------------------------------------------
|
562 |
|
|
// The host-side implementation of the infrastructure provides a number
|
563 |
|
|
// of additional functions that allow applications to provide their own
|
564 |
|
|
// implementation of cyg_assert_fail(). This is not strictly necessary
|
565 |
|
|
// since the presence of cyg_assert_fail() in the application would
|
566 |
|
|
// override the one in the library anyway, but it is useful to make
|
567 |
|
|
// certain functionality more readily available.
|
568 |
|
|
//
|
569 |
|
|
// These declarations are only available if the symbol
|
570 |
|
|
// CYG_DECLARE_HOST_ASSERTION_SUPPORT is defined.
|
571 |
|
|
#ifdef CYG_DECLARE_HOST_ASSERTION_SUPPORT
|
572 |
|
|
|
573 |
|
|
// The default assertion handler writes its output to a file and
|
574 |
|
|
// if possible a suitable message to stdout. It is possible to
|
575 |
|
|
// install an alternative handler. If this alternative returns false
|
576 |
|
|
// then the default handler will be invoked instead, otherwise the
|
577 |
|
|
// application will exit.
|
578 |
|
|
externC void cyg_assert_install_failure_handler(
|
579 |
|
|
bool (*)(const char* /* psz_func */,
|
580 |
|
|
const char* /* psz_file */,
|
581 |
|
|
cyg_uint32 /* linenum */,
|
582 |
|
|
const char* /* psz_msg */) );
|
583 |
|
|
|
584 |
|
|
// Register a callback that should get invoked as part of handling an
|
585 |
|
|
// assertion failure and that will typically produce some additional
|
586 |
|
|
// output. For example the trace code will install a callback to output
|
587 |
|
|
// trace information.
|
588 |
|
|
//
|
589 |
|
|
// The first argument is a string identifying the callback. The second
|
590 |
|
|
// argument is a function pointer for the callback itself, whose
|
591 |
|
|
// argument is another function that can be invoked for output.
|
592 |
|
|
externC void cyg_assert_install_failure_callback(
|
593 |
|
|
const char* /* name */,
|
594 |
|
|
void (*)( void (*)(const char*) ));
|
595 |
|
|
|
596 |
|
|
// This function can be called by assert failure handlers to invoke
|
597 |
|
|
// the installed callbacks. The three arguments are function pointers
|
598 |
|
|
// that get invoked prior to callback invocation, by the callback itself,
|
599 |
|
|
// and after each callback. In the first case the argument will be the
|
600 |
|
|
// callback name.
|
601 |
|
|
externC void cyg_assert_failure_invoke_callbacks(
|
602 |
|
|
void (*)(const char* /* name */),
|
603 |
|
|
void (*)(const char* /* callback data */ ),
|
604 |
|
|
void (*)(void) );
|
605 |
|
|
|
606 |
|
|
// This function is intended to be called from inside gdb instead of
|
607 |
|
|
// cyg_assert_fail(),, without the need to specify a filename or
|
608 |
|
|
// anything else.
|
609 |
|
|
externC void cyg_assert_quickfail(void);
|
610 |
|
|
|
611 |
|
|
#endif // CYG_DECLARE_HOST_ASSERTION_SUPPORT
|
612 |
|
|
|
613 |
|
|
// -------------------------------------------------------------------------
|
614 |
|
|
|
615 |
|
|
#endif // CYGONCE_INFRA_CYG_ASS_H multiple inclusion protection
|
616 |
|
|
// EOF cyg_ass.h
|