diff options
author | Mikhail Voytenko <mav@openoffice.org> | 2011-03-24 14:30:07 +0100 |
---|---|---|
committer | Mikhail Voytenko <mav@openoffice.org> | 2011-03-24 14:30:07 +0100 |
commit | a0af6240b3cc56af2ca9f250bf1d97e3a2df846a (patch) | |
tree | bb847270f30ad2f6a53c05f3326f64723dde95f4 /sal | |
parent | ab48268453eb21894f3fac215c6117d9391256be (diff) | |
parent | a24842b43a687808376f69d4bdbb45fcddde73c4 (diff) |
mav60: rebase to DEV300_m103
Diffstat (limited to 'sal')
-rw-r--r-- | sal/cppunittester/cppunittester.cxx | 102 | ||||
-rw-r--r-- | sal/inc/cppunittester/protectorfactory.hxx | 45 | ||||
-rw-r--r-- | sal/osl/os2/diagnose.c | 3 | ||||
-rw-r--r-- | sal/osl/unx/diagnose.c | 14 | ||||
-rw-r--r-- | sal/osl/unx/file.cxx | 3 | ||||
-rw-r--r-- | sal/osl/unx/file_misc.cxx | 6 | ||||
-rw-r--r-- | sal/osl/unx/file_path_helper.cxx | 80 | ||||
-rw-r--r-- | sal/osl/unx/process_impl.cxx | 19 | ||||
-rw-r--r-- | sal/osl/unx/profile.c | 1 | ||||
-rw-r--r-- | sal/osl/unx/socket.c | 39 | ||||
-rwxr-xr-x | sal/osl/w32/diagnose.c | 50 | ||||
-rw-r--r-- | sal/osl/w32/file_dirvol.cxx | 4 | ||||
-rwxr-xr-x[-rw-r--r--] | sal/osl/w32/module.cxx | 23 | ||||
-rwxr-xr-x[-rw-r--r--] | sal/osl/w32/procimpl.cxx | 35 | ||||
-rw-r--r-- | sal/prj/d.lst | 2 | ||||
-rw-r--r-- | sal/rtl/source/alloc.c | 1541 | ||||
-rw-r--r-- | sal/rtl/source/alloc_arena.c | 37 | ||||
-rw-r--r-- | sal/rtl/source/alloc_cache.c | 53 | ||||
-rwxr-xr-x | sal/rtl/source/alloc_fini.cxx (renamed from sal/rtl/source/memory_fini.cxx) | 23 | ||||
-rw-r--r-- | sal/rtl/source/alloc_global.c | 10 | ||||
-rw-r--r-- | sal/rtl/source/alloc_impl.h | 24 | ||||
-rw-r--r-- | sal/rtl/source/makefile.mk | 9 |
22 files changed, 438 insertions, 1685 deletions
diff --git a/sal/cppunittester/cppunittester.cxx b/sal/cppunittester/cppunittester.cxx index ac99601f5ce5..c89a350d6708 100644 --- a/sal/cppunittester/cppunittester.cxx +++ b/sal/cppunittester/cppunittester.cxx @@ -30,6 +30,20 @@ #include <cstdlib> #include <iostream> +#include <limits> +#include <string> + +#include "cppunittester/protectorfactory.hxx" +#include "osl/module.h" +#include "osl/module.hxx" +#include "osl/thread.h" +#include "rtl/process.h" +#include "rtl/string.h" +#include "rtl/string.hxx" +#include "rtl/textcvt.h" +#include "rtl/ustring.hxx" +#include "sal/main.h" +#include "sal/types.h" #include "preextstl.h" #include "cppunit/CompilerOutputter.h" @@ -40,25 +54,87 @@ #include "cppunit/plugin/PlugInManager.h" #include "cppunit/portability/Stream.h" #include "postextstl.h" -#include "osl/thread.h" -#include "rtl/process.h" -#include "rtl/string.hxx" -#include "rtl/ustring.hxx" -#include "sal/main.h" + +namespace { + +void usageFailure() { + std::cerr + << ("Usage: cppunittester (--protector <shared-library-path>" + " <function-symbol>)* <shared-library-path>") + << std::endl; + std::exit(EXIT_FAILURE); +} + +rtl::OUString getArgument(sal_Int32 index) { + rtl::OUString arg; + rtl_getAppCommandArg(index, &arg.pData); + return arg; +} + +std::string convertLazy(rtl::OUString const & s16) { + rtl::OString s8(rtl::OUStringToOString(s16, osl_getThreadTextEncoding())); + return std::string( + s8.getStr(), + ((static_cast< sal_uInt32 >(s8.getLength()) + > std::numeric_limits< std::string::size_type >::max()) + ? std::numeric_limits< std::string::size_type >::max() + : static_cast< std::string::size_type >(s8.getLength()))); +} + +std::string convertStrict(rtl::OUString const & s16) { + rtl::OString s8; + if (!s16.convertToString( + &s8, osl_getThreadTextEncoding(), + (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR + | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR)) + || (static_cast< sal_uInt32 >(s8.getLength()) + > std::numeric_limits< std::string::size_type >::max())) + { + std::cerr + << "Failure converting argument from UTF-16 back to system encoding" + << std::endl; + std::exit(EXIT_FAILURE); + } + return std::string( + s8.getStr(), static_cast< std::string::size_type >(s8.getLength())); +} + +} SAL_IMPLEMENT_MAIN() { - if (rtl_getAppCommandArgCount() != 1) { - std::cerr << "Usage: cppunittester <shared-library-path>" << std::endl; - return EXIT_FAILURE; + CppUnit::TestResult result; + sal_uInt32 index = 0; + for (; index < rtl_getAppCommandArgCount(); index += 3) { + if (!getArgument(index).equalsAsciiL( + RTL_CONSTASCII_STRINGPARAM("--protector"))) + { + break; + } + if (rtl_getAppCommandArgCount() - index < 3) { + usageFailure(); + } + rtl::OUString lib(getArgument(index + 1)); + rtl::OUString sym(getArgument(index + 2)); + oslGenericFunction fn = (new osl::Module(lib, SAL_LOADMODULE_GLOBAL)) + ->getFunctionSymbol(sym); + CppUnit::Protector * p = fn == 0 + ? 0 + : (*reinterpret_cast< cppunittester::ProtectorFactory * >(fn))(); + if (p == 0) { + std::cerr + << "Failure instantiating protector \"" << convertLazy(lib) + << "\", \"" << convertLazy(sym) << '"' << std::endl; + std::exit(EXIT_FAILURE); + } + result.pushProtector(p); + } + if (rtl_getAppCommandArgCount() - index != 1) { + usageFailure(); } - rtl::OUString path; - rtl_getAppCommandArg(0, &path.pData); CppUnit::PlugInManager manager; - manager.load( - rtl::OUStringToOString(path, osl_getThreadTextEncoding()).getStr()); + manager.load(convertStrict(getArgument(index))); CppUnit::TestRunner runner; runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest()); - CppUnit::TestResult result; CppUnit::TestResultCollector collector; result.addListener(&collector); runner.run(result); diff --git a/sal/inc/cppunittester/protectorfactory.hxx b/sal/inc/cppunittester/protectorfactory.hxx new file mode 100644 index 000000000000..c309dbe8aa85 --- /dev/null +++ b/sal/inc/cppunittester/protectorfactory.hxx @@ -0,0 +1,45 @@ +/************************************************************************* +* +* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +* +* Copyright 2000, 2011 Oracle and/or its affiliates. +* +* OpenOffice.org - a multi-platform office productivity suite +* +* This file is part of OpenOffice.org. +* +* OpenOffice.org is free software: you can redistribute it and/or modify +* it under the terms of the GNU Lesser General Public License version 3 +* only, as published by the Free Software Foundation. +* +* OpenOffice.org is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License version 3 for more details +* (a copy is included in the LICENSE file that accompanied this code). +* +* You should have received a copy of the GNU Lesser General Public License +* version 3 along with OpenOffice.org. If not, see +* <http://www.openoffice.org/license.html> +* for a copy of the LGPLv3 License. +* +************************************************************************/ + +#ifndef INCLUDED_CPPUNITTESTER_PROTECTORFACTORY_HXX +#define INCLUDED_CPPUNITTESTER_PROTECTORFACTORY_HXX + +#include "sal/config.h" + +#include "sal/types.h" + +namespace CppUnit { class Protector; } + +namespace cppunittester { + +// The type of CppUnit::Protector factory functions that can be plugged into +// cppunittester: +extern "C" typedef CppUnit::Protector * SAL_CALL ProtectorFactory(); + +} + +#endif diff --git a/sal/osl/os2/diagnose.c b/sal/osl/os2/diagnose.c index b105e0b78c29..b412a65ff090 100644 --- a/sal/osl/os2/diagnose.c +++ b/sal/osl/os2/diagnose.c @@ -124,7 +124,8 @@ sal_Bool SAL_CALL osl_assertFailedLine( const sal_Char* pszFileName, sal_Int32 n fputs(szMessage, stderr); - return sal_True; /* abort */ + char const * env = getenv( "SAL_DIAGNOSE_ABORT" ); + return ( ( env != NULL ) && ( *env != '\0' ) ); } /*----------------------------------------------------------------------------*/ diff --git a/sal/osl/unx/diagnose.c b/sal/osl/unx/diagnose.c index 02967b3ad7f7..5d9a4f31a40e 100644 --- a/sal/osl/unx/diagnose.c +++ b/sal/osl/unx/diagnose.c @@ -218,6 +218,11 @@ sal_Bool SAL_CALL osl_assertFailedLine ( oslDebugMessageFunc f = g_pDebugMessageFunc; char szMessage[1024]; + // after reporting the assertion, abort if told so by SAL_DIAGNOSE_ABORT, but *not* if + // assertions are routed to some external instance + char const * env = getenv( "SAL_DIAGNOSE_ABORT" ); + sal_Bool const doAbort = ( ( env != NULL ) && ( *env != '\0' ) && ( f == NULL ) ); + /* If there's a callback for detailed messages, use it */ if ( g_pDetailedDebugMessageFunc != NULL ) { @@ -227,7 +232,7 @@ sal_Bool SAL_CALL osl_assertFailedLine ( /* if SAL assertions are disabled in general, stop here */ if ( getenv("DISABLE_SAL_DBGBOX") ) - return sal_False; + return doAbort; /* format message into buffer */ if (pszMessage != 0) @@ -252,9 +257,10 @@ sal_Bool SAL_CALL osl_assertFailedLine ( /* output backtrace */ osl_diagnose_backtrace_Impl(f); - /* release lock and leave, w/o calling osl_breakDebug() */ + /* release lock and leave */ pthread_mutex_unlock(&g_mutex); - return sal_False; + + return doAbort; } /************************************************************************/ @@ -262,7 +268,7 @@ sal_Bool SAL_CALL osl_assertFailedLine ( /************************************************************************/ void SAL_CALL osl_breakDebug() { - exit(0); + abort(); } /************************************************************************/ diff --git a/sal/osl/unx/file.cxx b/sal/osl/unx/file.cxx index cc0c041bc328..67f1d05660a8 100644 --- a/sal/osl/unx/file.cxx +++ b/sal/osl/unx/file.cxx @@ -215,7 +215,8 @@ FileHandle_Impl::Allocator::~Allocator() void FileHandle_Impl::Allocator::allocate (sal_uInt8 ** ppBuffer, size_t * pnSize) { OSL_PRECOND((0 != ppBuffer) && (0 != pnSize), "FileHandle_Impl::Allocator::allocate(): contract violation"); - *ppBuffer = static_cast< sal_uInt8* >(rtl_cache_alloc(m_cache)), *pnSize = m_bufsiz; + if ((0 != ppBuffer) && (0 != pnSize)) + *ppBuffer = static_cast< sal_uInt8* >(rtl_cache_alloc(m_cache)), *pnSize = m_bufsiz; } void FileHandle_Impl::Allocator::deallocate (sal_uInt8 * pBuffer) { diff --git a/sal/osl/unx/file_misc.cxx b/sal/osl/unx/file_misc.cxx index 452d3eb2db70..2ed57b01a2c4 100644 --- a/sal/osl/unx/file_misc.cxx +++ b/sal/osl/unx/file_misc.cxx @@ -334,10 +334,8 @@ oslFileError SAL_CALL osl_getDirectoryItem( rtl_uString* ustrFileURL, oslDirecto rtl_uString* ustrSystemPath = NULL; oslFileError osl_error = osl_File_E_INVAL; - OSL_ASSERT(ustrFileURL); - OSL_ASSERT(pItem); - - if (0 == ustrFileURL->length || NULL == pItem) + OSL_ASSERT((0 != ustrFileURL) && (0 != pItem)); + if ((0 == ustrFileURL) || (0 == ustrFileURL->length) || (0 == pItem)) return osl_File_E_INVAL; osl_error = osl_getSystemPathFromFileURL_Ex(ustrFileURL, &ustrSystemPath, sal_False); diff --git a/sal/osl/unx/file_path_helper.cxx b/sal/osl/unx/file_path_helper.cxx index 04fdd13e7c15..9dd3b08493b0 100644 --- a/sal/osl/unx/file_path_helper.cxx +++ b/sal/osl/unx/file_path_helper.cxx @@ -73,19 +73,21 @@ void SAL_CALL osl_systemPathRemoveSeparator(rtl_uString* pustrPath) { - OSL_PRECOND(pustrPath, "osl_systemPathRemoveSeparator: Invalid parameter"); - - // maybe there are more than one separator at end - // so we run in a loop - while ((pustrPath->length > 1) && (FPH_CHAR_PATH_SEPARATOR == pustrPath->buffer[pustrPath->length - 1])) + OSL_PRECOND(0 != pustrPath, "osl_systemPathRemoveSeparator: Invalid parameter"); + if (0 != pustrPath) { - pustrPath->length--; - pustrPath->buffer[pustrPath->length] = (sal_Unicode)'\0'; - } + // maybe there are more than one separator at end + // so we run in a loop + while ((pustrPath->length > 1) && (FPH_CHAR_PATH_SEPARATOR == pustrPath->buffer[pustrPath->length - 1])) + { + pustrPath->length--; + pustrPath->buffer[pustrPath->length] = (sal_Unicode)'\0'; + } - OSL_POSTCOND((0 == pustrPath->length) || (1 == pustrPath->length) || \ - (pustrPath->length > 1 && pustrPath->buffer[pustrPath->length - 1] != FPH_CHAR_PATH_SEPARATOR), \ - "osl_systemPathRemoveSeparator: Post condition failed"); + OSL_POSTCOND((0 == pustrPath->length) || (1 == pustrPath->length) || \ + (pustrPath->length > 1 && pustrPath->buffer[pustrPath->length - 1] != FPH_CHAR_PATH_SEPARATOR), \ + "osl_systemPathRemoveSeparator: Post condition failed"); + } } /******************************************* @@ -94,21 +96,22 @@ void SAL_CALL osl_systemPathEnsureSeparator(rtl_uString** ppustrPath) { - OSL_PRECOND(ppustrPath && (NULL != *ppustrPath), \ - "osl_systemPathEnsureSeparator: Invalid parameter"); - - rtl::OUString path(*ppustrPath); - sal_Int32 lp = path.getLength(); - sal_Int32 i = path.lastIndexOf(FPH_CHAR_PATH_SEPARATOR); - - if ((lp > 1 && i != (lp - 1)) || ((lp < 2) && i < 0)) - { - path += FPH_PATH_SEPARATOR(); - rtl_uString_assign(ppustrPath, path.pData); - } - - OSL_POSTCOND(path.lastIndexOf(FPH_CHAR_PATH_SEPARATOR) == (path.getLength() - 1), \ - "osl_systemPathEnsureSeparator: Post condition failed"); + OSL_PRECOND((0 != ppustrPath) && (0 != *ppustrPath), "osl_systemPathEnsureSeparator: Invalid parameter"); + if ((0 != ppustrPath) && (0 != *ppustrPath)) + { + rtl::OUString path(*ppustrPath); + sal_Int32 lp = path.getLength(); + sal_Int32 i = path.lastIndexOf(FPH_CHAR_PATH_SEPARATOR); + + if ((lp > 1 && i != (lp - 1)) || ((lp < 2) && i < 0)) + { + path += FPH_PATH_SEPARATOR(); + rtl_uString_assign(ppustrPath, path.pData); + } + + OSL_POSTCOND(path.lastIndexOf(FPH_CHAR_PATH_SEPARATOR) == (path.getLength() - 1), \ + "osl_systemPathEnsureSeparator: Post condition failed"); + } } /******************************************* @@ -117,8 +120,8 @@ sal_Bool SAL_CALL osl_systemPathIsRelativePath(const rtl_uString* pustrPath) { - OSL_PRECOND(pustrPath, "osl_systemPathIsRelativePath: Invalid parameter"); - return ((0 == pustrPath->length) || (pustrPath->buffer[0] != FPH_CHAR_PATH_SEPARATOR)); + OSL_PRECOND(0 != pustrPath, "osl_systemPathIsRelativePath: Invalid parameter"); + return ((0 == pustrPath) || (0 == pustrPath->length) || (pustrPath->buffer[0] != FPH_CHAR_PATH_SEPARATOR)); } /****************************************** @@ -177,21 +180,16 @@ sal_Bool SAL_CALL osl_systemPathIsHiddenFileOrDirectoryEntry( const rtl_uString* pustrPath) { - OSL_PRECOND(pustrPath, "osl_systemPathIsHiddenFileOrDirectoryEntry: Invalid parameter"); - - sal_Bool is_hidden = sal_False; + OSL_PRECOND(0 != pustrPath, "osl_systemPathIsHiddenFileOrDirectoryEntry: Invalid parameter"); + if ((0 == pustrPath) || (0 == pustrPath->length)) + return sal_False; - if (pustrPath->length > 0) - { - rtl::OUString fdp; - - osl_systemPathGetFileNameOrLastDirectoryPart(pustrPath, &fdp.pData); - - is_hidden = ((fdp.pData->length > 0) && (fdp.pData->buffer[0] == FPH_CHAR_DOT) && - !osl_systemPathIsLocalOrParentDirectoryEntry(fdp.pData)); - } + rtl::OUString fdp; + osl_systemPathGetFileNameOrLastDirectoryPart(pustrPath, &fdp.pData); - return is_hidden; + return ((fdp.pData->length > 0) && + (fdp.pData->buffer[0] == FPH_CHAR_DOT) && + !osl_systemPathIsLocalOrParentDirectoryEntry(fdp.pData)); } diff --git a/sal/osl/unx/process_impl.cxx b/sal/osl/unx/process_impl.cxx index 66c0d0f5c701..498778758ded 100644 --- a/sal/osl/unx/process_impl.cxx +++ b/sal/osl/unx/process_impl.cxx @@ -483,17 +483,20 @@ extern "C" int _imp_setProcessLocale( rtl_Locale * ); *********************************************/ oslProcessError SAL_CALL osl_getProcessLocale( rtl_Locale ** ppLocale ) { + oslProcessError result = osl_Process_E_Unknown; OSL_PRECOND(ppLocale, "osl_getProcessLocale(): Invalid parameter."); + if (ppLocale) + { + pthread_mutex_lock(&(g_process_locale.m_mutex)); - pthread_mutex_lock(&(g_process_locale.m_mutex)); - - if (g_process_locale.m_pLocale == 0) - _imp_getProcessLocale (&(g_process_locale.m_pLocale)); - *ppLocale = g_process_locale.m_pLocale; - - pthread_mutex_unlock (&(g_process_locale.m_mutex)); + if (g_process_locale.m_pLocale == 0) + _imp_getProcessLocale (&(g_process_locale.m_pLocale)); + *ppLocale = g_process_locale.m_pLocale; + result = osl_Process_E_None; - return (osl_Process_E_None); + pthread_mutex_unlock (&(g_process_locale.m_mutex)); + } + return (result); } /********************************************** diff --git a/sal/osl/unx/profile.c b/sal/osl/unx/profile.c index c77a27543261..05d816c92755 100644 --- a/sal/osl/unx/profile.c +++ b/sal/osl/unx/profile.c @@ -514,7 +514,6 @@ sal_Bool SAL_CALL osl_readProfileString(oslProfile Profile, if ( pTmpProfile->m_bIsValid == sal_False ) { - OSL_ASSERT(pProfile->m_bIsValid); pthread_mutex_unlock(&(pTmpProfile->m_AccessLock)); #ifdef TRACE_OSL_PROFILE OSL_TRACE("Out osl_readProfileString [not valid]\n"); diff --git a/sal/osl/unx/socket.c b/sal/osl/unx/socket.c index c8faf6c028f5..2f7b62a4bfa4 100644 --- a/sal/osl/unx/socket.c +++ b/sal/osl/unx/socket.c @@ -605,16 +605,16 @@ sal_Bool SAL_CALL osl_isEqualSocketAddr ( oslSocketAddr Addr1, oslSocketAddr Addr2) { - struct sockaddr* pAddr1= &(Addr1->m_sockaddr); - struct sockaddr* pAddr2= &(Addr2->m_sockaddr); - - OSL_ASSERT(pAddr1); - OSL_ASSERT(pAddr2); - - if (pAddr1->sa_family == pAddr2->sa_family) + OSL_ASSERT((0 != Addr1) && (0 != Addr2)); + if ((0 != Addr1) || (0 != Addr2)) { - switch (pAddr1->sa_family) - { + struct sockaddr* pAddr1= &(Addr1->m_sockaddr); + struct sockaddr* pAddr2= &(Addr2->m_sockaddr); + + if (pAddr1->sa_family == pAddr2->sa_family) + { + switch (pAddr1->sa_family) + { case AF_INET: { struct sockaddr_in* pInetAddr1= (struct sockaddr_in*)pAddr1; @@ -623,16 +623,16 @@ sal_Bool SAL_CALL osl_isEqualSocketAddr ( if ((pInetAddr1->sin_family == pInetAddr2->sin_family) && (pInetAddr1->sin_addr.s_addr == pInetAddr2->sin_addr.s_addr) && (pInetAddr1->sin_port == pInetAddr2->sin_port)) - return (sal_True); + return (sal_True); } default: { - return (memcmp(pAddr1, Addr2, sizeof(struct sockaddr)) == 0); + return (memcmp(pAddr1, pAddr2, sizeof(struct sockaddr)) == 0); } - } + } + } } - return (sal_False); } @@ -1173,7 +1173,6 @@ oslHostAddr SAL_CALL osl_createHostAddr ( rtl_string_release(strHostname); } - return HostAddr; } @@ -1197,7 +1196,7 @@ oslHostAddr SAL_CALL osl_psz_createHostAddr ( pHostAddr= (oslHostAddr) malloc(sizeof(struct oslHostAddrImpl)); OSL_ASSERT(pHostAddr); - if (pAddr == NULL) + if (pHostAddr == NULL) { free (cn); return ((oslHostAddr)NULL); @@ -2530,7 +2529,10 @@ sal_Bool __osl_socket_poll ( int timeout; int result; - OSL_ASSERT(pSocket); + OSL_ASSERT(0 != pSocket); + if (0 == pSocket) + return sal_False; /* EINVAL */ + pSocket->m_nLastError = 0; fds.fd = pSocket->m_Socket; @@ -2573,7 +2575,10 @@ sal_Bool __osl_socket_poll ( struct timeval tv; int result; - OSL_ASSERT(pSocket); + OSL_ASSERT(0 != pSocket); + if (0 == pSocket) + return sal_False; /* EINVAL */ + pSocket->m_nLastError = 0; FD_ZERO(&fds); diff --git a/sal/osl/w32/diagnose.c b/sal/osl/w32/diagnose.c index 9c75e4502743..1feab6ded8da 100755 --- a/sal/osl/w32/diagnose.c +++ b/sal/osl/w32/diagnose.c @@ -93,6 +93,7 @@ sal_Bool SAL_CALL osl_assertFailedLine(const sal_Char* pszFileName, sal_Int32 nL /* get app name or NULL if unknown (don't call assert) */ LPCSTR lpszAppName = "Error"; sal_Char szMessage[512]; + char const * env = getenv( "SAL_DIAGNOSE_ABORT" ); /* format message into buffer */ szMessage[sizeof(szMessage)-1] = '\0'; /* zero terminate always */ @@ -105,39 +106,44 @@ sal_Bool SAL_CALL osl_assertFailedLine(const sal_Char* pszFileName, sal_Int32 nL _pPrintDetailedDebugMessage( pszFileName, nLine, pszMessage ); else if ( _pPrintDebugMessage ) _pPrintDebugMessage( szMessage ); - else if ( !getenv( "DISABLE_SAL_DBGBOX" ) ) + else { - TCHAR szBoxMessage[1024]; + if ( !getenv( "DISABLE_SAL_DBGBOX" ) ) + { + TCHAR szBoxMessage[1024]; - /* active popup window for the current thread */ - hWndParent = GetActiveWindow(); - if (hWndParent != NULL) - hWndParent = GetLastActivePopup(hWndParent); + /* active popup window for the current thread */ + hWndParent = GetActiveWindow(); + if (hWndParent != NULL) + hWndParent = GetLastActivePopup(hWndParent); - /* set message box flags */ - nFlags = MB_TASKMODAL | MB_ICONWARNING | MB_YESNOCANCEL | MB_DEFBUTTON2 | MB_SETFOREGROUND; - if (hWndParent == NULL) - nFlags |= MB_SERVICE_NOTIFICATION; + /* set message box flags */ + nFlags = MB_TASKMODAL | MB_ICONWARNING | MB_YESNOCANCEL | MB_DEFBUTTON2 | MB_SETFOREGROUND; + if (hWndParent == NULL) + nFlags |= MB_SERVICE_NOTIFICATION; - /* display the assert */ + /* display the assert */ - szBoxMessage[sizeof(szBoxMessage)-1] = 0; - _snprintf(szBoxMessage, sizeof(szBoxMessage)-1, "%s\n( Yes=Abort / No=Ignore / Cancel=Debugger )", - szMessage); + szBoxMessage[sizeof(szBoxMessage)-1] = 0; + _snprintf(szBoxMessage, sizeof(szBoxMessage)-1, "%s\n( Yes=Abort / No=Ignore / Cancel=Debugger )", + szMessage); - nCode = MessageBox(hWndParent, szBoxMessage, "Assertion Failed!", nFlags); + nCode = MessageBox(hWndParent, szBoxMessage, "Assertion Failed!", nFlags); - if (nCode == IDYES) - FatalExit(-1); + if (nCode == IDYES) + FatalExit(-1); - if (nCode == IDNO) - return sal_False; /* ignore */ + if (nCode == IDNO) + return sal_False; /* ignore */ - if (nCode == IDCANCEL) - return sal_True; /* will cause oslDebugBreak */ + if (nCode == IDCANCEL) + return sal_True; /* will cause oslDebugBreak */ + } + return ( ( env != NULL ) && ( *env != '\0' ) ); } + + return sal_False; #endif /* NO_DEBUG_CRT */ - return sal_False; /* not shure, not care */ } sal_Int32 SAL_CALL osl_reportError(sal_uInt32 nType, const sal_Char* pszMessage) diff --git a/sal/osl/w32/file_dirvol.cxx b/sal/osl/w32/file_dirvol.cxx index 09bdec7988c3..549f42861f94 100644 --- a/sal/osl/w32/file_dirvol.cxx +++ b/sal/osl/w32/file_dirvol.cxx @@ -155,7 +155,7 @@ namespace /* private */ void parse_UNC_path(const sal_Unicode* path, UNCComponents* puncc) { OSL_PRECOND(is_UNC_path(path), "Precondition violated: No UNC path"); - OSL_PRECOND(rtl_ustr_indexOfChar(path, SLASH) != -1, "Path must not contain slashes"); + OSL_PRECOND(rtl_ustr_indexOfChar(path, SLASH) == -1, "Path must not contain slashes"); const sal_Unicode* pend = path + rtl_ustr_getLength(path); const sal_Unicode* ppos = path + 2; @@ -691,7 +691,7 @@ static int path_make_parent(sal_Unicode* path) If there are no more parents 0 will be returned, e.g. 'c:\' or '\\Share' have no more parents */ - OSL_PRECOND(rtl_ustr_indexOfChar(path, SLASH) != -1, "Path must not contain slashes"); + OSL_PRECOND(rtl_ustr_indexOfChar(path, SLASH) == -1, "Path must not contain slashes"); OSL_PRECOND(has_path_parent(path), "Path must have a parent"); sal_Unicode* pos_last_backslash = path + rtl_ustr_lastIndexOfChar(path, BACKSLASH); diff --git a/sal/osl/w32/module.cxx b/sal/osl/w32/module.cxx index b730bd3347df..65a17eb1352d 100644..100755 --- a/sal/osl/w32/module.cxx +++ b/sal/osl/w32/module.cxx @@ -36,6 +36,7 @@ #include <osl/thread.h> #include <osl/file.h> #include <rtl/logfile.h> +#include <vector> /* under WIN32, we use the void* oslModule @@ -65,10 +66,32 @@ oslModule SAL_CALL osl_loadModule(rtl_uString *strModuleName, sal_Int32 nRtldMod rtl_uString_assign(&Module, strModuleName); hInstance = LoadLibraryW(reinterpret_cast<LPCWSTR>(Module->buffer)); + if (hInstance == NULL) hInstance = LoadLibraryExW(reinterpret_cast<LPCWSTR>(Module->buffer), NULL, LOAD_WITH_ALTERED_SEARCH_PATH); + //In case of long path names (\\?\c:\...) try to shorten the filename. + //LoadLibrary cannot handle file names which exceed 260 letters. + //In case the path is to long, the function will fail. However, the error + //code can be different. For example, it returned ERROR_FILENAME_EXCED_RANGE + //on Windows XP and ERROR_INSUFFICIENT_BUFFER on Windows 7 (64bit) + if (hInstance == NULL && Module->length > 260) + { + std::vector<sal_Unicode, rtl::Allocator<sal_Unicode> > vec(Module->length + 1); + DWORD len = GetShortPathNameW(reinterpret_cast<LPCWSTR>(Module->buffer), + &vec[0], Module->length + 1); + if (len ) + { + hInstance = LoadLibraryW(&vec[0]); + + if (hInstance == NULL) + hInstance = LoadLibraryExW(&vec[0], NULL, + LOAD_WITH_ALTERED_SEARCH_PATH); + } + } + + if (hInstance <= (HINSTANCE)HINSTANCE_ERROR) hInstance = 0; diff --git a/sal/osl/w32/procimpl.cxx b/sal/osl/w32/procimpl.cxx index fc04d5b84a8f..a2f86422df2a 100644..100755 --- a/sal/osl/w32/procimpl.cxx +++ b/sal/osl/w32/procimpl.cxx @@ -300,6 +300,39 @@ namespace /* private */ return quoted.makeStringAndClear(); } + //The parameter path must be a system path. If it is longer than 260 characters + //then it is shortened using the GetShortPathName function. This function only + //works if the path exists. Because "path" can be the path to an executable, it + //may not have the file extension ".exe". However, if the file on disk has the + //".exe" extension, then the function will fail. In this case a second attempt + //is started by adding the parameter "extension" to "path". + rtl::OUString getShortPath(rtl::OUString const & path, rtl::OUString const & extension) + { + rtl::OUString ret(path); + if (path.getLength() > 260) + { + std::vector<sal_Unicode, rtl::Allocator<sal_Unicode> > vec(path.getLength() + 1); + //GetShortPathNameW only works if the file can be found! + const DWORD len = GetShortPathNameW( + path.getStr(), &vec[0], path.getLength() + 1); + + if (!len && GetLastError() == ERROR_FILE_NOT_FOUND + && extension.getLength()) + { + const rtl::OUString extPath(path + extension); + std::vector<sal_Unicode, rtl::Allocator<sal_Unicode> > vec2( + extPath.getLength() + 1); + const DWORD len2 = GetShortPathNameW( + extPath.getStr(), &vec2[0], extPath.getLength() + 1); + ret = rtl::OUString(&vec2[0], len2); + } + else + { + ret = rtl::OUString(&vec[0], len); + } + } + return ret; + } //########################################################## // Returns the system path of the executable which can either // be provided via the strImageName parameter or as first @@ -326,6 +359,8 @@ namespace /* private */ if (osl_File_E_None != osl::FileBase::getSystemPathFromFileURL(exe_url, exe_path)) return rtl::OUString(); + exe_path = getShortPath(exe_path, rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(".exe"))); + if (exe_path.indexOf(' ') != -1) exe_path = quote_string(exe_path); diff --git a/sal/prj/d.lst b/sal/prj/d.lst index 3ce270bbbfc4..253d7ea7f718 100644 --- a/sal/prj/d.lst +++ b/sal/prj/d.lst @@ -1,9 +1,11 @@ +mkdir: %_DEST%\inc%_EXT%\cppunittester mkdir: %_DEST%\inc%_EXT%\sal mkdir: %_DEST%\inc%_EXT%\osl mkdir: %_DEST%\inc%_EXT%\rtl mkdir: %_DEST%\inc%_EXT%\systools mkdir: %_DEST%\inc%_EXT%\systools\win32 +..\inc\cppunittester\protectorfactory.hxx %_DEST%\inc%_EXT%\protectorfactory.hxx ..\%__SRC%\inc\rtlbootstrap.mk %_DEST%\inc%_EXT%\rtlbootstrap.mk ..\inc\sal\*.h %_DEST%\inc%_EXT%\sal\*.h ..\%__SRC%\inc\sal\typesizes.h %_DEST%\inc%_EXT%\sal\typesizes.h diff --git a/sal/rtl/source/alloc.c b/sal/rtl/source/alloc.c deleted file mode 100644 index 44b37c255004..000000000000 --- a/sal/rtl/source/alloc.c +++ /dev/null @@ -1,1541 +0,0 @@ -/************************************************************************* - * - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * Copyright 2000, 2010 Oracle and/or its affiliates. - * - * OpenOffice.org - a multi-platform office productivity suite - * - * This file is part of OpenOffice.org. - * - * OpenOffice.org is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License version 3 - * only, as published by the Free Software Foundation. - * - * OpenOffice.org is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License version 3 for more details - * (a copy is included in the LICENSE file that accompanied this code). - * - * You should have received a copy of the GNU Lesser General Public License - * version 3 along with OpenOffice.org. If not, see - * <http://www.openoffice.org/license.html> - * for a copy of the LGPLv3 License. - * - ************************************************************************/ - -#ifdef PROFILE -#undef OSL_DEBUG_LEVEL -#define OSL_DEBUG_LEVEL 0 -#endif /* PROFILE */ - -#include <sal/types.h> -#include <osl/diagnose.h> -#include <rtl/alloc.h> - -#ifndef INCLUDED_STDDEF_H -#include <stddef.h> -#define INCLUDED_STDDEF_H -#endif - -#ifndef INCLUDED_STDLIB_H -#include <stdlib.h> -#define INCLUDED_STDLIB_H -#endif - -#ifndef INCLUDED_STRING_H -#include <string.h> -#define INCLUDED_STRING_H -#endif - -#ifndef FORCE_SYSALLOC - -/*=========================================================================== - * - * rtl_memory (UNX) internals. - * - *=========================================================================*/ -#ifdef SAL_UNX - -#include <unistd.h> -#include <pthread.h> -#include <sys/mman.h> -#include <fcntl.h> - -typedef pthread_mutex_t mutex_type; - -#define RTL_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER -#define RTL_MUTEX_ACQUIRE(a) pthread_mutex_lock((a)) -#define RTL_MUTEX_RELEASE(a) pthread_mutex_unlock((a)) - -#if defined(FREEBSD) || defined(NETBSD) || defined(MACOSX) -static sal_Size __rtl_memory_vmpagesize (void) -{ - /* xBSD */ - return (sal_Size)(getpagesize()); -} -#elif defined(LINUX) || defined(SOLARIS) -static sal_Size __rtl_memory_vmpagesize (void) -{ - /* POSIX */ - return (sal_Size)(sysconf(_SC_PAGESIZE)); -} -#else -static sal_Size __rtl_memory_vmpagesize (void) -{ - /* other */ - return (sal_Size)(0x2000); -} -#endif /* FREEBSD || NETBSD || MACOSX || LINUX || SOLARIS */ - -#ifndef PROT_HEAP -#define PROT_HEAP (PROT_READ | PROT_WRITE | PROT_EXEC) -#endif - -/* #95880# building on Solaris 8 provides MAP_ANON, but it - is not available on Solaris 7 */ -#if defined (SOLARIS) -#ifdef MAP_ANON -#undef MAP_ANON -#endif -#endif - -#ifndef MAP_ANON -static void* __rtl_memory_vmalloc (sal_Size n) -{ - /* SYSV */ - int fd = open("/dev/zero", O_RDWR); - if (!(fd < 0)) - { - void * p = mmap(NULL, n, PROT_HEAP, MAP_PRIVATE, fd, 0); - close(fd); - return ((p == MAP_FAILED) ? NULL : p); - } - return (NULL); -} -#else /* MAP_ANON */ -static void* __rtl_memory_vmalloc (sal_Size n) -{ - /* xBSD */ - void * p = mmap(NULL, n, PROT_HEAP, MAP_PRIVATE | MAP_ANON, -1, 0); - return ((p == MAP_FAILED) ? NULL : p); -} -#endif /* MAP_ANON */ - -#define RTL_MEMORY_ALLOC(n) __rtl_memory_vmalloc((sal_Size)(n)) -#define RTL_MEMORY_FREE(p, n) munmap((void*)(p), (sal_Size)(n)) - -#endif /* SAL_UNX */ - -/*=========================================================================== - * - * rtl_memory (W32) internals. - * - *=========================================================================*/ -#ifdef SAL_W32 - -#define WIN32_LEAN_AND_MEAN -#ifdef _MSC_VER -#pragma warning(push,1) /* disable warnings within system headers */ -#endif -#include <windows.h> -#include <wchar.h> - -typedef CRITICAL_SECTION mutex_type; - -/* Static initializer (struct declared in WINNT.H). */ -#define RTL_MUTEX_INITIALIZER { NULL, -1, 0, NULL, NULL, 0 } - -/* - * __rtl_mutex_init (dynamic initialization). - * - * Static initialization (with DebugInfo == NULL) - * leads to Access Violation upon first contention. - */ -static void __rtl_mutex_init (LPCRITICAL_SECTION lpCriticalSection) -{ - static LONG g_spinlock = 0; - - while (InterlockedExchange (&g_spinlock, 1) == 1) - { - /* Already locked, spin */ - Sleep (0); - } - if (!(lpCriticalSection->DebugInfo)) - { - /* Dynamic initialization */ - InitializeCriticalSection (lpCriticalSection); - } - InterlockedExchange (&g_spinlock, 0); -} - -#define RTL_MUTEX_INIT(a) __rtl_mutex_init((LPCRITICAL_SECTION)(a)) -#define RTL_MUTEX_ACQUIRE(a) EnterCriticalSection((a)) -#define RTL_MUTEX_RELEASE(a) LeaveCriticalSection((a)) - -static sal_Size __rtl_memory_vmpagesize (void) -{ - SYSTEM_INFO info; - GetSystemInfo (&info); - return ((sal_Size)(info.dwPageSize)); -} - -#define RTL_MEMORY_ALLOC(n) \ -(void*)(VirtualAlloc (NULL, (SIZE_T)(n), MEM_COMMIT, PAGE_READWRITE)) - -#define RTL_MEMORY_FREE(p, n) \ -(void)(VirtualFree ((LPVOID)(p), (SIZE_T)(0), MEM_RELEASE)) - -#endif /* SAL_W32 */ - -/*=========================================================================== - * - * rtl_memory (OS2) internals. - * - *=========================================================================*/ -#ifdef SAL_OS2 - -#define INCL_DOS -#include <os2.h> - -typedef HMTX mutex_type; - -/* Static initializer */ -#define RTL_MUTEX_INITIALIZER -1 - -/* - * __rtl_mutex_init (dynamic initialization). - * - * Static initialization (with DebugInfo == NULL) - * leads to Access Violation upon first contention. - */ -static void __rtl_mutex_init (mutex_type* mutex) -{ - APIRET rc = 0; - - rc = DosCreateMutexSem(NULL,mutex,0,0); - -} - -static int __rtl_mutex_destroy (mutex_type* mutex) -{ - APIRET rc = 0; - - - do { - rc = DosCloseMutexSem(*mutex); - if (rc == 301) DosReleaseMutexSem(*mutex); - } while (rc == 301); - - *mutex = 0; - - /* Return the completion status: */ - return (0); -} - - -static int __rtl_mutex_acquire(mutex_type* mutex) -{ - int ret = 0; - int status = 0; - APIRET rc = 0; - - // initialize static semaphores created with PTHREAD_MUTEX_INITIALIZER state. - if (*mutex == -1) - __rtl_mutex_init( mutex); - - rc = DosRequestMutexSem(*mutex,SEM_INDEFINITE_WAIT); - if (rc) - return(1); - - /* Return the completion status: */ - return (0); -} - -static int __rtl_mutex_release(mutex_type* mutex) -{ - int ret = 0; - APIRET rc = 0; - int status; - - - // initialize static semaphores created with PTHREAD_MUTEX_INITIALIZER state. - if (*mutex == -1) - __rtl_mutex_init( mutex); - - rc = DosReleaseMutexSem(*mutex); - - /* Return the completion status: */ - return (0); -} - -#define RTL_MUTEX_INIT(a) __rtl_mutex_init((mutex_type*)(a)) -#define RTL_MUTEX_ACQUIRE(a) __rtl_mutex_acquire((mutex_type*)(a)) -#define RTL_MUTEX_RELEASE(a) __rtl_mutex_release((mutex_type*)(a)) - -static sal_Size __rtl_memory_vmpagesize (void) -{ - return (sal_Size)(getpagesize()); -} - -#define RTL_MEMORY_ALLOC(n) (void*)(malloc(n)) - -#define RTL_MEMORY_FREE(p, n) (void)(free(p)) - -#endif /* SAL_OS2 */ - -/*=========================================================================== - * - * rtl_memory (global) internals. - * - *=========================================================================*/ -#define __L__ 32 -#define __P__ 24 -#define __N__ ((__L__) + (__P__)) -#define __M__ 0x10000 - -static const sal_Size __T__ = (__M__) * 2 / 3; - -typedef struct __rtl_memory_desc_st memory_type; -struct __rtl_memory_desc_st -{ - sal_Size m_length; - sal_Size m_offset; - memory_type *m_flink; - memory_type *m_blink; -}; - -static const int __C__ = 2 * sizeof(sal_Size); -static const int __Q__ = 2 * sizeof(memory_type*); - -typedef struct __rtl_memory_stat_st memory_stat; -struct __rtl_memory_stat_st -{ - sal_uInt64 m_dequeue; - sal_uInt64 m_enqueue; - sal_Int32 m_delta_q; - - sal_uInt64 m_deqsize; - sal_uInt64 m_enqsize; - sal_Int32 m_delta_n; -}; - -#define RTL_MEMORY_ALIGN(n, m) (((n) + ((m) - 1)) & ~((m) - 1)) -#define RTL_MEMORY_SIZEOF(a) RTL_MEMORY_ALIGN(sizeof(a), sizeof(memory_type)) - -struct __rtl_memory_global_st -{ - sal_Size m_magic; - sal_Size m_align; - - union { - mutex_type m_lock; - char m_data[RTL_MEMORY_SIZEOF(mutex_type)]; - } m_mutex; - - memory_type m_alloc_head; - memory_type m_spare_head; - memory_type m_queue_head[__N__]; - -#if OSL_DEBUG_LEVEL > 0 - memory_stat m_queue_stat[__N__]; -#endif /* OSL_DEBUG_LEVEL */ -}; - -static struct __rtl_memory_global_st g_memory = -{ - 0, 0, { RTL_MUTEX_INITIALIZER }, - { 0, 0, NULL, NULL }, { 0, 0, NULL, NULL }, { { 0, 0, NULL, NULL } }, -#if OSL_DEBUG_LEVEL > 0 - { { 0, 0, 0, 0, 0, 0 } } -#endif /* OSL_DEBUG_LEVEL */ -}; - -void SAL_CALL ___rtl_memory_init (void); -void SAL_CALL ___rtl_memory_fini (void); - -#define RTL_MEMORY_ENTER() \ -{ \ - if (!(g_memory.m_align)) ___rtl_memory_init(); \ - RTL_MUTEX_ACQUIRE(&(g_memory.m_mutex.m_lock)); \ -} - -#define RTL_MEMORY_LEAVE() \ -{ \ - RTL_MUTEX_RELEASE(&(g_memory.m_mutex.m_lock)); \ -} - -/*=========================================================================== - * - * rtl_memory (queue) internals. - * - *=========================================================================*/ -#if defined(PROFILE) || (OSL_DEBUG_LEVEL > 0) -static sal_Size queue (sal_Size n) -{ - /* k = n div __C__ */ - register sal_Size k = n / __C__, m = __L__; - - OSL_PRECOND((__L__ == 32), - "__rtl_memory_queue(): internal logic error"); - if (k > m) - { - /* k = k div __L__ = k div 32 */ - k >>= 5; - while ((k >>= 1) > 0) m++; - k = m; - } - - OSL_POSTCOND((0 < k) && (k < __N__), - "__rtl_memory_queue(): " - "internal error: index out of bounds"); - return (k); -} -#else /* PRODUCT */ -#define queue(k, n) \ -{ \ - (k) = ((n) / __C__); \ - if ((k) > __L__) \ - { \ - register sal_Size m = __L__; \ - (k) >>= 5; \ - while (((k) >>= 1) > 0) m++; \ - (k) = m; \ - } \ -} -#endif /* OSL_DEBUG_LEVEL || PRODUCT */ - -#define queue_start(entry) \ -{ \ - (entry)->m_flink = (entry); \ - (entry)->m_blink = (entry); \ -} - -#define queue_remove(entry) \ -{ \ - (entry)->m_blink->m_flink = (entry)->m_flink; \ - (entry)->m_flink->m_blink = (entry)->m_blink; \ - queue_start(entry); \ -} - -#define queue_insert_head(head, entry) \ -{ \ - (entry)->m_blink = (head); \ - (entry)->m_flink = (head)->m_flink; \ - (head)->m_flink = (entry); \ - (entry)->m_flink->m_blink = (entry); \ -} - -#define queue_insert_tail(head, entry) \ -{ \ - (entry)->m_flink = (head); \ - (entry)->m_blink = (head)->m_blink; \ - (head)->m_blink = (entry); \ - (entry)->m_blink->m_flink = (entry); \ -} - -/*=========================================================================== - * - * rtl_memory (debug) internals. - * - *=========================================================================*/ -#if OSL_DEBUG_LEVEL > 0 - -#define __dbg_memory_succ(entry, length) \ -(memory_type*)((char*)((entry)) + ((length) & ~0x1)) - -#define __dbg_memory_pred(entry, offset) \ -(memory_type*)((char*)((entry)) - ((offset) & ~0x1)) - -#define __dbg_memory_ensure(entry) (!((sal_Size)(entry) & 0x7)) - -/* - * __dbg_memory_dequeue. - */ -static void __dbg_memory_dequeue (sal_Size n) -{ - register sal_Size k = queue(n); - - g_memory.m_queue_stat[k].m_dequeue += 1; - g_memory.m_queue_stat[k].m_delta_q += 1; - - g_memory.m_queue_stat[k].m_deqsize += n; - g_memory.m_queue_stat[k].m_delta_n += n; -} - -/* - * __dbg_memory_enqueue. - */ -static void __dbg_memory_enqueue (sal_Size n) -{ - register sal_Size k = queue(n); - - g_memory.m_queue_stat[k].m_enqueue += 1; - g_memory.m_queue_stat[k].m_delta_q -= 1; - - g_memory.m_queue_stat[k].m_enqsize += n; - g_memory.m_queue_stat[k].m_delta_n -= n; -} - -/* - * __dbg_memory_insert. - */ -static void __dbg_memory_insert (memory_type **ppMemory) -{ - register memory_type * succ; - succ = __dbg_memory_succ (*ppMemory, sizeof(memory_type)); - - succ->m_length = (*ppMemory)->m_length - sizeof(memory_type); - succ->m_offset = (*ppMemory)->m_offset; - - queue_insert_tail (&(g_memory.m_alloc_head), (*ppMemory)); - (*ppMemory) = succ; -} - -/* - * __dbg_memory_remove. - */ -static void __dbg_memory_remove (memory_type **ppMemory) -{ - (*ppMemory) = __dbg_memory_pred (*ppMemory, sizeof(memory_type)); - queue_remove (*ppMemory); -} - -/* - * __dbg_memory_verify_chain. - */ -static int __dbg_memory_verify_chain (memory_type * x) -{ - if (!__dbg_memory_ensure(x)) - { - OSL_ENSURE(0, "__rtl_memory_verify(): invalid pointer alignment."); - return (0); - } - if (!__dbg_memory_ensure(x->m_length & ~0x1)) - { - OSL_ENSURE(0, "__rtl_memory_verify(): dynamic memory corruption"); - return (0); - } - if (!__dbg_memory_ensure(x->m_offset & ~0x1)) - { - OSL_ENSURE(0, "__rtl_memory_verify(): dynamic memory corruption"); - return (0); - } - if (!(x->m_length & ~0x1)) - { - OSL_ENSURE(0, "__rtl_memory_verify(): dynamic memory corruption"); - return (0); - } - return (1); -} - -/* - * __dbg_memory_verify_queue. - */ -static int __dbg_memory_verify_queue (memory_type * x) -{ - if (!__dbg_memory_ensure(x)) - { - OSL_ENSURE(0, "__rtl_memory_verify(): invalid pointer alignment."); - return (0); - } - if (!__dbg_memory_ensure(x->m_flink)) - { - OSL_ENSURE(0, "__rtl_memory_verify(): free memory corruption"); - return (0); - } - if (!__dbg_memory_ensure(x->m_blink)) - { - OSL_ENSURE(0, "__rtl_memory_verify(): free memory corruption"); - return (0); - } - if ((x == x->m_flink) || (x == x->m_blink)) - { - OSL_ENSURE(0, "__rtl_memory_verify(): internal logic error"); - return (0); - } - return (1); -} - -/* - * __dbg_memory_verify_alloc. - */ -static int __dbg_memory_verify_alloc (memory_type * x) -{ - register memory_type *head, *entry; - head = entry = &(g_memory.m_alloc_head); - - if (!__dbg_memory_ensure(x)) - { - OSL_ENSURE(0, "__rtl_memory_verify(): invalid pointer alignment."); - return (0); - } - while (!((entry = entry->m_flink) == head)) - { - if ((entry < x) && (x < __dbg_memory_succ(entry, entry->m_length))) - { - head = entry = __dbg_memory_succ(entry, sizeof(memory_type)); - while (!((x == entry) || (entry->m_offset & 0x1))) - { - /* no match, not last */ - if (!__dbg_memory_verify_chain (entry)) - return (0); - entry = __dbg_memory_succ(entry, entry->m_length); - } - - /* match, or last */ - if (!__dbg_memory_verify_chain (entry)) - return (0); - break; - } - } - if (!(x == entry)) - { - OSL_ENSURE(0, "__rtl_memory_verify(): memory not allocated."); - return (0); - } - return (1); -} - -/* - * __dbg_memory_verify. - */ -static int __dbg_memory_verify (memory_type * x, int debug) -{ - /* dispatch upon 'debug' level */ - if (debug) - { - /* verify allocation */ - if (!__dbg_memory_verify_alloc (x)) - return (0); - } - else - { - /* verify 'chain' fields */ - if (!__dbg_memory_verify_chain (x)) - return (0); - } - - /* verify 'used' bit */ - if (!(x->m_length & 0x1)) - { - OSL_ENSURE(0, "__rtl_memory_verify(): memory not used."); - return (0); - } - return (1); -} - -#if OSL_DEBUG_LEVEL > 1 -/* - * __dbg_memory_usage_update. - */ -static sal_Size __dbg_memory_usage_update (memory_stat * stat, sal_Size length) -{ - register sal_Size n = (length & ~0x1), k = queue(n); - - stat[k].m_dequeue += 1; - stat[k].m_deqsize += n; - - if (!(length & 0x1)) - { - /* not used */ - stat[k].m_enqueue += 1; - stat[k].m_enqsize += n; - return (n); - } - else - { - /* used */ - stat[k].m_delta_q += 1; - stat[k].m_delta_n += n; - return (0); - } -} - -/* - * __dbg_memory_usage. - */ -static void __dbg_memory_usage (memory_stat * total) -{ - register memory_type *head, *entry, *memory; - memory_stat stat[__N__]; - - memset (stat, 0, __N__ * sizeof(memory_stat)); - - head = entry = &(g_memory.m_alloc_head); - while (!((entry = entry->m_flink) == head)) - { - register sal_Size k = 0, n = entry->m_length - sizeof(memory_type); - - memory = __dbg_memory_succ(entry, sizeof(memory_type)); - while (!(memory->m_offset & 0x1)) - { - /* not last */ - k += __dbg_memory_usage_update (stat, memory->m_length); - memory = __dbg_memory_succ(memory, memory->m_length); - } - - k += __dbg_memory_usage_update (stat, memory->m_length); - OSL_TRACE("%x %10d %10d", (sal_Size)(entry), n, k); - } - - if (total) - { - sal_Size i; - - memset (total, 0, sizeof(memory_stat)); - for (i = 0; i < __N__; i++) - { - total->m_dequeue += stat[i].m_dequeue; - total->m_enqueue += stat[i].m_enqueue; - total->m_delta_q += stat[i].m_delta_q; - - total->m_deqsize += stat[i].m_deqsize; - total->m_enqsize += stat[i].m_enqsize; - total->m_delta_n += stat[i].m_delta_n; - } - } -} -#endif /* OSL_DEBUG_LEVEL */ - -#endif /* OSL_DEBUG_LEVEL */ -#if OSL_DEBUG_LEVEL > 0 - -#define DBG_MEMORY_DEQUEUE(n) __dbg_memory_dequeue((sal_Size)(n) & ~0x1) -#define DBG_MEMORY_ENQUEUE(n) __dbg_memory_enqueue((sal_Size)(n) & ~0x1) - -#define DBG_MEMORY_DEQFILL(entry, offset, length) \ - memset(((char*)(entry) + (offset)), 0x77777777, (length)) -#define DBG_MEMORY_ENQFILL(entry, offset, length) \ - memset(((char*)(entry) + (offset)), 0x33333333, (length)) - -#define DBG_MEMORY_INSERT(entry) __dbg_memory_insert((entry)) -#define DBG_MEMORY_REMOVE(entry) __dbg_memory_remove((entry)) - -#if OSL_DEBUG_LEVEL > 1 -#define DBG_MEMORY_VERIFY(entry) __dbg_memory_verify((entry), 1) -#else /* OSL_DEBUG_LEVEL > 0 */ -#define DBG_MEMORY_VERIFY(entry) __dbg_memory_verify((entry), 0) -#endif /* OSL_DEBUG_LEVEL */ - -#define DBG_MEMORY_VERIFY_CHAIN(entry) __dbg_memory_verify_chain((entry)) -#define DBG_MEMORY_VERIFY_QUEUE(entry) __dbg_memory_verify_queue((entry)) - -#else /* PRODUCT */ - -#define DBG_MEMORY_DEQUEUE(n) -#define DBG_MEMORY_ENQUEUE(n) - -#define DBG_MEMORY_DEQFILL(entry, offset, length) -#define DBG_MEMORY_ENQFILL(entry, offset, length) - -#define DBG_MEMORY_INSERT(entry) -#define DBG_MEMORY_REMOVE(entry) - -#define DBG_MEMORY_VERIFY(entry) -#define DBG_MEMORY_VERIFY_CHAIN(entry) -#define DBG_MEMORY_VERIFY_QUEUE(entry) - -#endif /* OSL_DEBUG_LEVEL || PRODUCT */ - -/*=========================================================================== - * - * rtl_memory (manager) internals. - * - *=========================================================================*/ -#define queue_cast(entry, offset) \ -((memory_type*)((char*)(entry) + (ptrdiff_t)(offset))) - -#define __rtl_memory_used(entry) ((entry)->m_length & 0x1) -#define __rtl_memory_last(entry) ((entry)->m_offset & 0x1) -#define __rtl_memory_offset(entry) \ - ((ptrdiff_t)((entry)->m_offset & ~0x1)) - -/* - * ___rtl_memory_init. - */ -void SAL_CALL ___rtl_memory_init (void) -{ -#if defined(RTL_MUTEX_INIT) - RTL_MUTEX_INIT (&(g_memory.m_mutex.m_lock)); -#endif /* RTL_MUTEX_INIT */ - - RTL_MUTEX_ACQUIRE(&(g_memory.m_mutex.m_lock)); - if (!(g_memory.m_align)) - { - sal_Size pagesize; - int i; - - queue_start (&(g_memory.m_alloc_head)); - queue_start (&(g_memory.m_spare_head)); - - for (i = 0; i < __N__; i++) - queue_start (&(g_memory.m_queue_head[i])); - for (i = 1; i <= __L__; i++) - g_memory.m_queue_head[i].m_length = i * __C__; - for (i = __L__ + 1; i < __N__; i++) - g_memory.m_queue_head[i].m_length = - 2 * g_memory.m_queue_head[i - 1].m_length; - - pagesize = __rtl_memory_vmpagesize(); - g_memory.m_align = RTL_MEMORY_ALIGN(__M__, pagesize); - } - RTL_MUTEX_RELEASE(&(g_memory.m_mutex.m_lock)); -} - -/* - * ___rtl_memory_fini. - */ -void SAL_CALL ___rtl_memory_fini (void) -{ -#if OSL_DEBUG_LEVEL > 1 - - memory_stat total; - - __dbg_memory_usage (&total); - if (total.m_delta_n > 0) - { - OSL_TRACE("___rtl_memory_fini(): " - "Leak: %10d (Alloc: %10d, Free: %10d)", - total.m_delta_n, - (sal_uInt32)(total.m_deqsize & 0xffffffff), - (sal_uInt32)(total.m_enqsize & 0xffffffff)); - } - -#endif /* OSL_DEBUG_LEVEL */ -} - -/* - * __rtl_memory_merge. - */ -#if defined(PROFILE) || (OSL_DEBUG_LEVEL > 0) -static void __rtl_memory_merge (memory_type * prev, memory_type * next) -{ - /* adjust length */ - prev->m_length += next->m_length; - if (!__rtl_memory_last(next)) - { - /* not last, adjust offset */ - register memory_type * succ = queue_cast(prev, prev->m_length); - DBG_MEMORY_VERIFY_CHAIN (succ); - succ->m_offset = prev->m_length | __rtl_memory_last(succ); - } - - /* propagate 'last' bit */ - prev->m_offset |= __rtl_memory_last(next); -} -#else /* PRODUCT */ -#define __rtl_memory_merge(prev, next) \ -{ \ - (prev)->m_length += (next)->m_length; \ - if (!__rtl_memory_last((next))) \ - { \ - register memory_type * succ = queue_cast((prev), (prev)->m_length); \ - succ->m_offset = (prev)->m_length | __rtl_memory_last(succ); \ - } \ - (prev)->m_offset |= __rtl_memory_last((next)); \ -} -#endif /* OSL_DEBUG_LEVEL || PRODUCT */ - -/* - * __rtl_memory_split. - */ -#if defined(PROFILE) || (OSL_DEBUG_LEVEL > 0) -static void __rtl_memory_split (memory_type * prev, memory_type * next) -{ - /* adjust length */ - prev->m_length -= next->m_length; - if (!__rtl_memory_last(prev)) - { - /* not last, adjust offset */ - register memory_type * succ = queue_cast(next, next->m_length); - DBG_MEMORY_VERIFY_CHAIN (succ); - succ->m_offset = next->m_length | __rtl_memory_last(succ); - } - - /* propagate 'last' bit */ - next->m_offset |= __rtl_memory_last(prev); - prev->m_offset &= ~0x1; -} -#else /* PRODUCT */ -#define __rtl_memory_split(prev, next) \ -{ \ - (prev)->m_length -= (next)->m_length; \ - if (!__rtl_memory_last((prev))) \ - { \ - register memory_type * succ = queue_cast((next), (next)->m_length); \ - succ->m_offset = (next)->m_length | __rtl_memory_last(succ); \ - } \ -\ - (next)->m_offset |= __rtl_memory_last((prev)); \ - (prev)->m_offset &= ~0x1; \ -} -#endif /* OSL_DEBUG_LEVEL || PRODUCT */ - -/* - * __rtl_memory_insert. - */ -#if defined(PROFILE) || (OSL_DEBUG_LEVEL > 0) -static void __rtl_memory_insert (memory_type * memory, sal_Size n) -{ - /* obtain queue head */ - register memory_type *head; - - head = &(g_memory.m_queue_head[queue(n)]); - DBG_MEMORY_VERIFY_CHAIN (head); - - /* insert at queue tail (first-in first-out) */ - queue_insert_tail (head, memory); -} -#else /* PRODUCT */ -#define __rtl_memory_insert(memory, n) \ -{ \ - register sal_Size h; \ -\ - queue(h, (n)); \ - queue_insert_tail (&(g_memory.m_queue_head[h]), (memory)); \ -} -#endif /* OSL_DEBUG_LEVEL || PRODUCT */ - -/* - * __rtl_memory_resize. - */ -#if defined(PROFILE) || (OSL_DEBUG_LEVEL > 0) -static void __rtl_memory_resize (memory_type * memory, sal_Size n) -{ - register sal_Size k = (memory->m_length - n); - - OSL_ENSURE(!(memory->m_length & 0x1), - "__rtl_memory_resize(): " - "internal logic error."); - - if ((k >= sizeof(memory_type)) && (n <= __T__)) - { - /* split */ - register memory_type * remain = queue_cast(memory, n); - - remain->m_length = k; remain->m_offset = n; - __rtl_memory_split (memory, remain); - - /* check postcond */ - if (!__rtl_memory_last(remain)) - { - /* not last, verify used next entry */ - register memory_type *next; - - next = queue_cast(remain, remain->m_length); - DBG_MEMORY_VERIFY_CHAIN (next); - - OSL_POSTCOND(__rtl_memory_used(next), - "__rtl_memory_resize(): " - "internal logic error."); - } - - /* enqueue */ - __rtl_memory_insert (remain, k); - DBG_MEMORY_VERIFY_QUEUE (remain); - } - - DBG_MEMORY_DEQUEUE(memory->m_length); -} -#else /* PRODUCT */ -#define __rtl_memory_resize(memory, n) \ -{ \ - register sal_Size kn = ((memory)->m_length - (n)); \ - if ((kn >= sizeof(memory_type)) && (n <= __T__)) \ - { \ - register memory_type * remain = queue_cast((memory), (n)); \ -\ - remain->m_length = kn; remain->m_offset = (n); \ - __rtl_memory_split ((memory), remain); \ -\ - __rtl_memory_insert (remain, kn); \ - } \ -} -#endif /* OSL_DEBUG_LEVEL || PRODUCT */ - -/* - * __rtl_memory_dequeue. - */ -#if defined(PROFILE) || (OSL_DEBUG_LEVEL > 0) -static void __rtl_memory_dequeue (memory_type **ppMemory, sal_Size n) -{ - register memory_type *head, *entry; - register sal_Size k, m = n; - - OSL_PRECOND(!*ppMemory, "__rtl_memory_dequeue(): internal logic error."); - for (k = queue(m); k < __N__; k++) - { - /* first fit (equals best fit w/ ascending insert) */ - head = &(g_memory.m_queue_head[k]); - for (entry = head->m_flink; entry != head; entry = entry->m_flink) - { - /* queue not empty */ - DBG_MEMORY_VERIFY_CHAIN (entry); - if (entry->m_length >= m) - { - /* remove entry */ - DBG_MEMORY_VERIFY_QUEUE (entry); - queue_remove (entry); - - /* assign result */ - *ppMemory = entry; - goto dequeue_leave; - } - } - } - - head = &(g_memory.m_spare_head); - for (entry = head->m_flink; entry != head; entry = entry->m_flink) - { - /* queue not empty */ - DBG_MEMORY_VERIFY_CHAIN (entry); - if (entry->m_length >= m) - { - /* remove entry */ - DBG_MEMORY_VERIFY_QUEUE (entry); - queue_remove (entry); - - /* assign result */ - *ppMemory = entry; - goto dequeue_leave; - } - } - -#if OSL_DEBUG_LEVEL > 0 - /* adjust for DBG_MEMORY_INSERT() overhead */ - m += sizeof(memory_type); -#endif /* OSL_DEBUG_LEVEL */ - - k = RTL_MEMORY_ALIGN((m > __M__) ? m : __M__, g_memory.m_align); - if (!((entry = RTL_MEMORY_ALLOC(k)) == 0)) - { - entry->m_length = k; - entry->m_offset = 0x1; /* set 'last' bit */ - - *ppMemory = entry; - DBG_MEMORY_INSERT(ppMemory); - } - -dequeue_leave: - OSL_POSTCOND(*ppMemory, "__rtl_memory_dequeue(): out of memory."); - if ((entry = *ppMemory) != 0) - { - /* adjust length */ - __rtl_memory_resize (entry, n); - - /* fill w/ 'uninitialized' pattern */ - DBG_MEMORY_DEQFILL (entry, __C__, entry->m_length - __C__); - } -#if OSL_DEBUG_LEVEL > 1 - if (!entry) - { - memory_stat total; - __dbg_memory_usage (&total); - } -#endif /* OSL_DEBUG_LEVEL */ -} -#else /* PRODUCT */ -#define __rtl_memory_dequeue(ppMemory, n, label) \ -{ \ - register memory_type *head, *entry; \ - register sal_Size h, m = (n); \ -\ - queue (h, m); \ - for (; h < __N__; h++) \ - { \ - head = &(g_memory.m_queue_head[h]); \ - for (entry = head->m_flink; entry != head; entry = entry->m_flink) \ - { \ - if (entry->m_length >= m) \ - { \ - queue_remove (entry); \ - goto label; \ - } \ - } \ - } \ -\ - head = &(g_memory.m_spare_head); \ - for (entry = head->m_flink; entry != head; entry = entry->m_flink) \ - { \ - if (entry->m_length >= m) \ - { \ - queue_remove (entry); \ - goto label; \ - } \ - } \ -\ - h = RTL_MEMORY_ALIGN((m > __M__) ? m : __M__, g_memory.m_align); \ - if (!((entry = RTL_MEMORY_ALLOC(h)) == 0)) \ - { \ - entry->m_length = h; \ - entry->m_offset = 0x1; \ - } \ -\ -label: \ - if (entry) \ - { \ - __rtl_memory_resize (entry, (n)); \ - *(ppMemory) = entry; \ - } \ -} -#endif /* OSL_DEBUG_LEVEL || PRODUCT */ - -#if defined(PROFILE) || (OSL_DEBUG_LEVEL > 0) -#define RTL_MEMORY_DEQUEUE(m, n, l) __rtl_memory_dequeue((m), (n)) -#else /* PRODUCT */ -#define RTL_MEMORY_DEQUEUE(m, n, l) __rtl_memory_dequeue(m, n, l) -#endif /* OSL_DEBUG_LEVEL || PRODUCT */ - -/* - * __rtl_memory_enqueue. - */ -#if defined(PROFILE) || (OSL_DEBUG_LEVEL > 0) -static void __rtl_memory_enqueue (memory_type **ppMemory) -{ - register memory_type *head = *ppMemory; - - OSL_ENSURE(!__rtl_memory_used(head), - "__rtl_memory_enqueue(): " - "internal logic error."); - DBG_MEMORY_ENQUEUE (head->m_length); - - /* fill w/ 'deinitialized' pattern */ - DBG_MEMORY_ENQFILL (head, __C__, head->m_length - __C__); - - /* try merge w/ next entry */ - if (!__rtl_memory_last(head)) - { - /* not last, check next in chain */ - register memory_type * next; - - next = queue_cast(head, head->m_length); - DBG_MEMORY_VERIFY_CHAIN (next); - - if (!__rtl_memory_used(next)) - { - /* next not used */ - DBG_MEMORY_VERIFY_QUEUE (next); - queue_remove (next); - - /* merge w/ next */ - __rtl_memory_merge (head, next); - DBG_MEMORY_ENQFILL (next, 0, sizeof(memory_type)); - } - } - - /* try merge w/ prev entry */ - if (__rtl_memory_offset(head) > 0) - { - /* not first, check prev in chain */ - register memory_type * prev; - - prev = queue_cast(head, -(__rtl_memory_offset(head))); - DBG_MEMORY_VERIFY_CHAIN (prev); - - if (!__rtl_memory_used(prev)) - { - /* prev not used */ - DBG_MEMORY_VERIFY_QUEUE (prev); - queue_remove (prev); - - /* merge w/ prev */ - __rtl_memory_merge (prev, head); - DBG_MEMORY_ENQFILL (head, 0, sizeof(memory_type)); - head = prev; - } - } - - if (!(head->m_offset == 0x1)) - { - /* page still used, enqueue */ - __rtl_memory_insert (head, head->m_length); - head = 0; - } - else if (head->m_length <= g_memory.m_align) - { - /* small page unused, check spare page */ - register memory_type * spare; - - spare = &(g_memory.m_spare_head); - if (spare->m_flink == spare) - { - /* keep as spare page */ - queue_insert_tail (spare, head); - head = 0; - } - } - if ((*ppMemory = head) != 0) - { - /* page unused, remove */ - DBG_MEMORY_REMOVE(ppMemory); - } -} -#else /* PRODUCT */ -#define __rtl_memory_enqueue(ppMemory) \ -{ \ - register memory_type *head = *(ppMemory); \ -\ - if (!__rtl_memory_last(head)) \ - { \ - register memory_type * next; \ - next = queue_cast(head, head->m_length); \ - if (!__rtl_memory_used(next)) \ - { \ - queue_remove (next); \ - __rtl_memory_merge (head, next); \ - } \ - } \ -\ - if (__rtl_memory_offset(head) > 0) \ - { \ - register memory_type * prev; \ - prev = queue_cast(head, -(__rtl_memory_offset(head))); \ - if (!__rtl_memory_used(prev)) \ - { \ - queue_remove (prev); \ - __rtl_memory_merge (prev, head); \ - head = prev; \ - } \ - } \ -\ - if (!(head->m_offset == 0x1)) \ - { \ - register memory_type * used = head; \ - __rtl_memory_insert (used, used->m_length); \ - head = 0; \ - } \ - else if (head->m_length <= g_memory.m_align) \ - { \ - register memory_type * spare; \ - spare = &(g_memory.m_spare_head); \ - if (spare->m_flink == spare) \ - { \ - queue_insert_tail (spare, head); \ - head = 0; \ - } \ - } \ -\ - *(ppMemory) = head; \ -} -#endif /* OSL_DEBUG_LEVEL || PRODUCT */ - -#define RTL_MEMORY_ENQUEUE(m) __rtl_memory_enqueue((m)) - -#endif /* FORCE_SYSALLOC */ - -/*=========================================================================== - * - * rtl_memory (manager) implementation. - * - *=========================================================================*/ -/* - * rtl_reallocateMemory. - */ -#ifndef FORCE_SYSALLOC -void* SAL_CALL rtl_reallocateMemory (void * p, sal_Size n) SAL_THROW_EXTERN_C() -{ - memory_type * memory; - if (!(!p || !n)) - { - /* reallocate */ - register sal_Size datlen; - - memory = queue_cast(p, -(__C__)); p = 0; - n = RTL_MEMORY_ALIGN(n, __Q__) + __C__; - - RTL_MEMORY_ENTER(); - DBG_MEMORY_VERIFY(memory); - - /* clear 'used' bit */ - DBG_MEMORY_ENQUEUE (memory->m_length); - memory->m_length &= ~0x1; - - /* amount of data to be moved or copied */ - datlen = ((memory->m_length < n) ? memory->m_length : n); - - /* try merge w/ next entry */ - if (!__rtl_memory_last(memory)) - { - /* not last, check next in chain */ - register memory_type * next; - - next = queue_cast(memory, memory->m_length); - DBG_MEMORY_VERIFY_CHAIN(next); - - if (!__rtl_memory_used(next)) - { - /* next not used */ - DBG_MEMORY_VERIFY_QUEUE(next); - queue_remove (next); - - /* merge w/ next */ - __rtl_memory_merge (memory, next); - } - } - - /* try merge w/ prev entry */ - if (__rtl_memory_offset(memory) > 0) - { - /* not first, check prev in chain */ - register memory_type * prev; - - prev = queue_cast(memory, -(__rtl_memory_offset(memory))); - DBG_MEMORY_VERIFY_CHAIN (prev); - - if (!__rtl_memory_used(prev)) - { - /* prev not used, try merge, move */ - if ((memory->m_length + prev->m_length) >= n) - { - /* prev does fit */ - DBG_MEMORY_VERIFY_QUEUE (prev); - queue_remove (prev); - - /* merge w/ prev */ - __rtl_memory_merge (prev, memory); - - /* move to prev */ - memmove ( - queue_cast(prev, __C__), - queue_cast(memory, __C__), - datlen - __C__); - memory = prev; - } - } - } - - if (memory->m_length >= n) - { - /* adjust, set 'used' bit */ - __rtl_memory_resize (memory, n); - memory->m_length |= 0x1; - - /* assign result */ - p = queue_cast(memory, __C__); - } - else - { - /* allocate */ - memory_type * result = 0; - - /* restore 'used' bit */ - DBG_MEMORY_DEQUEUE (memory->m_length); - memory->m_length |= 0x80000000; - - RTL_MEMORY_DEQUEUE (&result, n, realloc_label_1); - if (result) - { - /* set 'used' bit */ - result->m_length |= 0x1; - - /* copy */ - memcpy ( - queue_cast(result, __C__), - queue_cast(memory, __C__), - datlen - __C__); - - /* clear 'used' bit, enqueue */ - memory->m_length &= 0x7fffffff; - RTL_MEMORY_ENQUEUE (&memory); - if (memory) - { - /* free memory page */ - RTL_MEMORY_FREE(memory, memory->m_length); - } - - /* assign result */ - p = queue_cast(result, __C__); - } - } - RTL_MEMORY_LEAVE(); - } - else if (!p) - { - /* allocate */ - memory = 0; - n = RTL_MEMORY_ALIGN(n, __Q__) + __C__; - - RTL_MEMORY_ENTER(); - RTL_MEMORY_DEQUEUE (&memory, n, realloc_label_2); - if (memory) - { - /* set 'used' bit */ - memory->m_length |= 0x1; - - /* assign result */ - p = queue_cast(memory, __C__); - } - RTL_MEMORY_LEAVE(); - } - else if (!n) - { - /* free */ - memory = queue_cast(p, -(__C__)); p = 0; - - RTL_MEMORY_ENTER(); - DBG_MEMORY_VERIFY(memory); - - /* clear 'used' bit, enqueue */ - memory->m_length &= ~0x1; - - RTL_MEMORY_ENQUEUE (&memory); - if (memory) - { - /* free memory page */ - RTL_MEMORY_FREE(memory, memory->m_length); - } - RTL_MEMORY_LEAVE(); - } - return (p); -} -#else /* FORCE_SYSALLOC */ -void* SAL_CALL rtl_reallocateMemory (void * p, sal_Size n) SAL_THROW_EXTERN_C() -{ - return realloc(p, (sal_Size)(n)); -} -#endif /* FORCE_SYSALLOC */ - -/* - * rtl_allocateMemory. - */ -#ifndef FORCE_SYSALLOC -void* SAL_CALL rtl_allocateMemory (sal_Size n) SAL_THROW_EXTERN_C() -{ - void * p = 0; - if (n > 0) - { - memory_type * memory = 0; - n = RTL_MEMORY_ALIGN(n, __Q__) + __C__; - - RTL_MEMORY_ENTER(); - RTL_MEMORY_DEQUEUE (&memory, n, alloc_label); - if (memory) - { - /* set 'used' bit */ - memory->m_length |= 0x1; - - /* assign result */ - p = queue_cast(memory, __C__); - } - RTL_MEMORY_LEAVE(); - } - return (p); -} -#else /* FORCE_SYSALLOC */ -void* SAL_CALL rtl_allocateMemory (sal_Size n) SAL_THROW_EXTERN_C() -{ - return malloc((sal_Size)(n)); -} -#endif /* FORCE_SYSALLOC */ - -/* - * rtl_freeMemory. - */ -#ifndef FORCE_SYSALLOC -void SAL_CALL rtl_freeMemory (void * p) SAL_THROW_EXTERN_C() -{ - if (p) - { - memory_type * memory = queue_cast(p, -(__C__)); - - RTL_MEMORY_ENTER(); - DBG_MEMORY_VERIFY(memory); - - /* clear 'used' bit, enqueue */ - memory->m_length &= ~0x1; - - RTL_MEMORY_ENQUEUE (&memory); - if (memory) - { - /* free memory page */ - RTL_MEMORY_FREE(memory, memory->m_length); - } - RTL_MEMORY_LEAVE(); - } -} -#else /* FORCE_SYSALLOC */ -void SAL_CALL rtl_freeMemory (void * p) SAL_THROW_EXTERN_C() -{ - free(p); -} -#endif /* FORCE_SYSALLOC */ - -/* - * rtl_allocateZeroMemory. - */ -#ifndef FORCE_SYSALLOC -void* SAL_CALL rtl_allocateZeroMemory (sal_Size n) SAL_THROW_EXTERN_C() -{ - void * p = 0; - if (n > 0) - { - memory_type * memory = 0; - n = RTL_MEMORY_ALIGN(n, __Q__) + __C__; - - RTL_MEMORY_ENTER(); - RTL_MEMORY_DEQUEUE (&memory, n, alloc_label); /* NYI: demand zero */ - if (memory) - { - /* zero, set 'used' bit */ - memset ((char*)memory + __C__, 0, memory->m_length - __C__); - memory->m_length |= 0x1; - - /* assign result */ - p = queue_cast(memory, __C__); - } - RTL_MEMORY_LEAVE(); - } - return (p); -} -#else /* FORCE_SYSALLOC */ -void* SAL_CALL rtl_allocateZeroMemory (sal_Size n) SAL_THROW_EXTERN_C() -{ - return calloc((sal_Size)(n), 1); -} -#endif /* FORCE_SYSALLOC */ - -/* - * rtl_freeZeroMemory. - */ -#ifndef FORCE_SYSALLOC -void SAL_CALL rtl_freeZeroMemory (void * p, sal_Size n) SAL_THROW_EXTERN_C() -{ - (void) n; /* unused */ - if (p) - { - memory_type * memory = queue_cast(p, -(__C__)); - - RTL_MEMORY_ENTER(); - DBG_MEMORY_VERIFY(memory); - - /* clear 'used' bit, zero, enqueue */ - memory->m_length &= ~0x1; - memset ((char*)memory + __C__, 0, memory->m_length - __C__); - - RTL_MEMORY_ENQUEUE (&memory); /* NYI: demand zero */ - if (memory) - { - /* free memory page */ - RTL_MEMORY_FREE(memory, memory->m_length); - } - RTL_MEMORY_LEAVE(); - } -} -#else /* FORCE_SYSALLOC */ -void SAL_CALL rtl_freeZeroMemory (void * p, sal_Size n) SAL_THROW_EXTERN_C() -{ - if (p) - { - memset(p, 0, n); - free(p); - } -} -#endif /* FORCE_SYSALLOC */ - -/*=========================================================================== - * - * The End. - * - *=========================================================================*/ diff --git a/sal/rtl/source/alloc_arena.c b/sal/rtl/source/alloc_arena.c index f7325fcc616d..1e2ea5a741e4 100644 --- a/sal/rtl/source/alloc_arena.c +++ b/sal/rtl/source/alloc_arena.c @@ -28,22 +28,13 @@ #define _BSD_SOURCE /* sys/mman.h: MAP_ANON */ #include "alloc_arena.h" -#ifndef INCLUDED_RTL_ARENA_IMPL_H #include "alloc_impl.h" -#endif #include "internal/once.h" #include "sal/macros.h" #include "osl/diagnose.h" -#ifndef INCLUDED_STRING_H #include <string.h> -#endif - -#ifndef INCLUDED_STDIO_H #include <stdio.h> -#endif - -#include "sal/types.h" #ifdef OS2 #undef OSL_TRACE @@ -966,6 +957,7 @@ try_alloc: if (result != 0) { rtl_arena_type * arena = result; + VALGRIND_CREATE_MEMPOOL(arena, 0, 0); rtl_arena_constructor (arena); if (!source_arena) @@ -988,6 +980,7 @@ try_alloc: { rtl_arena_deactivate (arena); rtl_arena_destructor (arena); + VALGRIND_DESTROY_MEMPOOL(arena); rtl_arena_free (gp_arena_arena, arena, size); } } @@ -1013,6 +1006,7 @@ SAL_CALL rtl_arena_destroy ( { rtl_arena_deactivate (arena); rtl_arena_destructor (arena); + VALGRIND_DESTROY_MEMPOOL(arena); rtl_arena_free (gp_arena_arena, arena, sizeof(rtl_arena_type)); } } @@ -1068,6 +1062,10 @@ SAL_CALL rtl_arena_alloc ( rtl_arena_hash_insert (arena, segment); + /* DEBUG ONLY: mark allocated, undefined */ + OSL_DEBUG_ONLY(memset((void*)(segment->m_addr), 0x77777777, segment->m_size)); + VALGRIND_MEMPOOL_ALLOC(arena, segment->m_addr, segment->m_size); + (*pSize) = segment->m_size; addr = (void*)(segment->m_addr); } @@ -1111,6 +1109,11 @@ SAL_CALL rtl_arena_free ( { rtl_arena_segment_type *next, *prev; + /* DEBUG ONLY: mark unallocated, undefined */ + VALGRIND_MEMPOOL_FREE(arena, segment->m_addr); + /* OSL_DEBUG_ONLY() */ VALGRIND_MAKE_MEM_UNDEFINED(segment->m_addr, segment->m_size); + OSL_DEBUG_ONLY(memset((void*)(segment->m_addr), 0x33333333, segment->m_size)); + /* coalesce w/ adjacent free segment(s) */ rtl_arena_segment_coalesce (arena, segment); @@ -1302,6 +1305,7 @@ rtl_arena_once_init (void) static rtl_arena_type g_machdep_arena; OSL_ASSERT(gp_machdep_arena == 0); + VALGRIND_CREATE_MEMPOOL(&g_machdep_arena, 0, 0); rtl_arena_constructor (&g_machdep_arena); gp_machdep_arena = rtl_arena_activate ( @@ -1318,6 +1322,7 @@ rtl_arena_once_init (void) static rtl_arena_type g_default_arena; OSL_ASSERT(gp_default_arena == 0); + VALGRIND_CREATE_MEMPOOL(&g_default_arena, 0, 0); rtl_arena_constructor (&g_default_arena); gp_default_arena = rtl_arena_activate ( @@ -1336,6 +1341,7 @@ rtl_arena_once_init (void) static rtl_arena_type g_arena_arena; OSL_ASSERT(gp_arena_arena == 0); + VALGRIND_CREATE_MEMPOOL(&g_arena_arena, 0, 0); rtl_arena_constructor (&g_arena_arena); gp_arena_arena = rtl_arena_activate ( @@ -1361,7 +1367,18 @@ rtl_arena_init (void) /* ================================================================= */ -#if defined(__GNUC__) +/* + Issue http://udk.openoffice.org/issues/show_bug.cgi?id=92388 + + Mac OS X does not seem to support "__cxa__atexit", thus leading + to the situation that "__attribute__((destructor))__" functions + (in particular "rtl_{memory|cache|arena}_fini") become called + _before_ global C++ object d'tors. + + Delegated the call to "rtl_arena_fini()" into a dummy C++ object, + see alloc_fini.cxx . +*/ +#if defined(__GNUC__) && !defined(MACOSX) static void rtl_arena_fini (void) __attribute__((destructor)); #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC) #pragma fini(rtl_arena_fini) diff --git a/sal/rtl/source/alloc_cache.c b/sal/rtl/source/alloc_cache.c index 4e2c030fb3a6..f4d34bfdb437 100644 --- a/sal/rtl/source/alloc_cache.c +++ b/sal/rtl/source/alloc_cache.c @@ -507,6 +507,10 @@ rtl_cache_slab_alloc ( addr = (void*)rtl_cache_hash_insert (cache, bufctl); else addr = bufctl; + + /* DEBUG ONLY: mark allocated, undefined */ + OSL_DEBUG_ONLY(memset(addr, 0x77777777, cache->m_type_size)); + VALGRIND_MEMPOOL_ALLOC(cache, addr, cache->m_type_size); } RTL_MEMORY_LOCK_RELEASE(&(cache->m_slab_lock)); @@ -529,6 +533,11 @@ rtl_cache_slab_free ( RTL_MEMORY_LOCK_ACQUIRE(&(cache->m_slab_lock)); + /* DEBUG ONLY: mark unallocated, undefined */ + VALGRIND_MEMPOOL_FREE(cache, addr); + /* OSL_DEBUG_ONLY() */ VALGRIND_MAKE_MEM_UNDEFINED(addr, cache->m_type_size); + OSL_DEBUG_ONLY(memset(addr, 0x33333333, cache->m_type_size)); + /* determine slab from addr */ if (cache->m_features & RTL_CACHE_FEATURE_HASH) { @@ -635,8 +644,13 @@ rtl_cache_magazine_clear ( void * obj = mag->m_objects[mag->m_mag_used - 1]; mag->m_objects[mag->m_mag_used - 1] = 0; + /* DEBUG ONLY: mark cached object allocated, undefined */ + VALGRIND_MEMPOOL_ALLOC(cache, obj, cache->m_type_size); if (cache->m_destructor != 0) { + /* DEBUG ONLY: keep constructed object defined */ + VALGRIND_MAKE_MEM_DEFINED(obj, cache->m_type_size); + /* destruct object */ (cache->m_destructor)(obj, cache->m_userarg); } @@ -965,11 +979,16 @@ rtl_cache_deactivate ( rtl_cache_type * cache ) { + int active = 1; + /* remove from cache list */ RTL_MEMORY_LOCK_ACQUIRE(&(g_cache_list.m_lock)); + active = QUEUE_STARTED_NAMED(cache, cache_) == 0; QUEUE_REMOVE_NAMED(cache, cache_); RTL_MEMORY_LOCK_RELEASE(&(g_cache_list.m_lock)); + OSL_PRECOND(active, "rtl_cache_deactivate(): orphaned cache."); + /* cleanup magazine layer */ if (cache->m_magazine_cache != 0) { @@ -1121,6 +1140,7 @@ try_alloc: if (result != 0) { rtl_cache_type * cache = result; + VALGRIND_CREATE_MEMPOOL(cache, 0, 0); (void) rtl_cache_constructor (cache); if (!source) @@ -1148,6 +1168,7 @@ try_alloc: /* activation failed */ rtl_cache_deactivate (cache); rtl_cache_destructor (cache); + VALGRIND_DESTROY_MEMPOOL(cache); rtl_arena_free (gp_cache_arena, cache, size); } } @@ -1172,6 +1193,7 @@ void SAL_CALL rtl_cache_destroy ( { rtl_cache_deactivate (cache); rtl_cache_destructor (cache); + VALGRIND_DESTROY_MEMPOOL(cache); rtl_arena_free (gp_cache_arena, cache, sizeof(rtl_cache_type)); } } @@ -1201,6 +1223,14 @@ SAL_CALL rtl_cache_alloc ( if ((curr != 0) && (curr->m_mag_used > 0)) { obj = curr->m_objects[--curr->m_mag_used]; +#if defined(HAVE_VALGRIND_MEMCHECK_H) + VALGRIND_MEMPOOL_ALLOC(cache, obj, cache->m_type_size); + if (cache->m_constructor != 0) + { + /* keep constructed object defined */ + VALGRIND_MAKE_MEM_DEFINED(obj, cache->m_type_size); + } +#endif /* HAVE_VALGRIND_MEMCHECK_H */ cache->m_cpu_stats.m_alloc += 1; RTL_MEMORY_LOCK_RELEASE(&(cache->m_depot_lock)); @@ -1244,7 +1274,6 @@ SAL_CALL rtl_cache_alloc ( rtl_cache_slab_free (cache, obj), obj = 0; } } - return (obj); } @@ -1269,6 +1298,9 @@ SAL_CALL rtl_cache_free ( if ((curr != 0) && (curr->m_mag_used < curr->m_mag_size)) { curr->m_objects[curr->m_mag_used++] = obj; +#if defined(HAVE_VALGRIND_MEMCHECK_H) + VALGRIND_MEMPOOL_FREE(cache, obj); +#endif /* HAVE_VALGRIND_MEMCHECK_H */ cache->m_cpu_stats.m_free += 1; RTL_MEMORY_LOCK_RELEASE(&(cache->m_depot_lock)); @@ -1582,6 +1614,7 @@ rtl_cache_once_init (void) static rtl_cache_type g_cache_magazine_cache; OSL_ASSERT(gp_cache_magazine_cache == 0); + VALGRIND_CREATE_MEMPOOL(&g_cache_magazine_cache, 0, 0); (void) rtl_cache_constructor (&g_cache_magazine_cache); gp_cache_magazine_cache = rtl_cache_activate ( @@ -1606,6 +1639,7 @@ rtl_cache_once_init (void) static rtl_cache_type g_cache_slab_cache; OSL_ASSERT(gp_cache_slab_cache == 0); + VALGRIND_CREATE_MEMPOOL(&g_cache_slab_cache, 0, 0); (void) rtl_cache_constructor (&g_cache_slab_cache); gp_cache_slab_cache = rtl_cache_activate ( @@ -1627,6 +1661,7 @@ rtl_cache_once_init (void) static rtl_cache_type g_cache_bufctl_cache; OSL_ASSERT(gp_cache_bufctl_cache == 0); + VALGRIND_CREATE_MEMPOOL(&g_cache_bufctl_cache, 0, 0); (void) rtl_cache_constructor (&g_cache_bufctl_cache); gp_cache_bufctl_cache = rtl_cache_activate ( @@ -1657,7 +1692,18 @@ rtl_cache_init (void) /* ================================================================= */ -#if defined(__GNUC__) +/* + Issue http://udk.openoffice.org/issues/show_bug.cgi?id=92388 + + Mac OS X does not seem to support "__cxa__atexit", thus leading + to the situation that "__attribute__((destructor))__" functions + (in particular "rtl_{memory|cache|arena}_fini") become called + _before_ global C++ object d'tors. + + Delegated the call to "rtl_cache_fini()" into a dummy C++ object, + see alloc_fini.cxx . +*/ +#if defined(__GNUC__) && !defined(MACOSX) static void rtl_cache_fini (void) __attribute__((destructor)); #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC) #pragma fini(rtl_cache_fini) @@ -1678,18 +1724,21 @@ rtl_cache_fini (void) cache = gp_cache_bufctl_cache, gp_cache_bufctl_cache = 0; rtl_cache_deactivate (cache); rtl_cache_destructor (cache); + VALGRIND_DESTROY_MEMPOOL(cache); } if (gp_cache_slab_cache != 0) { cache = gp_cache_slab_cache, gp_cache_slab_cache = 0; rtl_cache_deactivate (cache); rtl_cache_destructor (cache); + VALGRIND_DESTROY_MEMPOOL(cache); } if (gp_cache_magazine_cache != 0) { cache = gp_cache_magazine_cache, gp_cache_magazine_cache = 0; rtl_cache_deactivate (cache); rtl_cache_destructor (cache); + VALGRIND_DESTROY_MEMPOOL(cache); } if (gp_cache_arena != 0) { diff --git a/sal/rtl/source/memory_fini.cxx b/sal/rtl/source/alloc_fini.cxx index f88f09887595..cb04a525e75e 100755 --- a/sal/rtl/source/memory_fini.cxx +++ b/sal/rtl/source/alloc_fini.cxx @@ -25,30 +25,31 @@ * ************************************************************************/ - /* Issue http://udk.openoffice.org/issues/show_bug.cgi?id=92388 Mac OS X does not seem to support "__cxa__atexit", thus leading to the situation that "__attribute__((destructor))__" functions - (in particular "rtl_memory_fini") become called _before_ global - C++ object d'tors. + (in particular "rtl_{memory|cache|arena}_fini") become called + _before_ global C++ object d'tors. Using a C++ dummy object instead. */ -#include <stdio.h> - extern "C" void rtl_memory_fini (void); +extern "C" void rtl_cache_fini (void); +extern "C" void rtl_arena_fini (void); - -struct RTL_Memory_Fini { - ~RTL_Memory_Fini() ; +struct RTL_Alloc_Fini +{ + ~RTL_Alloc_Fini() ; }; -RTL_Memory_Fini::~RTL_Memory_Fini() { +RTL_Alloc_Fini::~RTL_Alloc_Fini() +{ rtl_memory_fini(); + rtl_cache_fini(); + rtl_arena_fini(); } - -static RTL_Memory_Fini rtl_Memory_Fini; +static RTL_Alloc_Fini g_RTL_Alloc_Fini; diff --git a/sal/rtl/source/alloc_global.c b/sal/rtl/source/alloc_global.c index 5137f868127d..5da66ca49d96 100644 --- a/sal/rtl/source/alloc_global.c +++ b/sal/rtl/source/alloc_global.c @@ -26,6 +26,7 @@ ************************************************************************/ #include "rtl/alloc.h" +#include "alloc_impl.h" #ifndef INCLUDED_STRING_H #include <string.h> @@ -44,7 +45,6 @@ #include <stdio.h> #define INCLUDED_STDIO_H #endif -#include "alloc_impl.h" #include "internal/once.h" #include "sal/macros.h" #include "osl/diagnose.h" @@ -151,11 +151,11 @@ rtl_memory_init (void) Mac OS X does not seem to support "__cxa__atexit", thus leading to the situation that "__attribute__((destructor))__" functions - (in particular "rtl_memory_fini") become called _before_ global - C++ object d'tors. + (in particular "rtl_{memory|cache|arena}_fini") become called + _before_ global C++ object d'tors. - Delegated the call to "rtl_memory_fini" into a dummy C++ object, - see memory_fini.cxx . + Delegated the call to "rtl_memory_fini()" into a dummy C++ object, + see alloc_fini.cxx . */ #if defined(__GNUC__) && !defined(MACOSX) static void rtl_memory_fini (void) __attribute__((destructor)); diff --git a/sal/rtl/source/alloc_impl.h b/sal/rtl/source/alloc_impl.h index d3d1924ddf91..a95b7c47465c 100644 --- a/sal/rtl/source/alloc_impl.h +++ b/sal/rtl/source/alloc_impl.h @@ -240,6 +240,30 @@ typedef CRITICAL_SECTION rtl_memory_lock_type; #define RTL_CACHE_FLAG_QUANTUMCACHE (2 << 13) /* used as arena quantum cache */ +/** Valgrind support macros. + */ +#if !defined(HAVE_MEMCHECK_H) || (OSL_DEBUG_LEVEL == 0) +#if !defined(NVALGRIND) +#define NVALGRIND 1 +#endif /* ! NVALGRIND */ +#endif /* ! HAVE_MEMCHECK_H || (OSL_DEBUG_LEVEL == 0) */ + +#if defined(NVALGRIND) +#define VALGRIND_MAKE_MEM_UNDEFINED(addr, size) +#define VALGRIND_MAKE_MEM_DEFINED(addr, size) +#define VALGRIND_MALLOCLIKE_BLOCK(addr, sizeB, rzB, is_zeroed) +#define VALGRIND_FREELIKE_BLOCK(addr, rzB) +#define VALGRIND_CREATE_MEMPOOL(pool, rzB, is_zeroed) +#define VALGRIND_DESTROY_MEMPOOL(pool) +#define VALGRIND_MEMPOOL_ALLOC(pool, addr, size) +#define VALGRIND_MEMPOOL_FREE(pool, addr) +#elif defined(HAVE_MEMCHECK_H) +#include <memcheck.h> +#if !defined(FORCE_SYSALLOC) +#define FORCE_SYSALLOC 1 +#endif /* !FORCE_SYSALLOC */ +#endif /* NVALGRIND || HAVE_MEMCHECK_H */ + #ifdef __cplusplus } #endif diff --git a/sal/rtl/source/makefile.mk b/sal/rtl/source/makefile.mk index 9968d8992be4..145aa50b8be0 100644 --- a/sal/rtl/source/makefile.mk +++ b/sal/rtl/source/makefile.mk @@ -46,6 +46,11 @@ TARGETTYPE=CUI .INCLUDE : settings.mk +.IF "$(VALGRIND_CFLAGS)" != "" +CFLAGS += $(VALGRIND_CFLAGS) +CDEFS += -DHAVE_MEMCHECK_H=1 +.ENDIF # VALGRIND_CFLAGS + .IF "$(ALLOC)" == "SYS_ALLOC" || "$(ALLOC)" == "TCMALLOC" || "$(ALLOC)" == "JEMALLOC" CDEFS+= -DFORCE_SYSALLOC .ENDIF @@ -96,7 +101,7 @@ SLOFILES= \ $(SLO)$/alloc_arena.obj .IF "$(OS)"=="MACOSX" -SLOFILES+=$(SLO)$/memory_fini.obj +SLOFILES+=$(SLO)$/alloc_fini.obj .ENDIF @@ -129,7 +134,7 @@ OBJFILES= \ $(OBJ)$/alloc_arena.obj .IF "$(OS)"=="MACOSX" -OBJFILES+=$(OBJ)$/memory_fini.obj +OBJFILES+=$(OBJ)$/alloc_fini.obj .ENDIF |