From 418aa1d621bf8bf50a9cbe5d9994b48ced0b29e1 Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Fri, 29 May 2015 13:11:47 +0200 Subject: [PATCH] globals: avoid copying other globals In several places, a global constant is initialized with the constant of another constant of the same type. This only works if these instances happen to be initialized in the right order, which is not guaranteed and indeed happened to fail when using gcc 4.9 (see https://github.com/Samsung/cynara/issues/9). The solution used here replaces the copies of the other constants with references to them, which minimizes source code changes and is also slightly more efficient. The references can be created at any time because the address of the real constants cannot change. This change was enough to get cynara-tests running; it is not necessarily complete. Upstream-status: Submitted [https://github.com/Samsung/cynara/issues/9] Signed-off-by: Patrick Ohly --- src/storage/ChecksumValidator.cpp | 4 ++-- src/storage/ChecksumValidator.h | 4 ++-- src/storage/InMemoryStorageBackend.cpp | 8 ++++---- src/storage/InMemoryStorageBackend.h | 8 ++++---- src/storage/Integrity.cpp | 8 ++++---- src/storage/Integrity.h | 8 ++++---- test/chsgen/checksumgenerator.cpp | 2 +- test/storage/checksum/checksumvalidator.cpp | 2 +- test/storage/checksum/checksumvalidatorfixture.h | 2 +- 9 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/storage/ChecksumValidator.cpp b/src/storage/ChecksumValidator.cpp index 1354ad1..b0898a7 100644 --- a/src/storage/ChecksumValidator.cpp +++ b/src/storage/ChecksumValidator.cpp @@ -38,8 +38,8 @@ namespace Cynara { -const std::string ChecksumValidator::m_checksumFilename(PathConfig::StoragePath::checksumFilename); -const std::string ChecksumValidator::m_backupFilenameSuffix( +const std::string &ChecksumValidator::m_checksumFilename(PathConfig::StoragePath::checksumFilename); +const std::string &ChecksumValidator::m_backupFilenameSuffix( PathConfig::StoragePath::backupFilenameSuffix); void ChecksumValidator::load(std::istream &stream) { diff --git a/src/storage/ChecksumValidator.h b/src/storage/ChecksumValidator.h index bfd577b..94a6edb 100644 --- a/src/storage/ChecksumValidator.h +++ b/src/storage/ChecksumValidator.h @@ -56,8 +56,8 @@ protected: Checksums m_sums; const std::string m_dbPath; - static const std::string m_checksumFilename; - static const std::string m_backupFilenameSuffix; + static const std::string &m_checksumFilename; + static const std::string &m_backupFilenameSuffix; }; } // namespace Cynara diff --git a/src/storage/InMemoryStorageBackend.cpp b/src/storage/InMemoryStorageBackend.cpp index 15f6a8a..69f74b9 100644 --- a/src/storage/InMemoryStorageBackend.cpp +++ b/src/storage/InMemoryStorageBackend.cpp @@ -53,11 +53,11 @@ namespace Cynara { -const std::string InMemoryStorageBackend::m_chsFilename(PathConfig::StoragePath::checksumFilename); -const std::string InMemoryStorageBackend::m_indexFilename(PathConfig::StoragePath::indexFilename); -const std::string InMemoryStorageBackend::m_backupFilenameSuffix( +const std::string &InMemoryStorageBackend::m_chsFilename(PathConfig::StoragePath::checksumFilename); +const std::string &InMemoryStorageBackend::m_indexFilename(PathConfig::StoragePath::indexFilename); +const std::string &InMemoryStorageBackend::m_backupFilenameSuffix( PathConfig::StoragePath::backupFilenameSuffix); -const std::string InMemoryStorageBackend::m_bucketFilenamePrefix( +const std::string &InMemoryStorageBackend::m_bucketFilenamePrefix( PathConfig::StoragePath::bucketFilenamePrefix); InMemoryStorageBackend::InMemoryStorageBackend(const std::string &path) : m_dbPath(path), diff --git a/src/storage/InMemoryStorageBackend.h b/src/storage/InMemoryStorageBackend.h index 80a505c..9e232c0 100644 --- a/src/storage/InMemoryStorageBackend.h +++ b/src/storage/InMemoryStorageBackend.h @@ -86,10 +86,10 @@ private: Buckets m_buckets; ChecksumValidator m_checksum; Integrity m_integrity; - static const std::string m_chsFilename; - static const std::string m_indexFilename; - static const std::string m_backupFilenameSuffix; - static const std::string m_bucketFilenamePrefix; + static const std::string &m_chsFilename; + static const std::string &m_indexFilename; + static const std::string &m_backupFilenameSuffix; + static const std::string &m_bucketFilenamePrefix; protected: virtual Buckets &buckets(void) { diff --git a/src/storage/Integrity.cpp b/src/storage/Integrity.cpp index 0b8e583..e0ed638 100644 --- a/src/storage/Integrity.cpp +++ b/src/storage/Integrity.cpp @@ -40,10 +40,10 @@ namespace Cynara { namespace StorageConfig = PathConfig::StoragePath; -const std::string Integrity::m_guardFilename(StorageConfig::guardFilename); -const std::string Integrity::m_indexFilename(StorageConfig::indexFilename); -const std::string Integrity::m_backupFilenameSuffix(StorageConfig::backupFilenameSuffix); -const std::string Integrity::m_bucketFilenamePrefix(StorageConfig::bucketFilenamePrefix); +const std::string &Integrity::m_guardFilename(StorageConfig::guardFilename); +const std::string &Integrity::m_indexFilename(StorageConfig::indexFilename); +const std::string &Integrity::m_backupFilenameSuffix(StorageConfig::backupFilenameSuffix); +const std::string &Integrity::m_bucketFilenamePrefix(StorageConfig::bucketFilenamePrefix); bool Integrity::backupGuardExists(void) const { struct stat buffer; diff --git a/src/storage/Integrity.h b/src/storage/Integrity.h index 569ca85..df1c4fc 100644 --- a/src/storage/Integrity.h +++ b/src/storage/Integrity.h @@ -60,10 +60,10 @@ protected: private: const std::string m_dbPath; - static const std::string m_indexFilename; - static const std::string m_backupFilenameSuffix; - static const std::string m_bucketFilenamePrefix; - static const std::string m_guardFilename; + static const std::string &m_indexFilename; + static const std::string &m_backupFilenameSuffix; + static const std::string &m_bucketFilenamePrefix; + static const std::string &m_guardFilename; }; } // namespace Cynara diff --git a/test/chsgen/checksumgenerator.cpp b/test/chsgen/checksumgenerator.cpp index 1d390d6..83cdc3f 100644 --- a/test/chsgen/checksumgenerator.cpp +++ b/test/chsgen/checksumgenerator.cpp @@ -33,7 +33,7 @@ namespace { const std::string execName("./cynara-db-chsgen"); -const std::string backupFilenameSuffix(Cynara::PathConfig::StoragePath::backupFilenameSuffix); +const std::string &backupFilenameSuffix(Cynara::PathConfig::StoragePath::backupFilenameSuffix); const char fieldSeparator(Cynara::PathConfig::StoragePath::fieldSeparator); } // namespace diff --git a/test/storage/checksum/checksumvalidator.cpp b/test/storage/checksum/checksumvalidator.cpp index 19918f3..4e4088f 100644 --- a/test/storage/checksum/checksumvalidator.cpp +++ b/test/storage/checksum/checksumvalidator.cpp @@ -41,7 +41,7 @@ using namespace Cynara; const std::string ChecksumValidatorFixture::m_dbPath("/fake/path/"); const std::string ChecksumValidatorFixture::m_filename("fakeFilename"); const std::string ChecksumValidatorFixture::m_checksum("$1$$fakeChecksum"); -const std::string ChecksumValidatorFixture::m_backupFilenameSuffix( +const std::string &ChecksumValidatorFixture::m_backupFilenameSuffix( PathConfig::StoragePath::backupFilenameSuffix); const char ChecksumValidatorFixture::m_fieldSeparator(PathConfig::StoragePath::fieldSeparator); const char ChecksumValidatorFixture::m_recordSeparator(PathConfig::StoragePath::recordSeparator); diff --git a/test/storage/checksum/checksumvalidatorfixture.h b/test/storage/checksum/checksumvalidatorfixture.h index 6fb28f6..559473e 100644 --- a/test/storage/checksum/checksumvalidatorfixture.h +++ b/test/storage/checksum/checksumvalidatorfixture.h @@ -45,7 +45,7 @@ protected: static const std::string m_dbPath; static const std::string m_filename; static const std::string m_checksum; - static const std::string m_backupFilenameSuffix; + static const std::string &m_backupFilenameSuffix; static const char m_fieldSeparator; static const char m_recordSeparator; static const size_t m_firstLine; -- 2.1.4