Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sal / osl / all / debugbase.cxx
blobb4b598589a00d7ad2aba53086e583bda8b429686
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
28 #include <algorithm>
29 #include <vector>
31 namespace {
33 const std::vector<OString>& StaticDebugBaseAddressFilter()
35 static const std::vector<OString> theFilter = []()
37 std::vector<OString> vec;
38 rtl_uString * pStr = nullptr;
39 OUString const name(
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);
44 sal_Int32 nIndex = 0;
45 do {
46 vec.push_back( OUStringToOString(
47 o3tl::getToken(str, 0, ';', nIndex ),
48 RTL_TEXTENCODING_ASCII_US ) );
50 while (nIndex >= 0);
52 return vec;
53 }();
54 return theFilter;
57 bool isSubStr( char const* pStr, OString const& subStr )
59 return rtl_str_indexOfStr( pStr, subStr.getStr() ) >= 0;
62 } // anon namespace
64 extern "C" {
66 // These functions presumably should not be extern "C", but changing
67 // that would break binary compatibility.
68 #ifdef __clang__
69 #pragma clang diagnostic push
70 #pragma clang diagnostic ignored "-Wreturn-type-c-linkage"
71 #endif
73 osl::Mutex & SAL_CALL osl_detail_ObjectRegistry_getMutex()
74 SAL_THROW_EXTERN_C()
76 static osl::Mutex aMutex;
77 return aMutex;
79 #ifdef __clang__
80 #pragma clang diagnostic pop
81 #endif
83 bool SAL_CALL osl_detail_ObjectRegistry_storeAddresses( char const* pName )
84 SAL_THROW_EXTERN_C()
86 std::vector<OString> const& rVec = StaticDebugBaseAddressFilter();
87 if (rVec.empty())
88 return false;
89 // check for "all":
90 OString const& rFirst = rVec[0];
91 if ( rFirst == "all" )
92 return true;
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 )
100 SAL_THROW_EXTERN_C()
102 std::size_t nSize;
103 if (rData.m_bStoreAddresses)
104 nSize = rData.m_addresses.size();
105 else
106 nSize = static_cast<std::size_t>(rData.m_nCount);
108 bool const bRet = (nSize == nExpected);
109 SAL_WARN_IF(
110 !bRet, "sal.osl",
111 "unexpected number of " << rData.m_pName << ": " << nSize
112 << "; Expected: " << nExpected);
113 return bRet;
116 void SAL_CALL osl_detail_ObjectRegistry_registerObject(
117 osl::detail::ObjectRegistryData & rData, void const* pObj )
118 SAL_THROW_EXTERN_C()
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!?");
126 else {
127 osl_atomic_increment(&rData.m_nCount);
131 void SAL_CALL osl_detail_ObjectRegistry_revokeObject(
132 osl::detail::ObjectRegistryData & rData, void const* pObj )
133 SAL_THROW_EXTERN_C()
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!?");
140 else {
141 osl_atomic_decrement(&rData.m_nCount);
145 } // extern "C"
147 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */