1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 #if ! defined(OSL_DIAGNOSE_HXX_INCLUDED) 24 #define OSL_DIAGNOSE_HXX_INCLUDED 25 26 #include "osl/diagnose.h" 27 #include "osl/interlck.h" 28 #include "osl/mutex.hxx" 29 #include "rtl/instance.hxx" 30 #include <unordered_set> 31 #include <functional> 32 #include <typeinfo> 33 34 namespace osl { 35 /// @internal 36 namespace detail { 37 38 struct ObjectRegistryData; 39 40 } // namespace detail 41 } // namespace osl 42 43 extern "C" { 44 45 /** @internal */ 46 bool SAL_CALL osl_detail_ObjectRegistry_storeAddresses( char const* pName ) 47 SAL_THROW_EXTERN_C(); 48 49 /** @internal */ 50 bool SAL_CALL osl_detail_ObjectRegistry_checkObjectCount( 51 ::osl::detail::ObjectRegistryData const& rData, ::std::size_t nExpected ) 52 SAL_THROW_EXTERN_C(); 53 54 /** @internal */ 55 void SAL_CALL osl_detail_ObjectRegistry_registerObject( 56 ::osl::detail::ObjectRegistryData & rData, void const* pObj ) 57 SAL_THROW_EXTERN_C(); 58 59 /** @internal */ 60 void SAL_CALL osl_detail_ObjectRegistry_revokeObject( 61 ::osl::detail::ObjectRegistryData & rData, void const* pObj ) 62 SAL_THROW_EXTERN_C(); 63 64 /** @internal */ 65 ::osl::Mutex & SAL_CALL osl_detail_ObjectRegistry_getMutex() 66 SAL_THROW_EXTERN_C(); 67 68 } // extern "C" 69 70 namespace osl { 71 72 /// @internal 73 namespace detail { 74 75 struct VoidPtrHash { 76 typedef void const* argument_type; 77 typedef ::std::size_t result_type; operator ()osl::detail::VoidPtrHash78 ::std::size_t operator()( void const* p ) const { 79 ::std::size_t const d = static_cast< ::std::size_t >( 80 reinterpret_cast< ::std::ptrdiff_t >(p) ); 81 return d + (d >> 3); 82 } 83 }; 84 85 typedef ::std::unordered_set<void const*, VoidPtrHash, ::std::equal_to<void const*> > VoidPointerSet; 86 87 struct ObjectRegistryData { ObjectRegistryDataosl::detail::ObjectRegistryData88 ObjectRegistryData( ::std::type_info const& rTypeInfo ) 89 : m_pName(rTypeInfo.name()), m_nCount(0), m_addresses(), 90 m_bStoreAddresses(osl_detail_ObjectRegistry_storeAddresses(m_pName)){} 91 92 char const* const m_pName; 93 oslInterlockedCount m_nCount; 94 VoidPointerSet m_addresses; 95 bool const m_bStoreAddresses; 96 }; 97 98 template <typename T> 99 class ObjectRegistry 100 { 101 public: ObjectRegistry()102 ObjectRegistry() : m_data( typeid(T) ) {} ~ObjectRegistry()103 ~ObjectRegistry() { checkObjectCount(0); } 104 checkObjectCount(::std::size_t nExpected) const105 bool checkObjectCount( ::std::size_t nExpected ) const { 106 bool const bRet = osl_detail_ObjectRegistry_checkObjectCount( 107 m_data, nExpected ); 108 if (!bRet && m_data.m_bStoreAddresses) { 109 MutexGuard const guard( osl_detail_ObjectRegistry_getMutex() ); 110 // following loop is for debugging purposes, iterating over map: 111 VoidPointerSet::const_iterator iPos(m_data.m_addresses.begin()); 112 VoidPointerSet::const_iterator const iEnd(m_data.m_addresses.end()); 113 for ( ; iPos != iEnd; ++iPos ) { 114 OSL_ASSERT( *iPos != 0 ); 115 } 116 } 117 return bRet; 118 } 119 registerObject(void const * pObj)120 void registerObject( void const* pObj ) { 121 osl_detail_ObjectRegistry_registerObject(m_data, pObj); 122 } 123 revokeObject(void const * pObj)124 void revokeObject( void const* pObj ) { 125 osl_detail_ObjectRegistry_revokeObject(m_data, pObj); 126 } 127 128 private: 129 // not impl: 130 ObjectRegistry( ObjectRegistry const& ); 131 ObjectRegistry const& operator=( ObjectRegistry const& ); 132 133 ObjectRegistryData m_data; 134 }; 135 136 } // namespace detail 137 138 /** Helper class which indicates leaking object(s) of a particular class in 139 non-pro builds; use e.g. 140 141 <pre> 142 class MyClass : private osl::DebugBase<MyClass> {...}; 143 </pre> 144 145 Using the environment variable 146 147 OSL_DEBUGBASE_STORE_ADDRESSES=MyClass;YourClass;... 148 149 you can specify a ';'-separated list of strings matching to class names 150 (or "all" for all classes), for which DebugBase stores addresses to created 151 objects instead of just counting them. This enables you to iterate over 152 leaking objects in your debugger. 153 154 @tpl InheritingClassT binds the template instance to that class 155 @internal Use at own risk. 156 For now this is just public (yet unpublished) API and may change 157 in the future! 158 */ 159 template <typename InheritingClassT> 160 class DebugBase 161 { 162 public: 163 #if OSL_DEBUG_LEVEL <= 0 checkObjectCount(::std::size_t=0)164 static bool checkObjectCount( ::std::size_t = 0 ) { return true; } 165 #else // OSL_DEBUG_LEVEL > 0 166 /** @return whether the expected number of objects is alive, 167 else this function OSL_ASSERTs 168 */ 169 static bool checkObjectCount( ::std::size_t nExpected = 0 ) { 170 return StaticObjectRegistry::get().checkObjectCount(nExpected); 171 } 172 173 protected: 174 DebugBase() { 175 StaticObjectRegistry::get().registerObject( this ); 176 } 177 ~DebugBase() { 178 StaticObjectRegistry::get().revokeObject( this ); 179 } 180 181 private: 182 struct StaticObjectRegistry 183 : ::rtl::Static<detail::ObjectRegistry<InheritingClassT>, 184 StaticObjectRegistry> {}; 185 #endif 186 }; 187 188 } // namespace osl 189 190 #endif // ! defined(OSL_DIAGNOSE_HXX_INCLUDED) 191