Bump for 3.6-28
[LibreOffice.git] / sal / osl / all / debugbase.cxx
blob6648d53bf4e27186fbd982365a41bf81bd2fa4e5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include "rtl/strbuf.hxx"
31 #include "rtl/string.hxx"
32 #include "rtl/ustring.hxx"
33 #include "osl/process.h"
34 #include "osl/diagnose.hxx"
35 #include "boost/bind.hpp"
36 #include <vector>
38 // define own ones, independent of OSL_DEBUG_LEVEL:
39 #define DEBUGBASE_ENSURE_(c, f, l, m) \
40 do \
41 { \
42 if (!(c) && _OSL_GLOBAL osl_assertFailedLine(f, l, m)) \
43 _OSL_GLOBAL osl_breakDebug(); \
44 } while (0)
45 #define DEBUGBASE_ENSURE(c, m) DEBUGBASE_ENSURE_(c, OSL_THIS_FILE, __LINE__, m)
47 namespace {
49 typedef std::vector<rtl::OString, rtl::Allocator<rtl::OString> > OStringVec;
51 struct StaticDebugBaseAddressFilter
52 : rtl::StaticWithInit<OStringVec, StaticDebugBaseAddressFilter> {
53 OStringVec operator()() const {
54 OStringVec vec;
55 rtl_uString * pStr = 0;
56 rtl::OUString const name(
57 RTL_CONSTASCII_USTRINGPARAM("OSL_DEBUGBASE_STORE_ADDRESSES") );
58 if (osl_getEnvironment( name.pData, &pStr ) == osl_Process_E_None) {
59 rtl::OUString const str(pStr);
60 rtl_uString_release(pStr);
61 sal_Int32 nIndex = 0;
62 do {
63 vec.push_back( rtl::OUStringToOString(
64 str.getToken( 0, ';', nIndex ),
65 RTL_TEXTENCODING_ASCII_US ) );
67 while (nIndex >= 0);
69 return vec;
73 inline bool isSubStr( char const* pStr, rtl::OString const& subStr )
75 return rtl_str_indexOfStr( pStr, subStr.getStr() ) >= 0;
78 struct DebugBaseMutex : ::rtl::Static<osl::Mutex, DebugBaseMutex> {};
80 } // anon namespace
82 extern "C" {
84 // These functions presumably should not be extern "C", but changing
85 // that would break binary compatibility.
86 #if SUPD < 400
87 #ifdef __clang__
88 #pragma clang diagnostic push
89 // Guard against slightly older clang versions that don't have
90 // -Wreturn-type-c-linkage...
91 #pragma clang diagnostic ignored "-Wunknown-pragmas"
92 #pragma clang diagnostic ignored "-Wreturn-type-c-linkage"
93 #endif
94 #endif
95 osl::Mutex & SAL_CALL osl_detail_ObjectRegistry_getMutex()
96 SAL_THROW_EXTERN_C()
98 return DebugBaseMutex::get();
100 #if SUPD < 400
101 #ifdef __clang__
102 #pragma clang diagnostic pop
103 #endif
104 #endif
106 bool SAL_CALL osl_detail_ObjectRegistry_storeAddresses( char const* pName )
107 SAL_THROW_EXTERN_C()
109 OStringVec const& rVec = StaticDebugBaseAddressFilter::get();
110 if (rVec.empty())
111 return false;
112 // check for "all":
113 rtl::OString const& rFirst = rVec[0];
114 if (rtl_str_compare_WithLength( rFirst.getStr(), rFirst.getLength(),
115 RTL_CONSTASCII_STRINGPARAM("all") ) == 0)
116 return true;
117 OStringVec::const_iterator const iEnd( rVec.end() );
118 return std::find_if( rVec.begin(), iEnd,
119 boost::bind( &isSubStr, pName, _1 ) ) != iEnd;
122 bool SAL_CALL osl_detail_ObjectRegistry_checkObjectCount(
123 osl::detail::ObjectRegistryData const& rData, std::size_t nExpected )
124 SAL_THROW_EXTERN_C()
126 std::size_t nSize;
127 if (rData.m_bStoreAddresses)
128 nSize = rData.m_addresses.size();
129 else
130 nSize = static_cast<std::size_t>(rData.m_nCount);
132 bool const bRet = (nSize == nExpected);
133 if (! bRet) {
134 rtl::OStringBuffer buf;
135 buf.append( RTL_CONSTASCII_STRINGPARAM("unexpected number of ") );
136 buf.append( rData.m_pName );
137 buf.append( RTL_CONSTASCII_STRINGPARAM(": ") );
138 buf.append( static_cast<sal_Int64>(nSize) );
139 buf.append("; Expected: ");
140 buf.append( static_cast<sal_Int64>(nExpected) );
141 DEBUGBASE_ENSURE( false, buf.makeStringAndClear().getStr() );
143 return bRet;
146 void SAL_CALL osl_detail_ObjectRegistry_registerObject(
147 osl::detail::ObjectRegistryData & rData, void const* pObj )
148 SAL_THROW_EXTERN_C()
150 if (rData.m_bStoreAddresses) {
151 osl::MutexGuard const guard( osl_detail_ObjectRegistry_getMutex() );
152 std::pair<osl::detail::VoidPointerSet::iterator, bool> const insertion(
153 rData.m_addresses.insert(pObj) );
154 DEBUGBASE_ENSURE( insertion.second, "### insertion failed!?" );
155 static_cast<void>(insertion);
157 else {
158 osl_incrementInterlockedCount(&rData.m_nCount);
162 void SAL_CALL osl_detail_ObjectRegistry_revokeObject(
163 osl::detail::ObjectRegistryData & rData, void const* pObj )
164 SAL_THROW_EXTERN_C()
166 if (rData.m_bStoreAddresses) {
167 osl::MutexGuard const guard( osl_detail_ObjectRegistry_getMutex() );
168 std::size_t const n = rData.m_addresses.erase(pObj);
169 DEBUGBASE_ENSURE( n == 1, "erased more than 1 entry!?" );
170 static_cast<void>(n);
172 else {
173 osl_decrementInterlockedCount(&rData.m_nCount);
177 } // extern "C"
179 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */