diff options
author | Noel Grandin <noel.grandin@collabora.co.uk> | 2018-11-22 08:52:59 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2018-11-22 12:07:54 +0100 |
commit | 85acc270fc670443cd700c471d9b495e12d4e6dc (patch) | |
tree | 59172f6ee9a51d92548dc64735946b4fcd2a3f7d /sal | |
parent | eea439b4b1305449616e8d0d368f3ccff1a7eca9 (diff) |
improve function-local statics in sal
Change-Id: I0853cf13162bae44cf8a5c44a4546a73f05772d9
Reviewed-on: https://gerrit.libreoffice.org/63780
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'sal')
-rw-r--r-- | sal/osl/unx/profile.cxx | 14 | ||||
-rw-r--r-- | sal/osl/w32/module.cxx | 5 | ||||
-rw-r--r-- | sal/osl/w32/time.cxx | 12 | ||||
-rw-r--r-- | sal/qa/osl/file/osl_File.cxx | 11 | ||||
-rw-r--r-- | sal/rtl/bootstrap.cxx | 17 | ||||
-rw-r--r-- | sal/rtl/hash.cxx | 7 |
6 files changed, 21 insertions, 45 deletions
diff --git a/sal/osl/unx/profile.cxx b/sal/osl/unx/profile.cxx index a986dfd0d41f..589662b9b62a 100644 --- a/sal/osl/unx/profile.cxx +++ b/sal/osl/unx/profile.cxx @@ -879,19 +879,7 @@ static osl_TStamp OslProfile_getFileStamp(osl_TFile* pFile) static bool OslProfile_lockFile(const osl_TFile* pFile, osl_TLockMode eMode) { struct flock lock; - /* boring hack, but initializers for static vars must be constant */ - static bool bIsInitialized = false; - static bool bLockingDisabled; - - if ( !bIsInitialized ) - { - sal_Char* pEnvValue; - pEnvValue = getenv( "STAR_PROFILE_LOCKING_DISABLED" ); - - bLockingDisabled = pEnvValue != nullptr; - - bIsInitialized = true; - } + static bool const bLockingDisabled = getenv( "STAR_PROFILE_LOCKING_DISABLED" ) != nullptr; if (pFile->m_Handle < 0) { diff --git a/sal/osl/w32/module.cxx b/sal/osl/w32/module.cxx index de9ce4db421b..8de791a7a8e1 100644 --- a/sal/osl/w32/module.cxx +++ b/sal/osl/w32/module.cxx @@ -327,10 +327,7 @@ typedef BOOL (WINAPI *GetModuleInformation_PROC)( static bool osl_addressGetModuleURL_NT_( void *pv, rtl_uString **pustrURL ) { bool bSuccess = false; /* Assume failure */ - static HMODULE hModPsapi = nullptr; - - if ( !hModPsapi ) - hModPsapi = LoadLibraryW( L"PSAPI.DLL" ); + static HMODULE hModPsapi = LoadLibraryW( L"PSAPI.DLL" ); if ( hModPsapi ) { diff --git a/sal/osl/w32/time.cxx b/sal/osl/w32/time.cxx index d1284b663fae..135aab368fc8 100644 --- a/sal/osl/w32/time.cxx +++ b/sal/osl/w32/time.cxx @@ -35,18 +35,14 @@ sal_Bool SAL_CALL osl_getSystemTime(TimeValue* pTimeVal) typedef VOID (WINAPI *GetSystemTimePreciseAsFileTime_PROC)(LPFILETIME); - static HMODULE hModule = nullptr; - static GetSystemTimePreciseAsFileTime_PROC pGetSystemTimePreciseAsFileTime = nullptr; - OSL_ASSERT(pTimeVal != nullptr); - if ( !hModule ) + static GetSystemTimePreciseAsFileTime_PROC pGetSystemTimePreciseAsFileTime = [&]() { - hModule = GetModuleHandleW( L"Kernel32.dll" ); - if ( hModule ) - pGetSystemTimePreciseAsFileTime = reinterpret_cast<GetSystemTimePreciseAsFileTime_PROC>( + HMODULE hModule = GetModuleHandleW( L"Kernel32.dll" ); + return reinterpret_cast<GetSystemTimePreciseAsFileTime_PROC>( GetProcAddress(hModule, "GetSystemTimePreciseAsFileTime")); - } + }(); // use ~1 microsecond resolution if available if (pGetSystemTimePreciseAsFileTime) diff --git a/sal/qa/osl/file/osl_File.cxx b/sal/qa/osl/file/osl_File.cxx index fcc8c41cf4ab..ecb65ae8cd3f 100644 --- a/sal/qa/osl/file/osl_File.cxx +++ b/sal/qa/osl/file/osl_File.cxx @@ -4922,8 +4922,7 @@ namespace osl_Directory static OUString const & get_test_path() { - static OUString test_path; - if (test_path.isEmpty()) + static OUString test_path = [&]() { OUString tmp; osl::FileBase::RC rc = osl::FileBase::getTempDirURL(tmp); @@ -4962,14 +4961,16 @@ namespace osl_Directory #endif tmp_x += OString(TEST_PATH_POSTFIX); - rc = osl::FileBase::getFileURLFromSystemPath(OStringToOUString(tmp_x, RTL_TEXTENCODING_UTF8), test_path); + OUString tmpTestPath; + rc = osl::FileBase::getFileURLFromSystemPath(OStringToOUString(tmp_x, RTL_TEXTENCODING_UTF8), tmpTestPath); CPPUNIT_ASSERT_EQUAL_MESSAGE ( "Cannot convert the system path back to an URL", osl::FileBase::E_None, rc - ); - } + ); + return tmpTestPath; + }(); return test_path; } diff --git a/sal/rtl/bootstrap.cxx b/sal/rtl/bootstrap.cxx index 897de890d890..9c6675586ef9 100644 --- a/sal/rtl/bootstrap.cxx +++ b/sal/rtl/bootstrap.cxx @@ -155,10 +155,9 @@ static bool getFromCommandLineArgs( { OSL_ASSERT(value); - static NameValueVector *pNameValueVector = nullptr; - if (!pNameValueVector) + static NameValueVector nameValueVector = [&]() { - static NameValueVector nameValueVector; + NameValueVector tmp; sal_Int32 nArgCount = osl_getCommandArgCount(); for(sal_Int32 i = 0; i < nArgCount; ++ i) @@ -189,18 +188,18 @@ static bool getFromCommandLineArgs( nameValue.sValue = nameValue.sValue.copy(0,nameValue.sValue.getLength()-1); } - nameValueVector.push_back( nameValue ); + tmp.push_back( nameValue ); } } rtl_uString_release( pArg ); - } - pNameValueVector = &nameValueVector; - } + }; + return tmp; + }(); bool found = false; - for(NameValueVector::iterator ii = pNameValueVector->begin(); - ii != pNameValueVector->end(); + for(NameValueVector::iterator ii = nameValueVector.begin(); + ii != nameValueVector.end(); ++ii) { if ((*ii).sName == key) diff --git a/sal/rtl/hash.cxx b/sal/rtl/hash.cxx index e12095f3d9e5..489030f62861 100644 --- a/sal/rtl/hash.cxx +++ b/sal/rtl/hash.cxx @@ -40,12 +40,7 @@ static void rtl_str_hash_free(StringHashTable *pHash); static StringHashTable * getHashTable() { - static StringHashTable *pInternPool = nullptr; - if (!pInternPool) - { - static StringHashTable* pHash = rtl_str_hash_new(1024); - pInternPool = pHash; - } + static StringHashTable* pInternPool = rtl_str_hash_new(1024); return pInternPool; } |