4 * Moscow Center for SPARC Technology
9 * This material is provided "as is", with absolutely no warranty expressed
10 * or implied. Any use is at your own risk.
12 * Permission to use or copy this software for any purpose is hereby granted
13 * without fee, provided the above notices are retained on all copies.
14 * Permission to modify the code and to distribute modified code is granted,
15 * provided the above notices are retained, and a notice that the code was
16 * modified is included with the above copyright notice.
23 #if defined (_STLP_ASSERTIONS) || defined (_STLP_DEBUG)
25 # ifndef _STLP_TYPE_TRAITS_H
26 # include <stl/type_traits.h>
29 # if !defined (_STLP_EXTRA_OPERATORS_FOR_DEBUG) && \
30 (defined (_STLP_BASE_MATCH_BUG) || (defined (_STLP_MSVC) && _STLP_MSVC < 1100))
31 # define _STLP_EXTRA_OPERATORS_FOR_DEBUG
34 # if !defined (_STLP_FILE__)
35 # define _STLP_FILE__ __FILE__
40 _STLP_MOVE_TO_PRIV_NAMESPACE
44 _StlFormat_ERROR_RETURN,
45 _StlFormat_ASSERTION_FAILURE,
46 _StlFormat_VERBOSE_ASSERTION_FAILURE,
47 _StlMsg_INVALID_ARGUMENT,
48 //Container/Iterator related errors
49 _StlMsg_INVALID_CONTAINER,
50 _StlMsg_EMPTY_CONTAINER,
51 _StlMsg_ERASE_PAST_THE_END,
52 _StlMsg_OUT_OF_BOUNDS,
54 _StlMsg_SHOULD_NOT_OWNER,
55 _StlMsg_INVALID_ITERATOR,
56 _StlMsg_INVALID_LEFTHAND_ITERATOR,
57 _StlMsg_INVALID_RIGHTHAND_ITERATOR,
58 _StlMsg_DIFFERENT_OWNERS ,
59 _StlMsg_NOT_DEREFERENCEABLE ,
60 _StlMsg_INVALID_RANGE ,
61 _StlMsg_NOT_IN_RANGE_1 ,
62 _StlMsg_NOT_IN_RANGE_2 ,
63 _StlMsg_INVALID_ADVANCE ,
64 _StlMsg_SINGULAR_ITERATOR ,
65 //Bad predicate for sorting
66 _StlMsg_INVALID_STRICT_WEAK_PREDICATE,
67 _StlMsg_INVALID_EQUIVALENT_PREDICATE,
68 // debug alloc messages
69 _StlMsg_DBA_DELETED_TWICE ,
70 _StlMsg_DBA_NEVER_ALLOCATED ,
71 _StlMsg_DBA_TYPE_MISMATCH ,
72 _StlMsg_DBA_SIZE_MISMATCH ,
73 _StlMsg_DBA_UNDERRUN ,
76 _StlMsg_AUTO_PTR_NULL ,
77 //Memory alignent message
78 _StlMsg_WRONG_MEMORY_ALIGNMENT,
83 /* have to hardcode that ;() */
84 # define _StlMsg_MAX 31
86 // This class is unique (not inherited from exception),
87 // to disallow catch in anything but (...)
88 struct __stl_debug_exception {
92 class _STLP_CLASS_DECLSPEC __owned_link;
93 class _STLP_CLASS_DECLSPEC __owned_list;
95 #if defined (_STLP_DEBUG_MODE_THROWS)
96 # define _STLP_MESSAGE_NORETURN _STLP_FUNCTION_THROWS
98 # define _STLP_MESSAGE_NORETURN
101 template <class _Dummy>
102 struct __stl_debug_engine {
104 // Basic routine to report any debug message
105 // Use _STLP_DEBUG_MESSAGE to override
106 static void _STLP_MESSAGE_NORETURN _STLP_CALL _Message(const char * format_str, ...);
108 // Micsellanous function to report indexed error message
109 static void _STLP_CALL _IndexedError(int __ind, const char* __f, int __l);
111 // Basic assertion report mechanism.
112 // Reports failed assertion via __stl_debug_message and calls _Terminate
113 // if _STLP_DEBUG_TERMINATE is specified, calls __stl_debug_terminate instead
114 static void _STLP_CALL _Assert(const char* __expr, const char* __f, int __l);
116 // The same, with additional diagnostics
117 static void _STLP_CALL _VerboseAssert(const char* __expr, int __error_ind, const char* __f, int __l);
119 // If exceptions are present, sends unique exception
120 // If not, calls _STLP_ABORT() to terminate
121 // Use _STLP_DEBUG_TERMINATE to override
122 static void _STLP_CALL _Terminate();
124 # if defined (_STLP_DEBUG)
125 // owned_list/link delegate non-inline functions here
127 static bool _STLP_CALL _Check_same_owner( const __owned_link& __i1,
128 const __owned_link& __i2);
129 static bool _STLP_CALL _Check_same_or_null_owner( const __owned_link& __i1,
130 const __owned_link& __i2);
131 static bool _STLP_CALL _Check_if_owner( const __owned_list*, const __owned_link&);
133 static bool _STLP_CALL _Check_if_not_owner( const __owned_list*, const __owned_link&);
135 static void _STLP_CALL _Verify(const __owned_list*);
137 static void _STLP_CALL _Swap_owners(__owned_list&, __owned_list&);
139 static void _STLP_CALL _Invalidate_all(__owned_list*);
141 static void _STLP_CALL _Set_owner(__owned_list& /*src*/, __owned_list& /*dst*/);
143 static void _STLP_CALL _Stamp_all(__owned_list*, __owned_list*);
145 static void _STLP_CALL _M_detach(__owned_list*, __owned_link*);
147 static void _STLP_CALL _M_attach(__owned_list*, __owned_link*);
149 // accessor : check and get pointer to the container
150 static void* _STLP_CALL _Get_container_ptr(const __owned_link*);
153 // debug messages and formats
154 static _STLP_STATIC_MEMBER_DECLSPEC const char* _Message_table[_StlMsg_MAX];
157 #undef _STLP_MESSAGE_NORETURN
159 # if defined (_STLP_USE_TEMPLATE_EXPORT)
160 _STLP_EXPORT_TEMPLATE struct _STLP_CLASS_DECLSPEC __stl_debug_engine<bool>;
161 # endif /* _STLP_USE_TEMPLATE_EXPORT */
163 typedef __stl_debug_engine<bool> __stl_debugger;
165 _STLP_MOVE_TO_STD_NAMESPACE
169 # if !defined (_STLP_ASSERT)
170 # define _STLP_ASSERT(expr) \
171 if (!(expr)) { _STLP_PRIV __stl_debugger::_Assert( # expr, _STLP_FILE__, __LINE__); }
174 #endif /* _STLP_ASSERTIONS || _STLP_DEBUG */
176 // this section is for _STLP_DEBUG only
177 #if defined (_STLP_DEBUG)
179 # if !defined (_STLP_VERBOSE_ASSERT)
180 // fbp : new form not requiring ";"
181 # define _STLP_VERBOSE_ASSERT(expr, __diag_num) \
182 if (!(expr)) { _STLP_PRIV __stl_debugger::_VerboseAssert\
183 ( # expr, _STLP_PRIV __diag_num, _STLP_FILE__, __LINE__ ); \
187 # define _STLP_DEBUG_CHECK(expr) _STLP_ASSERT(expr)
188 # define _STLP_DEBUG_DO(expr) expr;
190 # if (_STLP_DEBUG_LEVEL == _STLP_STANDARD_DBG_LEVEL)
191 # define _STLP_STD_DEBUG_CHECK(expr) _STLP_DEBUG_CHECK(expr)
192 # define _STLP_STD_DEBUG_DO(expr) _STLP_DEBUG_DO(expr)
194 # define _STLP_STD_DEBUG_CHECK(expr)
195 # define _STLP_STD_DEBUG_DO(expr)
198 # if !defined (_STLP_VERBOSE_RETURN)
199 # define _STLP_VERBOSE_RETURN(__expr,__diag_num) if (!(__expr)) { \
200 _STLP_PRIV __stl_debugger::_IndexedError(__diag_num, _STLP_FILE__ , __LINE__); \
204 # if !defined (_STLP_VERBOSE_RETURN_0)
205 # define _STLP_VERBOSE_RETURN_0(__expr,__diag_num) if (!(__expr)) { \
206 _STLP_PRIV __stl_debugger::_IndexedError(__diag_num, _STLP_FILE__, __LINE__); \
210 # ifndef _STLP_INTERNAL_THREADS_H
211 # include <stl/_threads.h>
214 # ifndef _STLP_INTERNAL_ITERATOR_BASE_H
215 # include <stl/_iterator_base.h>
218 _STLP_BEGIN_NAMESPACE
220 _STLP_MOVE_TO_PRIV_NAMESPACE
223 * Special debug iterator traits having an additionnal static member
224 * method _Check. It is used by the slit debug implementation to check
225 * the special before_begin iterator.
227 template <class _Traits>
228 struct _DbgTraits : _Traits {
229 typedef _DbgTraits<typename _Traits::_ConstTraits> _ConstTraits;
230 typedef _DbgTraits<typename _Traits::_NonConstTraits> _NonConstTraits;
232 template <class _Iterator>
233 static bool _Check(const _Iterator&) {return true;}
236 //=============================================================
237 template <class _Iterator>
238 inline bool _STLP_CALL __valid_range(const _Iterator& __i1 ,const _Iterator& __i2,
239 const random_access_iterator_tag&)
240 { return (__i1 < __i2) || (__i1 == __i2); }
242 template <class _Iterator>
243 inline bool _STLP_CALL __valid_range(const _Iterator& __i1 ,const _Iterator& __i2,
244 const bidirectional_iterator_tag&) {
245 // check if comparable
246 bool __dummy(__i1==__i2);
247 return (__dummy==__dummy);
250 template <class _Iterator>
251 inline bool _STLP_CALL __valid_range(const _Iterator& __i1 ,const _Iterator& __i2,
252 const forward_iterator_tag&) {
253 // check if comparable
254 bool __dummy(__i1==__i2);
255 return (__dummy==__dummy);
258 template <class _Iterator>
259 inline bool _STLP_CALL __valid_range(const _Iterator&,const _Iterator&,
260 const input_iterator_tag&)
263 template <class _Iterator>
264 inline bool _STLP_CALL __valid_range(const _Iterator&,const _Iterator&,
265 const output_iterator_tag&)
268 template <class _Iterator>
269 inline bool _STLP_CALL __valid_range(const _Iterator& __i1, const _Iterator& __i2)
270 { return __valid_range(__i1,__i2,_STLP_ITERATOR_CATEGORY(__i1, _Iterator)); }
272 // Note : that means in range [i1, i2].
273 template <class _Iterator>
274 inline bool _STLP_CALL __in_range(const _Iterator& _It,
275 const _Iterator& __i1, const _Iterator& __i2)
276 { return __valid_range(__i1,_It) && __valid_range(_It,__i2); }
278 template <class _Iterator>
279 inline bool _STLP_CALL __in_range(const _Iterator& __first, const _Iterator& __last,
280 const _Iterator& __start, const _Iterator& __finish)
281 { return __valid_range(__first,__last) && __valid_range(__start,__first) && __valid_range(__last,__finish); }
283 //==========================================================
284 class _STLP_CLASS_DECLSPEC __owned_link {
286 __owned_link() : _M_owner(0) {}
287 __owned_link(const __owned_list* __c) : _M_owner(0), _M_next(0)
288 { __stl_debugger::_M_attach(__CONST_CAST(__owned_list*,__c), this); }
289 __owned_link(const __owned_link& __rhs): _M_owner(0)
290 { __stl_debugger::_M_attach(__CONST_CAST(__owned_list*,__rhs._M_owner), this); }
291 __owned_link& operator=(const __owned_link& __rhs) {
292 __owned_list* __new_owner = __CONST_CAST(__owned_list*,__rhs._M_owner);
293 __owned_list* __old_owner = _M_owner;
294 if ( __old_owner != __new_owner ) {
295 __stl_debugger::_M_detach(__old_owner, this);
296 __stl_debugger::_M_attach(__new_owner, this);
301 __stl_debugger::_M_detach(_M_owner, this);
305 const __owned_list* _Owner() const { return _M_owner; }
306 __owned_list* _Owner() { return _M_owner; }
307 void _Set_owner(const __owned_list* __o) { _M_owner= __CONST_CAST(__owned_list*,__o); }
308 bool _Valid() const { return _M_owner != 0; }
309 void _Invalidate() { _M_owner = 0; _M_next = 0; }
310 void _Link_to_self() { _M_next = 0; }
312 __owned_link* _Next() { return _M_next; }
313 const __owned_link* _Next() const { return _M_next; }
316 __owned_list* _M_owner;
317 __owned_link* _M_next;
321 class _STLP_CLASS_DECLSPEC __owned_list {
323 __owned_list(void* __o) {
324 // fprintf(stderr, "__owned_list(): %p\n",(void*)this);
325 _M_node._M_owner = __REINTERPRET_CAST(__owned_list*,__o);
329 // fprintf(stderr, "~__owned_list(): %p\n",(void*)this);
331 // that prevents detach
332 _M_node._Invalidate();
334 const void* _Owner() const { return (const void*)_M_node._M_owner; }
335 void* _Owner() { return (void*)_M_node._M_owner; }
336 bool _Valid() const { return _M_node._M_owner != 0; }
337 void _Invalidate() { _M_node._M_owner = 0; }
339 __owned_link* _First() { return _M_node._Next(); }
340 __owned_link* _Last() { return 0 ; }
342 const __owned_link* _First() const { return (__owned_link*)_M_node._M_next; }
343 const __owned_link* _Last() const { return 0 ;}
345 void _Verify() const { __stl_debugger::_Verify(this); }
346 void _Swap_owners(__owned_list& __y) { __stl_debugger::_Swap_owners(*this, __y); }
347 void _Invalidate_all() { __stl_debugger::_Invalidate_all(this); }
348 void _Set_owner(__owned_list& __y) { __stl_debugger::_Set_owner(*this, __y); }
350 mutable __owned_link _M_node;
351 mutable _STLP_mutex _M_lock;
354 // should never be called, should be left not implemented,
355 // but some compilers complain about it ;(
356 __owned_list(const __owned_list&){}
357 __owned_list& operator = (const __owned_list&) { return *this; }
359 friend class __owned_link;
360 friend struct __stl_debug_engine<bool>;
364 //==========================================================
366 // forward declaratioins
368 template <class _Iterator>
369 bool _STLP_CALL __check_range(const _Iterator&, const _Iterator&);
370 template <class _Iterator>
371 bool _STLP_CALL __check_range(const _Iterator&,
372 const _Iterator&, const _Iterator&);
373 template <class _Iterator>
374 bool _STLP_CALL __check_range(const _Iterator&, const _Iterator& ,
375 const _Iterator&, const _Iterator& );
377 bool _STLP_CALL __check_ptr_range(const _Tp*, const _Tp*);
380 template <class _Iterator>
381 void _STLP_CALL __invalidate_range(const __owned_list* __base,
382 const _Iterator& __first,
383 const _Iterator& __last);
385 template <class _Iterator>
386 void _STLP_CALL __invalidate_iterator(const __owned_list* __base,
387 const _Iterator& __it);
389 template <class _Iterator>
390 void _STLP_CALL __change_range_owner(const _Iterator& __first,
391 const _Iterator& __last,
392 const __owned_list* __dst);
394 template <class _Iterator>
395 void _STLP_CALL __change_ite_owner(const _Iterator& __it,
396 const __owned_list* __dst);
398 //============================================================
399 inline bool _STLP_CALL
400 __check_same_owner(const __owned_link& __i1, const __owned_link& __i2)
401 { return __stl_debugger::_Check_same_owner(__i1,__i2); }
403 inline bool _STLP_CALL
404 __check_same_or_null_owner(const __owned_link& __i1, const __owned_link& __i2)
405 { return __stl_debugger::_Check_same_or_null_owner(__i1,__i2); }
407 template <class _Iterator>
408 inline bool _STLP_CALL __check_if_owner( const __owned_list* __owner,
409 const _Iterator& __it)
410 { return __stl_debugger::_Check_if_owner(__owner, (const __owned_link&)__it); }
412 template <class _Iterator>
413 inline bool _STLP_CALL __check_if_not_owner( const __owned_list* /*__owner*/,
414 const _Iterator& /*__it*/,
418 template <class _Iterator>
419 inline bool _STLP_CALL __check_if_not_owner( const __owned_list* __owner,
420 const _Iterator& __it,
422 { return __stl_debugger::_Check_if_not_owner(__owner, (const __owned_link&)__it); }
424 _STLP_MOVE_TO_STD_NAMESPACE
428 #endif /* _STLP_DEBUG */
430 #if defined (_STLP_ASSERTIONS)
432 # if !defined (_STLP_ASSERT_MSG_TRAILER)
433 # define _STLP_ASSERT_MSG_TRAILER
436 // dwa 12/30/98 - if _STLP_DEBUG_MESSAGE is defined, the user can supply own definition.
437 # if !defined (_STLP_DEBUG_MESSAGE)
438 # define __stl_debug_message __stl_debugger::_Message
440 extern void __stl_debug_message(const char * format_str, ...);
443 // fbp: if _STLP_DEBUG_TERMINATE is defined, the user can supply own definition.
444 # if !defined (_STLP_DEBUG_TERMINATE)
445 # define __stl_debug_terminate __stl_debugger::_Terminate
447 extern void __stl_debug_terminate();
452 #if !defined (_STLP_LINK_TIME_INSTANTIATION)
453 # include <stl/debug/_debug.c>