Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include "rtl/strbuf.hxx"
22 : #include "rtl/string.hxx"
23 : #include "rtl/ustring.hxx"
24 : #include "osl/process.h"
25 : #include "osl/diagnose.hxx"
26 : #include "boost/bind.hpp"
27 : #include <vector>
28 :
29 : // define own ones, independent of OSL_DEBUG_LEVEL:
30 : #define DEBUGBASE_ENSURE_(c, f, l, m) \
31 : do \
32 : { \
33 : if (!(c) && _OSL_GLOBAL osl_assertFailedLine(f, l, m)) \
34 : _OSL_GLOBAL osl_breakDebug(); \
35 : } while (0)
36 : #define DEBUGBASE_ENSURE(c, m) DEBUGBASE_ENSURE_(c, OSL_THIS_FILE, __LINE__, m)
37 :
38 : namespace {
39 :
40 : typedef std::vector<rtl::OString, rtl::Allocator<rtl::OString> > OStringVec;
41 :
42 : struct StaticDebugBaseAddressFilter
43 : : rtl::StaticWithInit<OStringVec, StaticDebugBaseAddressFilter> {
44 0 : OStringVec operator()() const {
45 0 : OStringVec vec;
46 0 : rtl_uString * pStr = 0;
47 : rtl::OUString const name(
48 0 : RTL_CONSTASCII_USTRINGPARAM("OSL_DEBUGBASE_STORE_ADDRESSES") );
49 0 : if (osl_getEnvironment( name.pData, &pStr ) == osl_Process_E_None) {
50 0 : rtl::OUString const str(pStr);
51 0 : rtl_uString_release(pStr);
52 0 : sal_Int32 nIndex = 0;
53 0 : do {
54 : vec.push_back( rtl::OUStringToOString(
55 : str.getToken( 0, ';', nIndex ),
56 0 : RTL_TEXTENCODING_ASCII_US ) );
57 : }
58 0 : while (nIndex >= 0);
59 : }
60 0 : return vec;
61 : }
62 : };
63 :
64 0 : inline bool isSubStr( char const* pStr, rtl::OString const& subStr )
65 : {
66 0 : return rtl_str_indexOfStr( pStr, subStr.getStr() ) >= 0;
67 : }
68 :
69 : struct DebugBaseMutex : ::rtl::Static<osl::Mutex, DebugBaseMutex> {};
70 :
71 : } // anon namespace
72 :
73 : extern "C" {
74 :
75 : // These functions presumably should not be extern "C", but changing
76 : // that would break binary compatibility.
77 : #ifdef __clang__
78 : #pragma clang diagnostic push
79 : // Guard against slightly older clang versions that don't have
80 : // -Wreturn-type-c-linkage...
81 : #pragma clang diagnostic ignored "-Wunknown-pragmas"
82 : #pragma clang diagnostic ignored "-Wreturn-type-c-linkage"
83 : #endif
84 :
85 0 : osl::Mutex & SAL_CALL osl_detail_ObjectRegistry_getMutex()
86 : SAL_THROW_EXTERN_C()
87 : {
88 0 : return DebugBaseMutex::get();
89 : }
90 : #ifdef __clang__
91 : #pragma clang diagnostic pop
92 : #endif
93 :
94 0 : bool SAL_CALL osl_detail_ObjectRegistry_storeAddresses( char const* pName )
95 : SAL_THROW_EXTERN_C()
96 : {
97 0 : OStringVec const& rVec = StaticDebugBaseAddressFilter::get();
98 0 : if (rVec.empty())
99 0 : return false;
100 : // check for "all":
101 0 : rtl::OString const& rFirst = rVec[0];
102 0 : if (rtl_str_compare_WithLength( rFirst.getStr(), rFirst.getLength(),
103 0 : RTL_CONSTASCII_STRINGPARAM("all") ) == 0)
104 0 : return true;
105 0 : OStringVec::const_iterator const iEnd( rVec.end() );
106 : return std::find_if( rVec.begin(), iEnd,
107 0 : boost::bind( &isSubStr, pName, _1 ) ) != iEnd;
108 : }
109 :
110 0 : bool SAL_CALL osl_detail_ObjectRegistry_checkObjectCount(
111 : osl::detail::ObjectRegistryData const& rData, std::size_t nExpected )
112 : SAL_THROW_EXTERN_C()
113 : {
114 : std::size_t nSize;
115 0 : if (rData.m_bStoreAddresses)
116 0 : nSize = rData.m_addresses.size();
117 : else
118 0 : nSize = static_cast<std::size_t>(rData.m_nCount);
119 :
120 0 : bool const bRet = (nSize == nExpected);
121 0 : if (! bRet) {
122 0 : rtl::OStringBuffer buf;
123 0 : buf.append( RTL_CONSTASCII_STRINGPARAM("unexpected number of ") );
124 0 : buf.append( rData.m_pName );
125 0 : buf.append( RTL_CONSTASCII_STRINGPARAM(": ") );
126 0 : buf.append( static_cast<sal_Int64>(nSize) );
127 0 : buf.append("; Expected: ");
128 0 : buf.append( static_cast<sal_Int64>(nExpected) );
129 0 : DEBUGBASE_ENSURE( false, buf.makeStringAndClear().getStr() );
130 : }
131 0 : return bRet;
132 : }
133 :
134 0 : void SAL_CALL osl_detail_ObjectRegistry_registerObject(
135 : osl::detail::ObjectRegistryData & rData, void const* pObj )
136 : SAL_THROW_EXTERN_C()
137 : {
138 0 : if (rData.m_bStoreAddresses) {
139 0 : osl::MutexGuard const guard( osl_detail_ObjectRegistry_getMutex() );
140 : std::pair<osl::detail::VoidPointerSet::iterator, bool> const insertion(
141 0 : rData.m_addresses.insert(pObj) );
142 0 : DEBUGBASE_ENSURE( insertion.second, "### insertion failed!?" );
143 0 : static_cast<void>(insertion);
144 : }
145 : else {
146 0 : osl_atomic_increment(&rData.m_nCount);
147 : }
148 0 : }
149 :
150 0 : void SAL_CALL osl_detail_ObjectRegistry_revokeObject(
151 : osl::detail::ObjectRegistryData & rData, void const* pObj )
152 : SAL_THROW_EXTERN_C()
153 : {
154 0 : if (rData.m_bStoreAddresses) {
155 0 : osl::MutexGuard const guard( osl_detail_ObjectRegistry_getMutex() );
156 0 : std::size_t const n = rData.m_addresses.erase(pObj);
157 0 : DEBUGBASE_ENSURE( n == 1, "erased more than 1 entry!?" );
158 0 : static_cast<void>(n);
159 : }
160 : else {
161 0 : osl_atomic_decrement(&rData.m_nCount);
162 : }
163 0 : }
164 :
165 3831 : } // extern "C"
166 :
167 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|