LibreOffice
LibreOffice 4.3 SDK C/C++ API Reference
diagnose.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 #ifndef INCLUDED_OSL_DIAGNOSE_HXX
20 #define INCLUDED_OSL_DIAGNOSE_HXX
21 
23 
24 #include <sal/config.h>
25 
26 #include <functional>
27 #include <typeinfo>
28 
29 #include <config_global.h>
30 
31 #if !HAVE_CXX11
32 #define BOOST_NO_0X_HDR_TYPEINDEX
33 #endif
34 #include <boost/unordered_set.hpp>
35 #include <osl/diagnose.h>
36 #include <osl/interlck.h>
37 #include <osl/mutex.hxx>
38 #include <rtl/instance.hxx>
39 #include <sal/log.hxx>
40 #include <sal/saldllapi.h>
41 #include <sal/types.h>
42 
43 namespace osl {
44 namespace detail {
45 
46 struct ObjectRegistryData;
47 
48 } // namespace detail
49 } // namespace osl
50 
51 extern "C" {
52 
53 SAL_DLLPUBLIC bool SAL_CALL osl_detail_ObjectRegistry_storeAddresses(
54  char const* pName )
56 
57 SAL_DLLPUBLIC bool SAL_CALL osl_detail_ObjectRegistry_checkObjectCount(
58  ::osl::detail::ObjectRegistryData const& rData, ::std::size_t nExpected )
60 
61 SAL_DLLPUBLIC void SAL_CALL osl_detail_ObjectRegistry_registerObject(
62  ::osl::detail::ObjectRegistryData & rData, void const* pObj )
64 
65 SAL_DLLPUBLIC void SAL_CALL osl_detail_ObjectRegistry_revokeObject(
66  ::osl::detail::ObjectRegistryData & rData, void const* pObj )
68 
69 // These functions presumably should not be extern "C", but changing
70 // that would break binary compatibility.
71 #ifdef __clang__
72 #pragma clang diagnostic push
73 // Guard against slightly older clang versions that don't have
74 // -Wreturn-type-c-linkage...
75 #pragma clang diagnostic ignored "-Wunknown-pragmas"
76 #pragma clang diagnostic ignored "-Wreturn-type-c-linkage"
77 #endif
78 
79 SAL_DLLPUBLIC ::osl::Mutex & SAL_CALL osl_detail_ObjectRegistry_getMutex()
81 
82 #ifdef __clang__
83 #pragma clang diagnostic pop
84 #endif
85 
86 } // extern "C"
87 
88 namespace osl {
89 
90 namespace detail {
91 
92 struct VoidPtrHash : ::std::unary_function<void const*, ::std::size_t> {
93  ::std::size_t operator()( void const* p ) const {
94  ::std::size_t const d = static_cast< ::std::size_t >(
95  reinterpret_cast< ::std::ptrdiff_t >(p) );
96  return d + (d >> 3);
97  }
98 };
99 
100 typedef ::boost::unordered_set<void const*, VoidPtrHash, ::std::equal_to<void const*> > VoidPointerSet;
101 
102 struct ObjectRegistryData {
103  ObjectRegistryData( ::std::type_info const& rTypeInfo )
104  : m_pName(rTypeInfo.name()), m_nCount(0), m_addresses(),
105  m_bStoreAddresses(osl_detail_ObjectRegistry_storeAddresses(m_pName)){}
106 
107  char const* const m_pName;
108  oslInterlockedCount m_nCount;
109  VoidPointerSet m_addresses;
110  bool const m_bStoreAddresses;
111 };
112 
113 template <typename T>
114 class ObjectRegistry
115 {
116 public:
117  ObjectRegistry() : m_data( typeid(T) ) {}
118  ~ObjectRegistry() { checkObjectCount(0); }
119 
120  bool checkObjectCount( ::std::size_t nExpected ) const {
121  bool const bRet = osl_detail_ObjectRegistry_checkObjectCount(
122  m_data, nExpected );
123  if (!bRet && m_data.m_bStoreAddresses) {
124  MutexGuard const guard( osl_detail_ObjectRegistry_getMutex() );
125  // following loop is for debugging purposes, iterating over map:
126  VoidPointerSet::const_iterator iPos(m_data.m_addresses.begin());
127  VoidPointerSet::const_iterator const iEnd(m_data.m_addresses.end());
128  for ( ; iPos != iEnd; ++iPos ) {
129  SAL_WARN_IF( *iPos == 0, "sal.debug", "null pointer" );
130  }
131  }
132  return bRet;
133  }
134 
135  void registerObject( void const* pObj ) {
136  osl_detail_ObjectRegistry_registerObject(m_data, pObj);
137  }
138 
139  void revokeObject( void const* pObj ) {
140  osl_detail_ObjectRegistry_revokeObject(m_data, pObj);
141  }
142 
143 private:
144  // not impl:
145  ObjectRegistry( ObjectRegistry const& );
146  ObjectRegistry const& operator=( ObjectRegistry const& );
147 
148  ObjectRegistryData m_data;
149 };
150 
151 } // namespace detail
152 
174 template <typename InheritingClassT>
175 class DebugBase
176 {
177 public:
178 #if OSL_DEBUG_LEVEL <= 0
179  static bool checkObjectCount( ::std::size_t = 0 ) { return true; }
180 #else // OSL_DEBUG_LEVEL > 0
181 
184  static bool checkObjectCount( ::std::size_t nExpected = 0 ) {
185  return StaticObjectRegistry::get().checkObjectCount(nExpected);
186  }
187 
188 protected:
189  DebugBase() {
190  StaticObjectRegistry::get().registerObject( this );
191  }
192  ~DebugBase() {
193  StaticObjectRegistry::get().revokeObject( this );
194  }
195 
196 private:
197  struct StaticObjectRegistry
198  : ::rtl::Static<detail::ObjectRegistry<InheritingClassT>,
199  StaticObjectRegistry> {};
200 #endif
201 };
202 
203 } // namespace osl
204 
206 
207 #endif // ! defined( INCLUDED_OSL_DIAGNOSE_HXX)
208 
209 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int32 oslInterlockedCount
Definition: interlck.h:32
Helper base class for a late-initialized (default-constructed) static variable, implementing the doub...
Definition: instance.hxx:399
Definition: conditn.hxx:30
#define SAL_WARN_IF(condition, area, stream)
Produce warning entry from stream in the given log area if condition is true.
Definition: log.hxx:304
#define SAL_DLLPUBLIC
Definition: saldllapi.h:30
#define SAL_THROW_EXTERN_C()
Nothrow specification for C functions.
Definition: types.h:367
Guard< Mutex > MutexGuard
Definition: mutex.hxx:238