ossrv_pub/debug/inc/stdapis/stlport/stl/debug/_debug.h
changeset 31 ce057bb09d0b
child 45 4b03adbd26ca
equal deleted inserted replaced
30:e20de85af2ee 31:ce057bb09d0b
       
     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 
       
    20 #ifndef _STLP_DEBUG_H
       
    21 # define _STLP_DEBUG_H
       
    22 
       
    23 # if defined (_STLP_ASSERTIONS) || defined (_STLP_DEBUG)
       
    24 
       
    25 #ifndef _STLP_CONFIG_H
       
    26 # include <stl/_config.h>
       
    27 #endif
       
    28 
       
    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
       
    32 # endif
       
    33 
       
    34 # if !defined(_STLP_FILE__)
       
    35 #   define _STLP_FILE__ __FILE__
       
    36 # endif
       
    37  
       
    38 enum {
       
    39   _StlFormat_ERROR_RETURN,
       
    40   _StlFormat_ASSERTION_FAILURE,
       
    41   _StlFormat_VERBOSE_ASSERTION_FAILURE,
       
    42   _StlMsg_INVALID_ARGUMENT,
       
    43   _StlMsg_INVALID_CONTAINER,
       
    44   _StlMsg_EMPTY_CONTAINER,
       
    45   _StlMsg_ERASE_PAST_THE_END,
       
    46   _StlMsg_OUT_OF_BOUNDS,
       
    47   _StlMsg_NOT_OWNER,
       
    48   _StlMsg_INVALID_ITERATOR,
       
    49   _StlMsg_INVALID_LEFTHAND_ITERATOR,
       
    50   _StlMsg_INVALID_RIGHTHAND_ITERATOR,
       
    51   _StlMsg_DIFFERENT_OWNERS     ,
       
    52   _StlMsg_NOT_DEREFERENCEABLE  ,
       
    53   _StlMsg_INVALID_RANGE        ,
       
    54   _StlMsg_NOT_IN_RANGE_1       ,
       
    55   _StlMsg_NOT_IN_RANGE_2       ,
       
    56   _StlMsg_INVALID_ADVANCE      ,
       
    57   _StlMsg_SINGULAR_ITERATOR    ,
       
    58   // debug alloc messages
       
    59   _StlMsg_DBA_DELETED_TWICE    ,
       
    60   _StlMsg_DBA_NEVER_ALLOCATED  ,
       
    61   _StlMsg_DBA_TYPE_MISMATCH    ,
       
    62   _StlMsg_DBA_SIZE_MISMATCH    ,
       
    63   _StlMsg_DBA_UNDERRUN         ,
       
    64   _StlMsg_DBA_OVERRUN          ,
       
    65   // auto_ptr messages
       
    66   _StlMsg_AUTO_PTR_NULL    ,
       
    67   _StlMsg_UNKNOWN
       
    68   /* _StlMsg_MAX */
       
    69 };
       
    70 
       
    71 /* have to hardcode that ;() */
       
    72 # define _StlMsg_MAX 27
       
    73 
       
    74 _STLP_BEGIN_NAMESPACE
       
    75 
       
    76 // This class is unique (not inherited from exception),
       
    77 // to disallow catch in anything but (...)
       
    78 struct __stl_debug_exception {
       
    79   // no members
       
    80 };
       
    81 
       
    82 class _STLP_CLASS_DECLSPEC __owned_link;
       
    83 class _STLP_CLASS_DECLSPEC __owned_list;
       
    84 
       
    85 template <class _Dummy>
       
    86 struct __stl_debug_engine {
       
    87 
       
    88   // Basic routine to report any debug message
       
    89   // Use _STLP_DEBUG_MESSAGE to override
       
    90   static void _STLP_CALL _Message(const char * format_str, ...);
       
    91 
       
    92   // Micsellanous function to report indexed error message
       
    93   static void _STLP_CALL  _IndexedError(int __ind, const char* __f, int __l);
       
    94 
       
    95   // Basic assertion report mechanism.
       
    96   // Reports failed assertion via __stl_debug_message and calls _Terminate
       
    97   // if _STLP_DEBUG_TERMINATE is specified, calls __stl_debug_terminate instead
       
    98   static void _STLP_CALL  _Assert(const char* __expr, const char* __f, int __l);
       
    99 
       
   100   // The same, with additional diagnostics
       
   101   static void _STLP_CALL  _VerboseAssert(const char* __expr, int __error_ind, const char* __f, int __l);
       
   102 
       
   103   // If exceptions are present, sends unique exception
       
   104   // If not, calls _STLP_ABORT() to terminate
       
   105   // Use _STLP_DEBUG_TERMINATE to override
       
   106   static void _STLP_CALL  _Terminate();
       
   107 
       
   108 # ifdef _STLP_DEBUG
       
   109 
       
   110   // owned_list/link delegate non-inline functions here
       
   111 
       
   112   static bool _STLP_CALL  _Check_same_owner( const __owned_link& __i1, 
       
   113                                              const __owned_link& __i2);
       
   114   static bool _STLP_CALL  _Check_same_owner_or_null( const __owned_link& __i1, 
       
   115                                                      const __owned_link& __i2);
       
   116   static bool  _STLP_CALL _Check_if_owner( const __owned_list*, const __owned_link&);
       
   117 
       
   118   static void _STLP_CALL  _Verify(const __owned_list*);
       
   119   
       
   120   static void _STLP_CALL  _Swap_owners(__owned_list&, __owned_list& /*, bool __swap_roots */ );
       
   121  
       
   122   static void _STLP_CALL  _Invalidate_all(__owned_list*);
       
   123   
       
   124   static void _STLP_CALL  _Stamp_all(__owned_list*, __owned_list*);
       
   125 
       
   126   static void _STLP_CALL  _M_detach(__owned_list*, __owned_link*);
       
   127 
       
   128   static void _STLP_CALL  _M_attach(__owned_list*, __owned_link*);
       
   129 
       
   130   // accessor : check and get pointer to the container
       
   131   static void* _STLP_CALL  _Get_container_ptr(const __owned_link*);
       
   132 # endif /* _STLP_DEBUG */
       
   133 
       
   134   // debug messages and formats
       
   135   static const char* _Message_table[_StlMsg_MAX];
       
   136 };
       
   137 
       
   138 
       
   139 # if defined (_STLP_USE_TEMPLATE_EXPORT)
       
   140 _STLP_EXPORT_TEMPLATE struct _STLP_CLASS_DECLSPEC __stl_debug_engine<bool>;
       
   141 # endif /* _STLP_USE_TEMPLATE_EXPORT */
       
   142 
       
   143 typedef __stl_debug_engine<bool> __stl_debugger;
       
   144 
       
   145 _STLP_END_NAMESPACE
       
   146 
       
   147 #  ifndef _STLP_ASSERT
       
   148 #   define _STLP_ASSERT(expr) \
       
   149      if (!(expr)) {STLPORT::__stl_debugger::_Assert( # expr, _STLP_FILE__, __LINE__);}
       
   150 #  endif
       
   151 
       
   152 # endif /* _STLP_ASSERTIONS || _STLP_DEBUG */
       
   153 
       
   154 
       
   155 // this section is for _STLP_DEBUG only 
       
   156 #if defined ( _STLP_DEBUG )
       
   157 
       
   158 # ifndef _STLP_VERBOSE_ASSERT
       
   159 // fbp : new form not requiring ";"
       
   160 #  define _STLP_VERBOSE_ASSERT(expr,__diag_num) \
       
   161     if (!(expr)) { STLPORT::__stl_debugger::_VerboseAssert\
       
   162                                  ( # expr,  __diag_num, _STLP_FILE__, __LINE__ ); \
       
   163          }
       
   164 # endif
       
   165 
       
   166 #  define _STLP_DEBUG_CHECK(expr) _STLP_ASSERT(expr)
       
   167 #  define _STLP_DEBUG_DO(expr)    expr;
       
   168 
       
   169 # ifndef _STLP_VERBOSE_RETURN
       
   170 #  define _STLP_VERBOSE_RETURN(__expr,__diag_num) if (!(__expr)) { \
       
   171        __stl_debugger::_IndexedError(__diag_num, __FILE__ , __LINE__); \
       
   172        return false; }
       
   173 # endif
       
   174 
       
   175 # ifndef _STLP_VERBOSE_RETURN_0
       
   176 #  define _STLP_VERBOSE_RETURN_0(__expr,__diag_num) if (!(__expr)) { \
       
   177        __stl_debugger::_IndexedError(__diag_num, __FILE__ , __LINE__); \
       
   178        return 0; }
       
   179 # endif
       
   180 
       
   181 #if ! defined (_STLP_INTERNAL_THREADS_H)
       
   182 # include <stl/_threads.h>
       
   183 #endif
       
   184 
       
   185 #ifndef _STLP_INTERNAL_ITERATOR_BASE_H
       
   186 # include <stl/_iterator_base.h>
       
   187 #endif
       
   188 
       
   189 _STLP_BEGIN_NAMESPACE
       
   190 
       
   191 //=============================================================
       
   192 template <class _Iterator>
       
   193 inline bool  _STLP_CALL __valid_range(const _Iterator& __i1 ,const _Iterator& __i2, 
       
   194                                       const random_access_iterator_tag&) { 
       
   195     return (__i1< __i2) || (__i1 == __i2);
       
   196 }
       
   197 
       
   198 template <class _Iterator>
       
   199 inline bool  _STLP_CALL __valid_range(const _Iterator& __i1 ,const _Iterator& __i2,
       
   200                                       const bidirectional_iterator_tag&) { 
       
   201     // check if comparable
       
   202     bool __dummy(__i1==__i2);
       
   203     return (__dummy==__dummy); 
       
   204 }
       
   205 
       
   206 template <class _Iterator>
       
   207 inline bool  _STLP_CALL __valid_range(const _Iterator& __i1 ,const _Iterator& __i2, const forward_iterator_tag&) { 
       
   208     // check if comparable
       
   209     bool __dummy(__i1==__i2);
       
   210     return (__dummy==__dummy); 
       
   211 }
       
   212 
       
   213 template <class _Iterator>
       
   214 inline bool  _STLP_CALL __valid_range(const _Iterator&,const _Iterator&, const input_iterator_tag&) { 
       
   215     return true; 
       
   216 }
       
   217 
       
   218 template <class _Iterator>
       
   219 inline bool  _STLP_CALL __valid_range(const _Iterator&,const _Iterator&, const output_iterator_tag&) { 
       
   220     return true; 
       
   221 }
       
   222 
       
   223 template <class _Iterator>
       
   224 inline bool  _STLP_CALL __valid_range(const _Iterator& __i1, const _Iterator& __i2) { 
       
   225     return __valid_range(__i1,__i2,_STLP_ITERATOR_CATEGORY(__i1, _Iterator));
       
   226 }
       
   227 
       
   228 // Note : that means in range [i1, i2].
       
   229 template <class _Iterator>
       
   230 inline bool  _STLP_CALL __in_range(const _Iterator& _It, const _Iterator& __i1,
       
   231                                    const _Iterator& __i2) { 
       
   232     return __valid_range(__i1,_It,_STLP_ITERATOR_CATEGORY(__i1, _Iterator)) && 
       
   233         __valid_range(_It,__i2,_STLP_ITERATOR_CATEGORY(_It, _Iterator));
       
   234 }
       
   235 
       
   236 template <class _Iterator>
       
   237 inline bool  _STLP_CALL __in_range(const _Iterator& __first, const _Iterator& __last, 
       
   238                                    const _Iterator& __start, const _Iterator& __finish) { 
       
   239     return __valid_range(__first,__last,_STLP_ITERATOR_CATEGORY(__first, _Iterator)) &&
       
   240         __valid_range(__start,__first,_STLP_ITERATOR_CATEGORY(__first, _Iterator)) && 
       
   241         __valid_range(__last,__finish,_STLP_ITERATOR_CATEGORY(__last, _Iterator));
       
   242 }
       
   243 
       
   244 //==========================================================
       
   245 
       
   246 
       
   247 class _STLP_CLASS_DECLSPEC __owned_link {
       
   248 public:
       
   249 
       
   250   __owned_link() : _M_owner(0) {}
       
   251   __owned_link(const __owned_list* __c) : _M_owner(0), _M_next(0) {
       
   252     __stl_debugger::_M_attach(__CONST_CAST(__owned_list*,__c), this);
       
   253   }
       
   254   __owned_link(const __owned_link& __rhs): _M_owner(0) {
       
   255     __stl_debugger::_M_attach(__CONST_CAST(__owned_list*,__rhs._M_owner), this);
       
   256   }
       
   257   __owned_link& operator=(const __owned_link& __rhs) {
       
   258     __owned_list* __new_owner = __CONST_CAST(__owned_list*,__rhs._M_owner);
       
   259     __owned_list* __old_owner = _M_owner;
       
   260     if ( __old_owner != __new_owner ) {
       
   261       __stl_debugger::_M_detach(__old_owner, this);
       
   262       __stl_debugger::_M_attach(__new_owner, this);
       
   263     }
       
   264     return *this;
       
   265   }
       
   266   ~__owned_link() {
       
   267     __stl_debugger::_M_detach(_M_owner, this);
       
   268     _Invalidate();
       
   269   }
       
   270   
       
   271   const __owned_list* _Owner() const { 
       
   272     return _M_owner; 
       
   273   }
       
   274   __owned_list* _Owner() { 
       
   275     return _M_owner; 
       
   276   }
       
   277   void _Set_owner(const __owned_list* __o)  { 
       
   278     _M_owner= __CONST_CAST(__owned_list*,__o); 
       
   279   }
       
   280   bool  _Valid() const { 
       
   281     return _M_owner !=0; 
       
   282   }
       
   283 
       
   284   void _Invalidate() { _M_owner=0; _M_next = 0; }
       
   285   void _Link_to_self() { _M_next= 0; }
       
   286 
       
   287   __owned_link* _Next() { return _M_next; }
       
   288   const __owned_link* _Next() const { return _M_next; }
       
   289 
       
   290 public:
       
   291   __owned_list* _M_owner;
       
   292   __owned_link* _M_next;
       
   293 };
       
   294 
       
   295 
       
   296 class _STLP_CLASS_DECLSPEC __owned_list {
       
   297 public:
       
   298   __owned_list(const void* __o) {
       
   299     //    fprintf(stderr, "__owned_list(): %p\n",(void*)this);
       
   300     _M_node._M_owner = __CONST_CAST(__owned_list*, __REINTERPRET_CAST(const __owned_list*,__o)); 
       
   301     _M_node._M_next=0;
       
   302   }
       
   303   ~__owned_list() {
       
   304     //    fprintf(stderr, "~__owned_list(): %p\n",(void*)this);
       
   305     _Invalidate_all();
       
   306     // that prevents detach
       
   307     _M_node._Invalidate();
       
   308   }
       
   309   const void* _Owner() const { 
       
   310     return (const void*)_M_node._M_owner; 
       
   311   }
       
   312   void* _Owner() { 
       
   313     return (void*)_M_node._M_owner; 
       
   314   }
       
   315   bool  _Valid() const { 
       
   316     return _M_node._M_owner!=0; 
       
   317   }
       
   318   void _Invalidate() { _M_node._M_owner=0; }
       
   319   
       
   320   __owned_link* _First() { return _M_node._Next(); }
       
   321   __owned_link* _Last() { return 0 ; }
       
   322 
       
   323   const __owned_link* _First() const { return (__owned_link*)_M_node._M_next; }
       
   324   const __owned_link* _Last() const { return 0 ;}
       
   325   
       
   326   void _Verify() const {
       
   327     __stl_debugger::_Verify(this); 
       
   328   }
       
   329   
       
   330   void _Swap_owners(__owned_list& __y) { 
       
   331     __stl_debugger::_Swap_owners(*this, __y); 
       
   332   }
       
   333  
       
   334   void _Invalidate_all() { 
       
   335     __stl_debugger::_Invalidate_all(this);
       
   336   }
       
   337   
       
   338   mutable __owned_link              _M_node; 
       
   339   mutable _STLP_mutex            _M_lock;
       
   340   
       
   341 private:
       
   342   // should never be called, should be left undefined,
       
   343   // but some compilers complain about it ;(
       
   344   __owned_list(const __owned_list&){}
       
   345   void operator=(const __owned_list&) {}
       
   346 
       
   347   friend class __owned_link;
       
   348   friend struct __stl_debug_engine<bool>;
       
   349 };
       
   350 
       
   351 
       
   352 //==========================================================
       
   353 
       
   354 // forward declaratioins
       
   355 
       
   356 template <class _Iterator>
       
   357 bool  _STLP_CALL __check_range(const _Iterator&, const _Iterator&);
       
   358 template <class _Iterator>
       
   359 bool _STLP_CALL  __check_range(const _Iterator&, 
       
   360                                const _Iterator&, const _Iterator&);
       
   361 template <class _Iterator>
       
   362 bool _STLP_CALL  __check_range(const _Iterator&, const _Iterator& , 
       
   363                                const _Iterator&, const _Iterator& );
       
   364 
       
   365 template <class _Iterator>
       
   366 void _STLP_CALL  __invalidate_range(const __owned_list* __base, 
       
   367                                     const _Iterator& __first,
       
   368                                     const _Iterator& __last);
       
   369 
       
   370 template <class _Iterator>
       
   371 void  _STLP_CALL __invalidate_iterator(const __owned_list* __base, 
       
   372                                        const _Iterator& __it);
       
   373 
       
   374 //============================================================
       
   375 
       
   376 inline bool _STLP_CALL 
       
   377 __check_same_owner( const __owned_link& __i1, const __owned_link& __i2) {
       
   378   return __stl_debugger::_Check_same_owner(__i1,__i2);
       
   379 }
       
   380 inline bool _STLP_CALL  
       
   381 __check_same_owner_or_null( const __owned_link& __i1, const __owned_link& __i2) {
       
   382   return __stl_debugger::_Check_same_owner_or_null(__i1,__i2);
       
   383 }
       
   384 
       
   385 template <class _Iterator>
       
   386 inline bool _STLP_CALL  __check_if_owner( const __owned_list* __owner, 
       
   387                                           const _Iterator& __it) {
       
   388   return __stl_debugger::_Check_if_owner(__owner, (const __owned_link&)__it);
       
   389 }
       
   390 
       
   391 _STLP_END_NAMESPACE
       
   392 
       
   393 # endif /* _STLP_DEBUG */
       
   394 
       
   395 #if defined ( _STLP_ASSERTIONS )
       
   396 
       
   397 # ifndef _STLP_ASSERT_MSG_TRAILER
       
   398 #  define _STLP_ASSERT_MSG_TRAILER
       
   399 # endif
       
   400 
       
   401 // dwa 12/30/98 - if _STLP_DEBUG_MESSAGE is defined, the user can supply own definition.
       
   402 # if !defined( _STLP_DEBUG_MESSAGE )
       
   403 #   define __stl_debug_message __stl_debugger::_Message
       
   404 # else
       
   405     extern  void __stl_debug_message(const char * format_str, ...);
       
   406 # endif
       
   407 
       
   408 // fbp: if _STLP_DEBUG_TERMINATE is defined, the user can supply own definition.
       
   409 # if !defined( _STLP_DEBUG_TERMINATE )
       
   410 #   define __stl_debug_terminate __stl_debugger::_Terminate
       
   411 # else
       
   412     extern  void __stl_debug_terminate(void);
       
   413 # endif
       
   414 
       
   415 #endif
       
   416 
       
   417 # if !defined (_STLP_LINK_TIME_INSTANTIATION)
       
   418 #  include <stl/debug/_debug.c>
       
   419 # endif
       
   420 
       
   421 #endif /* DEBUG_H */
       
   422 
       
   423 // Local Variables:
       
   424 // mode:C++
       
   425 // End:
       
   426