xref: /aoo42x/main/sal/osl/all/debugbase.cxx (revision 87d2adbc)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sal.hxx"
26 
27 #include "rtl/strbuf.hxx"
28 #include "rtl/string.hxx"
29 #include "rtl/ustring.hxx"
30 #include "osl/process.h"
31 #include "osl/diagnose.hxx"
32 #include "boost/bind.hpp"
33 #include <vector>
34 
35 // define own ones, independent of OSL_DEBUG_LEVEL:
36 #define DEBUGBASE_ENSURE_(c, f, l, m) \
37 	do \
38 	{  \
39 	    if (!(c) && _OSL_GLOBAL osl_assertFailedLine(f, l, m)) \
40 		    _OSL_GLOBAL osl_breakDebug(); \
41 	} while (0)
42 #define DEBUGBASE_ENSURE(c, m) DEBUGBASE_ENSURE_(c, OSL_THIS_FILE, __LINE__, m)
43 
44 namespace {
45 
46 typedef std::vector<rtl::OString, rtl::Allocator<rtl::OString> > OStringVec;
47 
48 struct StaticDebugBaseAddressFilter
49     : rtl::StaticWithInit<OStringVec const, StaticDebugBaseAddressFilter> {
50     OStringVec const operator()() const {
51         OStringVec vec;
52         rtl_uString * pStr = 0;
53         rtl::OUString const name(
54             RTL_CONSTASCII_USTRINGPARAM("OSL_DEBUGBASE_STORE_ADDRESSES") );
55         if (osl_getEnvironment( name.pData, &pStr ) == osl_Process_E_None) {
56             rtl::OUString const str(pStr);
57             rtl_uString_release(pStr);
58             sal_Int32 nIndex = 0;
59             do {
60                 vec.push_back( rtl::OUStringToOString(
61                                    str.getToken( 0, ';', nIndex ),
62                                    RTL_TEXTENCODING_ASCII_US ) );
63             }
64             while (nIndex >= 0);
65         }
66         return vec;
67     }
68 };
69 
70 inline bool isSubStr( char const* pStr, rtl::OString const& subStr )
71 {
72     return rtl_str_indexOfStr( pStr, subStr.getStr() ) >= 0;
73 }
74 
75 struct DebugBaseMutex : ::rtl::Static<osl::Mutex, DebugBaseMutex> {};
76 
77 } // anon namespace
78 
79 extern "C" {
80 
81 osl::Mutex & SAL_CALL osl_detail_ObjectRegistry_getMutex()
82     SAL_THROW_EXTERN_C()
83 {
84     return DebugBaseMutex::get();
85 }
86 
87 bool SAL_CALL osl_detail_ObjectRegistry_storeAddresses( char const* pName )
88     SAL_THROW_EXTERN_C()
89 {
90     OStringVec const& rVec = StaticDebugBaseAddressFilter::get();
91     if (rVec.empty())
92         return false;
93     // check for "all":
94     rtl::OString const& rFirst = rVec[0];
95     if (rtl_str_compare_WithLength( rFirst.getStr(), rFirst.getLength(),
96                                     RTL_CONSTASCII_STRINGPARAM("all") ) == 0)
97         return true;
98     OStringVec::const_iterator const iEnd( rVec.end() );
99     return std::find_if( rVec.begin(), iEnd,
100                          boost::bind( &isSubStr, pName, _1 ) ) != iEnd;
101 }
102 
103 bool SAL_CALL osl_detail_ObjectRegistry_checkObjectCount(
104     osl::detail::ObjectRegistryData const& rData, std::size_t nExpected )
105     SAL_THROW_EXTERN_C()
106 {
107     std::size_t nSize;
108     if (rData.m_bStoreAddresses)
109         nSize = rData.m_addresses.size();
110     else
111         nSize = static_cast<std::size_t>(rData.m_nCount);
112 
113     bool const bRet = (nSize == nExpected);
114     if (! bRet) {
115         rtl::OStringBuffer buf;
116         buf.append( RTL_CONSTASCII_STRINGPARAM("unexpected number of ") );
117         buf.append( rData.m_pName );
118         buf.append( RTL_CONSTASCII_STRINGPARAM(": ") );
119         buf.append( static_cast<sal_Int64>(nSize) );
120         DEBUGBASE_ENSURE( false, buf.makeStringAndClear().getStr() );
121     }
122     return bRet;
123 }
124 
125 void SAL_CALL osl_detail_ObjectRegistry_registerObject(
126     osl::detail::ObjectRegistryData & rData, void const* pObj )
127     SAL_THROW_EXTERN_C()
128 {
129     if (rData.m_bStoreAddresses) {
130         osl::MutexGuard const guard( osl_detail_ObjectRegistry_getMutex() );
131         std::pair<osl::detail::VoidPointerSet::iterator, bool> const insertion(
132             rData.m_addresses.insert(pObj) );
133         DEBUGBASE_ENSURE( insertion.second, "### insertion failed!?" );
134         static_cast<void>(insertion);
135     }
136     else {
137         osl_incrementInterlockedCount(&rData.m_nCount);
138     }
139 }
140 
141 void SAL_CALL osl_detail_ObjectRegistry_revokeObject(
142     osl::detail::ObjectRegistryData & rData, void const* pObj )
143     SAL_THROW_EXTERN_C()
144 {
145     if (rData.m_bStoreAddresses) {
146         osl::MutexGuard const guard( osl_detail_ObjectRegistry_getMutex() );
147         std::size_t const n = rData.m_addresses.erase(pObj);
148         DEBUGBASE_ENSURE( n == 1, "erased more than 1 entry!?" );
149         static_cast<void>(n);
150     }
151     else {
152         osl_decrementInterlockedCount(&rData.m_nCount);
153     }
154 }
155 
156 } // extern "C"
157 
158