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"
38 // define own ones, independent of OSL_DEBUG_LEVEL:
39 #define DEBUGBASE_ENSURE_(c, f, l, m) \
42 if (!(c) && _OSL_GLOBAL osl_assertFailedLine(f, l, m)) \
43 _OSL_GLOBAL osl_breakDebug(); \
45 #define DEBUGBASE_ENSURE(c, m) DEBUGBASE_ENSURE_(c, OSL_THIS_FILE, __LINE__, m)
49 typedef std::vector
<rtl::OString
, rtl::Allocator
<rtl::OString
> > OStringVec
;
51 struct StaticDebugBaseAddressFilter
52 : rtl::StaticWithInit
<OStringVec
, StaticDebugBaseAddressFilter
> {
53 OStringVec
operator()() const {
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
);
63 vec
.push_back( rtl::OUStringToOString(
64 str
.getToken( 0, ';', nIndex
),
65 RTL_TEXTENCODING_ASCII_US
) );
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
> {};
84 // These functions presumably should not be extern "C", but changing
85 // that would break binary compatibility.
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"
95 osl::Mutex
& SAL_CALL
osl_detail_ObjectRegistry_getMutex()
98 return DebugBaseMutex::get();
102 #pragma clang diagnostic pop
106 bool SAL_CALL
osl_detail_ObjectRegistry_storeAddresses( char const* pName
)
109 OStringVec
const& rVec
= StaticDebugBaseAddressFilter::get();
113 rtl::OString
const& rFirst
= rVec
[0];
114 if (rtl_str_compare_WithLength( rFirst
.getStr(), rFirst
.getLength(),
115 RTL_CONSTASCII_STRINGPARAM("all") ) == 0)
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
)
127 if (rData
.m_bStoreAddresses
)
128 nSize
= rData
.m_addresses
.size();
130 nSize
= static_cast<std::size_t>(rData
.m_nCount
);
132 bool const bRet
= (nSize
== nExpected
);
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() );
146 void SAL_CALL
osl_detail_ObjectRegistry_registerObject(
147 osl::detail::ObjectRegistryData
& rData
, void const* pObj
)
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
);
158 osl_incrementInterlockedCount(&rData
.m_nCount
);
162 void SAL_CALL
osl_detail_ObjectRegistry_revokeObject(
163 osl::detail::ObjectRegistryData
& rData
, void const* pObj
)
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
);
173 osl_decrementInterlockedCount(&rData
.m_nCount
);
179 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */