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/string.hxx>
21 #include <rtl/ustring.hxx>
22 #include <osl/process.h>
23 #include <osl/diagnose.hxx>
24 #include <sal/log.hxx>
25 #include <o3tl/string_view.hxx>
32 const std::vector
<OString
>& StaticDebugBaseAddressFilter()
34 static const std::vector
<OString
> theFilter
= []()
36 std::vector
<OString
> vec
;
37 rtl_uString
* pStr
= nullptr;
38 if (osl_getEnvironment( u
"OSL_DEBUGBASE_STORE_ADDRESSES"_ustr
.pData
, &pStr
)
39 == osl_Process_E_None
)
41 OUString
const str(pStr
);
42 rtl_uString_release(pStr
);
45 vec
.push_back( OUStringToOString(
46 o3tl::getToken(str
, 0, ';', nIndex
),
47 RTL_TEXTENCODING_ASCII_US
) );
56 bool isSubStr( char const* pStr
, OString
const& subStr
)
58 return rtl_str_indexOfStr( pStr
, subStr
.getStr() ) >= 0;
65 // These functions presumably should not be extern "C", but changing
66 // that would break binary compatibility.
68 #pragma clang diagnostic push
69 #pragma clang diagnostic ignored "-Wreturn-type-c-linkage"
72 osl::Mutex
& SAL_CALL
osl_detail_ObjectRegistry_getMutex() noexcept
74 static osl::Mutex aMutex
;
78 #pragma clang diagnostic pop
81 bool SAL_CALL
osl_detail_ObjectRegistry_storeAddresses( char const* pName
) noexcept
83 std::vector
<OString
> const& rVec
= StaticDebugBaseAddressFilter();
87 OString
const& rFirst
= rVec
[0];
88 if ( rFirst
== "all" )
90 auto const iEnd( rVec
.cend() );
91 return std::any_of( rVec
.begin(), iEnd
,
92 [pName
] (OString
const& it
) { return isSubStr(pName
, it
); });
95 bool SAL_CALL
osl_detail_ObjectRegistry_checkObjectCount(
96 osl::detail::ObjectRegistryData
const& rData
, std::size_t nExpected
) noexcept
99 if (rData
.m_bStoreAddresses
)
100 nSize
= rData
.m_addresses
.size();
102 nSize
= static_cast<std::size_t>(rData
.m_nCount
);
104 bool const bRet
= (nSize
== nExpected
);
107 "unexpected number of " << rData
.m_pName
<< ": " << nSize
108 << "; Expected: " << nExpected
);
112 void SAL_CALL
osl_detail_ObjectRegistry_registerObject(
113 osl::detail::ObjectRegistryData
& rData
, void const* pObj
) noexcept
115 if (rData
.m_bStoreAddresses
) {
116 osl::MutexGuard
const guard( osl_detail_ObjectRegistry_getMutex() );
117 std::pair
<osl::detail::VoidPointerSet::iterator
, bool> const insertion(
118 rData
.m_addresses
.insert(pObj
) );
119 SAL_WARN_IF(!insertion
.second
, "sal.osl", "insertion failed!?");
122 osl_atomic_increment(&rData
.m_nCount
);
126 void SAL_CALL
osl_detail_ObjectRegistry_revokeObject(
127 osl::detail::ObjectRegistryData
& rData
, void const* pObj
) noexcept
129 if (rData
.m_bStoreAddresses
) {
130 osl::MutexGuard
const guard( osl_detail_ObjectRegistry_getMutex() );
131 std::size_t const n
= rData
.m_addresses
.erase(pObj
);
132 SAL_WARN_IF(n
!= 1, "sal.osl", "erased more than 1 entry!?");
135 osl_atomic_decrement(&rData
.m_nCount
);
141 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */