merge the formfield patch from ooo-build
[ooovba.git] / sal / inc / osl / diagnose.hxx
blob74f53c2f9e4ccade62af4a4a9a5b1439f8de9f6f
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: diagnose.hxx,v $
10 * $Revision: 1.3 $
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"
35 #endif
36 #if ! defined(_OSL_INTERLOCK_H_)
37 #include "osl/interlck.h"
38 #endif
39 #if ! defined(_OSL_MUTEX_HXX_)
40 #include "osl/mutex.hxx"
41 #endif
42 #if ! defined(INCLUDED_RTL_ALLOCATOR_HXX)
43 #include "rtl/allocator.hxx"
44 #endif
45 #if ! defined(_RTL_INSTANCE_HXX_)
46 #include "rtl/instance.hxx"
47 #endif
48 #include <hash_set>
49 #include <functional>
50 #include <typeinfo>
52 namespace osl {
53 /// @internal
54 namespace detail {
56 struct ObjectRegistryData;
58 } // namespace detail
59 } // namespace osl
61 extern "C" {
63 /** @internal */
64 bool SAL_CALL osl_detail_ObjectRegistry_storeAddresses( char const* pName )
65 SAL_THROW_EXTERN_C();
67 /** @internal */
68 bool SAL_CALL osl_detail_ObjectRegistry_checkObjectCount(
69 ::osl::detail::ObjectRegistryData const& rData, ::std::size_t nExpected )
70 SAL_THROW_EXTERN_C();
72 /** @internal */
73 void SAL_CALL osl_detail_ObjectRegistry_registerObject(
74 ::osl::detail::ObjectRegistryData & rData, void const* pObj )
75 SAL_THROW_EXTERN_C();
77 /** @internal */
78 void SAL_CALL osl_detail_ObjectRegistry_revokeObject(
79 ::osl::detail::ObjectRegistryData & rData, void const* pObj )
80 SAL_THROW_EXTERN_C();
82 /** @internal */
83 ::osl::Mutex & SAL_CALL osl_detail_ObjectRegistry_getMutex()
84 SAL_THROW_EXTERN_C();
86 } // extern "C"
88 namespace osl {
90 /// @internal
91 namespace detail {
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) );
97 return d + (d >> 3);
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>
116 class ObjectRegistry
118 public:
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(
124 m_data, nExpected );
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);
136 return bRet;
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);
147 private:
148 // not impl:
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.
160 <pre>
161 class MyClass : private osl::DebugBase<MyClass> {...};
162 </pre>
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
176 in the future!
178 template <typename InheritingClassT>
179 class DebugBase
181 public:
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);
192 protected:
193 DebugBase() {
194 StaticObjectRegistry::get().registerObject(
195 static_cast<InheritingClassT const*>(this) );
197 ~DebugBase() {
198 StaticObjectRegistry::get().revokeObject(
199 static_cast<InheritingClassT const*>(this) );
202 private:
203 struct StaticObjectRegistry
204 : ::rtl::Static<detail::ObjectRegistry<InheritingClassT>,
205 StaticObjectRegistry> {};
206 #endif
209 } // namespace osl
211 #endif // ! defined(OSL_DIAGNOSE_HXX_INCLUDED)