diff options
Diffstat (limited to 'src/mongo/platform')
26 files changed, 1515 insertions, 1524 deletions
diff --git a/src/mongo/platform/atomic_word.h b/src/mongo/platform/atomic_word.h index ac39a24c0ed..cd5395c45ba 100644 --- a/src/mongo/platform/atomic_word.h +++ b/src/mongo/platform/atomic_word.h @@ -34,135 +34,133 @@ namespace mongo { +/** + * Implementation of the AtomicWord interface in terms of the C++11 Atomics. + */ +template <typename _WordType> +class AtomicWord { +public: /** - * Implementation of the AtomicWord interface in terms of the C++11 Atomics. + * Underlying value type. */ - template <typename _WordType> - class AtomicWord { - - public: - /** - * Underlying value type. - */ - typedef _WordType WordType; - - /** - * Construct a new word with the given initial value. - */ - explicit AtomicWord(WordType value=WordType(0)) : _value(value) {} - - /** - * Gets the current value of this AtomicWord. - * - * Has acquire and release semantics. - */ - WordType load() const { - return _value.load(); - } - - /** - * Gets the current value of this AtomicWord. - * - * Has relaxed semantics. - */ - WordType loadRelaxed() const { - return _value.load(std::memory_order_relaxed); - } - - /** - * Sets the value of this AtomicWord to "newValue". - * - * Has acquire and release semantics. - */ - void store(WordType newValue) { - return _value.store(newValue); - } - - /** - * Atomically swaps the current value of this with "newValue". - * - * Returns the old value. - * - * Has acquire and release semantics. - */ - WordType swap(WordType newValue) { - return _value.exchange(newValue); - } - - /** - * Atomic compare and swap. - * - * If this value equals "expected", sets this to "newValue". - * Always returns the original of this. - * - * Has acquire and release semantics. - */ - WordType compareAndSwap(WordType expected, WordType newValue) { - // NOTE: Subtle: compare_exchange mutates its first argument. - _value.compare_exchange_strong(expected, newValue); - return expected; - } - - /** - * Get the current value of this, add "increment" and store it, atomically. - * - * Returns the value of this before incrementing. - * - * Has acquire and release semantics. - */ - WordType fetchAndAdd(WordType increment) { - return _value.fetch_add(increment); - } - - /** - * Get the current value of this, subtract "decrement" and store it, atomically. - * - * Returns the value of this before decrementing. - * - * Has acquire and release semantics. - */ - WordType fetchAndSubtract(WordType decrement) { - return _value.fetch_sub(decrement); - } - - /** - * Get the current value of this, add "increment" and store it, atomically. - * - * Returns the value of this after incrementing. - * - * Has acquire and release semantics. - */ - WordType addAndFetch(WordType increment) { - return fetchAndAdd(increment) + increment; - } - - /** - * Get the current value of this, subtract "decrement" and store it, atomically. - * - * Returns the value of this after decrementing. - * - * Has acquire and release semantics. - */ - WordType subtractAndFetch(WordType decrement) { - return fetchAndSubtract(decrement) - decrement; - } - - private: - std::atomic<WordType> _value; - }; - -#define _ATOMIC_WORD_DECLARE(NAME, WTYPE) \ - typedef class AtomicWord<WTYPE> NAME; \ - namespace { \ - BOOST_STATIC_ASSERT(sizeof(NAME) == sizeof(WTYPE)); \ - BOOST_STATIC_ASSERT(std::is_standard_layout<WTYPE>::value); \ - } // namespace - - _ATOMIC_WORD_DECLARE(AtomicUInt32, unsigned); - _ATOMIC_WORD_DECLARE(AtomicUInt64, unsigned long long); - _ATOMIC_WORD_DECLARE(AtomicInt32, int); - _ATOMIC_WORD_DECLARE(AtomicInt64, long long); -#undef _ATOMIC_WORD_DECLARE + typedef _WordType WordType; + + /** + * Construct a new word with the given initial value. + */ + explicit AtomicWord(WordType value = WordType(0)) : _value(value) {} + + /** + * Gets the current value of this AtomicWord. + * + * Has acquire and release semantics. + */ + WordType load() const { + return _value.load(); + } + + /** + * Gets the current value of this AtomicWord. + * + * Has relaxed semantics. + */ + WordType loadRelaxed() const { + return _value.load(std::memory_order_relaxed); + } + + /** + * Sets the value of this AtomicWord to "newValue". + * + * Has acquire and release semantics. + */ + void store(WordType newValue) { + return _value.store(newValue); + } + + /** + * Atomically swaps the current value of this with "newValue". + * + * Returns the old value. + * + * Has acquire and release semantics. + */ + WordType swap(WordType newValue) { + return _value.exchange(newValue); + } + + /** + * Atomic compare and swap. + * + * If this value equals "expected", sets this to "newValue". + * Always returns the original of this. + * + * Has acquire and release semantics. + */ + WordType compareAndSwap(WordType expected, WordType newValue) { + // NOTE: Subtle: compare_exchange mutates its first argument. + _value.compare_exchange_strong(expected, newValue); + return expected; + } + + /** + * Get the current value of this, add "increment" and store it, atomically. + * + * Returns the value of this before incrementing. + * + * Has acquire and release semantics. + */ + WordType fetchAndAdd(WordType increment) { + return _value.fetch_add(increment); + } + + /** + * Get the current value of this, subtract "decrement" and store it, atomically. + * + * Returns the value of this before decrementing. + * + * Has acquire and release semantics. + */ + WordType fetchAndSubtract(WordType decrement) { + return _value.fetch_sub(decrement); + } + + /** + * Get the current value of this, add "increment" and store it, atomically. + * + * Returns the value of this after incrementing. + * + * Has acquire and release semantics. + */ + WordType addAndFetch(WordType increment) { + return fetchAndAdd(increment) + increment; + } -} // namespace mongo + /** + * Get the current value of this, subtract "decrement" and store it, atomically. + * + * Returns the value of this after decrementing. + * + * Has acquire and release semantics. + */ + WordType subtractAndFetch(WordType decrement) { + return fetchAndSubtract(decrement) - decrement; + } + +private: + std::atomic<WordType> _value; +}; + +#define _ATOMIC_WORD_DECLARE(NAME, WTYPE) \ + typedef class AtomicWord<WTYPE> NAME; \ + namespace { \ + BOOST_STATIC_ASSERT(sizeof(NAME) == sizeof(WTYPE)); \ + BOOST_STATIC_ASSERT(std::is_standard_layout<WTYPE>::value); \ + } // namespace + +_ATOMIC_WORD_DECLARE(AtomicUInt32, unsigned); +_ATOMIC_WORD_DECLARE(AtomicUInt64, unsigned long long); +_ATOMIC_WORD_DECLARE(AtomicInt32, int); +_ATOMIC_WORD_DECLARE(AtomicInt64, long long); +#undef _ATOMIC_WORD_DECLARE +} // namespace mongo diff --git a/src/mongo/platform/atomic_word_test.cpp b/src/mongo/platform/atomic_word_test.cpp index c8ba7c1defe..76af3b67eba 100644 --- a/src/mongo/platform/atomic_word_test.cpp +++ b/src/mongo/platform/atomic_word_test.cpp @@ -35,83 +35,83 @@ #include "mongo/unittest/unittest.h" namespace mongo { - namespace { - - template <typename _AtomicWordType> - void testAtomicWordBasicOperations() { - typedef typename _AtomicWordType::WordType WordType; - _AtomicWordType w; - - ASSERT_EQUALS(WordType(0), w.load()); - - w.store(1); - ASSERT_EQUALS(WordType(1), w.load()); - - ASSERT_EQUALS(WordType(1), w.swap(2)); - ASSERT_EQUALS(WordType(2), w.load()); - - ASSERT_EQUALS(WordType(2), w.compareAndSwap(0, 1)); - ASSERT_EQUALS(WordType(2), w.load()); - ASSERT_EQUALS(WordType(2), w.compareAndSwap(2, 1)); - ASSERT_EQUALS(WordType(1), w.load()); - - ASSERT_EQUALS(WordType(1), w.fetchAndAdd(14)); - ASSERT_EQUALS(WordType(17), w.addAndFetch(2)); - ASSERT_EQUALS(WordType(16), w.subtractAndFetch(1)); - ASSERT_EQUALS(WordType(16), w.fetchAndSubtract(1)); - ASSERT_EQUALS(WordType(15), w.compareAndSwap(15, 0)); - ASSERT_EQUALS(WordType(0), w.load()); - } - - TEST(AtomicWordTests, BasicOperationsUnsigned32Bit) { - typedef AtomicUInt32::WordType WordType; - testAtomicWordBasicOperations<AtomicUInt32>(); - - AtomicUInt32 w(0xdeadbeef); - ASSERT_EQUALS(WordType(0xdeadbeef), w.compareAndSwap(0, 1)); - ASSERT_EQUALS(WordType(0xdeadbeef), w.compareAndSwap(0xdeadbeef, 0xcafe1234)); - ASSERT_EQUALS(WordType(0xcafe1234), w.fetchAndAdd(0xf000)); - ASSERT_EQUALS(WordType(0xcaff0234), w.swap(0)); - ASSERT_EQUALS(WordType(0), w.load()); - } - - TEST(AtomicWordTests, BasicOperationsUnsigned64Bit) { - typedef AtomicUInt64::WordType WordType; - testAtomicWordBasicOperations<AtomicUInt64>(); - - AtomicUInt64 w(0xdeadbeefcafe1234ULL); - ASSERT_EQUALS(WordType(0xdeadbeefcafe1234ULL), w.compareAndSwap(0, 1)); - ASSERT_EQUALS(WordType(0xdeadbeefcafe1234ULL), - w.compareAndSwap(0xdeadbeefcafe1234ULL, 0xfedcba9876543210ULL)); - ASSERT_EQUALS(WordType(0xfedcba9876543210ULL), w.fetchAndAdd(0xf0000000ULL)); - ASSERT_EQUALS(WordType(0xfedcba9966543210ULL), w.swap(0)); - ASSERT_EQUALS(WordType(0), w.load()); - } - - TEST(AtomicWordTests, BasicOperationsSigned32Bit) { - typedef AtomicInt32::WordType WordType; - testAtomicWordBasicOperations<AtomicInt32>(); - - AtomicInt32 w(0xdeadbeef); - ASSERT_EQUALS(WordType(0xdeadbeef), w.compareAndSwap(0, 1)); - ASSERT_EQUALS(WordType(0xdeadbeef), w.compareAndSwap(0xdeadbeef, 0xcafe1234)); - ASSERT_EQUALS(WordType(0xcafe1234), w.fetchAndAdd(0xf000)); - ASSERT_EQUALS(WordType(0xcaff0234), w.swap(0)); - ASSERT_EQUALS(WordType(0), w.load()); - } - - TEST(AtomicWordTests, BasicOperationsSigned64Bit) { - typedef AtomicInt64::WordType WordType; - testAtomicWordBasicOperations<AtomicInt64>(); - - AtomicInt64 w(0xdeadbeefcafe1234ULL); - ASSERT_EQUALS(WordType(0xdeadbeefcafe1234LL), w.compareAndSwap(0, 1)); - ASSERT_EQUALS(WordType(0xdeadbeefcafe1234LL), - w.compareAndSwap(0xdeadbeefcafe1234LL, 0xfedcba9876543210LL)); - ASSERT_EQUALS(WordType(0xfedcba9876543210LL), w.fetchAndAdd(0xf0000000LL)); - ASSERT_EQUALS(WordType(0xfedcba9966543210LL), w.swap(0)); - ASSERT_EQUALS(WordType(0), w.load()); - } - - } // namespace +namespace { + +template <typename _AtomicWordType> +void testAtomicWordBasicOperations() { + typedef typename _AtomicWordType::WordType WordType; + _AtomicWordType w; + + ASSERT_EQUALS(WordType(0), w.load()); + + w.store(1); + ASSERT_EQUALS(WordType(1), w.load()); + + ASSERT_EQUALS(WordType(1), w.swap(2)); + ASSERT_EQUALS(WordType(2), w.load()); + + ASSERT_EQUALS(WordType(2), w.compareAndSwap(0, 1)); + ASSERT_EQUALS(WordType(2), w.load()); + ASSERT_EQUALS(WordType(2), w.compareAndSwap(2, 1)); + ASSERT_EQUALS(WordType(1), w.load()); + + ASSERT_EQUALS(WordType(1), w.fetchAndAdd(14)); + ASSERT_EQUALS(WordType(17), w.addAndFetch(2)); + ASSERT_EQUALS(WordType(16), w.subtractAndFetch(1)); + ASSERT_EQUALS(WordType(16), w.fetchAndSubtract(1)); + ASSERT_EQUALS(WordType(15), w.compareAndSwap(15, 0)); + ASSERT_EQUALS(WordType(0), w.load()); +} + +TEST(AtomicWordTests, BasicOperationsUnsigned32Bit) { + typedef AtomicUInt32::WordType WordType; + testAtomicWordBasicOperations<AtomicUInt32>(); + + AtomicUInt32 w(0xdeadbeef); + ASSERT_EQUALS(WordType(0xdeadbeef), w.compareAndSwap(0, 1)); + ASSERT_EQUALS(WordType(0xdeadbeef), w.compareAndSwap(0xdeadbeef, 0xcafe1234)); + ASSERT_EQUALS(WordType(0xcafe1234), w.fetchAndAdd(0xf000)); + ASSERT_EQUALS(WordType(0xcaff0234), w.swap(0)); + ASSERT_EQUALS(WordType(0), w.load()); +} + +TEST(AtomicWordTests, BasicOperationsUnsigned64Bit) { + typedef AtomicUInt64::WordType WordType; + testAtomicWordBasicOperations<AtomicUInt64>(); + + AtomicUInt64 w(0xdeadbeefcafe1234ULL); + ASSERT_EQUALS(WordType(0xdeadbeefcafe1234ULL), w.compareAndSwap(0, 1)); + ASSERT_EQUALS(WordType(0xdeadbeefcafe1234ULL), + w.compareAndSwap(0xdeadbeefcafe1234ULL, 0xfedcba9876543210ULL)); + ASSERT_EQUALS(WordType(0xfedcba9876543210ULL), w.fetchAndAdd(0xf0000000ULL)); + ASSERT_EQUALS(WordType(0xfedcba9966543210ULL), w.swap(0)); + ASSERT_EQUALS(WordType(0), w.load()); +} + +TEST(AtomicWordTests, BasicOperationsSigned32Bit) { + typedef AtomicInt32::WordType WordType; + testAtomicWordBasicOperations<AtomicInt32>(); + + AtomicInt32 w(0xdeadbeef); + ASSERT_EQUALS(WordType(0xdeadbeef), w.compareAndSwap(0, 1)); + ASSERT_EQUALS(WordType(0xdeadbeef), w.compareAndSwap(0xdeadbeef, 0xcafe1234)); + ASSERT_EQUALS(WordType(0xcafe1234), w.fetchAndAdd(0xf000)); + ASSERT_EQUALS(WordType(0xcaff0234), w.swap(0)); + ASSERT_EQUALS(WordType(0), w.load()); +} + +TEST(AtomicWordTests, BasicOperationsSigned64Bit) { + typedef AtomicInt64::WordType WordType; + testAtomicWordBasicOperations<AtomicInt64>(); + + AtomicInt64 w(0xdeadbeefcafe1234ULL); + ASSERT_EQUALS(WordType(0xdeadbeefcafe1234LL), w.compareAndSwap(0, 1)); + ASSERT_EQUALS(WordType(0xdeadbeefcafe1234LL), + w.compareAndSwap(0xdeadbeefcafe1234LL, 0xfedcba9876543210LL)); + ASSERT_EQUALS(WordType(0xfedcba9876543210LL), w.fetchAndAdd(0xf0000000LL)); + ASSERT_EQUALS(WordType(0xfedcba9966543210LL), w.swap(0)); + ASSERT_EQUALS(WordType(0), w.load()); +} + +} // namespace } // namespace mongo diff --git a/src/mongo/platform/basic.h b/src/mongo/platform/basic.h index bb46ef5e01f..ed456a3e312 100644 --- a/src/mongo/platform/basic.h +++ b/src/mongo/platform/basic.h @@ -33,4 +33,3 @@ #ifdef _WIN32 #include "windows_basic.h" #endif - diff --git a/src/mongo/platform/bits.h b/src/mongo/platform/bits.h index 2276318ba1a..8a59614af8f 100644 --- a/src/mongo/platform/bits.h +++ b/src/mongo/platform/bits.h @@ -35,7 +35,8 @@ // figure out if we're on a 64 or 32 bit system -#if defined(__x86_64__) || defined(__amd64__) || defined(_WIN64) || defined(__aarch64__) || defined(__powerpc64__) +#if defined(__x86_64__) || defined(__amd64__) || defined(_WIN64) || defined(__aarch64__) || \ + defined(__powerpc64__) #define MONGO_PLATFORM_64 #elif defined(__i386__) || defined(_WIN32) || defined(__arm__) #define MONGO_PLATFORM_32 @@ -45,60 +46,62 @@ namespace mongo { - /** - * Returns the number of leading 0-bits in num. Returns 64 if num is 0. - */ - inline int countLeadingZeros64(unsigned long long num); +/** + * Returns the number of leading 0-bits in num. Returns 64 if num is 0. + */ +inline int countLeadingZeros64(unsigned long long num); - /** - * Returns the number of trailing 0-bits in num. Returns 64 if num is 0. - */ - inline int countTrailingZeros64(unsigned long long num); +/** + * Returns the number of trailing 0-bits in num. Returns 64 if num is 0. + */ +inline int countTrailingZeros64(unsigned long long num); #if defined(__GNUC__) - int countLeadingZeros64(unsigned long long num) { - if (num == 0) return 64; - return __builtin_clzll(num); - } - - int countTrailingZeros64(unsigned long long num) { - if (num == 0) return 64; - return __builtin_ctzll(num); - } -#elif defined(_MSC_VER) && defined(_WIN64) - int countLeadingZeros64(unsigned long long num) { - unsigned long out; - if (_BitScanReverse64(&out, num)) - return 63 ^ out; +int countLeadingZeros64(unsigned long long num) { + if (num == 0) return 64; - } + return __builtin_clzll(num); +} - int countTrailingZeros64(unsigned long long num) { - unsigned long out; - if (_BitScanForward64(&out, num)) - return out; +int countTrailingZeros64(unsigned long long num) { + if (num == 0) return 64; - } + return __builtin_ctzll(num); +} +#elif defined(_MSC_VER) && defined(_WIN64) +int countLeadingZeros64(unsigned long long num) { + unsigned long out; + if (_BitScanReverse64(&out, num)) + return 63 ^ out; + return 64; +} + +int countTrailingZeros64(unsigned long long num) { + unsigned long out; + if (_BitScanForward64(&out, num)) + return out; + return 64; +} #elif defined(_MSC_VER) && defined(_WIN32) - int countLeadingZeros64(unsigned long long num) { - unsigned long out; - if (_BitScanReverse(&out, static_cast<unsigned long>(num >> 32))) - return 31 ^ out; - if (_BitScanReverse(&out, static_cast<unsigned long>(num))) - return 63 ^ out; - return 64; - } +int countLeadingZeros64(unsigned long long num) { + unsigned long out; + if (_BitScanReverse(&out, static_cast<unsigned long>(num >> 32))) + return 31 ^ out; + if (_BitScanReverse(&out, static_cast<unsigned long>(num))) + return 63 ^ out; + return 64; +} - int countTrailingZeros64(unsigned long long num) { - unsigned long out; - if (_BitScanForward(&out, static_cast<unsigned long>(num))) - return out; - if (_BitScanForward(&out, static_cast<unsigned long>(num >> 32))) - return out + 32; - return 64; - } +int countTrailingZeros64(unsigned long long num) { + unsigned long out; + if (_BitScanForward(&out, static_cast<unsigned long>(num))) + return out; + if (_BitScanForward(&out, static_cast<unsigned long>(num >> 32))) + return out + 32; + return 64; +} #else -# error "No bit-ops definitions for your platform" +#error "No bit-ops definitions for your platform" #endif } diff --git a/src/mongo/platform/bits_test.cpp b/src/mongo/platform/bits_test.cpp index 56e88fccb0b..06cc9a77cf1 100644 --- a/src/mongo/platform/bits_test.cpp +++ b/src/mongo/platform/bits_test.cpp @@ -33,35 +33,35 @@ namespace mongo { - TEST( BitsTest, HeaderCorrect ) { +TEST(BitsTest, HeaderCorrect) { #if defined(MONGO_PLATFORM_64) - ASSERT_EQUALS( 8U, sizeof(char*) ); + ASSERT_EQUALS(8U, sizeof(char*)); #elif defined(MONGO_PLATFORM_32) - ASSERT_EQUALS( 4U, sizeof(char*) ); + ASSERT_EQUALS(4U, sizeof(char*)); #else - ASSERT( false ); + ASSERT(false); #endif - } +} - TEST( BitsTest_CountZeros, Constants ) { - ASSERT_EQUALS( countLeadingZeros64(0ull), 64 ); - ASSERT_EQUALS( countTrailingZeros64(0ull), 64 ); +TEST(BitsTest_CountZeros, Constants) { + ASSERT_EQUALS(countLeadingZeros64(0ull), 64); + ASSERT_EQUALS(countTrailingZeros64(0ull), 64); - ASSERT_EQUALS( countLeadingZeros64(0x1234ull), 64 - 13); - ASSERT_EQUALS( countTrailingZeros64(0x1234ull), 2); + ASSERT_EQUALS(countLeadingZeros64(0x1234ull), 64 - 13); + ASSERT_EQUALS(countTrailingZeros64(0x1234ull), 2); - ASSERT_EQUALS( countLeadingZeros64(0x1234ull << 32), 32 - 13); - ASSERT_EQUALS( countTrailingZeros64(0x1234ull << 32), 2 + 32); + ASSERT_EQUALS(countLeadingZeros64(0x1234ull << 32), 32 - 13); + ASSERT_EQUALS(countTrailingZeros64(0x1234ull << 32), 2 + 32); - ASSERT_EQUALS( countLeadingZeros64((0x1234ull << 32) | 0x1234ull), 32 - 13); - ASSERT_EQUALS( countTrailingZeros64((0x1234ull << 32) | 0x1234ull), 2); - } - - TEST( BitsTest_CountZeros, EachBit ) { - for ( int i = 0; i < 64; i++ ) { - unsigned long long x = 1ULL << i; - ASSERT_EQUALS( countLeadingZeros64(x), 64 - 1 - i ); - ASSERT_EQUALS( countTrailingZeros64(x), i ); - } + ASSERT_EQUALS(countLeadingZeros64((0x1234ull << 32) | 0x1234ull), 32 - 13); + ASSERT_EQUALS(countTrailingZeros64((0x1234ull << 32) | 0x1234ull), 2); +} + +TEST(BitsTest_CountZeros, EachBit) { + for (int i = 0; i < 64; i++) { + unsigned long long x = 1ULL << i; + ASSERT_EQUALS(countLeadingZeros64(x), 64 - 1 - i); + ASSERT_EQUALS(countTrailingZeros64(x), i); } } +} diff --git a/src/mongo/platform/compiler_gcc.h b/src/mongo/platform/compiler_gcc.h index 431465893c9..46579eab963 100644 --- a/src/mongo/platform/compiler_gcc.h +++ b/src/mongo/platform/compiler_gcc.h @@ -38,9 +38,9 @@ #define MONGO_COMPILER_VARIABLE_UNUSED __attribute__((__unused__)) -#define MONGO_COMPILER_ALIGN_TYPE(ALIGNMENT) __attribute__(( __aligned__(ALIGNMENT) )) +#define MONGO_COMPILER_ALIGN_TYPE(ALIGNMENT) __attribute__((__aligned__(ALIGNMENT))) -#define MONGO_COMPILER_ALIGN_VARIABLE(ALIGNMENT) __attribute__(( __aligned__(ALIGNMENT) )) +#define MONGO_COMPILER_ALIGN_VARIABLE(ALIGNMENT) __attribute__((__aligned__(ALIGNMENT))) // NOTE(schwerin): These visibility and calling-convention macro definitions assume we're not using // GCC/CLANG to target native Windows. If/when we decide to do such targeting, we'll need to change @@ -58,7 +58,7 @@ // #endif // #else ... fall through to the definitions below. -#define MONGO_COMPILER_API_EXPORT __attribute__(( __visibility__("default") )) +#define MONGO_COMPILER_API_EXPORT __attribute__((__visibility__("default"))) #define MONGO_COMPILER_API_IMPORT #define MONGO_COMPILER_API_CALLING_CONVENTION diff --git a/src/mongo/platform/compiler_msvc.h b/src/mongo/platform/compiler_msvc.h index 50ceb8851d2..e1e87cc1744 100644 --- a/src/mongo/platform/compiler_msvc.h +++ b/src/mongo/platform/compiler_msvc.h @@ -38,9 +38,9 @@ #define MONGO_COMPILER_VARIABLE_UNUSED -#define MONGO_COMPILER_ALIGN_TYPE(ALIGNMENT) __declspec( align( ALIGNMENT ) ) +#define MONGO_COMPILER_ALIGN_TYPE(ALIGNMENT) __declspec(align(ALIGNMENT)) -#define MONGO_COMPILER_ALIGN_VARIABLE(ALIGNMENT) __declspec( align( ALIGNMENT ) ) +#define MONGO_COMPILER_ALIGN_VARIABLE(ALIGNMENT) __declspec(align(ALIGNMENT)) #define MONGO_COMPILER_API_EXPORT __declspec(dllexport) #define MONGO_COMPILER_API_IMPORT __declspec(dllimport) @@ -56,4 +56,3 @@ #define MONGO_likely(x) bool(x) #define MONGO_unlikely(x) bool(x) - diff --git a/src/mongo/platform/endian.h b/src/mongo/platform/endian.h index c4b5547c920..083fcda3a3c 100644 --- a/src/mongo/platform/endian.h +++ b/src/mongo/platform/endian.h @@ -76,427 +76,408 @@ #define MONGO_BIG_ENDIAN 4321 #if defined(_MSC_VER) && (_MSC_VER >= 1300) -# include <cstdlib> -# define MONGO_UINT16_SWAB(v) _byteswap_ushort(v) -# define MONGO_UINT32_SWAB(v) _byteswap_ulong(v) -# define MONGO_UINT64_SWAB(v) _byteswap_uint64(v) +#include <cstdlib> +#define MONGO_UINT16_SWAB(v) _byteswap_ushort(v) +#define MONGO_UINT32_SWAB(v) _byteswap_ulong(v) +#define MONGO_UINT64_SWAB(v) _byteswap_uint64(v) #elif defined(__clang__) && defined(__clang_major__) && defined(__clang_minor__) && \ - (__clang_major__ >= 3) && (__clang_minor__ >= 1) -# if __has_builtin(__builtin_bswap16) -# define MONGO_UINT16_SWAB(v) __builtin_bswap16(v) -# endif -# if __has_builtin(__builtin_bswap32) -# define MONGO_UINT32_SWAB(v) __builtin_bswap32(v) -# endif -# if __has_builtin(__builtin_bswap64) -# define MONGO_UINT64_SWAB(v) __builtin_bswap64(v) -# endif + (__clang_major__ >= 3) && (__clang_minor__ >= 1) +#if __has_builtin(__builtin_bswap16) +#define MONGO_UINT16_SWAB(v) __builtin_bswap16(v) +#endif +#if __has_builtin(__builtin_bswap32) +#define MONGO_UINT32_SWAB(v) __builtin_bswap32(v) +#endif +#if __has_builtin(__builtin_bswap64) +#define MONGO_UINT64_SWAB(v) __builtin_bswap64(v) +#endif #elif defined(__GNUC__) && (__GNUC__ >= 4) -# if __GNUC__ >= 4 && defined (__GNUC_MINOR__) && __GNUC_MINOR__ >= 3 -# define MONGO_UINT32_SWAB(v) __builtin_bswap32(v) -# define MONGO_UINT64_SWAB(v) __builtin_bswap64(v) -# endif -# if __GNUC__ >= 4 && defined (__GNUC_MINOR__) && __GNUC_MINOR__ >= 8 -# define MONGO_UINT16_SWAB(v) __builtin_bswap16(v) -# endif +#if __GNUC__ >= 4 && defined(__GNUC_MINOR__) && __GNUC_MINOR__ >= 3 +#define MONGO_UINT32_SWAB(v) __builtin_bswap32(v) +#define MONGO_UINT64_SWAB(v) __builtin_bswap64(v) +#endif +#if __GNUC__ >= 4 && defined(__GNUC_MINOR__) && __GNUC_MINOR__ >= 8 +#define MONGO_UINT16_SWAB(v) __builtin_bswap16(v) +#endif #elif defined(__sun) -# include <sys/byteorder.h> -# define MONGO_UINT16_SWAB(v) BSWAP_16(v) -# define MONGO_UINT32_SWAB(v) BSWAP_32(v) -# define MONGO_UINT64_SWAB(v) BSWAP_64(v) +#include <sys/byteorder.h> +#define MONGO_UINT16_SWAB(v) BSWAP_16(v) +#define MONGO_UINT32_SWAB(v) BSWAP_32(v) +#define MONGO_UINT64_SWAB(v) BSWAP_64(v) #endif #ifndef MONGO_UINT16_SWAB -# define MONGO_UINT16_SWAB(v) endian::bswap_slow16(v) +#define MONGO_UINT16_SWAB(v) endian::bswap_slow16(v) #endif #ifndef MONGO_UINT32_SWAB -# define MONGO_UINT32_SWAB(v) endian::bswap_slow32(v) +#define MONGO_UINT32_SWAB(v) endian::bswap_slow32(v) #endif #ifndef MONGO_UINT64_SWAB -# define MONGO_UINT64_SWAB(v) endian::bswap_slow64(v) +#define MONGO_UINT64_SWAB(v) endian::bswap_slow64(v) #endif #if MONGO_CONFIG_BYTE_ORDER == MONGO_LITTLE_ENDIAN -# define htobe16(v) MONGO_UINT16_SWAB(v) -# define htobe32(v) MONGO_UINT32_SWAB(v) -# define htobe64(v) MONGO_UINT64_SWAB(v) -# define htole16(v) (v) -# define htole32(v) (v) -# define htole64(v) (v) -# define be16toh(v) MONGO_UINT16_SWAB(v) -# define be32toh(v) MONGO_UINT32_SWAB(v) -# define be64toh(v) MONGO_UINT64_SWAB(v) -# define le16toh(v) (v) -# define le32toh(v) (v) -# define le64toh(v) (v) +#define htobe16(v) MONGO_UINT16_SWAB(v) +#define htobe32(v) MONGO_UINT32_SWAB(v) +#define htobe64(v) MONGO_UINT64_SWAB(v) +#define htole16(v) (v) +#define htole32(v) (v) +#define htole64(v) (v) +#define be16toh(v) MONGO_UINT16_SWAB(v) +#define be32toh(v) MONGO_UINT32_SWAB(v) +#define be64toh(v) MONGO_UINT64_SWAB(v) +#define le16toh(v) (v) +#define le32toh(v) (v) +#define le64toh(v) (v) #elif MONGO_CONFIG_BYTE_ORDER == MONGO_BIG_ENDIAN -# define htobe16(v) (v) -# define htobe32(v) (v) -# define htobe64(v) (v) -# define htole16(v) MONGO_UINT16_SWAB(v) -# define htole32(v) MONGO_UINT32_SWAB(v) -# define htole64(v) MONGO_UINT64_SWAB(v) -# define be16toh(v) (v) -# define be32toh(v) (v) -# define be64toh(v) (v) -# define le16toh(v) MONGO_UINT16_SWAB(v) -# define le32toh(v) MONGO_UINT32_SWAB(v) -# define le64toh(v) MONGO_UINT64_SWAB(v) +#define htobe16(v) (v) +#define htobe32(v) (v) +#define htobe64(v) (v) +#define htole16(v) MONGO_UINT16_SWAB(v) +#define htole32(v) MONGO_UINT32_SWAB(v) +#define htole64(v) MONGO_UINT64_SWAB(v) +#define be16toh(v) (v) +#define be32toh(v) (v) +#define be64toh(v) (v) +#define le16toh(v) MONGO_UINT16_SWAB(v) +#define le32toh(v) MONGO_UINT32_SWAB(v) +#define le64toh(v) MONGO_UINT64_SWAB(v) #else -# error "The endianness of target architecture is unknown. " \ +#error \ + "The endianness of target architecture is unknown. " \ "Please define MONGO_CONFIG_BYTE_ORDER" #endif namespace mongo { namespace endian { - static inline uint16_t bswap_slow16(uint16_t v) { - return ((v & 0x00FF) << 8) | - ((v & 0xFF00) >> 8); +static inline uint16_t bswap_slow16(uint16_t v) { + return ((v & 0x00FF) << 8) | ((v & 0xFF00) >> 8); +} + +static inline uint32_t bswap_slow32(uint32_t v) { + return ((v & 0x000000FFUL) << 24) | ((v & 0x0000FF00UL) << 8) | ((v & 0x00FF0000UL) >> 8) | + ((v & 0xFF000000UL) >> 24); +} + +static inline uint64_t bswap_slow64(uint64_t v) { + return ((v & 0x00000000000000FFULL) << 56) | ((v & 0x000000000000FF00ULL) << 40) | + ((v & 0x0000000000FF0000ULL) << 24) | ((v & 0x00000000FF000000ULL) << 8) | + ((v & 0x000000FF00000000ULL) >> 8) | ((v & 0x0000FF0000000000ULL) >> 24) | + ((v & 0x00FF000000000000ULL) >> 40) | ((v & 0xFF00000000000000ULL) >> 56); +} + +template <typename T> +struct ByteOrderConverter; + +template <> +struct ByteOrderConverter<uint8_t> { + typedef uint8_t T; + + inline static T nativeToBig(T t) { + return t; + } + + inline static T bigToNative(T t) { + return t; + } + + inline static T nativeToLittle(T t) { + return t; } - static inline uint32_t bswap_slow32(uint32_t v) { - return ((v & 0x000000FFUL) << 24) | - ((v & 0x0000FF00UL) << 8) | - ((v & 0x00FF0000UL) >> 8) | - ((v & 0xFF000000UL) >> 24); + inline static T littleToNative(T t) { + return t; } +}; - static inline uint64_t bswap_slow64(uint64_t v) { - return ((v & 0x00000000000000FFULL) << 56) | - ((v & 0x000000000000FF00ULL) << 40) | - ((v & 0x0000000000FF0000ULL) << 24) | - ((v & 0x00000000FF000000ULL) << 8) | - ((v & 0x000000FF00000000ULL) >> 8) | - ((v & 0x0000FF0000000000ULL) >> 24) | - ((v & 0x00FF000000000000ULL) >> 40) | - ((v & 0xFF00000000000000ULL) >> 56); +template <> +struct ByteOrderConverter<uint16_t> { + typedef uint16_t T; + + inline static T nativeToBig(T t) { + return htobe16(t); + } + + inline static T bigToNative(T t) { + return be16toh(t); + } + + inline static T nativeToLittle(T t) { + return htole16(t); + } + + inline static T littleToNative(T t) { + return le16toh(t); } +}; - template<typename T> - struct ByteOrderConverter; +template <> +struct ByteOrderConverter<uint32_t> { + typedef uint32_t T; - template<> - struct ByteOrderConverter<uint8_t> { + inline static T nativeToBig(T t) { + return htobe32(t); + } + + inline static T bigToNative(T t) { + return be32toh(t); + } + + inline static T nativeToLittle(T t) { + return htole32(t); + } + + inline static T littleToNative(T t) { + return le32toh(t); + } +}; - typedef uint8_t T; +template <> +struct ByteOrderConverter<uint64_t> { + typedef uint64_t T; - inline static T nativeToBig(T t) { - return t; - } + inline static T nativeToBig(T t) { + return htobe64(t); + } - inline static T bigToNative(T t) { - return t; - } + inline static T bigToNative(T t) { + return be64toh(t); + } - inline static T nativeToLittle(T t) { - return t; - } + inline static T nativeToLittle(T t) { + return htole64(t); + } - inline static T littleToNative(T t) { - return t; - } - }; + inline static T littleToNative(T t) { + return le64toh(t); + } +}; - template<> - struct ByteOrderConverter<uint16_t> { +template <> +struct ByteOrderConverter<int8_t> { + typedef int8_t T; - typedef uint16_t T; + inline static T nativeToBig(T t) { + return t; + } - inline static T nativeToBig(T t) { - return htobe16(t); - } + inline static T bigToNative(T t) { + return t; + } - inline static T bigToNative(T t) { - return be16toh(t); - } + inline static T nativeToLittle(T t) { + return t; + } - inline static T nativeToLittle(T t) { - return htole16(t); - } + inline static T littleToNative(T t) { + return t; + } +}; - inline static T littleToNative(T t) { - return le16toh(t); - } - }; +template <> +struct ByteOrderConverter<int16_t> { + typedef int16_t T; - template<> - struct ByteOrderConverter<uint32_t> { + inline static T nativeToBig(T t) { + return htobe16(static_cast<uint16_t>(t)); + } - typedef uint32_t T; + inline static T bigToNative(T t) { + return be16toh(static_cast<uint16_t>(t)); + } - inline static T nativeToBig(T t) { - return htobe32(t); - } + inline static T nativeToLittle(T t) { + return htole16(static_cast<uint16_t>(t)); + } - inline static T bigToNative(T t) { - return be32toh(t); - } + inline static T littleToNative(T t) { + return le16toh(static_cast<uint16_t>(t)); + } +}; - inline static T nativeToLittle(T t) { - return htole32(t); - } +template <> +struct ByteOrderConverter<int32_t> { + typedef int32_t T; - inline static T littleToNative(T t) { - return le32toh(t); - } - }; + inline static T nativeToBig(T t) { + return htobe32(static_cast<uint32_t>(t)); + } - template<> - struct ByteOrderConverter<uint64_t> { + inline static T bigToNative(T t) { + return be32toh(static_cast<uint32_t>(t)); + } - typedef uint64_t T; + inline static T nativeToLittle(T t) { + return htole32(static_cast<uint32_t>(t)); + } - inline static T nativeToBig(T t) { - return htobe64(t); - } + inline static T littleToNative(T t) { + return le32toh(static_cast<uint32_t>(t)); + } +}; - inline static T bigToNative(T t) { - return be64toh(t); - } +template <> +struct ByteOrderConverter<int64_t> { + typedef int64_t T; - inline static T nativeToLittle(T t) { - return htole64(t); - } + inline static T nativeToBig(T t) { + return htobe64(static_cast<uint64_t>(t)); + } - inline static T littleToNative(T t) { - return le64toh(t); - } - }; + inline static T bigToNative(T t) { + return be64toh(static_cast<uint64_t>(t)); + } - template<> - struct ByteOrderConverter<int8_t> { + inline static T nativeToLittle(T t) { + return htole64(static_cast<uint64_t>(t)); + } - typedef int8_t T; + inline static T littleToNative(T t) { + return le64toh(static_cast<uint64_t>(t)); + } +}; - inline static T nativeToBig(T t) { - return t; - } +template <> +struct ByteOrderConverter<float> { + typedef float T; - inline static T bigToNative(T t) { - return t; - } + inline static T nativeToBig(T t) { + BOOST_STATIC_ASSERT(sizeof(T) == sizeof(uint32_t)); - inline static T nativeToLittle(T t) { - return t; - } + uint32_t temp; + std::memcpy(&temp, &t, sizeof(t)); + temp = htobe32(temp); + std::memcpy(&t, &temp, sizeof(t)); + return t; + } - inline static T littleToNative(T t) { - return t; - } - }; + inline static T bigToNative(T t) { + uint32_t temp; + std::memcpy(&temp, &t, sizeof(t)); + temp = be32toh(temp); + std::memcpy(&t, &temp, sizeof(t)); + return t; + } - template<> - struct ByteOrderConverter<int16_t> { + inline static T nativeToLittle(T t) { + uint32_t temp; + std::memcpy(&temp, &t, sizeof(t)); + temp = htole32(temp); + std::memcpy(&t, &temp, sizeof(t)); + return t; + } - typedef int16_t T; + inline static T littleToNative(T t) { + uint32_t temp; + std::memcpy(&temp, &t, sizeof(t)); + temp = le32toh(temp); + std::memcpy(&t, &temp, sizeof(t)); + return t; + } +}; - inline static T nativeToBig(T t) { - return htobe16(static_cast<uint16_t>(t)); - } +template <> +struct ByteOrderConverter<double> { + typedef double T; - inline static T bigToNative(T t) { - return be16toh(static_cast<uint16_t>(t)); - } + inline static T nativeToBig(T t) { + BOOST_STATIC_ASSERT(sizeof(T) == sizeof(uint64_t)); - inline static T nativeToLittle(T t) { - return htole16(static_cast<uint16_t>(t)); - } + uint64_t temp; + std::memcpy(&temp, &t, sizeof(t)); + temp = htobe64(temp); + std::memcpy(&t, &temp, sizeof(t)); + return t; + } - inline static T littleToNative(T t) { - return le16toh(static_cast<uint16_t>(t)); - } - }; + inline static T bigToNative(T t) { + uint64_t temp; + std::memcpy(&temp, &t, sizeof(t)); + temp = be64toh(temp); + std::memcpy(&t, &temp, sizeof(t)); + return t; + } - template<> - struct ByteOrderConverter<int32_t> { + inline static T nativeToLittle(T t) { + uint64_t temp; + std::memcpy(&temp, &t, sizeof(t)); + temp = htole64(temp); + std::memcpy(&t, &temp, sizeof(t)); + return t; + } - typedef int32_t T; - - inline static T nativeToBig(T t) { - return htobe32(static_cast<uint32_t>(t)); - } - - inline static T bigToNative(T t) { - return be32toh(static_cast<uint32_t>(t)); - } - - inline static T nativeToLittle(T t) { - return htole32(static_cast<uint32_t>(t)); - } - - inline static T littleToNative(T t) { - return le32toh(static_cast<uint32_t>(t)); - } - }; - - template<> - struct ByteOrderConverter<int64_t> { - - typedef int64_t T; - - inline static T nativeToBig(T t) { - return htobe64(static_cast<uint64_t>(t)); - } - - inline static T bigToNative(T t) { - return be64toh(static_cast<uint64_t>(t)); - } - - inline static T nativeToLittle(T t) { - return htole64(static_cast<uint64_t>(t)); - } - - inline static T littleToNative(T t) { - return le64toh(static_cast<uint64_t>(t)); - } - }; - - template<> - struct ByteOrderConverter<float> { - - typedef float T; - - inline static T nativeToBig(T t) { - BOOST_STATIC_ASSERT(sizeof(T) == sizeof(uint32_t)); - - uint32_t temp; - std::memcpy(&temp, &t, sizeof(t)); - temp = htobe32(temp); - std::memcpy(&t, &temp, sizeof(t)); - return t; - } - - inline static T bigToNative(T t) { - uint32_t temp; - std::memcpy(&temp, &t, sizeof(t)); - temp = be32toh(temp); - std::memcpy(&t, &temp, sizeof(t)); - return t; - } - - inline static T nativeToLittle(T t) { - uint32_t temp; - std::memcpy(&temp, &t, sizeof(t)); - temp = htole32(temp); - std::memcpy(&t, &temp, sizeof(t)); - return t; - } - - inline static T littleToNative(T t) { - uint32_t temp; - std::memcpy(&temp, &t, sizeof(t)); - temp = le32toh(temp); - std::memcpy(&t, &temp, sizeof(t)); - return t; - } - }; - - template<> - struct ByteOrderConverter<double> { - - typedef double T; - - inline static T nativeToBig(T t) { - BOOST_STATIC_ASSERT(sizeof(T) == sizeof(uint64_t)); - - uint64_t temp; - std::memcpy(&temp, &t, sizeof(t)); - temp = htobe64(temp); - std::memcpy(&t, &temp, sizeof(t)); - return t; - } - - inline static T bigToNative(T t) { - uint64_t temp; - std::memcpy(&temp, &t, sizeof(t)); - temp = be64toh(temp); - std::memcpy(&t, &temp, sizeof(t)); - return t; - } - - inline static T nativeToLittle(T t) { - uint64_t temp; - std::memcpy(&temp, &t, sizeof(t)); - temp = htole64(temp); - std::memcpy(&t, &temp, sizeof(t)); - return t; - } - - inline static T littleToNative(T t) { - uint64_t temp; - std::memcpy(&temp, &t, sizeof(t)); - temp = le64toh(temp); - std::memcpy(&t, &temp, sizeof(t)); - return t; - } - }; - - // Use a typemape to normalize non-fixed-width integral types to the associated fixed width - // types. - - template<typename T> - struct IntegralTypeMap { - typedef T type; - }; - - template<> - struct IntegralTypeMap<signed char> { - BOOST_STATIC_ASSERT(CHAR_BIT == 8); - typedef int8_t type; - }; - - template<> - struct IntegralTypeMap<unsigned char> { - BOOST_STATIC_ASSERT(CHAR_BIT == 8); - typedef uint8_t type; - }; - - template<> - struct IntegralTypeMap<char> { - BOOST_STATIC_ASSERT(CHAR_BIT == 8); - typedef boost::mpl::if_c< - boost::is_signed<char>::value, - int8_t, - uint8_t>::type type; - }; - - template<> - struct IntegralTypeMap<long long> { - BOOST_STATIC_ASSERT(sizeof(long long) == sizeof(int64_t)); - typedef int64_t type; - }; - - template<> - struct IntegralTypeMap<unsigned long long> { - BOOST_STATIC_ASSERT(sizeof(unsigned long long) == sizeof(uint64_t)); - typedef uint64_t type; - }; - - template<typename T> - inline T nativeToBig(T t) { - return ByteOrderConverter<typename IntegralTypeMap<T>::type>::nativeToBig(t); - } - - template<typename T> - inline T bigToNative(T t) { - return ByteOrderConverter<typename IntegralTypeMap<T>::type>::bigToNative(t); - } - - template<typename T> - inline T nativeToLittle(T t) { - return ByteOrderConverter<typename IntegralTypeMap<T>::type>::nativeToLittle(t); - } - - template<typename T> - inline T littleToNative(T t) { - return ByteOrderConverter<typename IntegralTypeMap<T>::type>::littleToNative(t); - } - -} // namespace endian -} // namespace mongo + inline static T littleToNative(T t) { + uint64_t temp; + std::memcpy(&temp, &t, sizeof(t)); + temp = le64toh(temp); + std::memcpy(&t, &temp, sizeof(t)); + return t; + } +}; + +// Use a typemape to normalize non-fixed-width integral types to the associated fixed width +// types. + +template <typename T> +struct IntegralTypeMap { + typedef T type; +}; + +template <> +struct IntegralTypeMap<signed char> { + BOOST_STATIC_ASSERT(CHAR_BIT == 8); + typedef int8_t type; +}; + +template <> +struct IntegralTypeMap<unsigned char> { + BOOST_STATIC_ASSERT(CHAR_BIT == 8); + typedef uint8_t type; +}; + +template <> +struct IntegralTypeMap<char> { + BOOST_STATIC_ASSERT(CHAR_BIT == 8); + typedef boost::mpl::if_c<boost::is_signed<char>::value, int8_t, uint8_t>::type type; +}; + +template <> +struct IntegralTypeMap<long long> { + BOOST_STATIC_ASSERT(sizeof(long long) == sizeof(int64_t)); + typedef int64_t type; +}; + +template <> +struct IntegralTypeMap<unsigned long long> { + BOOST_STATIC_ASSERT(sizeof(unsigned long long) == sizeof(uint64_t)); + typedef uint64_t type; +}; + +template <typename T> +inline T nativeToBig(T t) { + return ByteOrderConverter<typename IntegralTypeMap<T>::type>::nativeToBig(t); +} + +template <typename T> +inline T bigToNative(T t) { + return ByteOrderConverter<typename IntegralTypeMap<T>::type>::bigToNative(t); +} + +template <typename T> +inline T nativeToLittle(T t) { + return ByteOrderConverter<typename IntegralTypeMap<T>::type>::nativeToLittle(t); +} + +template <typename T> +inline T littleToNative(T t) { + return ByteOrderConverter<typename IntegralTypeMap<T>::type>::littleToNative(t); +} + +} // namespace endian +} // namespace mongo #undef MONGO_UINT16_SWAB #undef MONGO_UINT32_SWAB diff --git a/src/mongo/platform/endian_test.cpp b/src/mongo/platform/endian_test.cpp index 0188b438dd7..ec74211da3c 100644 --- a/src/mongo/platform/endian_test.cpp +++ b/src/mongo/platform/endian_test.cpp @@ -34,522 +34,522 @@ namespace mongo { - using namespace endian; +using namespace endian; - TEST( EndianTest, TestSlow16 ) { - uint8_t le_buf[] = { 0x01, 0x02 }; - uint8_t be_buf[] = { 0x02, 0x01 }; - uint16_t le; - uint16_t be; - std::memcpy(&le, le_buf, sizeof(le)); - std::memcpy(&be, be_buf, sizeof(be)); +TEST(EndianTest, TestSlow16) { + uint8_t le_buf[] = {0x01, 0x02}; + uint8_t be_buf[] = {0x02, 0x01}; + uint16_t le; + uint16_t be; + std::memcpy(&le, le_buf, sizeof(le)); + std::memcpy(&be, be_buf, sizeof(be)); - ASSERT_EQUALS(be, endian::bswap_slow16(le)); - ASSERT_EQUALS(le, endian::bswap_slow16(be)); - } + ASSERT_EQUALS(be, endian::bswap_slow16(le)); + ASSERT_EQUALS(le, endian::bswap_slow16(be)); +} - TEST( EndianTest, TestSlow32 ) { - uint8_t le_buf[] = { 0x01, 0x02, 0x03, 0x04 }; - uint8_t be_buf[] = { 0x04, 0x03, 0x02, 0x01 }; - uint32_t le; - uint32_t be; - std::memcpy(&le, le_buf, sizeof(le)); - std::memcpy(&be, be_buf, sizeof(be)); +TEST(EndianTest, TestSlow32) { + uint8_t le_buf[] = {0x01, 0x02, 0x03, 0x04}; + uint8_t be_buf[] = {0x04, 0x03, 0x02, 0x01}; + uint32_t le; + uint32_t be; + std::memcpy(&le, le_buf, sizeof(le)); + std::memcpy(&be, be_buf, sizeof(be)); - ASSERT_EQUALS(be, endian::bswap_slow32(le)); - ASSERT_EQUALS(le, endian::bswap_slow32(be)); - } + ASSERT_EQUALS(be, endian::bswap_slow32(le)); + ASSERT_EQUALS(le, endian::bswap_slow32(be)); +} - TEST( EndianTest, TestSlow64 ) { - uint8_t le_buf[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; - uint8_t be_buf[] = { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01 }; - uint64_t le; - uint64_t be; - std::memcpy(&le, le_buf, sizeof(le)); - std::memcpy(&be, be_buf, sizeof(be)); +TEST(EndianTest, TestSlow64) { + uint8_t le_buf[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; + uint8_t be_buf[] = {0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01}; + uint64_t le; + uint64_t be; + std::memcpy(&le, le_buf, sizeof(le)); + std::memcpy(&be, be_buf, sizeof(be)); - ASSERT_EQUALS(be, endian::bswap_slow64(le)); - ASSERT_EQUALS(le, endian::bswap_slow64(be)); - } + ASSERT_EQUALS(be, endian::bswap_slow64(le)); + ASSERT_EQUALS(le, endian::bswap_slow64(be)); +} - TEST( EndianTest, NativeToBig_uint16_t ) { - uint8_t le_buf[] = { 0x01, 0x02 }; - uint8_t be_buf[] = { 0x02, 0x01 }; - uint16_t le; - uint16_t be; - std::memcpy(&le, le_buf, sizeof(le)); - std::memcpy(&be, be_buf, sizeof(be)); +TEST(EndianTest, NativeToBig_uint16_t) { + uint8_t le_buf[] = {0x01, 0x02}; + uint8_t be_buf[] = {0x02, 0x01}; + uint16_t le; + uint16_t be; + std::memcpy(&le, le_buf, sizeof(le)); + std::memcpy(&be, be_buf, sizeof(be)); #if MONGO_CONFIG_BYTE_ORDER == 1234 - ASSERT_EQUALS(be, nativeToBig(le)); + ASSERT_EQUALS(be, nativeToBig(le)); #else - ASSERT_EQUALS(be, nativeToBig(be)); + ASSERT_EQUALS(be, nativeToBig(be)); #endif - } +} - TEST( EndianTest, NativeToBig_uint32_t ) { - uint8_t le_buf[] = { 0x01, 0x02, 0x03, 0x04 }; - uint8_t be_buf[] = { 0x04, 0x03, 0x02, 0x01 }; - uint32_t le; - uint32_t be; - std::memcpy(&le, le_buf, sizeof(le)); - std::memcpy(&be, be_buf, sizeof(be)); +TEST(EndianTest, NativeToBig_uint32_t) { + uint8_t le_buf[] = {0x01, 0x02, 0x03, 0x04}; + uint8_t be_buf[] = {0x04, 0x03, 0x02, 0x01}; + uint32_t le; + uint32_t be; + std::memcpy(&le, le_buf, sizeof(le)); + std::memcpy(&be, be_buf, sizeof(be)); #if MONGO_CONFIG_BYTE_ORDER == 1234 - ASSERT_EQUALS(be, nativeToBig(le)); + ASSERT_EQUALS(be, nativeToBig(le)); #else - ASSERT_EQUALS(be, nativeToBig(be)); + ASSERT_EQUALS(be, nativeToBig(be)); #endif - } +} - TEST( EndianTest, NativeToBig_uint64_t ) { - uint8_t le_buf[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; - uint8_t be_buf[] = { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01 }; - uint64_t le; - uint64_t be; - std::memcpy(&le, le_buf, sizeof(le)); - std::memcpy(&be, be_buf, sizeof(be)); +TEST(EndianTest, NativeToBig_uint64_t) { + uint8_t le_buf[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; + uint8_t be_buf[] = {0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01}; + uint64_t le; + uint64_t be; + std::memcpy(&le, le_buf, sizeof(le)); + std::memcpy(&be, be_buf, sizeof(be)); #if MONGO_CONFIG_BYTE_ORDER == 1234 - ASSERT_EQUALS(be, nativeToBig(le)); + ASSERT_EQUALS(be, nativeToBig(le)); #else - ASSERT_EQUALS(be, nativeToBig(be)); + ASSERT_EQUALS(be, nativeToBig(be)); #endif - } +} - TEST( EndianTest, NativeToBig_int16_t ) { - uint8_t le_buf[] = { 0x01, 0x02 }; - uint8_t be_buf[] = { 0x02, 0x01 }; - int16_t le; - int16_t be; - std::memcpy(&le, le_buf, sizeof(le)); - std::memcpy(&be, be_buf, sizeof(be)); +TEST(EndianTest, NativeToBig_int16_t) { + uint8_t le_buf[] = {0x01, 0x02}; + uint8_t be_buf[] = {0x02, 0x01}; + int16_t le; + int16_t be; + std::memcpy(&le, le_buf, sizeof(le)); + std::memcpy(&be, be_buf, sizeof(be)); #if MONGO_CONFIG_BYTE_ORDER == 1234 - ASSERT_EQUALS(be, nativeToBig(le)); + ASSERT_EQUALS(be, nativeToBig(le)); #else - ASSERT_EQUALS(be, nativeToBig(be)); + ASSERT_EQUALS(be, nativeToBig(be)); #endif - } +} - TEST( EndianTest, NativeToBig_int32_t ) { - uint8_t le_buf[] = { 0x01, 0x02, 0x03, 0x04 }; - uint8_t be_buf[] = { 0x04, 0x03, 0x02, 0x01 }; - int32_t le; - int32_t be; - std::memcpy(&le, le_buf, sizeof(le)); - std::memcpy(&be, be_buf, sizeof(be)); +TEST(EndianTest, NativeToBig_int32_t) { + uint8_t le_buf[] = {0x01, 0x02, 0x03, 0x04}; + uint8_t be_buf[] = {0x04, 0x03, 0x02, 0x01}; + int32_t le; + int32_t be; + std::memcpy(&le, le_buf, sizeof(le)); + std::memcpy(&be, be_buf, sizeof(be)); #if MONGO_CONFIG_BYTE_ORDER == 1234 - ASSERT_EQUALS(be, nativeToBig(le)); + ASSERT_EQUALS(be, nativeToBig(le)); #else - ASSERT_EQUALS(be, nativeToBig(be)); + ASSERT_EQUALS(be, nativeToBig(be)); #endif - } +} - TEST( EndianTest, NativeToBig_int64_t ) { - uint8_t le_buf[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; - uint8_t be_buf[] = { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01 }; - int64_t le; - int64_t be; - std::memcpy(&le, le_buf, sizeof(le)); - std::memcpy(&be, be_buf, sizeof(be)); +TEST(EndianTest, NativeToBig_int64_t) { + uint8_t le_buf[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; + uint8_t be_buf[] = {0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01}; + int64_t le; + int64_t be; + std::memcpy(&le, le_buf, sizeof(le)); + std::memcpy(&be, be_buf, sizeof(be)); #if MONGO_CONFIG_BYTE_ORDER == 1234 - ASSERT_EQUALS(be, nativeToBig(le)); + ASSERT_EQUALS(be, nativeToBig(le)); #else - ASSERT_EQUALS(be, nativeToBig(be)); + ASSERT_EQUALS(be, nativeToBig(be)); #endif - } +} - TEST( EndianTest, NativeToBig_float ) { - uint8_t le_buf[] = { 0x01, 0x02, 0x03, 0x04 }; - uint8_t be_buf[] = { 0x04, 0x03, 0x02, 0x01 }; - float le; - float be; - std::memcpy(&le, le_buf, sizeof(le)); - std::memcpy(&be, be_buf, sizeof(be)); +TEST(EndianTest, NativeToBig_float) { + uint8_t le_buf[] = {0x01, 0x02, 0x03, 0x04}; + uint8_t be_buf[] = {0x04, 0x03, 0x02, 0x01}; + float le; + float be; + std::memcpy(&le, le_buf, sizeof(le)); + std::memcpy(&be, be_buf, sizeof(be)); #if MONGO_CONFIG_BYTE_ORDER == 1234 - ASSERT_EQUALS(be, nativeToBig(le)); + ASSERT_EQUALS(be, nativeToBig(le)); #else - ASSERT_EQUALS(be, nativeToBig(be)); + ASSERT_EQUALS(be, nativeToBig(be)); #endif - } +} - TEST( EndianTest, NativeToBig_double ) { - uint8_t le_buf[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; - uint8_t be_buf[] = { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01 }; - double le; - double be; - std::memcpy(&le, le_buf, sizeof(le)); - std::memcpy(&be, be_buf, sizeof(be)); +TEST(EndianTest, NativeToBig_double) { + uint8_t le_buf[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; + uint8_t be_buf[] = {0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01}; + double le; + double be; + std::memcpy(&le, le_buf, sizeof(le)); + std::memcpy(&be, be_buf, sizeof(be)); #if MONGO_CONFIG_BYTE_ORDER == 1234 - ASSERT_EQUALS(be, nativeToBig(le)); + ASSERT_EQUALS(be, nativeToBig(le)); #else - ASSERT_EQUALS(be, nativeToBig(be)); + ASSERT_EQUALS(be, nativeToBig(be)); #endif - } +} - TEST( EndianTest, NativeToLittle_uint16_t ) { - uint8_t le_buf[] = { 0x01, 0x02 }; - uint8_t be_buf[] = { 0x02, 0x01 }; - uint16_t le; - uint16_t be; - std::memcpy(&le, le_buf, sizeof(le)); - std::memcpy(&be, be_buf, sizeof(be)); +TEST(EndianTest, NativeToLittle_uint16_t) { + uint8_t le_buf[] = {0x01, 0x02}; + uint8_t be_buf[] = {0x02, 0x01}; + uint16_t le; + uint16_t be; + std::memcpy(&le, le_buf, sizeof(le)); + std::memcpy(&be, be_buf, sizeof(be)); #if MONGO_CONFIG_BYTE_ORDER == 1234 - ASSERT_EQUALS(le, nativeToLittle(le)); + ASSERT_EQUALS(le, nativeToLittle(le)); #else - ASSERT_EQUALS(le, nativeToLittle(be)); + ASSERT_EQUALS(le, nativeToLittle(be)); #endif - } +} - TEST( EndianTest, NativeToLittle_uint32_t ) { - uint8_t le_buf[] = { 0x01, 0x02, 0x03, 0x04 }; - uint8_t be_buf[] = { 0x04, 0x03, 0x02, 0x01 }; - uint32_t le; - uint32_t be; - std::memcpy(&le, le_buf, sizeof(le)); - std::memcpy(&be, be_buf, sizeof(be)); +TEST(EndianTest, NativeToLittle_uint32_t) { + uint8_t le_buf[] = {0x01, 0x02, 0x03, 0x04}; + uint8_t be_buf[] = {0x04, 0x03, 0x02, 0x01}; + uint32_t le; + uint32_t be; + std::memcpy(&le, le_buf, sizeof(le)); + std::memcpy(&be, be_buf, sizeof(be)); #if MONGO_CONFIG_BYTE_ORDER == 1234 - ASSERT_EQUALS(le, nativeToLittle(le)); + ASSERT_EQUALS(le, nativeToLittle(le)); #else - ASSERT_EQUALS(le, nativeToLittle(be)); + ASSERT_EQUALS(le, nativeToLittle(be)); #endif - } +} - TEST( EndianTest, NativeToLittle_uint64_t ) { - uint8_t le_buf[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; - uint8_t be_buf[] = { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01 }; - uint64_t le; - uint64_t be; - std::memcpy(&le, le_buf, sizeof(le)); - std::memcpy(&be, be_buf, sizeof(be)); +TEST(EndianTest, NativeToLittle_uint64_t) { + uint8_t le_buf[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; + uint8_t be_buf[] = {0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01}; + uint64_t le; + uint64_t be; + std::memcpy(&le, le_buf, sizeof(le)); + std::memcpy(&be, be_buf, sizeof(be)); #if MONGO_CONFIG_BYTE_ORDER == 1234 - ASSERT_EQUALS(le, nativeToLittle(le)); + ASSERT_EQUALS(le, nativeToLittle(le)); #else - ASSERT_EQUALS(le, nativeToLittle(be)); + ASSERT_EQUALS(le, nativeToLittle(be)); #endif - } +} - TEST( EndianTest, NativeToLittle_int16_t ) { - uint8_t le_buf[] = { 0x01, 0x02 }; - uint8_t be_buf[] = { 0x02, 0x01 }; - int16_t le; - int16_t be; - std::memcpy(&le, le_buf, sizeof(le)); - std::memcpy(&be, be_buf, sizeof(be)); +TEST(EndianTest, NativeToLittle_int16_t) { + uint8_t le_buf[] = {0x01, 0x02}; + uint8_t be_buf[] = {0x02, 0x01}; + int16_t le; + int16_t be; + std::memcpy(&le, le_buf, sizeof(le)); + std::memcpy(&be, be_buf, sizeof(be)); #if MONGO_CONFIG_BYTE_ORDER == 1234 - ASSERT_EQUALS(le, nativeToLittle(le)); + ASSERT_EQUALS(le, nativeToLittle(le)); #else - ASSERT_EQUALS(le, nativeToLittle(be)); + ASSERT_EQUALS(le, nativeToLittle(be)); #endif - } +} - TEST( EndianTest, NativeToLittle_int32_t ) { - uint8_t le_buf[] = { 0x01, 0x02, 0x03, 0x04 }; - uint8_t be_buf[] = { 0x04, 0x03, 0x02, 0x01 }; - int32_t le; - int32_t be; - std::memcpy(&le, le_buf, sizeof(le)); - std::memcpy(&be, be_buf, sizeof(be)); +TEST(EndianTest, NativeToLittle_int32_t) { + uint8_t le_buf[] = {0x01, 0x02, 0x03, 0x04}; + uint8_t be_buf[] = {0x04, 0x03, 0x02, 0x01}; + int32_t le; + int32_t be; + std::memcpy(&le, le_buf, sizeof(le)); + std::memcpy(&be, be_buf, sizeof(be)); #if MONGO_CONFIG_BYTE_ORDER == 1234 - ASSERT_EQUALS(le, nativeToLittle(le)); + ASSERT_EQUALS(le, nativeToLittle(le)); #else - ASSERT_EQUALS(le, nativeToLittle(be)); + ASSERT_EQUALS(le, nativeToLittle(be)); #endif - } +} - TEST( EndianTest, NativeToLittle_int64_t ) { - uint8_t le_buf[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; - uint8_t be_buf[] = { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01 }; - int64_t le; - int64_t be; - std::memcpy(&le, le_buf, sizeof(le)); - std::memcpy(&be, be_buf, sizeof(be)); +TEST(EndianTest, NativeToLittle_int64_t) { + uint8_t le_buf[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; + uint8_t be_buf[] = {0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01}; + int64_t le; + int64_t be; + std::memcpy(&le, le_buf, sizeof(le)); + std::memcpy(&be, be_buf, sizeof(be)); #if MONGO_CONFIG_BYTE_ORDER == 1234 - ASSERT_EQUALS(le, nativeToLittle(le)); + ASSERT_EQUALS(le, nativeToLittle(le)); #else - ASSERT_EQUALS(le, nativeToLittle(be)); + ASSERT_EQUALS(le, nativeToLittle(be)); #endif - } +} - TEST( EndianTest, NativeToLittle_float ) { - uint8_t le_buf[] = { 0x01, 0x02, 0x03, 0x04 }; - uint8_t be_buf[] = { 0x04, 0x03, 0x02, 0x01 }; - float le; - float be; - std::memcpy(&le, le_buf, sizeof(le)); - std::memcpy(&be, be_buf, sizeof(be)); +TEST(EndianTest, NativeToLittle_float) { + uint8_t le_buf[] = {0x01, 0x02, 0x03, 0x04}; + uint8_t be_buf[] = {0x04, 0x03, 0x02, 0x01}; + float le; + float be; + std::memcpy(&le, le_buf, sizeof(le)); + std::memcpy(&be, be_buf, sizeof(be)); #if MONGO_CONFIG_BYTE_ORDER == 1234 - ASSERT_EQUALS(le, nativeToLittle(le)); + ASSERT_EQUALS(le, nativeToLittle(le)); #else - ASSERT_EQUALS(le, nativeToLittle(be)); + ASSERT_EQUALS(le, nativeToLittle(be)); #endif - } +} - TEST( EndianTest, NativeToLittle_double ) { - uint8_t le_buf[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; - uint8_t be_buf[] = { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01 }; - double le; - double be; - std::memcpy(&le, le_buf, sizeof(le)); - std::memcpy(&be, be_buf, sizeof(be)); +TEST(EndianTest, NativeToLittle_double) { + uint8_t le_buf[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; + uint8_t be_buf[] = {0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01}; + double le; + double be; + std::memcpy(&le, le_buf, sizeof(le)); + std::memcpy(&be, be_buf, sizeof(be)); #if MONGO_CONFIG_BYTE_ORDER == 1234 - ASSERT_EQUALS(le, nativeToLittle(le)); + ASSERT_EQUALS(le, nativeToLittle(le)); #else - ASSERT_EQUALS(le, nativeToLittle(be)); + ASSERT_EQUALS(le, nativeToLittle(be)); #endif - } +} - TEST( EndianTest, LittleToNative_uint16_t ) { - uint8_t le_buf[] = { 0x01, 0x02 }; - uint8_t be_buf[] = { 0x02, 0x01 }; - uint16_t le; - uint16_t be; - std::memcpy(&le, le_buf, sizeof(le)); - std::memcpy(&be, be_buf, sizeof(be)); +TEST(EndianTest, LittleToNative_uint16_t) { + uint8_t le_buf[] = {0x01, 0x02}; + uint8_t be_buf[] = {0x02, 0x01}; + uint16_t le; + uint16_t be; + std::memcpy(&le, le_buf, sizeof(le)); + std::memcpy(&be, be_buf, sizeof(be)); #if MONGO_CONFIG_BYTE_ORDER == 1234 - ASSERT_EQUALS(le, littleToNative(le)); + ASSERT_EQUALS(le, littleToNative(le)); #else - ASSERT_EQUALS(be, littleToNative(le)); + ASSERT_EQUALS(be, littleToNative(le)); #endif - } +} - TEST( EndianTest, LittleToNative_uint32_t ) { - uint8_t le_buf[] = { 0x01, 0x02, 0x03, 0x04 }; - uint8_t be_buf[] = { 0x04, 0x03, 0x02, 0x01 }; - uint32_t le; - uint32_t be; - std::memcpy(&le, le_buf, sizeof(le)); - std::memcpy(&be, be_buf, sizeof(be)); +TEST(EndianTest, LittleToNative_uint32_t) { + uint8_t le_buf[] = {0x01, 0x02, 0x03, 0x04}; + uint8_t be_buf[] = {0x04, 0x03, 0x02, 0x01}; + uint32_t le; + uint32_t be; + std::memcpy(&le, le_buf, sizeof(le)); + std::memcpy(&be, be_buf, sizeof(be)); #if MONGO_CONFIG_BYTE_ORDER == 1234 - ASSERT_EQUALS(le, littleToNative(le)); + ASSERT_EQUALS(le, littleToNative(le)); #else - ASSERT_EQUALS(be, littleToNative(le)); + ASSERT_EQUALS(be, littleToNative(le)); #endif - } +} - TEST( EndianTest, LittleToNative_uint64_t ) { - uint8_t le_buf[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; - uint8_t be_buf[] = { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01 }; - uint64_t le; - uint64_t be; - std::memcpy(&le, le_buf, sizeof(le)); - std::memcpy(&be, be_buf, sizeof(be)); +TEST(EndianTest, LittleToNative_uint64_t) { + uint8_t le_buf[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; + uint8_t be_buf[] = {0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01}; + uint64_t le; + uint64_t be; + std::memcpy(&le, le_buf, sizeof(le)); + std::memcpy(&be, be_buf, sizeof(be)); #if MONGO_CONFIG_BYTE_ORDER == 1234 - ASSERT_EQUALS(le, littleToNative(le)); + ASSERT_EQUALS(le, littleToNative(le)); #else - ASSERT_EQUALS(be, littleToNative(le)); + ASSERT_EQUALS(be, littleToNative(le)); #endif - } +} - TEST( EndianTest, LittleToNative_int16_t ) { - uint8_t le_buf[] = { 0x01, 0x02 }; - uint8_t be_buf[] = { 0x02, 0x01 }; - int16_t le; - int16_t be; - std::memcpy(&le, le_buf, sizeof(le)); - std::memcpy(&be, be_buf, sizeof(be)); +TEST(EndianTest, LittleToNative_int16_t) { + uint8_t le_buf[] = {0x01, 0x02}; + uint8_t be_buf[] = {0x02, 0x01}; + int16_t le; + int16_t be; + std::memcpy(&le, le_buf, sizeof(le)); + std::memcpy(&be, be_buf, sizeof(be)); #if MONGO_CONFIG_BYTE_ORDER == 1234 - ASSERT_EQUALS(le, littleToNative(le)); + ASSERT_EQUALS(le, littleToNative(le)); #else - ASSERT_EQUALS(be, littleToNative(le)); + ASSERT_EQUALS(be, littleToNative(le)); #endif - } +} - TEST( EndianTest, LittleToNative_int32_t ) { - uint8_t le_buf[] = { 0x01, 0x02, 0x03, 0x04 }; - uint8_t be_buf[] = { 0x04, 0x03, 0x02, 0x01 }; - int32_t le; - int32_t be; - std::memcpy(&le, le_buf, sizeof(le)); - std::memcpy(&be, be_buf, sizeof(be)); +TEST(EndianTest, LittleToNative_int32_t) { + uint8_t le_buf[] = {0x01, 0x02, 0x03, 0x04}; + uint8_t be_buf[] = {0x04, 0x03, 0x02, 0x01}; + int32_t le; + int32_t be; + std::memcpy(&le, le_buf, sizeof(le)); + std::memcpy(&be, be_buf, sizeof(be)); #if MONGO_CONFIG_BYTE_ORDER == 1234 - ASSERT_EQUALS(le, littleToNative(le)); + ASSERT_EQUALS(le, littleToNative(le)); #else - ASSERT_EQUALS(be, littleToNative(le)); + ASSERT_EQUALS(be, littleToNative(le)); #endif - } +} - TEST( EndianTest, LittleToNative_int64_t ) { - uint8_t le_buf[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; - uint8_t be_buf[] = { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01 }; - int64_t le; - int64_t be; - std::memcpy(&le, le_buf, sizeof(le)); - std::memcpy(&be, be_buf, sizeof(be)); +TEST(EndianTest, LittleToNative_int64_t) { + uint8_t le_buf[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; + uint8_t be_buf[] = {0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01}; + int64_t le; + int64_t be; + std::memcpy(&le, le_buf, sizeof(le)); + std::memcpy(&be, be_buf, sizeof(be)); #if MONGO_CONFIG_BYTE_ORDER == 1234 - ASSERT_EQUALS(le, littleToNative(le)); + ASSERT_EQUALS(le, littleToNative(le)); #else - ASSERT_EQUALS(be, littleToNative(le)); + ASSERT_EQUALS(be, littleToNative(le)); #endif - } +} - TEST( EndianTest, LittleToNative_float ) { - uint8_t le_buf[] = { 0x01, 0x02, 0x03, 0x04 }; - uint8_t be_buf[] = { 0x04, 0x03, 0x02, 0x01 }; - float le; - float be; - std::memcpy(&le, le_buf, sizeof(le)); - std::memcpy(&be, be_buf, sizeof(be)); +TEST(EndianTest, LittleToNative_float) { + uint8_t le_buf[] = {0x01, 0x02, 0x03, 0x04}; + uint8_t be_buf[] = {0x04, 0x03, 0x02, 0x01}; + float le; + float be; + std::memcpy(&le, le_buf, sizeof(le)); + std::memcpy(&be, be_buf, sizeof(be)); #if MONGO_CONFIG_BYTE_ORDER == 1234 - ASSERT_EQUALS(le, littleToNative(le)); + ASSERT_EQUALS(le, littleToNative(le)); #else - ASSERT_EQUALS(be, littleToNative(le)); + ASSERT_EQUALS(be, littleToNative(le)); #endif - } +} - TEST( EndianTest, LittleToNative_double ) { - uint8_t le_buf[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; - uint8_t be_buf[] = { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01 }; - double le; - double be; - std::memcpy(&le, le_buf, sizeof(le)); - std::memcpy(&be, be_buf, sizeof(be)); +TEST(EndianTest, LittleToNative_double) { + uint8_t le_buf[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; + uint8_t be_buf[] = {0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01}; + double le; + double be; + std::memcpy(&le, le_buf, sizeof(le)); + std::memcpy(&be, be_buf, sizeof(be)); #if MONGO_CONFIG_BYTE_ORDER == 1234 - ASSERT_EQUALS(le, littleToNative(le)); + ASSERT_EQUALS(le, littleToNative(le)); #else - ASSERT_EQUALS(be, littleToNative(le)); + ASSERT_EQUALS(be, littleToNative(le)); #endif - } +} - TEST( EndianTest, BigToNative_uint16_t ) { - uint8_t le_buf[] = { 0x01, 0x02 }; - uint8_t be_buf[] = { 0x02, 0x01 }; - uint16_t le; - uint16_t be; - std::memcpy(&le, le_buf, sizeof(le)); - std::memcpy(&be, be_buf, sizeof(be)); +TEST(EndianTest, BigToNative_uint16_t) { + uint8_t le_buf[] = {0x01, 0x02}; + uint8_t be_buf[] = {0x02, 0x01}; + uint16_t le; + uint16_t be; + std::memcpy(&le, le_buf, sizeof(le)); + std::memcpy(&be, be_buf, sizeof(be)); #if MONGO_CONFIG_BYTE_ORDER == 1234 - ASSERT_EQUALS(le, bigToNative(be)); + ASSERT_EQUALS(le, bigToNative(be)); #else - ASSERT_EQUALS(be, bigToNative(be)); + ASSERT_EQUALS(be, bigToNative(be)); #endif - } +} - TEST( EndianTest, BigToNative_uint32_t ) { - uint8_t le_buf[] = { 0x01, 0x02, 0x03, 0x04 }; - uint8_t be_buf[] = { 0x04, 0x03, 0x02, 0x01 }; - uint32_t le; - uint32_t be; - std::memcpy(&le, le_buf, sizeof(le)); - std::memcpy(&be, be_buf, sizeof(be)); +TEST(EndianTest, BigToNative_uint32_t) { + uint8_t le_buf[] = {0x01, 0x02, 0x03, 0x04}; + uint8_t be_buf[] = {0x04, 0x03, 0x02, 0x01}; + uint32_t le; + uint32_t be; + std::memcpy(&le, le_buf, sizeof(le)); + std::memcpy(&be, be_buf, sizeof(be)); #if MONGO_CONFIG_BYTE_ORDER == 1234 - ASSERT_EQUALS(le, bigToNative(be)); + ASSERT_EQUALS(le, bigToNative(be)); #else - ASSERT_EQUALS(be, bigToNative(be)); + ASSERT_EQUALS(be, bigToNative(be)); #endif - } +} - TEST( EndianTest, BigToNative_uint64_t ) { - uint8_t le_buf[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; - uint8_t be_buf[] = { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01 }; - uint64_t le; - uint64_t be; - std::memcpy(&le, le_buf, sizeof(le)); - std::memcpy(&be, be_buf, sizeof(be)); +TEST(EndianTest, BigToNative_uint64_t) { + uint8_t le_buf[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; + uint8_t be_buf[] = {0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01}; + uint64_t le; + uint64_t be; + std::memcpy(&le, le_buf, sizeof(le)); + std::memcpy(&be, be_buf, sizeof(be)); #if MONGO_CONFIG_BYTE_ORDER == 1234 - ASSERT_EQUALS(le, bigToNative(be)); + ASSERT_EQUALS(le, bigToNative(be)); #else - ASSERT_EQUALS(be, bigToNative(be)); + ASSERT_EQUALS(be, bigToNative(be)); #endif - } +} - TEST( EndianTest, BigToNative_int16_t ) { - uint8_t le_buf[] = { 0x01, 0x02 }; - uint8_t be_buf[] = { 0x02, 0x01 }; - int16_t le; - int16_t be; - std::memcpy(&le, le_buf, sizeof(le)); - std::memcpy(&be, be_buf, sizeof(be)); +TEST(EndianTest, BigToNative_int16_t) { + uint8_t le_buf[] = {0x01, 0x02}; + uint8_t be_buf[] = {0x02, 0x01}; + int16_t le; + int16_t be; + std::memcpy(&le, le_buf, sizeof(le)); + std::memcpy(&be, be_buf, sizeof(be)); #if MONGO_CONFIG_BYTE_ORDER == 1234 - ASSERT_EQUALS(le, bigToNative(be)); + ASSERT_EQUALS(le, bigToNative(be)); #else - ASSERT_EQUALS(be, bigToNative(be)); + ASSERT_EQUALS(be, bigToNative(be)); #endif - } +} - TEST( EndianTest, BigToNative_int32_t ) { - uint8_t le_buf[] = { 0x01, 0x02, 0x03, 0x04 }; - uint8_t be_buf[] = { 0x04, 0x03, 0x02, 0x01 }; - int32_t le; - int32_t be; - std::memcpy(&le, le_buf, sizeof(le)); - std::memcpy(&be, be_buf, sizeof(be)); +TEST(EndianTest, BigToNative_int32_t) { + uint8_t le_buf[] = {0x01, 0x02, 0x03, 0x04}; + uint8_t be_buf[] = {0x04, 0x03, 0x02, 0x01}; + int32_t le; + int32_t be; + std::memcpy(&le, le_buf, sizeof(le)); + std::memcpy(&be, be_buf, sizeof(be)); #if MONGO_CONFIG_BYTE_ORDER == 1234 - ASSERT_EQUALS(le, bigToNative(be)); + ASSERT_EQUALS(le, bigToNative(be)); #else - ASSERT_EQUALS(be, bigToNative(be)); + ASSERT_EQUALS(be, bigToNative(be)); #endif - } +} - TEST( EndianTest, BigToNative_int64_t ) { - uint8_t le_buf[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; - uint8_t be_buf[] = { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01 }; - int64_t le; - int64_t be; - std::memcpy(&le, le_buf, sizeof(le)); - std::memcpy(&be, be_buf, sizeof(be)); +TEST(EndianTest, BigToNative_int64_t) { + uint8_t le_buf[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; + uint8_t be_buf[] = {0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01}; + int64_t le; + int64_t be; + std::memcpy(&le, le_buf, sizeof(le)); + std::memcpy(&be, be_buf, sizeof(be)); #if MONGO_CONFIG_BYTE_ORDER == 1234 - ASSERT_EQUALS(le, bigToNative(be)); + ASSERT_EQUALS(le, bigToNative(be)); #else - ASSERT_EQUALS(be, bigToNative(be)); + ASSERT_EQUALS(be, bigToNative(be)); #endif - } +} - TEST( EndianTest, BigToNative_float ) { - uint8_t le_buf[] = { 0x01, 0x02, 0x03, 0x04 }; - uint8_t be_buf[] = { 0x04, 0x03, 0x02, 0x01 }; - float le; - float be; - std::memcpy(&le, le_buf, sizeof(le)); - std::memcpy(&be, be_buf, sizeof(be)); +TEST(EndianTest, BigToNative_float) { + uint8_t le_buf[] = {0x01, 0x02, 0x03, 0x04}; + uint8_t be_buf[] = {0x04, 0x03, 0x02, 0x01}; + float le; + float be; + std::memcpy(&le, le_buf, sizeof(le)); + std::memcpy(&be, be_buf, sizeof(be)); #if MONGO_CONFIG_BYTE_ORDER == 1234 - ASSERT_EQUALS(le, bigToNative(be)); + ASSERT_EQUALS(le, bigToNative(be)); #else - ASSERT_EQUALS(be, bigToNative(be)); + ASSERT_EQUALS(be, bigToNative(be)); #endif - } +} - TEST( EndianTest, BigToNative_double ) { - uint8_t le_buf[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; - uint8_t be_buf[] = { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01 }; - double le; - double be; - std::memcpy(&le, le_buf, sizeof(le)); - std::memcpy(&be, be_buf, sizeof(be)); +TEST(EndianTest, BigToNative_double) { + uint8_t le_buf[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; + uint8_t be_buf[] = {0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01}; + double le; + double be; + std::memcpy(&le, le_buf, sizeof(le)); + std::memcpy(&be, be_buf, sizeof(be)); #if MONGO_CONFIG_BYTE_ORDER == 1234 - ASSERT_EQUALS(le, bigToNative(be)); + ASSERT_EQUALS(le, bigToNative(be)); #else - ASSERT_EQUALS(be, bigToNative(be)); + ASSERT_EQUALS(be, bigToNative(be)); #endif - } +} -} // namespace mongo +} // namespace mongo diff --git a/src/mongo/platform/posix_fadvise.cpp b/src/mongo/platform/posix_fadvise.cpp index b7787c55b0a..eecf49e5351 100644 --- a/src/mongo/platform/posix_fadvise.cpp +++ b/src/mongo/platform/posix_fadvise.cpp @@ -37,32 +37,32 @@ namespace mongo { namespace pal { - int posix_fadvise_emulation(int fd, off_t offset, off_t len, int advice) { - return 0; - } +int posix_fadvise_emulation(int fd, off_t offset, off_t len, int advice) { + return 0; +} - typedef int (*PosixFadviseFunc)(int fd, off_t offset, off_t len, int advice); - static PosixFadviseFunc posix_fadvise_switcher = mongo::pal::posix_fadvise_emulation; +typedef int (*PosixFadviseFunc)(int fd, off_t offset, off_t len, int advice); +static PosixFadviseFunc posix_fadvise_switcher = mongo::pal::posix_fadvise_emulation; - int posix_fadvise(int fd, off_t offset, off_t len, int advice) { - return posix_fadvise_switcher(fd, offset, len, advice); - } +int posix_fadvise(int fd, off_t offset, off_t len, int advice) { + return posix_fadvise_switcher(fd, offset, len, advice); +} -} // namespace pal +} // namespace pal - // 'posix_fadvise()' on Solaris will call the emulation if the symbol is not found - // - MONGO_INITIALIZER_GENERAL(SolarisPosixFadvise, - MONGO_NO_PREREQUISITES, - ("default"))(InitializerContext* context) { - void* functionAddress = dlsym(RTLD_DEFAULT, "posix_fadvise"); - if (functionAddress != NULL) { - mongo::pal::posix_fadvise_switcher = - reinterpret_cast<mongo::pal::PosixFadviseFunc>(functionAddress); - } - return Status::OK(); +// 'posix_fadvise()' on Solaris will call the emulation if the symbol is not found +// +MONGO_INITIALIZER_GENERAL(SolarisPosixFadvise, + MONGO_NO_PREREQUISITES, + ("default"))(InitializerContext* context) { + void* functionAddress = dlsym(RTLD_DEFAULT, "posix_fadvise"); + if (functionAddress != NULL) { + mongo::pal::posix_fadvise_switcher = + reinterpret_cast<mongo::pal::PosixFadviseFunc>(functionAddress); } + return Status::OK(); +} -} // namespace mongo +} // namespace mongo -#endif // #if defined(__sun) +#endif // #if defined(__sun) diff --git a/src/mongo/platform/posix_fadvise.h b/src/mongo/platform/posix_fadvise.h index dc0a539c6c9..b38a0807663 100644 --- a/src/mongo/platform/posix_fadvise.h +++ b/src/mongo/platform/posix_fadvise.h @@ -36,17 +36,17 @@ #include <sys/types.h> namespace mongo { - namespace pal { - int posix_fadvise(int fd, off_t offset, off_t len, int advice); - } // namespace pal - using pal::posix_fadvise; -} // namespace mongo +namespace pal { +int posix_fadvise(int fd, off_t offset, off_t len, int advice); +} // namespace pal +using pal::posix_fadvise; +} // namespace mongo #elif defined(POSIX_FADV_DONTNEED) namespace mongo { - using ::posix_fadvise; -} // namespace mongo +using ::posix_fadvise; +} // namespace mongo #endif diff --git a/src/mongo/platform/process_id.cpp b/src/mongo/platform/process_id.cpp index e2bcc09bd1c..20576c7b93c 100644 --- a/src/mongo/platform/process_id.cpp +++ b/src/mongo/platform/process_id.cpp @@ -36,40 +36,44 @@ namespace mongo { - BOOST_STATIC_ASSERT(sizeof(NativeProcessId) == sizeof(uint32_t)); +BOOST_STATIC_ASSERT(sizeof(NativeProcessId) == sizeof(uint32_t)); - namespace { +namespace { #ifdef _WIN32 - inline NativeProcessId getCurrentNativeProcessId() { return GetCurrentProcessId(); } +inline NativeProcessId getCurrentNativeProcessId() { + return GetCurrentProcessId(); +} #else - inline NativeProcessId getCurrentNativeProcessId() { return getpid(); } +inline NativeProcessId getCurrentNativeProcessId() { + return getpid(); +} #endif - } // namespace +} // namespace - ProcessId ProcessId::getCurrent() { - return fromNative(getCurrentNativeProcessId()); - } +ProcessId ProcessId::getCurrent() { + return fromNative(getCurrentNativeProcessId()); +} - int64_t ProcessId::asInt64() const { - typedef std::numeric_limits<NativeProcessId> limits; - if (limits::is_signed) - return _npid; - else - return static_cast<int64_t>(static_cast<uint64_t>(_npid)); - } +int64_t ProcessId::asInt64() const { + typedef std::numeric_limits<NativeProcessId> limits; + if (limits::is_signed) + return _npid; + else + return static_cast<int64_t>(static_cast<uint64_t>(_npid)); +} - long long ProcessId::asLongLong() const { - return static_cast<long long>(asInt64()); - } +long long ProcessId::asLongLong() const { + return static_cast<long long>(asInt64()); +} - std::string ProcessId::toString() const { - std::ostringstream os; - os << *this; - return os.str(); - } +std::string ProcessId::toString() const { + std::ostringstream os; + os << *this; + return os.str(); +} - std::ostream& operator<<(std::ostream& os, ProcessId pid) { - return os << pid.toNative(); - } +std::ostream& operator<<(std::ostream& os, ProcessId pid) { + return os << pid.toNative(); +} } // namespace mongo diff --git a/src/mongo/platform/process_id.h b/src/mongo/platform/process_id.h index ce63e7deb5f..d566e985be1 100644 --- a/src/mongo/platform/process_id.h +++ b/src/mongo/platform/process_id.h @@ -40,85 +40,104 @@ namespace mongo { #ifdef _WIN32 - typedef DWORD NativeProcessId; +typedef DWORD NativeProcessId; #else - typedef pid_t NativeProcessId; +typedef pid_t NativeProcessId; #endif +/** + * Platform-independent representation of a process identifier. + */ +class ProcessId { +public: + /** + * Gets the process id for the currently executing process. + */ + static ProcessId getCurrent(); + + /** + * Constructs a ProcessId from a NativeProcessId. + */ + static inline ProcessId fromNative(NativeProcessId npid) { + return ProcessId(npid); + } + + /** + * Constructs a ProcessId with an unspecified value. + */ + ProcessId() {} + + /** + * Gets the native process id corresponding to this ProcessId. + */ + NativeProcessId toNative() const { + return _npid; + } + + /** + * Represents this process id as a signed 64-bit integer. + * + * This representation will faithfully serialize and format to text files, but is at least + * twice the number of bits needed to uniquely represent valid process numbers on supported + * OSes.. + */ + int64_t asInt64() const; + /** - * Platform-independent representation of a process identifier. + * Similar to asInt64(), for compatibility with code that uses "long long" to mean 64-bit + * signed integer. */ - class ProcessId { - public: - /** - * Gets the process id for the currently executing process. - */ - static ProcessId getCurrent(); - - /** - * Constructs a ProcessId from a NativeProcessId. - */ - static inline ProcessId fromNative(NativeProcessId npid) { return ProcessId(npid); } - - /** - * Constructs a ProcessId with an unspecified value. - */ - ProcessId() {} - - /** - * Gets the native process id corresponding to this ProcessId. - */ - NativeProcessId toNative() const { return _npid; } - - /** - * Represents this process id as a signed 64-bit integer. - * - * This representation will faithfully serialize and format to text files, but is at least - * twice the number of bits needed to uniquely represent valid process numbers on supported - * OSes.. - */ - int64_t asInt64() const; - - /** - * Similar to asInt64(), for compatibility with code that uses "long long" to mean 64-bit - * signed integer. - */ - long long asLongLong() const; - - /** - * Represents this process id as an unsigned 32-bit integer. - * - * This representation will contain all of the bits of the native process id, but may not - * serialize or format to text files meaningfully. - */ - uint32_t asUInt32() const { return static_cast<uint32_t>(_npid); } - - /** - * Provides a std::string representation of the pid. - */ - std::string toString() const; - - bool operator==(const ProcessId other) const { return _npid == other._npid; } - bool operator!=(const ProcessId other) const { return _npid != other._npid; } - bool operator<(const ProcessId other) const { return _npid < other._npid; } - bool operator<=(const ProcessId other) const { return _npid <= other._npid; } - bool operator>(const ProcessId other) const { return _npid > other._npid; } - bool operator>=(const ProcessId other) const { return _npid >= other._npid; } - - private: - explicit ProcessId(NativeProcessId npid): _npid(npid) {} - - NativeProcessId _npid; - }; - - std::ostream& operator<<(std::ostream& os, ProcessId pid); + long long asLongLong() const; + + /** + * Represents this process id as an unsigned 32-bit integer. + * + * This representation will contain all of the bits of the native process id, but may not + * serialize or format to text files meaningfully. + */ + uint32_t asUInt32() const { + return static_cast<uint32_t>(_npid); + } + + /** + * Provides a std::string representation of the pid. + */ + std::string toString() const; + + bool operator==(const ProcessId other) const { + return _npid == other._npid; + } + bool operator!=(const ProcessId other) const { + return _npid != other._npid; + } + bool operator<(const ProcessId other) const { + return _npid < other._npid; + } + bool operator<=(const ProcessId other) const { + return _npid <= other._npid; + } + bool operator>(const ProcessId other) const { + return _npid > other._npid; + } + bool operator>=(const ProcessId other) const { + return _npid >= other._npid; + } + +private: + explicit ProcessId(NativeProcessId npid) : _npid(npid) {} + + NativeProcessId _npid; +}; + +std::ostream& operator<<(std::ostream& os, ProcessId pid); } // namespace mongo MONGO_HASH_NAMESPACE_START -template<> struct hash< ::mongo::ProcessId > { +template <> +struct hash<::mongo::ProcessId> { size_t operator()(const ::mongo::ProcessId pid) const { - return hash< ::std::uint32_t >()(pid.asUInt32()); + return hash<::std::uint32_t>()(pid.asUInt32()); } }; MONGO_HASH_NAMESPACE_END diff --git a/src/mongo/platform/process_id_test.cpp b/src/mongo/platform/process_id_test.cpp index 7ac01c3d7a2..7a9c4628bff 100644 --- a/src/mongo/platform/process_id_test.cpp +++ b/src/mongo/platform/process_id_test.cpp @@ -33,36 +33,36 @@ namespace mongo { namespace { - TEST(ProcessId, Comparison) { - const ProcessId p1 = ProcessId::fromNative(NativeProcessId(1)); - const ProcessId p2 = ProcessId::fromNative(NativeProcessId(2)); +TEST(ProcessId, Comparison) { + const ProcessId p1 = ProcessId::fromNative(NativeProcessId(1)); + const ProcessId p2 = ProcessId::fromNative(NativeProcessId(2)); - ASSERT_FALSE(p1 == p2); - ASSERT_TRUE(p1 == p1); + ASSERT_FALSE(p1 == p2); + ASSERT_TRUE(p1 == p1); - ASSERT_TRUE(p1 != p2); - ASSERT_FALSE(p1 != p1); + ASSERT_TRUE(p1 != p2); + ASSERT_FALSE(p1 != p1); - ASSERT_TRUE(p1 < p2); - ASSERT_FALSE(p1 < p1); - ASSERT_FALSE(p2 < p1); + ASSERT_TRUE(p1 < p2); + ASSERT_FALSE(p1 < p1); + ASSERT_FALSE(p2 < p1); - ASSERT_TRUE(p1 <= p2); - ASSERT_TRUE(p1 <= p1); - ASSERT_FALSE(p2 <= p1); + ASSERT_TRUE(p1 <= p2); + ASSERT_TRUE(p1 <= p1); + ASSERT_FALSE(p2 <= p1); - ASSERT_TRUE(p2 > p1); - ASSERT_FALSE(p2 > p2); - ASSERT_FALSE(p1 > p2); + ASSERT_TRUE(p2 > p1); + ASSERT_FALSE(p2 > p2); + ASSERT_FALSE(p1 > p2); - ASSERT_TRUE(p2 >= p1); - ASSERT_TRUE(p2 >= p2); - ASSERT_FALSE(p1 >= p2); - } + ASSERT_TRUE(p2 >= p1); + ASSERT_TRUE(p2 >= p2); + ASSERT_FALSE(p1 >= p2); +} - TEST(ProcessId, GetCurrentEqualsSelf) { - ASSERT_EQUALS(ProcessId::getCurrent(), ProcessId::getCurrent()); - } +TEST(ProcessId, GetCurrentEqualsSelf) { + ASSERT_EQUALS(ProcessId::getCurrent(), ProcessId::getCurrent()); +} } // namespace } // namespace mongo diff --git a/src/mongo/platform/random.cpp b/src/mongo/platform/random.cpp index dcbd6f66ffc..b7fd984c411 100644 --- a/src/mongo/platform/random.cpp +++ b/src/mongo/platform/random.cpp @@ -45,130 +45,128 @@ namespace mongo { - // ---- PseudoRandom ----- - - int32_t PseudoRandom::nextInt32() { - int32_t t = _x ^ (_x << 11); - _x = _y; - _y = _z; - _z = _w; - return _w = _w ^ (_w >> 19) ^ (t ^ (t >> 8)); - } +// ---- PseudoRandom ----- + +int32_t PseudoRandom::nextInt32() { + int32_t t = _x ^ (_x << 11); + _x = _y; + _y = _z; + _z = _w; + return _w = _w ^ (_w >> 19) ^ (t ^ (t >> 8)); +} - namespace { - const int32_t default_y = 362436069; - const int32_t default_z = 521288629; - const int32_t default_w = 88675123; - } +namespace { +const int32_t default_y = 362436069; +const int32_t default_z = 521288629; +const int32_t default_w = 88675123; +} - PseudoRandom::PseudoRandom( int32_t seed ) { - _x = seed; - _y = default_y; - _z = default_z; - _w = default_w; - } +PseudoRandom::PseudoRandom(int32_t seed) { + _x = seed; + _y = default_y; + _z = default_z; + _w = default_w; +} - PseudoRandom::PseudoRandom( uint32_t seed ) { - _x = static_cast<int32_t>(seed); - _y = default_y; - _z = default_z; - _w = default_w; - } +PseudoRandom::PseudoRandom(uint32_t seed) { + _x = static_cast<int32_t>(seed); + _y = default_y; + _z = default_z; + _w = default_w; +} - PseudoRandom::PseudoRandom( int64_t seed ) { - int32_t high = seed >> 32; - int32_t low = seed & 0xFFFFFFFF; +PseudoRandom::PseudoRandom(int64_t seed) { + int32_t high = seed >> 32; + int32_t low = seed & 0xFFFFFFFF; - _x = high ^ low; - _y = default_y; - _z = default_z; - _w = default_w; - } + _x = high ^ low; + _y = default_y; + _z = default_z; + _w = default_w; +} - int64_t PseudoRandom::nextInt64() { - int64_t a = nextInt32(); - int64_t b = nextInt32(); - return ( a << 32 ) | b; - } +int64_t PseudoRandom::nextInt64() { + int64_t a = nextInt32(); + int64_t b = nextInt32(); + return (a << 32) | b; +} - // --- SecureRandom ---- +// --- SecureRandom ---- - SecureRandom::~SecureRandom() { - } +SecureRandom::~SecureRandom() {} #ifdef _WIN32 - class WinSecureRandom : public SecureRandom { - virtual ~WinSecureRandom(){} - int64_t nextInt64() { - uint32_t a, b; - if ( rand_s(&a) ) { - abort(); - } - if ( rand_s(&b) ) { - abort(); - } - return ( static_cast<int64_t>(a) << 32 ) | b; +class WinSecureRandom : public SecureRandom { + virtual ~WinSecureRandom() {} + int64_t nextInt64() { + uint32_t a, b; + if (rand_s(&a)) { + abort(); } - }; - - SecureRandom* SecureRandom::create() { - return new WinSecureRandom(); + if (rand_s(&b)) { + abort(); + } + return (static_cast<int64_t>(a) << 32) | b; } +}; + +SecureRandom* SecureRandom::create() { + return new WinSecureRandom(); +} #elif defined(__linux__) || defined(__sun) || defined(__APPLE__) || defined(__FreeBSD__) - class InputStreamSecureRandom : public SecureRandom { - public: - InputStreamSecureRandom( const char* fn ) { - _in = new std::ifstream( fn, std::ios::binary | std::ios::in ); - if ( !_in->is_open() ) { - std::cerr << "can't open " << fn << " " << strerror(errno) << std::endl; - abort(); - } +class InputStreamSecureRandom : public SecureRandom { +public: + InputStreamSecureRandom(const char* fn) { + _in = new std::ifstream(fn, std::ios::binary | std::ios::in); + if (!_in->is_open()) { + std::cerr << "can't open " << fn << " " << strerror(errno) << std::endl; + abort(); } + } - ~InputStreamSecureRandom() { - delete _in; - } + ~InputStreamSecureRandom() { + delete _in; + } - int64_t nextInt64() { - int64_t r; - _in->read( reinterpret_cast<char*>( &r ), sizeof(r) ); - if ( _in->fail() ) { - abort(); - } - return r; + int64_t nextInt64() { + int64_t r; + _in->read(reinterpret_cast<char*>(&r), sizeof(r)); + if (_in->fail()) { + abort(); } + return r; + } - private: - std::ifstream* _in; - }; +private: + std::ifstream* _in; +}; - SecureRandom* SecureRandom::create() { - return new InputStreamSecureRandom( "/dev/urandom" ); - } +SecureRandom* SecureRandom::create() { + return new InputStreamSecureRandom("/dev/urandom"); +} #elif defined(__OpenBSD__) - class Arc4SecureRandom : public SecureRandom { - public: - int64_t nextInt64() { - int64_t value; - arc4random_buf(&value, sizeof(value)); - return value; - } - }; - - SecureRandom* SecureRandom::create() { - return new Arc4SecureRandom(); +class Arc4SecureRandom : public SecureRandom { +public: + int64_t nextInt64() { + int64_t value; + arc4random_buf(&value, sizeof(value)); + return value; } +}; + +SecureRandom* SecureRandom::create() { + return new Arc4SecureRandom(); +} #else #error Must implement SecureRandom for platform #endif - } diff --git a/src/mongo/platform/random.h b/src/mongo/platform/random.h index 706b204cbdc..ea63a66852a 100644 --- a/src/mongo/platform/random.h +++ b/src/mongo/platform/random.h @@ -33,61 +33,64 @@ namespace mongo { +/** + * Uses http://en.wikipedia.org/wiki/Xorshift + */ +class PseudoRandom { +public: + PseudoRandom(int32_t seed); + + PseudoRandom(uint32_t seed); + + PseudoRandom(int64_t seed); + + int32_t nextInt32(); + + int64_t nextInt64(); + /** - * Uses http://en.wikipedia.org/wiki/Xorshift + * @return a number between 0 and max */ - class PseudoRandom { - public: - PseudoRandom( int32_t seed ); - - PseudoRandom( uint32_t seed ); - - PseudoRandom( int64_t seed ); - - int32_t nextInt32(); - - int64_t nextInt64(); - - /** - * @return a number between 0 and max - */ - int32_t nextInt32( int32_t max ) { return nextInt32() % max; } - - /** - * @return a number between 0 and max - */ - int64_t nextInt64( int64_t max ) { return nextInt64() % max; } - - /** - * @return a number between 0 and max - * - * This makes PsuedoRandom instances passable as the third argument to std::random_shuffle - */ - intptr_t operator()(intptr_t max) { - if (sizeof(intptr_t) == 4) - return static_cast<intptr_t>(nextInt32(static_cast<int32_t>(max))); - return static_cast<intptr_t>(nextInt64(static_cast<int64_t>(max))); - } - - private: - int32_t _x; - int32_t _y; - int32_t _z; - int32_t _w; - }; + int32_t nextInt32(int32_t max) { + return nextInt32() % max; + } /** - * More secure random numbers - * Suitable for nonce/crypto - * Slower than PseudoRandom, so only use when really need + * @return a number between 0 and max */ - class SecureRandom { - public: - virtual ~SecureRandom(); + int64_t nextInt64(int64_t max) { + return nextInt64() % max; + } - virtual int64_t nextInt64() = 0; + /** + * @return a number between 0 and max + * + * This makes PsuedoRandom instances passable as the third argument to std::random_shuffle + */ + intptr_t operator()(intptr_t max) { + if (sizeof(intptr_t) == 4) + return static_cast<intptr_t>(nextInt32(static_cast<int32_t>(max))); + return static_cast<intptr_t>(nextInt64(static_cast<int64_t>(max))); + } + +private: + int32_t _x; + int32_t _y; + int32_t _z; + int32_t _w; +}; + +/** + * More secure random numbers + * Suitable for nonce/crypto + * Slower than PseudoRandom, so only use when really need + */ +class SecureRandom { +public: + virtual ~SecureRandom(); - static SecureRandom* create(); - }; + virtual int64_t nextInt64() = 0; + static SecureRandom* create(); +}; } diff --git a/src/mongo/platform/random_test.cpp b/src/mongo/platform/random_test.cpp index bedc3ccbcd8..3aecfb2e98b 100644 --- a/src/mongo/platform/random_test.cpp +++ b/src/mongo/platform/random_test.cpp @@ -36,79 +36,78 @@ namespace mongo { - TEST( RandomTest, Seed1 ) { - PseudoRandom a( 12 ); - PseudoRandom b( 12 ); +TEST(RandomTest, Seed1) { + PseudoRandom a(12); + PseudoRandom b(12); - for ( int i = 0; i < 100; i++ ) { - ASSERT_EQUALS( a.nextInt32(), b.nextInt32() ); - } + for (int i = 0; i < 100; i++) { + ASSERT_EQUALS(a.nextInt32(), b.nextInt32()); } +} - TEST( RandomTest, Seed2 ) { - PseudoRandom a( 12 ); - PseudoRandom b( 12 ); +TEST(RandomTest, Seed2) { + PseudoRandom a(12); + PseudoRandom b(12); - for ( int i = 0; i < 100; i++ ) { - ASSERT_EQUALS( a.nextInt64(), b.nextInt64() ); - } + for (int i = 0; i < 100; i++) { + ASSERT_EQUALS(a.nextInt64(), b.nextInt64()); } +} - TEST( RandomTest, Seed3 ) { - PseudoRandom a( 11 ); - PseudoRandom b( 12 ); +TEST(RandomTest, Seed3) { + PseudoRandom a(11); + PseudoRandom b(12); - ASSERT_NOT_EQUALS( a.nextInt32(), b.nextInt32() ); - } + ASSERT_NOT_EQUALS(a.nextInt32(), b.nextInt32()); +} - TEST( RandomTest, Seed4 ) { - PseudoRandom a( 11 ); - std::set<int32_t> s; - for ( int i = 0; i < 100; i++ ) { - s.insert( a.nextInt32() ); - } - ASSERT_EQUALS( 100U, s.size() ); +TEST(RandomTest, Seed4) { + PseudoRandom a(11); + std::set<int32_t> s; + for (int i = 0; i < 100; i++) { + s.insert(a.nextInt32()); } + ASSERT_EQUALS(100U, s.size()); +} - TEST( RandomTest, Seed5 ) { - const int64_t seed = 0xCC453456FA345FABLL; - PseudoRandom a(seed); - std::set<int32_t> s; - for ( int i = 0; i < 100; i++ ) { - s.insert( a.nextInt32() ); - } - ASSERT_EQUALS( 100U, s.size() ); +TEST(RandomTest, Seed5) { + const int64_t seed = 0xCC453456FA345FABLL; + PseudoRandom a(seed); + std::set<int32_t> s; + for (int i = 0; i < 100; i++) { + s.insert(a.nextInt32()); } + ASSERT_EQUALS(100U, s.size()); +} - TEST( RandomTest, R1 ) { - PseudoRandom a( 11 ); - std::set<int32_t> s; - for ( int i = 0; i < 100; i++ ) { - s.insert( a.nextInt32() ); - } - ASSERT_EQUALS( 100U, s.size() ); +TEST(RandomTest, R1) { + PseudoRandom a(11); + std::set<int32_t> s; + for (int i = 0; i < 100; i++) { + s.insert(a.nextInt32()); } + ASSERT_EQUALS(100U, s.size()); +} - TEST( RandomTest, R2 ) { - PseudoRandom a( 11 ); - std::set<int64_t> s; - for ( int i = 0; i < 100; i++ ) { - s.insert( a.nextInt64() ); - } - ASSERT_EQUALS( 100U, s.size() ); +TEST(RandomTest, R2) { + PseudoRandom a(11); + std::set<int64_t> s; + for (int i = 0; i < 100; i++) { + s.insert(a.nextInt64()); } + ASSERT_EQUALS(100U, s.size()); +} - TEST( RandomTest, Secure1 ) { - SecureRandom* a = SecureRandom::create(); - SecureRandom* b = SecureRandom::create(); - - for ( int i = 0; i< 100; i++ ) { - ASSERT_NOT_EQUALS( a->nextInt64(), b->nextInt64() ); - } - - delete a; - delete b; +TEST(RandomTest, Secure1) { + SecureRandom* a = SecureRandom::create(); + SecureRandom* b = SecureRandom::create(); + for (int i = 0; i < 100; i++) { + ASSERT_NOT_EQUALS(a->nextInt64(), b->nextInt64()); } + + delete a; + delete b; +} } diff --git a/src/mongo/platform/shared_library.cpp b/src/mongo/platform/shared_library.cpp index e8822ac3dd1..85d7a3aa0bf 100644 --- a/src/mongo/platform/shared_library.cpp +++ b/src/mongo/platform/shared_library.cpp @@ -32,20 +32,18 @@ namespace mongo { - typedef StatusWith<void (*)()> StatusWithFunc; +typedef StatusWith<void (*)()> StatusWithFunc; - SharedLibrary::SharedLibrary(void* handle) - : _handle(handle) - {} +SharedLibrary::SharedLibrary(void* handle) : _handle(handle) {} - StatusWithFunc SharedLibrary::getFunction(StringData name) { - StatusWith<void*> s = getSymbol(name); +StatusWithFunc SharedLibrary::getFunction(StringData name) { + StatusWith<void*> s = getSymbol(name); - if (!s.isOK()) { - return StatusWithFunc(s.getStatus()); - } - - return StatusWithFunc(reinterpret_cast<void (*)()>(s.getValue())); + if (!s.isOK()) { + return StatusWithFunc(s.getStatus()); } -} // namespace mongo + return StatusWithFunc(reinterpret_cast<void (*)()>(s.getValue())); +} + +} // namespace mongo diff --git a/src/mongo/platform/shared_library.h b/src/mongo/platform/shared_library.h index c77f8290f85..67d0eeb110d 100644 --- a/src/mongo/platform/shared_library.h +++ b/src/mongo/platform/shared_library.h @@ -34,65 +34,64 @@ namespace mongo { +/** + * Loads shared library or DLL at runtime + * Provides functionality to resolve symols and functions at runtime. + * Note: shared library is released by destructor + */ +class SharedLibrary { +public: /** - * Loads shared library or DLL at runtime - * Provides functionality to resolve symols and functions at runtime. - * Note: shared library is released by destructor + * Releases reference to shared library on destruction. + * + * May unload the shared library. + * May invalidate all symbol pointers, depends on OS implementation. */ - class SharedLibrary { - public: - /** - * Releases reference to shared library on destruction. - * - * May unload the shared library. - * May invalidate all symbol pointers, depends on OS implementation. - */ - ~SharedLibrary(); - - /* - * Loads the shared library - * - * Returns a handle to a SharedLibrary on success otherwise StatusWith contains the - * appropriate error. - */ - static StatusWith<std::unique_ptr<SharedLibrary>> create( - const boost::filesystem::path& full_path); + ~SharedLibrary(); - /** - * Retrieves the public symbol of a shared library specified in the name parameter. - * - * Returns a pointer to the symbol if it exists with Status::OK, - * returns NULL if the symbol does not exist with Status::OK, - * otherwise returns an error if the underlying OS infrastructure returns an error. - */ - StatusWith<void*> getSymbol(StringData name); - - /** - * A generic function version of getSymbol, see notes in getSymbol for more information - * Callers should use getFunctionAs. - */ - StatusWith<void (*)()> getFunction(StringData name); + /* + * Loads the shared library + * + * Returns a handle to a SharedLibrary on success otherwise StatusWith contains the + * appropriate error. + */ + static StatusWith<std::unique_ptr<SharedLibrary>> create( + const boost::filesystem::path& full_path); - /** - * A type-safe version of getFunction, see notes in getSymbol for more information - */ - template<typename FuncT> - StatusWith<FuncT> getFunctionAs(StringData name) { + /** + * Retrieves the public symbol of a shared library specified in the name parameter. + * + * Returns a pointer to the symbol if it exists with Status::OK, + * returns NULL if the symbol does not exist with Status::OK, + * otherwise returns an error if the underlying OS infrastructure returns an error. + */ + StatusWith<void*> getSymbol(StringData name); - StatusWith<void (*) ()> s = getFunction(name); + /** + * A generic function version of getSymbol, see notes in getSymbol for more information + * Callers should use getFunctionAs. + */ + StatusWith<void (*)()> getFunction(StringData name); - if (!s.isOK()) { - return StatusWith<FuncT>(s.getStatus()); - } + /** + * A type-safe version of getFunction, see notes in getSymbol for more information + */ + template <typename FuncT> + StatusWith<FuncT> getFunctionAs(StringData name) { + StatusWith<void (*)()> s = getFunction(name); - return StatusWith<FuncT>(reinterpret_cast<FuncT>(s.getValue())); + if (!s.isOK()) { + return StatusWith<FuncT>(s.getStatus()); } - private: - SharedLibrary(void* handle); + return StatusWith<FuncT>(reinterpret_cast<FuncT>(s.getValue())); + } + +private: + SharedLibrary(void* handle); - private: - void* const _handle; - }; +private: + void* const _handle; +}; -} // namespace mongo +} // namespace mongo diff --git a/src/mongo/platform/shared_library_posix.cpp b/src/mongo/platform/shared_library_posix.cpp index 62b19d740b4..2cad9be817e 100644 --- a/src/mongo/platform/shared_library_posix.cpp +++ b/src/mongo/platform/shared_library_posix.cpp @@ -41,48 +41,47 @@ namespace mongo { - SharedLibrary::~SharedLibrary() { - if (_handle) { - if (dlclose(_handle) != 0) { - LOG(2) << "Load Library close failed " << dlerror(); - } +SharedLibrary::~SharedLibrary() { + if (_handle) { + if (dlclose(_handle) != 0) { + LOG(2) << "Load Library close failed " << dlerror(); } } +} - StatusWith<std::unique_ptr<SharedLibrary>> SharedLibrary::create( - const boost::filesystem::path& full_path) { +StatusWith<std::unique_ptr<SharedLibrary>> SharedLibrary::create( + const boost::filesystem::path& full_path) { + LOG(1) << "Loading library: " << full_path.c_str(); - LOG(1) << "Loading library: " << full_path.c_str(); - - void* handle = dlopen(full_path.c_str(), RTLD_NOW | RTLD_GLOBAL); - if (handle == nullptr) { - return Status(ErrorCodes::InternalError, - str::stream() << "Load library failed: " << dlerror()); - } - - return StatusWith<std::unique_ptr<SharedLibrary>>( - std::unique_ptr<SharedLibrary>(new SharedLibrary(handle))); + void* handle = dlopen(full_path.c_str(), RTLD_NOW | RTLD_GLOBAL); + if (handle == nullptr) { + return Status(ErrorCodes::InternalError, + str::stream() << "Load library failed: " << dlerror()); } - StatusWith<void*> SharedLibrary::getSymbol(StringData name) { - // Clear dlerror() before calling dlsym, - // see man dlerror(3) or dlerror(3p) on any POSIX system for details - // Ignore return - dlerror(); + return StatusWith<std::unique_ptr<SharedLibrary>>( + std::unique_ptr<SharedLibrary>(new SharedLibrary(handle))); +} - // StringData is not assued to be null-terminated - std::string symbolName = name.toString(); +StatusWith<void*> SharedLibrary::getSymbol(StringData name) { + // Clear dlerror() before calling dlsym, + // see man dlerror(3) or dlerror(3p) on any POSIX system for details + // Ignore return + dlerror(); - void* symbol = dlsym(_handle, symbolName.c_str()); + // StringData is not assued to be null-terminated + std::string symbolName = name.toString(); - char* error_msg = dlerror(); - if (error_msg != nullptr) { - return StatusWith<void*>(ErrorCodes::InternalError, - str::stream() << "dlsym failed for symbol " - << name << " with error message: " << error_msg); - } + void* symbol = dlsym(_handle, symbolName.c_str()); - return StatusWith<void*>(symbol); + char* error_msg = dlerror(); + if (error_msg != nullptr) { + return StatusWith<void*>(ErrorCodes::InternalError, + str::stream() << "dlsym failed for symbol " << name + << " with error message: " << error_msg); } -} // namespace mongo + return StatusWith<void*>(symbol); +} + +} // namespace mongo diff --git a/src/mongo/platform/shared_library_windows.cpp b/src/mongo/platform/shared_library_windows.cpp index 1a3337b0a10..e3f9431ce60 100644 --- a/src/mongo/platform/shared_library_windows.cpp +++ b/src/mongo/platform/shared_library_windows.cpp @@ -40,47 +40,46 @@ namespace mongo { - SharedLibrary::~SharedLibrary() { - if (_handle) { - if (FreeLibrary(static_cast<HMODULE>(_handle)) == 0) { - DWORD lasterror = GetLastError(); - LOG(2) << "Load library close failed: " << errnoWithDescription(lasterror); - } +SharedLibrary::~SharedLibrary() { + if (_handle) { + if (FreeLibrary(static_cast<HMODULE>(_handle)) == 0) { + DWORD lasterror = GetLastError(); + LOG(2) << "Load library close failed: " << errnoWithDescription(lasterror); } } +} - StatusWith<std::unique_ptr<SharedLibrary>> SharedLibrary::create( - const boost::filesystem::path& full_path) { - - LOG(1) << "Loading library: " << toUtf8String(full_path.c_str()); - - HMODULE handle = LoadLibraryW(full_path.c_str()); - if (handle == nullptr) { - return StatusWith<std::unique_ptr<SharedLibrary>>(ErrorCodes::InternalError, - str::stream() << "Load library failed: " << errnoWithDescription()); - } +StatusWith<std::unique_ptr<SharedLibrary>> SharedLibrary::create( + const boost::filesystem::path& full_path) { + LOG(1) << "Loading library: " << toUtf8String(full_path.c_str()); + HMODULE handle = LoadLibraryW(full_path.c_str()); + if (handle == nullptr) { return StatusWith<std::unique_ptr<SharedLibrary>>( - std::unique_ptr<SharedLibrary>(new SharedLibrary(handle))); + ErrorCodes::InternalError, + str::stream() << "Load library failed: " << errnoWithDescription()); } - StatusWith<void*> SharedLibrary::getSymbol(StringData name) { - // StringData is not assued to be null-terminated - std::string symbolName = name.toString(); + return StatusWith<std::unique_ptr<SharedLibrary>>( + std::unique_ptr<SharedLibrary>(new SharedLibrary(handle))); +} - void* function = GetProcAddress(static_cast<HMODULE>(_handle), symbolName.c_str()); +StatusWith<void*> SharedLibrary::getSymbol(StringData name) { + // StringData is not assued to be null-terminated + std::string symbolName = name.toString(); - if (function == nullptr) { - DWORD gle = GetLastError(); - if (gle != ERROR_PROC_NOT_FOUND) { - return StatusWith<void*>(ErrorCodes::InternalError, - str::stream() << "GetProcAddress failed for symbol: " - << errnoWithDescription()); - } - } + void* function = GetProcAddress(static_cast<HMODULE>(_handle), symbolName.c_str()); - return StatusWith<void*>(function); + if (function == nullptr) { + DWORD gle = GetLastError(); + if (gle != ERROR_PROC_NOT_FOUND) { + return StatusWith<void*>( + ErrorCodes::InternalError, + str::stream() << "GetProcAddress failed for symbol: " << errnoWithDescription()); + } } -} // namespace mongo + return StatusWith<void*>(function); +} +} // namespace mongo diff --git a/src/mongo/platform/strcasestr.cpp b/src/mongo/platform/strcasestr.cpp index 2f91386b8e5..eb5f39b442e 100644 --- a/src/mongo/platform/strcasestr.cpp +++ b/src/mongo/platform/strcasestr.cpp @@ -50,68 +50,61 @@ namespace mongo { namespace pal { - /** - * strcasestr -- case-insensitive search for a substring within another string. - * - * @param haystack ptr to C-string to search - * @param needle ptr to C-string to try to find within 'haystack' - * @return ptr to start of 'needle' within 'haystack' if found, NULL otherwise - */ - const char* STRCASESTR_EMULATION_NAME(const char* haystack, const char* needle) { - - std::string haystackLower(haystack); - std::transform(haystackLower.begin(), - haystackLower.end(), - haystackLower.begin(), - ::tolower); - - std::string needleLower(needle); - std::transform(needleLower.begin(), - needleLower.end(), - needleLower.begin(), - ::tolower); - - // Use strstr() to find 'lowercased needle' in 'lowercased haystack' - // If found, use the location to compute the matching location in the original string - // If not found, return NULL - const char* haystackLowerStart = haystackLower.c_str(); - const char* location = strstr(haystackLowerStart, needleLower.c_str()); - return location ? (haystack + (location - haystackLowerStart)) : NULL; - } +/** + * strcasestr -- case-insensitive search for a substring within another string. + * + * @param haystack ptr to C-string to search + * @param needle ptr to C-string to try to find within 'haystack' + * @return ptr to start of 'needle' within 'haystack' if found, NULL otherwise + */ +const char* STRCASESTR_EMULATION_NAME(const char* haystack, const char* needle) { + std::string haystackLower(haystack); + std::transform(haystackLower.begin(), haystackLower.end(), haystackLower.begin(), ::tolower); + + std::string needleLower(needle); + std::transform(needleLower.begin(), needleLower.end(), needleLower.begin(), ::tolower); + + // Use strstr() to find 'lowercased needle' in 'lowercased haystack' + // If found, use the location to compute the matching location in the original string + // If not found, return NULL + const char* haystackLowerStart = haystackLower.c_str(); + const char* location = strstr(haystackLowerStart, needleLower.c_str()); + return location ? (haystack + (location - haystackLowerStart)) : NULL; +} #if defined(__sun) - typedef const char* (*StrCaseStrFunc)(const char* haystack, const char* needle); - static StrCaseStrFunc strcasestr_switcher = mongo::pal::strcasestr_emulation; +typedef const char* (*StrCaseStrFunc)(const char* haystack, const char* needle); +static StrCaseStrFunc strcasestr_switcher = mongo::pal::strcasestr_emulation; - const char* strcasestr(const char* haystack, const char* needle) { - return strcasestr_switcher(haystack, needle); - } +const char* strcasestr(const char* haystack, const char* needle) { + return strcasestr_switcher(haystack, needle); +} -#endif // #if defined(__sun) +#endif // #if defined(__sun) -} // namespace pal -} // namespace mongo +} // namespace pal +} // namespace mongo -#endif // #if defined(_WIN32) || defined(__sun) +#endif // #if defined(_WIN32) || defined(__sun) #if defined(__sun) namespace mongo { - // 'strcasestr()' on Solaris will call the emulation if the symbol is not found - // - MONGO_INITIALIZER_GENERAL(SolarisStrCaseCmp, - MONGO_NO_PREREQUISITES, - ("default"))(InitializerContext* context) { - void* functionAddress = dlsym(RTLD_DEFAULT, "strcasestr"); - if (functionAddress != NULL) { - mongo::pal::strcasestr_switcher = - reinterpret_cast<mongo::pal::StrCaseStrFunc>(functionAddress); - } - return Status::OK(); +// 'strcasestr()' on Solaris will call the emulation if the symbol is not found +// +MONGO_INITIALIZER_GENERAL(SolarisStrCaseCmp, + MONGO_NO_PREREQUISITES, + ("default"))(InitializerContext* context) { + void* functionAddress = dlsym(RTLD_DEFAULT, "strcasestr"); + if (functionAddress != NULL) { + mongo::pal::strcasestr_switcher = + reinterpret_cast<mongo::pal::StrCaseStrFunc>(functionAddress); } + return Status::OK(); +} -} // namespace mongo +} // namespace mongo -#endif // __sun +#endif // __sun diff --git a/src/mongo/platform/strcasestr.h b/src/mongo/platform/strcasestr.h index cdc2564c21e..44cd1b0122a 100644 --- a/src/mongo/platform/strcasestr.h +++ b/src/mongo/platform/strcasestr.h @@ -31,9 +31,9 @@ namespace mongo { namespace pal { - const char* strcasestr(const char* haystack, const char* needle); +const char* strcasestr(const char* haystack, const char* needle); } - using mongo::pal::strcasestr; +using mongo::pal::strcasestr; } #else @@ -41,7 +41,7 @@ namespace pal { #include <cstring> namespace mongo { - using ::strcasestr; +using ::strcasestr; } #endif diff --git a/src/mongo/platform/unordered_map.h b/src/mongo/platform/unordered_map.h index 5bf3e340962..81651e6079d 100644 --- a/src/mongo/platform/unordered_map.h +++ b/src/mongo/platform/unordered_map.h @@ -31,6 +31,6 @@ namespace mongo { - using std::unordered_map; +using std::unordered_map; } // namespace mongo diff --git a/src/mongo/platform/unordered_set.h b/src/mongo/platform/unordered_set.h index 4733ab73973..5921e9ccded 100644 --- a/src/mongo/platform/unordered_set.h +++ b/src/mongo/platform/unordered_set.h @@ -31,6 +31,6 @@ namespace mongo { - using std::unordered_set; +using std::unordered_set; } // namespace mongo diff --git a/src/mongo/platform/windows_basic.h b/src/mongo/platform/windows_basic.h index 9eb28ca1186..030edcaa677 100644 --- a/src/mongo/platform/windows_basic.h +++ b/src/mongo/platform/windows_basic.h @@ -70,22 +70,22 @@ // No need to set WINVER, SdkDdkVer.h does that for us, we double check this below. // for rand_s() usage: -# define _CRT_RAND_S -# ifndef NOMINMAX -# define NOMINMAX -# endif +#define _CRT_RAND_S +#ifndef NOMINMAX +#define NOMINMAX +#endif // Do not complain that about standard library functions that Windows believes should have // underscores in front of them, such as unlink(). #define _CRT_NONSTDC_NO_DEPRECATE // tell windows.h not to include a bunch of headers we don't need: -# define WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN -# include <winsock2.h> //this must be included before the first windows.h include -# include <ws2tcpip.h> -# include <wspiapi.h> -# include <windows.h> +#include <winsock2.h> //this must be included before the first windows.h include +#include <ws2tcpip.h> +#include <wspiapi.h> +#include <windows.h> // Should come either from the command line, or if not set there, the inclusion of sdkddkver.h // via windows.h above should set it based in _WIN32_WINNT, which is assuredly set by now. |