1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: diagnose.hxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #if ! defined(OSL_DIAGNOSE_HXX_INCLUDED)
31 #define OSL_DIAGNOSE_HXX_INCLUDED
33 #if ! defined(_OSL_DIAGNOSE_H_)
34 #include "osl/diagnose.h"
36 #if ! defined(_OSL_INTERLOCK_H_)
37 #include "osl/interlck.h"
39 #if ! defined(_OSL_MUTEX_HXX_)
40 #include "osl/mutex.hxx"
42 #if ! defined(INCLUDED_RTL_ALLOCATOR_HXX)
43 #include "rtl/allocator.hxx"
45 #if ! defined(_RTL_INSTANCE_HXX_)
46 #include "rtl/instance.hxx"
56 struct ObjectRegistryData
;
64 bool SAL_CALL
osl_detail_ObjectRegistry_storeAddresses( char const* pName
)
68 bool SAL_CALL
osl_detail_ObjectRegistry_checkObjectCount(
69 ::osl::detail::ObjectRegistryData
const& rData
, ::std::size_t nExpected
)
73 void SAL_CALL
osl_detail_ObjectRegistry_registerObject(
74 ::osl::detail::ObjectRegistryData
& rData
, void const* pObj
)
78 void SAL_CALL
osl_detail_ObjectRegistry_revokeObject(
79 ::osl::detail::ObjectRegistryData
& rData
, void const* pObj
)
83 ::osl::Mutex
& SAL_CALL
osl_detail_ObjectRegistry_getMutex()
93 struct VoidPtrHash
: ::std::unary_function
<void const*, ::std::size_t> {
94 ::std::size_t operator()( void const* p
) const {
95 ::std::size_t const d
= static_cast< ::std::size_t >(
96 reinterpret_cast< ::std::ptrdiff_t >(p
) );
101 typedef ::std::hash_set
<void const*, VoidPtrHash
, ::std::equal_to
<void const*>,
102 ::rtl::Allocator
<void const*> > VoidPointerSet
;
104 struct ObjectRegistryData
{
105 ObjectRegistryData( ::std::type_info
const& rTypeInfo
)
106 : m_pName(rTypeInfo
.name()), m_nCount(0), m_addresses(),
107 m_bStoreAddresses(osl_detail_ObjectRegistry_storeAddresses(m_pName
)){}
109 char const* const m_pName
;
110 oslInterlockedCount m_nCount
;
111 VoidPointerSet m_addresses
;
112 bool const m_bStoreAddresses
;
115 template <typename T
>
119 ObjectRegistry() : m_data( typeid(T
) ) {}
120 ~ObjectRegistry() { checkObjectCount(0); }
122 bool checkObjectCount( ::std::size_t nExpected
) const {
123 bool const bRet
= osl_detail_ObjectRegistry_checkObjectCount(
125 if (!bRet
&& m_data
.m_bStoreAddresses
) {
126 MutexGuard
const guard( osl_detail_ObjectRegistry_getMutex() );
127 // following loop is for debugging purposes, iterating over map:
128 VoidPointerSet::const_iterator
iPos(m_data
.m_addresses
.begin());
129 VoidPointerSet::const_iterator
const iEnd(m_data
.m_addresses
.end());
130 for ( ; iPos
!= iEnd
; ++iPos
) {
131 T
const* pLeakingObj
= static_cast<T
const*>(*iPos
);
132 OSL_ASSERT( pLeakingObj
!= 0 );
133 static_cast<void>(pLeakingObj
);
139 void registerObject( T
const* pObj
) {
140 osl_detail_ObjectRegistry_registerObject(m_data
, pObj
);
143 void revokeObject( T
const* pObj
) {
144 osl_detail_ObjectRegistry_revokeObject(m_data
, pObj
);
149 ObjectRegistry( ObjectRegistry
const& );
150 ObjectRegistry
const& operator=( ObjectRegistry
const& );
152 ObjectRegistryData m_data
;
155 } // namespace detail
157 /** Helper class which indicates leaking object(s) of a particular class in
158 non-pro builds; use e.g.
161 class MyClass : private osl::DebugBase<MyClass> {...};
164 Using the environment variable
166 OSL_DEBUGBASE_STORE_ADDRESSES=MyClass;YourClass;...
168 you can specify a ';'-separated list of strings matching to class names
169 (or "all" for all classes), for which DebugBase stores addresses to created
170 objects instead of just counting them. This enables you to iterate over
171 leaking objects in your debugger.
173 @tpl InheritingClassT binds the template instance to that class
174 @internal Use at own risk.
175 For now this is just public (yet unpublished) API and may change
178 template <typename InheritingClassT
>
182 #if OSL_DEBUG_LEVEL <= 0
183 static bool checkObjectCount( ::std::size_t = 0 ) { return true; }
184 #else // OSL_DEBUG_LEVEL > 0
185 /** @return whether the expected number of objects is alive,
186 else this function OSL_ASSERTs
188 static bool checkObjectCount( ::std::size_t nExpected
= 0 ) {
189 return StaticObjectRegistry::get().checkObjectCount(nExpected
);
194 StaticObjectRegistry::get().registerObject(
195 static_cast<InheritingClassT
const*>(this) );
198 StaticObjectRegistry::get().revokeObject(
199 static_cast<InheritingClassT
const*>(this) );
203 struct StaticObjectRegistry
204 : ::rtl::Static
<detail::ObjectRegistry
<InheritingClassT
>,
205 StaticObjectRegistry
> {};
211 #endif // ! defined(OSL_DIAGNOSE_HXX_INCLUDED)