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 <o3tl/string_view.hxx>
33 const std::vector
<OString
>& StaticDebugBaseAddressFilter()
35 static const std::vector
<OString
> theFilter
= []()
37 std::vector
<OString
> vec
;
38 rtl_uString
* pStr
= nullptr;
40 "OSL_DEBUGBASE_STORE_ADDRESSES" );
41 if (osl_getEnvironment( name
.pData
, &pStr
) == osl_Process_E_None
) {
42 OUString
const str(pStr
);
43 rtl_uString_release(pStr
);
46 vec
.push_back( OUStringToOString(
47 o3tl::getToken(str
, 0, ';', nIndex
),
48 RTL_TEXTENCODING_ASCII_US
) );
57 bool isSubStr( char const* pStr
, OString
const& subStr
)
59 return rtl_str_indexOfStr( pStr
, subStr
.getStr() ) >= 0;
66 // These functions presumably should not be extern "C", but changing
67 // that would break binary compatibility.
69 #pragma clang diagnostic push
70 #pragma clang diagnostic ignored "-Wreturn-type-c-linkage"
73 osl::Mutex
& SAL_CALL
osl_detail_ObjectRegistry_getMutex()
76 static osl::Mutex aMutex
;
80 #pragma clang diagnostic pop
83 bool SAL_CALL
osl_detail_ObjectRegistry_storeAddresses( char const* pName
)
86 std::vector
<OString
> const& rVec
= StaticDebugBaseAddressFilter();
90 OString
const& rFirst
= rVec
[0];
91 if ( rFirst
== "all" )
93 auto const iEnd( rVec
.cend() );
94 return std::any_of( rVec
.begin(), iEnd
,
95 [pName
] (OString
const& it
) { return isSubStr(pName
, it
); });
98 bool SAL_CALL
osl_detail_ObjectRegistry_checkObjectCount(
99 osl::detail::ObjectRegistryData
const& rData
, std::size_t nExpected
)
103 if (rData
.m_bStoreAddresses
)
104 nSize
= rData
.m_addresses
.size();
106 nSize
= static_cast<std::size_t>(rData
.m_nCount
);
108 bool const bRet
= (nSize
== nExpected
);
111 "unexpected number of " << rData
.m_pName
<< ": " << nSize
112 << "; Expected: " << nExpected
);
116 void SAL_CALL
osl_detail_ObjectRegistry_registerObject(
117 osl::detail::ObjectRegistryData
& rData
, void const* pObj
)
120 if (rData
.m_bStoreAddresses
) {
121 osl::MutexGuard
const guard( osl_detail_ObjectRegistry_getMutex() );
122 std::pair
<osl::detail::VoidPointerSet::iterator
, bool> const insertion(
123 rData
.m_addresses
.insert(pObj
) );
124 SAL_WARN_IF(!insertion
.second
, "sal.osl", "insertion failed!?");
127 osl_atomic_increment(&rData
.m_nCount
);
131 void SAL_CALL
osl_detail_ObjectRegistry_revokeObject(
132 osl::detail::ObjectRegistryData
& rData
, void const* pObj
)
135 if (rData
.m_bStoreAddresses
) {
136 osl::MutexGuard
const guard( osl_detail_ObjectRegistry_getMutex() );
137 std::size_t const n
= rData
.m_addresses
.erase(pObj
);
138 SAL_WARN_IF(n
!= 1, "sal.osl", "erased more than 1 entry!?");
141 osl_atomic_decrement(&rData
.m_nCount
);
147 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */