bump product version to 5.0.4.1
[LibreOffice.git] / sal / osl / all / debugbase.cxx
blobae097ee891b178a6cccee6b06f64474d2485e139
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 "boost/bind.hpp"
27 #include <algorithm>
28 #include <vector>
30 namespace {
32 typedef std::vector<rtl::OString> OStringVec;
34 struct StaticDebugBaseAddressFilter
35 : rtl::StaticWithInit<OStringVec, StaticDebugBaseAddressFilter> {
36 OStringVec operator()() const {
37 OStringVec vec;
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);
44 sal_Int32 nIndex = 0;
45 do {
46 vec.push_back( rtl::OUStringToOString(
47 str.getToken( 0, ';', nIndex ),
48 RTL_TEXTENCODING_ASCII_US ) );
50 while (nIndex >= 0);
52 return vec;
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> {};
63 } // anon namespace
65 extern "C" {
67 // These functions presumably should not be extern "C", but changing
68 // that would break binary compatibility.
69 #ifdef __clang__
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"
75 #endif
77 osl::Mutex & SAL_CALL osl_detail_ObjectRegistry_getMutex()
78 SAL_THROW_EXTERN_C()
80 return DebugBaseMutex::get();
82 #ifdef __clang__
83 #pragma clang diagnostic pop
84 #endif
86 bool SAL_CALL osl_detail_ObjectRegistry_storeAddresses( char const* pName )
87 SAL_THROW_EXTERN_C()
89 OStringVec const& rVec = StaticDebugBaseAddressFilter::get();
90 if (rVec.empty())
91 return false;
92 // check for "all":
93 rtl::OString const& rFirst = rVec[0];
94 if (rtl_str_compare_WithLength( rFirst.getStr(), rFirst.getLength(),
95 RTL_CONSTASCII_STRINGPARAM("all") ) == 0)
96 return true;
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 )
104 SAL_THROW_EXTERN_C()
106 std::size_t nSize;
107 if (rData.m_bStoreAddresses)
108 nSize = rData.m_addresses.size();
109 else
110 nSize = static_cast<std::size_t>(rData.m_nCount);
112 bool const bRet = (nSize == nExpected);
113 SAL_WARN_IF(
114 !bRet, "sal.osl",
115 "unexpected number of " << rData.m_pName << ": " << nSize
116 << "; Expected: " << nExpected);
117 return bRet;
120 void SAL_CALL osl_detail_ObjectRegistry_registerObject(
121 osl::detail::ObjectRegistryData & rData, void const* pObj )
122 SAL_THROW_EXTERN_C()
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!?");
130 else {
131 osl_atomic_increment(&rData.m_nCount);
135 void SAL_CALL osl_detail_ObjectRegistry_revokeObject(
136 osl::detail::ObjectRegistryData & rData, void const* pObj )
137 SAL_THROW_EXTERN_C()
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!?");
144 else {
145 osl_atomic_decrement(&rData.m_nCount);
149 } // extern "C"
151 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */