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