From 65220fefdfd7fa2595e420baa784319e32ffab2e Mon Sep 17 00:00:00 2001 From: Orvid King Date: Wed, 29 Jul 2015 11:44:32 -0700 Subject: [PATCH] Added FOLLY_ALIGNED(), to allow aligning on MSVC as well Summary: This adds `FOLLY_ALIGNED` to `Portability.h`, and adjusts the places that were previously using the raw aligned attribute to use this instead. Closes #262 Reviewed By: @yfeldblum Differential Revision: D2283639 Pulled By: @sgolemon --- folly/Arena.h | 6 ++---- folly/Conv.h | 10 +++++----- folly/MicroSpinLock.h | 4 ++-- folly/Portability.h | 9 +++++++++ folly/detail/CacheLocality.h | 2 +- 5 files changed, 19 insertions(+), 12 deletions(-) diff --git a/folly/Arena.h b/folly/Arena.h index 00caa947..530e0505 100644 --- a/folly/Arena.h +++ b/folly/Arena.h @@ -132,7 +132,7 @@ class Arena { typedef boost::intrusive::slist_member_hook< boost::intrusive::tag> BlockLink; - struct Block { + struct FOLLY_ALIGNED_MAX Block { BlockLink link; // Allocate a block with at least size bytes of storage. @@ -150,9 +150,7 @@ class Arena { private: Block() = default; ~Block() = default; - } __attribute__((__aligned__)); - // This should be alignas(std::max_align_t) but neither alignas nor - // max_align_t are supported by gcc 4.6.2. + }; public: static constexpr size_t kDefaultMinBlockSize = 4096 - sizeof(Block); diff --git a/folly/Conv.h b/folly/Conv.h index 2991f02e..1d1c0561 100644 --- a/folly/Conv.h +++ b/folly/Conv.h @@ -237,7 +237,7 @@ inline uint32_t digits10(uint64_t v) { // 10^i, defined for i 0 through 19. // This is 20 * 8 == 160 bytes, which fits neatly into 5 cache lines // (assuming a cache line size of 64). - static const uint64_t powersOf10[20] __attribute__((__aligned__(64))) = { + static const uint64_t powersOf10[20] FOLLY_ALIGNED(64) = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, 100000000000, 1000000000000, 10000000000000, 100000000000000, 1000000000000000, 10000000000000000, 100000000000000000, @@ -986,7 +986,7 @@ namespace detail { // still not overflow uint16_t. constexpr int32_t OOR = 10000; -__attribute__((__aligned__(16))) constexpr uint16_t shift1[] = { +FOLLY_ALIGNED(16) constexpr uint16_t shift1[] = { OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 0-9 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20 @@ -1015,7 +1015,7 @@ __attribute__((__aligned__(16))) constexpr uint16_t shift1[] = { OOR, OOR, OOR, OOR, OOR, OOR // 250 }; -__attribute__((__aligned__(16))) constexpr uint16_t shift10[] = { +FOLLY_ALIGNED(16) constexpr uint16_t shift10[] = { OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 0-9 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20 @@ -1044,7 +1044,7 @@ __attribute__((__aligned__(16))) constexpr uint16_t shift10[] = { OOR, OOR, OOR, OOR, OOR, OOR // 250 }; -__attribute__((__aligned__(16))) constexpr uint16_t shift100[] = { +FOLLY_ALIGNED(16) constexpr uint16_t shift100[] = { OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 0-9 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20 @@ -1073,7 +1073,7 @@ __attribute__((__aligned__(16))) constexpr uint16_t shift100[] = { OOR, OOR, OOR, OOR, OOR, OOR // 250 }; -__attribute__((__aligned__(16))) constexpr uint16_t shift1000[] = { +FOLLY_ALIGNED(16) constexpr uint16_t shift1000[] = { OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 0-9 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20 diff --git a/folly/MicroSpinLock.h b/folly/MicroSpinLock.h index cccba15d..6a7588f9 100644 --- a/folly/MicroSpinLock.h +++ b/folly/MicroSpinLock.h @@ -102,7 +102,7 @@ struct MicroSpinLock { #define FOLLY_CACHE_LINE_SIZE 64 template -struct SpinLockArray { +struct FOLLY_ALIGNED_MAX SpinLockArray { T& operator[](size_t i) { return data_[i].lock; } @@ -132,7 +132,7 @@ struct SpinLockArray { char padding_[FOLLY_CACHE_LINE_SIZE]; std::array data_; -} __attribute__((__aligned__)); +}; ////////////////////////////////////////////////////////////////////// diff --git a/folly/Portability.h b/folly/Portability.h index c81ce638..bce4dd1f 100644 --- a/folly/Portability.h +++ b/folly/Portability.h @@ -63,6 +63,15 @@ struct MaxAlign { char c; } __attribute__((__aligned__)); // compiler specific attribute translation // msvc should come first, so if clang is in msvc mode it gets the right defines +#if defined(__clang__) || defined(__GNUC__) +# define FOLLY_ALIGNED(size) __attribute__((__aligned__(size))) +#elif defined(_MSC_VER) +# define FOLLY_ALIGNED(size) __declspec(align(size)) +#else +# error Cannot define FOLLY_ALIGNED on this platform +#endif +#define FOLLY_ALIGNED_MAX FOLLY_ALIGNED(alignof(MaxAlign)) + // NOTE: this will only do checking in msvc with versions that support /analyze #if _MSC_VER # ifdef _USE_ATTRIBUTES_FOR_SAL diff --git a/folly/detail/CacheLocality.h b/folly/detail/CacheLocality.h index 0273100b..40d554f4 100644 --- a/folly/detail/CacheLocality.h +++ b/folly/detail/CacheLocality.h @@ -127,7 +127,7 @@ struct CacheLocality { /// An attribute that will cause a variable or field to be aligned so that /// it doesn't have false sharing with anything at a smaller memory address. -#define FOLLY_ALIGN_TO_AVOID_FALSE_SHARING __attribute__((__aligned__(128))) +#define FOLLY_ALIGN_TO_AVOID_FALSE_SHARING FOLLY_ALIGNED(128) /// Holds a function pointer to the VDSO implementation of getcpu(2), /// if available -- 2.34.1