_debug.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. *
  3. * Copyright (c) 1997
  4. * Moscow Center for SPARC Technology
  5. *
  6. * Copyright (c) 1999
  7. * Boris Fomitchev
  8. *
  9. * This material is provided "as is", with absolutely no warranty expressed
  10. * or implied. Any use is at your own risk.
  11. *
  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.
  17. *
  18. */
  19. #ifndef _STLP_DEBUG_H
  20. #define _STLP_DEBUG_H
  21. #if (defined (_STLP_DEBUG) || defined (_STLP_DEBUG_ALLOC)) && \
  22. !defined (_STLP_ASSERTIONS)
  23. # define _STLP_ASSERTIONS 1
  24. #endif
  25. #if defined (_STLP_ASSERTIONS)
  26. # if !defined (_STLP_FILE__)
  27. # define _STLP_FILE__ __FILE__
  28. # endif
  29. _STLP_BEGIN_NAMESPACE
  30. _STLP_MOVE_TO_PRIV_NAMESPACE
  31. enum {
  32. //General errors
  33. _StlFormat_ERROR_RETURN,
  34. _StlFormat_ASSERTION_FAILURE,
  35. _StlFormat_VERBOSE_ASSERTION_FAILURE,
  36. _StlMsg_INVALID_ARGUMENT,
  37. //Container/Iterator related errors
  38. _StlMsg_INVALID_CONTAINER,
  39. _StlMsg_EMPTY_CONTAINER,
  40. _StlMsg_ERASE_PAST_THE_END,
  41. _StlMsg_OUT_OF_BOUNDS,
  42. _StlMsg_NOT_OWNER,
  43. _StlMsg_SHOULD_NOT_OWNER,
  44. _StlMsg_INVALID_ITERATOR,
  45. _StlMsg_INVALID_LEFTHAND_ITERATOR,
  46. _StlMsg_INVALID_RIGHTHAND_ITERATOR,
  47. _StlMsg_DIFFERENT_OWNERS ,
  48. _StlMsg_NOT_DEREFERENCEABLE ,
  49. _StlMsg_INVALID_RANGE ,
  50. _StlMsg_NOT_IN_RANGE_1 ,
  51. _StlMsg_NOT_IN_RANGE_2 ,
  52. _StlMsg_INVALID_ADVANCE ,
  53. _StlMsg_SINGULAR_ITERATOR ,
  54. //Bad predicate for sorting
  55. _StlMsg_INVALID_STRICT_WEAK_PREDICATE,
  56. _StlMsg_INVALID_EQUIVALENT_PREDICATE,
  57. // debug alloc messages
  58. _StlMsg_DBA_DELETED_TWICE ,
  59. _StlMsg_DBA_NEVER_ALLOCATED ,
  60. _StlMsg_DBA_TYPE_MISMATCH ,
  61. _StlMsg_DBA_SIZE_MISMATCH ,
  62. _StlMsg_DBA_UNDERRUN ,
  63. _StlMsg_DBA_OVERRUN ,
  64. // auto_ptr messages
  65. _StlMsg_AUTO_PTR_NULL ,
  66. //Memory alignent message
  67. _StlMsg_WRONG_MEMORY_ALIGNMENT,
  68. _StlMsg_UNKNOWN
  69. /* _StlMsg_MAX */
  70. };
  71. /* have to hardcode that ;() */
  72. # define _StlMsg_MAX 31
  73. class __owned_link;
  74. class __owned_list;
  75. # if defined (_STLP_DEBUG_MODE_THROWS)
  76. # define _STLP_MESSAGE_NORETURN _STLP_FUNCTION_THROWS
  77. # else
  78. # define _STLP_MESSAGE_NORETURN
  79. # endif
  80. template <class _Dummy>
  81. class __stl_debug_engine {
  82. public:
  83. // Basic routine to report any debug message
  84. // Use _STLP_DEBUG_MESSAGE to override
  85. static void _STLP_MESSAGE_NORETURN _STLP_CALL _Message(const char * format_str, ...);
  86. // Micsellanous function to report indexed error message
  87. static void _STLP_CALL _IndexedError(int __ind, const char* __f, int __l);
  88. // Basic assertion report mechanism.
  89. // Reports failed assertion via __stl_debug_message and calls _Terminate
  90. // if _STLP_DEBUG_TERMINATE is specified, calls __stl_debug_terminate instead
  91. static void _STLP_CALL _Assert(const char* __expr, const char* __f, int __l);
  92. // The same, with additional diagnostics
  93. static void _STLP_CALL _VerboseAssert(const char* __expr, int __error_ind, const char* __f, int __l);
  94. // If exceptions are present, sends unique exception
  95. // If not, calls _STLP_ABORT() to terminate
  96. // Use _STLP_DEBUG_TERMINATE to override
  97. static void _STLP_CALL _Terminate();
  98. # if defined (_STLP_DEBUG)
  99. // owned_list/link delegate non-inline functions here
  100. static bool _STLP_CALL _Check_same_owner( const __owned_link& __i1,
  101. const __owned_link& __i2);
  102. static bool _STLP_CALL _Check_same_or_null_owner( const __owned_link& __i1,
  103. const __owned_link& __i2);
  104. static bool _STLP_CALL _Check_if_owner( const __owned_list*, const __owned_link&);
  105. static bool _STLP_CALL _Check_if_not_owner( const __owned_list*, const __owned_link&);
  106. static void _STLP_CALL _Verify(const __owned_list*);
  107. static void _STLP_CALL _Swap_owners(__owned_list&, __owned_list&);
  108. static void _STLP_CALL _Invalidate_all(__owned_list*);
  109. static void _STLP_CALL _Set_owner(__owned_list& /*src*/, __owned_list& /*dst*/);
  110. static void _STLP_CALL _Stamp_all(__owned_list*, __owned_list*);
  111. static void _STLP_CALL _M_detach(__owned_list*, __owned_link*);
  112. static void _STLP_CALL _M_attach(__owned_list*, __owned_link*);
  113. // accessor : check and get pointer to the container
  114. static void* _STLP_CALL _Get_container_ptr(const __owned_link*);
  115. # endif
  116. // debug messages and formats
  117. static const char* _Message_table[_StlMsg_MAX];
  118. };
  119. # undef _STLP_MESSAGE_NORETURN
  120. # if defined (_STLP_USE_TEMPLATE_EXPORT)
  121. _STLP_EXPORT_TEMPLATE_CLASS __stl_debug_engine<bool>;
  122. # endif
  123. typedef __stl_debug_engine<bool> __stl_debugger;
  124. _STLP_MOVE_TO_STD_NAMESPACE
  125. _STLP_END_NAMESPACE
  126. # if !defined (_STLP_ASSERT)
  127. # define _STLP_ASSERT(expr) \
  128. if (!(expr)) { _STLP_PRIV __stl_debugger::_Assert( # expr, _STLP_FILE__, __LINE__); }
  129. # endif
  130. #else
  131. # define _STLP_ASSERT(expr)
  132. #endif
  133. // this section is for _STLP_DEBUG only
  134. #if defined (_STLP_DEBUG)
  135. # if !defined (_STLP_VERBOSE_ASSERT)
  136. // fbp : new form not requiring ";"
  137. # define _STLP_VERBOSE_ASSERT(expr, __diag_num) \
  138. if (!(expr)) { _STLP_PRIV __stl_debugger::_VerboseAssert\
  139. ( # expr, _STLP_PRIV __diag_num, _STLP_FILE__, __LINE__ ); \
  140. }
  141. # endif
  142. # define _STLP_DEBUG_CHECK(expr) _STLP_ASSERT(expr)
  143. # if (_STLP_DEBUG_LEVEL == _STLP_STANDARD_DBG_LEVEL)
  144. # define _STLP_STD_DEBUG_CHECK(expr) _STLP_DEBUG_CHECK(expr)
  145. # else
  146. # define _STLP_STD_DEBUG_CHECK(expr)
  147. # endif
  148. # if !defined (_STLP_VERBOSE_RETURN)
  149. # define _STLP_VERBOSE_RETURN(__expr,__diag_num) if (!(__expr)) { \
  150. _STLP_PRIV __stl_debugger::_IndexedError(__diag_num, _STLP_FILE__ , __LINE__); \
  151. return false; }
  152. # endif
  153. # if !defined (_STLP_VERBOSE_RETURN_0)
  154. # define _STLP_VERBOSE_RETURN_0(__expr,__diag_num) if (!(__expr)) { \
  155. _STLP_PRIV __stl_debugger::_IndexedError(__diag_num, _STLP_FILE__, __LINE__); \
  156. return 0; }
  157. # endif
  158. # ifndef _STLP_INTERNAL_THREADS_H
  159. # include <stl/_threads.h>
  160. # endif
  161. # ifndef _STLP_INTERNAL_ITERATOR_BASE_H
  162. # include <stl/_iterator_base.h>
  163. # endif
  164. # ifndef _STLP_TYPE_TRAITS_H
  165. # include <stl/type_traits.h>
  166. # endif
  167. _STLP_BEGIN_NAMESPACE
  168. _STLP_MOVE_TO_PRIV_NAMESPACE
  169. /*
  170. * Special debug iterator traits having an additionnal static member
  171. * method _Check. It is used by the slist debug implementation to check
  172. * the special before_begin iterator.
  173. */
  174. template <class _Traits>
  175. struct _DbgTraits : _Traits {
  176. typedef _DbgTraits<typename _Traits::_ConstTraits> _ConstTraits;
  177. typedef _DbgTraits<typename _Traits::_NonConstTraits> _NonConstTraits;
  178. template <class _Iterator>
  179. static bool _Check(const _Iterator&) {return true;}
  180. };
  181. //=============================================================
  182. template <class _Iterator>
  183. inline bool _STLP_CALL __valid_range(const _Iterator& __i1 ,const _Iterator& __i2,
  184. const random_access_iterator_tag&)
  185. { return (__i1 < __i2) || (__i1 == __i2); }
  186. template <class _Iterator>
  187. inline bool _STLP_CALL __valid_range(const _Iterator& __i1 ,const _Iterator& __i2,
  188. const bidirectional_iterator_tag&) {
  189. // check if comparable
  190. bool __dummy(__i1==__i2);
  191. return (__dummy==__dummy);
  192. }
  193. template <class _Iterator>
  194. inline bool _STLP_CALL __valid_range(const _Iterator& __i1 ,const _Iterator& __i2,
  195. const forward_iterator_tag&) {
  196. // check if comparable
  197. bool __dummy(__i1==__i2);
  198. return (__dummy==__dummy);
  199. }
  200. template <class _Iterator>
  201. inline bool _STLP_CALL __valid_range(const _Iterator&,const _Iterator&,
  202. const input_iterator_tag&)
  203. { return true; }
  204. template <class _Iterator>
  205. inline bool _STLP_CALL __valid_range(const _Iterator&,const _Iterator&,
  206. const output_iterator_tag&)
  207. { return true; }
  208. template <class _Iterator>
  209. inline bool _STLP_CALL __valid_range(const _Iterator& __i1, const _Iterator& __i2)
  210. { return __valid_range(__i1,__i2,_STLP_ITERATOR_CATEGORY(__i1, _Iterator)); }
  211. // Note : that means in range [i1, i2].
  212. template <class _Iterator>
  213. inline bool _STLP_CALL stlp_in_range(const _Iterator& _It,
  214. const _Iterator& __i1, const _Iterator& __i2)
  215. { return __valid_range(__i1,_It) && __valid_range(_It,__i2); }
  216. template <class _Iterator>
  217. inline bool _STLP_CALL stlp_in_range(const _Iterator& __first, const _Iterator& __last,
  218. const _Iterator& __start, const _Iterator& __finish)
  219. { return __valid_range(__first,__last) && __valid_range(__start,__first) && __valid_range(__last,__finish); }
  220. //==========================================================
  221. class _STLP_CLASS_DECLSPEC __owned_link {
  222. public:
  223. // Note: This and the following special defines for compiling under Windows CE under ARM
  224. // is needed for correctly using _STLP_DEBUG mode. This comes from a bug in the ARM
  225. // compiler where checked iterators that are passed by value call _M_attach with the wrong
  226. // this pointer and calling _M_detach can't find the correct pointer to the __owned_link.
  227. // This is circumvented by managing a _M_self pointer that points to the correct value.
  228. // Ugly but works.
  229. #if defined(_STLP_WCE) && defined(_ARM_)
  230. __owned_link() : _M_self(this), _M_owner(0) {}
  231. __owned_link(const __owned_list* __c) : _M_self(this), _M_owner(0), _M_next(0)
  232. { __stl_debugger::_M_attach(__CONST_CAST(__owned_list*,__c), this); }
  233. __owned_link(const __owned_link& __rhs): _M_self(this), _M_owner(0)
  234. { __stl_debugger::_M_attach(__CONST_CAST(__owned_list*,__rhs._M_owner), this); }
  235. #else
  236. __owned_link() : _M_owner(0) {}
  237. __owned_link(const __owned_list* __c) : _M_owner(0), _M_next(0)
  238. { __stl_debugger::_M_attach(__CONST_CAST(__owned_list*,__c), this); }
  239. __owned_link(const __owned_link& __rhs): _M_owner(0)
  240. { __stl_debugger::_M_attach(__CONST_CAST(__owned_list*,__rhs._M_owner), this); }
  241. #endif
  242. __owned_link& operator=(const __owned_link& __rhs) {
  243. __owned_list* __new_owner = __CONST_CAST(__owned_list*,__rhs._M_owner);
  244. __owned_list* __old_owner = _M_owner;
  245. if ( __old_owner != __new_owner ) {
  246. __stl_debugger::_M_detach(__old_owner, this);
  247. __stl_debugger::_M_attach(__new_owner, this);
  248. }
  249. return *this;
  250. }
  251. #if defined(_STLP_WCE) && defined(_ARM_)
  252. ~__owned_link() {
  253. __stl_debugger::_M_detach(_M_owner, _M_self);
  254. _Invalidate();
  255. }
  256. #else
  257. ~__owned_link() {
  258. __stl_debugger::_M_detach(_M_owner, this);
  259. _Invalidate();
  260. }
  261. #endif
  262. const __owned_list* _Owner() const { return _M_owner; }
  263. __owned_list* _Owner() { return _M_owner; }
  264. void _Set_owner(const __owned_list* __o) { _M_owner= __CONST_CAST(__owned_list*,__o); }
  265. bool _Valid() const { return _M_owner != 0; }
  266. void _Invalidate() { _M_owner = 0; _M_next = 0; }
  267. void _Link_to_self() { _M_next = 0; }
  268. __owned_link* _Next() { return _M_next; }
  269. const __owned_link* _Next() const { return _M_next; }
  270. public:
  271. #if defined(_STLP_WCE) && defined(_ARM_)
  272. __owned_link* _M_self;
  273. #endif
  274. __owned_list* _M_owner;
  275. __owned_link* _M_next;
  276. };
  277. class _STLP_CLASS_DECLSPEC __owned_list {
  278. public:
  279. __owned_list(void* __o) {
  280. // fprintf(stderr, "__owned_list(): %p\n",(void*)this);
  281. _M_node._M_owner = __REINTERPRET_CAST(__owned_list*,__o);
  282. _M_node._M_next = 0;
  283. }
  284. ~__owned_list() {
  285. // fprintf(stderr, "~__owned_list(): %p\n",(void*)this);
  286. _Invalidate_all();
  287. // that prevents detach
  288. _M_node._Invalidate();
  289. }
  290. const void* _Owner() const { return (const void*)_M_node._M_owner; }
  291. void* _Owner() { return (void*)_M_node._M_owner; }
  292. bool _Valid() const { return _M_node._M_owner != 0; }
  293. void _Invalidate() { _M_node._M_owner = 0; }
  294. __owned_link* _First() { return _M_node._Next(); }
  295. __owned_link* _Last() { return 0 ; }
  296. const __owned_link* _First() const { return (__owned_link*)_M_node._M_next; }
  297. const __owned_link* _Last() const { return 0 ;}
  298. void _Verify() const { __stl_debugger::_Verify(this); }
  299. void _Swap_owners(__owned_list& __y) { __stl_debugger::_Swap_owners(*this, __y); }
  300. void _Invalidate_all() { __stl_debugger::_Invalidate_all(this); }
  301. void _Set_owner(__owned_list& __y) { __stl_debugger::_Set_owner(*this, __y); }
  302. mutable __owned_link _M_node;
  303. mutable _STLP_mutex _M_lock;
  304. private:
  305. // should never be called, should be left not implemented,
  306. // but some compilers complain about it ;(
  307. __owned_list(const __owned_list&){}
  308. __owned_list& operator = (const __owned_list&) { return *this; }
  309. friend class __owned_link;
  310. friend class __stl_debug_engine<bool>;
  311. };
  312. //==========================================================
  313. // forward declaratioins
  314. template <class _Iterator>
  315. bool _STLP_CALL __check_range(const _Iterator&, const _Iterator&);
  316. template <class _Iterator>
  317. bool _STLP_CALL __check_range(const _Iterator&,
  318. const _Iterator&, const _Iterator&);
  319. template <class _Iterator>
  320. bool _STLP_CALL __check_range(const _Iterator&, const _Iterator& ,
  321. const _Iterator&, const _Iterator& );
  322. template <class _Tp>
  323. bool _STLP_CALL __check_ptr_range(const _Tp*, const _Tp*);
  324. template <class _Iterator>
  325. void _STLP_CALL __invalidate_range(const __owned_list* __base,
  326. const _Iterator& __first,
  327. const _Iterator& __last);
  328. template <class _Iterator>
  329. void _STLP_CALL __invalidate_iterator(const __owned_list* __base,
  330. const _Iterator& __it);
  331. template <class _Iterator>
  332. void _STLP_CALL __change_range_owner(const _Iterator& __first,
  333. const _Iterator& __last,
  334. const __owned_list* __dst);
  335. template <class _Iterator>
  336. void _STLP_CALL __change_ite_owner(const _Iterator& __it,
  337. const __owned_list* __dst);
  338. //============================================================
  339. inline bool _STLP_CALL
  340. __check_same_owner(const __owned_link& __i1, const __owned_link& __i2)
  341. { return __stl_debugger::_Check_same_owner(__i1,__i2); }
  342. inline bool _STLP_CALL
  343. __check_same_or_null_owner(const __owned_link& __i1, const __owned_link& __i2)
  344. { return __stl_debugger::_Check_same_or_null_owner(__i1,__i2); }
  345. template <class _Iterator>
  346. inline bool _STLP_CALL __check_if_owner( const __owned_list* __owner,
  347. const _Iterator& __it)
  348. { return __stl_debugger::_Check_if_owner(__owner, (const __owned_link&)__it); }
  349. template <class _Iterator>
  350. inline bool _STLP_CALL __check_if_not_owner( const __owned_list* __owner,
  351. const _Iterator& __it)
  352. { return __stl_debugger::_Check_if_not_owner(__owner, (const __owned_link&)__it); }
  353. _STLP_MOVE_TO_STD_NAMESPACE
  354. _STLP_END_NAMESPACE
  355. #else
  356. # define _STLP_VERBOSE_ASSERT(expr, diagnostic)
  357. # define _STLP_DEBUG_CHECK(expr)
  358. #endif /* _STLP_DEBUG */
  359. #if defined (_STLP_ASSERTIONS)
  360. # if !defined (_STLP_ASSERT_MSG_TRAILER)
  361. # define _STLP_ASSERT_MSG_TRAILER
  362. # endif
  363. // dwa 12/30/98 - if _STLP_DEBUG_MESSAGE is defined, the user can supply own definition.
  364. # if !defined (_STLP_DEBUG_MESSAGE)
  365. # define __stl_debug_message __stl_debugger::_Message
  366. # else
  367. extern void __stl_debug_message(const char * format_str, ...);
  368. # endif
  369. // fbp: if _STLP_DEBUG_TERMINATE is defined, the user can supply own definition.
  370. # if !defined (_STLP_DEBUG_TERMINATE)
  371. # define __stl_debug_terminate __stl_debugger::_Terminate
  372. # else
  373. extern void __stl_debug_terminate();
  374. # endif
  375. #endif
  376. #if defined (_STLP_ASSERTIONS) && !defined (_STLP_LINK_TIME_INSTANTIATION)
  377. # include <stl/debug/_debug.c>
  378. #endif
  379. #endif /* DEBUG_H */
  380. // Local Variables:
  381. // mode:C++
  382. // End: