1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "rtl/strbuf.hxx"
21 #include "rtl/string.hxx"
22 #include "rtl/ustring.hxx"
23 #include "osl/process.h"
24 #include "osl/diagnose.hxx"
25 #include "sal/log.hxx"
26 #include "boost/bind.hpp"
32 typedef std::vector
<rtl::OString
> OStringVec
;
34 struct StaticDebugBaseAddressFilter
35 : rtl::StaticWithInit
<OStringVec
, StaticDebugBaseAddressFilter
> {
36 OStringVec
operator()() const {
38 rtl_uString
* pStr
= 0;
39 rtl::OUString
const name(
40 "OSL_DEBUGBASE_STORE_ADDRESSES" );
41 if (osl_getEnvironment( name
.pData
, &pStr
) == osl_Process_E_None
) {
42 rtl::OUString
const str(pStr
);
43 rtl_uString_release(pStr
);
46 vec
.push_back( rtl::OUStringToOString(
47 str
.getToken( 0, ';', nIndex
),
48 RTL_TEXTENCODING_ASCII_US
) );
56 inline bool isSubStr( char const* pStr
, rtl::OString
const& subStr
)
58 return rtl_str_indexOfStr( pStr
, subStr
.getStr() ) >= 0;
61 struct DebugBaseMutex
: rtl::Static
<osl::Mutex
, DebugBaseMutex
> {};
67 // These functions presumably should not be extern "C", but changing
68 // that would break binary compatibility.
70 #pragma clang diagnostic push
71 // Guard against slightly older clang versions that don't have
72 // -Wreturn-type-c-linkage...
73 #pragma clang diagnostic ignored "-Wunknown-pragmas"
74 #pragma clang diagnostic ignored "-Wreturn-type-c-linkage"
77 osl::Mutex
& SAL_CALL
osl_detail_ObjectRegistry_getMutex()
80 return DebugBaseMutex::get();
83 #pragma clang diagnostic pop
86 bool SAL_CALL
osl_detail_ObjectRegistry_storeAddresses( char const* pName
)
89 OStringVec
const& rVec
= StaticDebugBaseAddressFilter::get();
93 rtl::OString
const& rFirst
= rVec
[0];
94 if (rtl_str_compare_WithLength( rFirst
.getStr(), rFirst
.getLength(),
95 RTL_CONSTASCII_STRINGPARAM("all") ) == 0)
97 OStringVec::const_iterator
const iEnd( rVec
.end() );
98 return std::find_if( rVec
.begin(), iEnd
,
99 boost::bind( &isSubStr
, pName
, _1
) ) != iEnd
;
102 bool SAL_CALL
osl_detail_ObjectRegistry_checkObjectCount(
103 osl::detail::ObjectRegistryData
const& rData
, std::size_t nExpected
)
107 if (rData
.m_bStoreAddresses
)
108 nSize
= rData
.m_addresses
.size();
110 nSize
= static_cast<std::size_t>(rData
.m_nCount
);
112 bool const bRet
= (nSize
== nExpected
);
115 "unexpected number of " << rData
.m_pName
<< ": " << nSize
116 << "; Expected: " << nExpected
);
120 void SAL_CALL
osl_detail_ObjectRegistry_registerObject(
121 osl::detail::ObjectRegistryData
& rData
, void const* pObj
)
124 if (rData
.m_bStoreAddresses
) {
125 osl::MutexGuard
const guard( osl_detail_ObjectRegistry_getMutex() );
126 std::pair
<osl::detail::VoidPointerSet::iterator
, bool> const insertion(
127 rData
.m_addresses
.insert(pObj
) );
128 SAL_WARN_IF(!insertion
.second
, "sal.osl", "insertion failed!?");
131 osl_atomic_increment(&rData
.m_nCount
);
135 void SAL_CALL
osl_detail_ObjectRegistry_revokeObject(
136 osl::detail::ObjectRegistryData
& rData
, void const* pObj
)
139 if (rData
.m_bStoreAddresses
) {
140 osl::MutexGuard
const guard( osl_detail_ObjectRegistry_getMutex() );
141 std::size_t const n
= rData
.m_addresses
.erase(pObj
);
142 SAL_WARN_IF(n
!= 1, "sal.osl", "erased more than 1 entry!?");
145 osl_atomic_decrement(&rData
.m_nCount
);
151 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */