From 48cd90498827e3e497862f9a59a55792763e0368 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Wed, 3 Jul 2013 08:55:06 -0700 Subject: [PATCH] Various beast cleanups --- .../modules/beast_core/memory/beast_Atomic.h | 17 ++++++++-------- .../beast_core/native/beast_bsd_Threads.cpp | 3 ++- .../native/beast_linux_SystemStats.cpp | 14 ++++++------- .../modules/beast_core/text/beast_String.cpp | 7 ++++--- .../beast_core/text/beast_TextDiff.cpp | 6 +++--- TODO.txt | 20 +++++++++++++++++++ 6 files changed, 43 insertions(+), 24 deletions(-) diff --git a/Subtrees/beast/modules/beast_core/memory/beast_Atomic.h b/Subtrees/beast/modules/beast_core/memory/beast_Atomic.h index 4342dbde3..74ba13c13 100644 --- a/Subtrees/beast/modules/beast_core/memory/beast_Atomic.h +++ b/Subtrees/beast/modules/beast_core/memory/beast_Atomic.h @@ -148,15 +148,14 @@ public: volatile Type value; private: - #if BEAST_CLANG || __GNUC__ >= 4 - #define BEAST_ATTRIBUTE_MAY_ALIAS __attribute__((__may_alias__)) - #else - #define BEAST_ATTRIBUTE_MAY_ALIAS - #endif - static inline Type castFrom32Bit (int32 value) noexcept { Type * BEAST_ATTRIBUTE_MAY_ALIAS tmp = (Type*)&value; return *tmp; } - static inline Type castFrom64Bit (int64 value) noexcept { Type * BEAST_ATTRIBUTE_MAY_ALIAS tmp = (Type*)&value; return *tmp; } - static inline int32 castTo32Bit (Type value) noexcept { int32 * BEAST_ATTRIBUTE_MAY_ALIAS tmp = (int32*)&value; return *tmp; } - static inline int64 castTo64Bit (Type value) noexcept { int64 * BEAST_ATTRIBUTE_MAY_ALIAS tmp = (int64*)&value; return *tmp; } + template + static inline Dest castTo (Source value) noexcept { union { Dest d; Source s; } u; u.s = value; return u.d; } + + static inline Type castFrom32Bit (int32 value) noexcept { return castTo (value); } + static inline Type castFrom64Bit (int64 value) noexcept { return castTo (value); } + static inline int32 castTo32Bit (Type value) noexcept { return castTo (value); } + static inline int64 castTo64Bit (Type value) noexcept { return castTo (value); } + Type operator++ (int); // better to just use pre-increment with atomics.. Type operator-- (int); diff --git a/Subtrees/beast/modules/beast_core/native/beast_bsd_Threads.cpp b/Subtrees/beast/modules/beast_core/native/beast_bsd_Threads.cpp index 3c7349f4e..1255f375e 100644 --- a/Subtrees/beast/modules/beast_core/native/beast_bsd_Threads.cpp +++ b/Subtrees/beast/modules/beast_core/native/beast_bsd_Threads.cpp @@ -54,7 +54,8 @@ void Process::terminate() BEAST_API bool BEAST_CALLTYPE beast_isRunningUnderDebugger() { - bassertfalse; // XXX not implemented for FreeBSD! + // XXX not implemented for FreeBSD! + bassertfalse; return false; } diff --git a/Subtrees/beast/modules/beast_core/native/beast_linux_SystemStats.cpp b/Subtrees/beast/modules/beast_core/native/beast_linux_SystemStats.cpp index 3764f477c..57ebaaf1c 100644 --- a/Subtrees/beast/modules/beast_core/native/beast_linux_SystemStats.cpp +++ b/Subtrees/beast/modules/beast_core/native/beast_linux_SystemStats.cpp @@ -78,7 +78,7 @@ int SystemStats::getMemorySizeInMegabytes() struct sysinfo sysi; if (sysinfo (&sysi) == 0) - return (sysi.totalram * sysi.mem_unit / (1024 * 1024)); + return sysi.totalram * sysi.mem_unit / (1024 * 1024); return 0; } @@ -94,11 +94,8 @@ String SystemStats::getLogonName() const char* user = getenv ("USER"); if (user == nullptr) - { - struct passwd* const pw = getpwuid (getuid()); - if (pw != nullptr) + if (passwd* const pw = getpwuid (getuid())) user = pw->pw_name; - } return CharPointer_UTF8 (user); } @@ -117,11 +114,12 @@ String SystemStats::getComputerName() return String::empty; } -String getLocaleValue (nl_item key) +static String getLocaleValue (nl_item key) { const char* oldLocale = ::setlocale (LC_ALL, ""); - return String (const_cast (nl_langinfo (key))); + String result (String::fromUTF8 (nl_langinfo (key))); ::setlocale (LC_ALL, oldLocale); + return result; } String SystemStats::getUserLanguage() { return getLocaleValue (_NL_IDENTIFICATION_LANGUAGE); } @@ -141,7 +139,7 @@ SystemStats::CPUFlags::CPUFlags() } //============================================================================== -uint32 beast_millisecondsSinceStartup() noexcept +uint32 BEAST_millisecondsSinceStartup() noexcept { timespec t; clock_gettime (CLOCK_MONOTONIC, &t); diff --git a/Subtrees/beast/modules/beast_core/text/beast_String.cpp b/Subtrees/beast/modules/beast_core/text/beast_String.cpp index 042c05793..8bd0c5317 100644 --- a/Subtrees/beast/modules/beast_core/text/beast_String.cpp +++ b/Subtrees/beast/modules/beast_core/text/beast_String.cpp @@ -1200,8 +1200,8 @@ public: dest = result.getCharPointer(); } - StringCreationHelper (const String::CharPointerType& source_) - : source (source_), dest (nullptr), allocatedBytes (StringHolder::getAllocatedNumBytes (source)), bytesWritten (0) + StringCreationHelper (const String::CharPointerType s) + : source (s), dest (nullptr), allocatedBytes (StringHolder::getAllocatedNumBytes (s)), bytesWritten (0) { result.preallocateBytes (allocatedBytes); dest = result.getCharPointer(); @@ -1531,7 +1531,8 @@ String String::quoted (const beast_wchar quoteCharacter) const } //============================================================================== -static String::CharPointerType findTrimmedEnd (const String::CharPointerType& start, String::CharPointerType end) +static String::CharPointerType findTrimmedEnd (const String::CharPointerType start, + String::CharPointerType end) { while (end > start) { diff --git a/Subtrees/beast/modules/beast_core/text/beast_TextDiff.cpp b/Subtrees/beast/modules/beast_core/text/beast_TextDiff.cpp index 5739da32e..18645157d 100644 --- a/Subtrees/beast/modules/beast_core/text/beast_TextDiff.cpp +++ b/Subtrees/beast/modules/beast_core/text/beast_TextDiff.cpp @@ -30,14 +30,14 @@ struct TextDiffHelpers StringRegion (const String& s) noexcept : text (s.getCharPointer()), start (0), length (s.length()) {} - StringRegion (const String::CharPointerType& t, int s, int len) noexcept + StringRegion (const String::CharPointerType t, int s, int len) noexcept : text (t), start (s), length (len) {} String::CharPointerType text; int start, length; }; - static void addInsertion (TextDiff& td, const String::CharPointerType& text, int index, int length) + static void addInsertion (TextDiff& td, const String::CharPointerType text, int index, int length) { TextDiff::Change c; c.insertedText = String (text, (size_t) length); @@ -99,7 +99,7 @@ struct TextDiffHelpers } static int findLongestCommonSubstring (String::CharPointerType a, const int lenA, - const String::CharPointerType& b, const int lenB, + const String::CharPointerType b, const int lenB, int& indexInA, int& indexInB) { if (lenA == 0 || lenB == 0) diff --git a/TODO.txt b/TODO.txt index b4dde7a2e..8d772e67e 100644 --- a/TODO.txt +++ b/TODO.txt @@ -208,3 +208,23 @@ boost::recursive_mutex Replace recursive_mutex with beast::Mutex to remove boost dependency +-------------------------------------------------------------------------------- +Davidisms +-------------------------------------------------------------------------------- + +(Figure out a good place to record information like this permanently) + +Regarding a defect where a failing transaction was being submitted over and over + again on the network (July 3, 2013) + + The core problem was an interaction between two bits of logic. + 1) Normally, we won't relay a transaction again if we already recently relayed + it. But this is bypassed if the transaction failed in a way that could + allow it to succeed later. This way, if one server discovers a transaction + can now work, it can get all servers to retry it. + 2) Normally, we won't relay a transaction if we think it can't claim a fee. + But if we're not sure it can't claim a fee because we're in an unhealthy + state, we propagate the transaction to let other servers decide if they + think it can claim a fee. + With these two bits of logic, two unhealthy servers could infinitely propagate + a transaction back and forth between each other.