xref: /trunk/main/sal/osl/all/debugbase.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_sal.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "rtl/strbuf.hxx"
32*cdf0e10cSrcweir #include "rtl/string.hxx"
33*cdf0e10cSrcweir #include "rtl/ustring.hxx"
34*cdf0e10cSrcweir #include "osl/process.h"
35*cdf0e10cSrcweir #include "osl/diagnose.hxx"
36*cdf0e10cSrcweir #include "boost/bind.hpp"
37*cdf0e10cSrcweir #include <vector>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir // define own ones, independent of OSL_DEBUG_LEVEL:
40*cdf0e10cSrcweir #define DEBUGBASE_ENSURE_(c, f, l, m) \
41*cdf0e10cSrcweir     do \
42*cdf0e10cSrcweir     {  \
43*cdf0e10cSrcweir         if (!(c) && _OSL_GLOBAL osl_assertFailedLine(f, l, m)) \
44*cdf0e10cSrcweir             _OSL_GLOBAL osl_breakDebug(); \
45*cdf0e10cSrcweir     } while (0)
46*cdf0e10cSrcweir #define DEBUGBASE_ENSURE(c, m) DEBUGBASE_ENSURE_(c, OSL_THIS_FILE, __LINE__, m)
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir namespace {
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir typedef std::vector<rtl::OString, rtl::Allocator<rtl::OString> > OStringVec;
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir struct StaticDebugBaseAddressFilter
53*cdf0e10cSrcweir     : rtl::StaticWithInit<OStringVec const, StaticDebugBaseAddressFilter> {
54*cdf0e10cSrcweir     OStringVec const operator()() const {
55*cdf0e10cSrcweir         OStringVec vec;
56*cdf0e10cSrcweir         rtl_uString * pStr = 0;
57*cdf0e10cSrcweir         rtl::OUString const name(
58*cdf0e10cSrcweir             RTL_CONSTASCII_USTRINGPARAM("OSL_DEBUGBASE_STORE_ADDRESSES") );
59*cdf0e10cSrcweir         if (osl_getEnvironment( name.pData, &pStr ) == osl_Process_E_None) {
60*cdf0e10cSrcweir             rtl::OUString const str(pStr);
61*cdf0e10cSrcweir             rtl_uString_release(pStr);
62*cdf0e10cSrcweir             sal_Int32 nIndex = 0;
63*cdf0e10cSrcweir             do {
64*cdf0e10cSrcweir                 vec.push_back( rtl::OUStringToOString(
65*cdf0e10cSrcweir                                    str.getToken( 0, ';', nIndex ),
66*cdf0e10cSrcweir                                    RTL_TEXTENCODING_ASCII_US ) );
67*cdf0e10cSrcweir             }
68*cdf0e10cSrcweir             while (nIndex >= 0);
69*cdf0e10cSrcweir         }
70*cdf0e10cSrcweir         return vec;
71*cdf0e10cSrcweir     }
72*cdf0e10cSrcweir };
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir inline bool isSubStr( char const* pStr, rtl::OString const& subStr )
75*cdf0e10cSrcweir {
76*cdf0e10cSrcweir     return rtl_str_indexOfStr( pStr, subStr.getStr() ) >= 0;
77*cdf0e10cSrcweir }
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir struct DebugBaseMutex : ::rtl::Static<osl::Mutex, DebugBaseMutex> {};
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir } // anon namespace
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir extern "C" {
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir osl::Mutex & SAL_CALL osl_detail_ObjectRegistry_getMutex()
86*cdf0e10cSrcweir     SAL_THROW_EXTERN_C()
87*cdf0e10cSrcweir {
88*cdf0e10cSrcweir     return DebugBaseMutex::get();
89*cdf0e10cSrcweir }
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir bool SAL_CALL osl_detail_ObjectRegistry_storeAddresses( char const* pName )
92*cdf0e10cSrcweir     SAL_THROW_EXTERN_C()
93*cdf0e10cSrcweir {
94*cdf0e10cSrcweir     OStringVec const& rVec = StaticDebugBaseAddressFilter::get();
95*cdf0e10cSrcweir     if (rVec.empty())
96*cdf0e10cSrcweir         return false;
97*cdf0e10cSrcweir     // check for "all":
98*cdf0e10cSrcweir     rtl::OString const& rFirst = rVec[0];
99*cdf0e10cSrcweir     if (rtl_str_compare_WithLength( rFirst.getStr(), rFirst.getLength(),
100*cdf0e10cSrcweir                                     RTL_CONSTASCII_STRINGPARAM("all") ) == 0)
101*cdf0e10cSrcweir         return true;
102*cdf0e10cSrcweir     OStringVec::const_iterator const iEnd( rVec.end() );
103*cdf0e10cSrcweir     return std::find_if( rVec.begin(), iEnd,
104*cdf0e10cSrcweir                          boost::bind( &isSubStr, pName, _1 ) ) != iEnd;
105*cdf0e10cSrcweir }
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir bool SAL_CALL osl_detail_ObjectRegistry_checkObjectCount(
108*cdf0e10cSrcweir     osl::detail::ObjectRegistryData const& rData, std::size_t nExpected )
109*cdf0e10cSrcweir     SAL_THROW_EXTERN_C()
110*cdf0e10cSrcweir {
111*cdf0e10cSrcweir     std::size_t nSize;
112*cdf0e10cSrcweir     if (rData.m_bStoreAddresses)
113*cdf0e10cSrcweir         nSize = rData.m_addresses.size();
114*cdf0e10cSrcweir     else
115*cdf0e10cSrcweir         nSize = static_cast<std::size_t>(rData.m_nCount);
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir     bool const bRet = (nSize == nExpected);
118*cdf0e10cSrcweir     if (! bRet) {
119*cdf0e10cSrcweir         rtl::OStringBuffer buf;
120*cdf0e10cSrcweir         buf.append( RTL_CONSTASCII_STRINGPARAM("unexpected number of ") );
121*cdf0e10cSrcweir         buf.append( rData.m_pName );
122*cdf0e10cSrcweir         buf.append( RTL_CONSTASCII_STRINGPARAM(": ") );
123*cdf0e10cSrcweir         buf.append( static_cast<sal_Int64>(nSize) );
124*cdf0e10cSrcweir         DEBUGBASE_ENSURE( false, buf.makeStringAndClear().getStr() );
125*cdf0e10cSrcweir     }
126*cdf0e10cSrcweir     return bRet;
127*cdf0e10cSrcweir }
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir void SAL_CALL osl_detail_ObjectRegistry_registerObject(
130*cdf0e10cSrcweir     osl::detail::ObjectRegistryData & rData, void const* pObj )
131*cdf0e10cSrcweir     SAL_THROW_EXTERN_C()
132*cdf0e10cSrcweir {
133*cdf0e10cSrcweir     if (rData.m_bStoreAddresses) {
134*cdf0e10cSrcweir         osl::MutexGuard const guard( osl_detail_ObjectRegistry_getMutex() );
135*cdf0e10cSrcweir         std::pair<osl::detail::VoidPointerSet::iterator, bool> const insertion(
136*cdf0e10cSrcweir             rData.m_addresses.insert(pObj) );
137*cdf0e10cSrcweir         DEBUGBASE_ENSURE( insertion.second, "### insertion failed!?" );
138*cdf0e10cSrcweir         static_cast<void>(insertion);
139*cdf0e10cSrcweir     }
140*cdf0e10cSrcweir     else {
141*cdf0e10cSrcweir         osl_incrementInterlockedCount(&rData.m_nCount);
142*cdf0e10cSrcweir     }
143*cdf0e10cSrcweir }
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir void SAL_CALL osl_detail_ObjectRegistry_revokeObject(
146*cdf0e10cSrcweir     osl::detail::ObjectRegistryData & rData, void const* pObj )
147*cdf0e10cSrcweir     SAL_THROW_EXTERN_C()
148*cdf0e10cSrcweir {
149*cdf0e10cSrcweir     if (rData.m_bStoreAddresses) {
150*cdf0e10cSrcweir         osl::MutexGuard const guard( osl_detail_ObjectRegistry_getMutex() );
151*cdf0e10cSrcweir         std::size_t const n = rData.m_addresses.erase(pObj);
152*cdf0e10cSrcweir         DEBUGBASE_ENSURE( n == 1, "erased more than 1 entry!?" );
153*cdf0e10cSrcweir         static_cast<void>(n);
154*cdf0e10cSrcweir     }
155*cdf0e10cSrcweir     else {
156*cdf0e10cSrcweir         osl_decrementInterlockedCount(&rData.m_nCount);
157*cdf0e10cSrcweir     }
158*cdf0e10cSrcweir }
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir } // extern "C"
161*cdf0e10cSrcweir 
162