Commit 65220fef authored by Orvid King's avatar Orvid King Committed by facebook-github-bot-4

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
parent 5e6f4e77
...@@ -132,7 +132,7 @@ class Arena { ...@@ -132,7 +132,7 @@ class Arena {
typedef boost::intrusive::slist_member_hook< typedef boost::intrusive::slist_member_hook<
boost::intrusive::tag<Arena>> BlockLink; boost::intrusive::tag<Arena>> BlockLink;
struct Block { struct FOLLY_ALIGNED_MAX Block {
BlockLink link; BlockLink link;
// Allocate a block with at least size bytes of storage. // Allocate a block with at least size bytes of storage.
...@@ -150,9 +150,7 @@ class Arena { ...@@ -150,9 +150,7 @@ class Arena {
private: private:
Block() = default; Block() = default;
~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: public:
static constexpr size_t kDefaultMinBlockSize = 4096 - sizeof(Block); static constexpr size_t kDefaultMinBlockSize = 4096 - sizeof(Block);
......
...@@ -237,7 +237,7 @@ inline uint32_t digits10(uint64_t v) { ...@@ -237,7 +237,7 @@ inline uint32_t digits10(uint64_t v) {
// 10^i, defined for i 0 through 19. // 10^i, defined for i 0 through 19.
// This is 20 * 8 == 160 bytes, which fits neatly into 5 cache lines // This is 20 * 8 == 160 bytes, which fits neatly into 5 cache lines
// (assuming a cache line size of 64). // (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, 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000,
10000000000, 100000000000, 1000000000000, 10000000000000, 100000000000000, 10000000000, 100000000000, 1000000000000, 10000000000000, 100000000000000,
1000000000000000, 10000000000000000, 100000000000000000, 1000000000000000, 10000000000000000, 100000000000000000,
...@@ -986,7 +986,7 @@ namespace detail { ...@@ -986,7 +986,7 @@ namespace detail {
// still not overflow uint16_t. // still not overflow uint16_t.
constexpr int32_t OOR = 10000; 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, // 0-9
OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10
OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20
...@@ -1015,7 +1015,7 @@ __attribute__((__aligned__(16))) constexpr uint16_t shift1[] = { ...@@ -1015,7 +1015,7 @@ __attribute__((__aligned__(16))) constexpr uint16_t shift1[] = {
OOR, OOR, OOR, OOR, OOR, OOR // 250 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, // 0-9
OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10
OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20
...@@ -1044,7 +1044,7 @@ __attribute__((__aligned__(16))) constexpr uint16_t shift10[] = { ...@@ -1044,7 +1044,7 @@ __attribute__((__aligned__(16))) constexpr uint16_t shift10[] = {
OOR, OOR, OOR, OOR, OOR, OOR // 250 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, // 0-9
OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10
OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20
...@@ -1073,7 +1073,7 @@ __attribute__((__aligned__(16))) constexpr uint16_t shift100[] = { ...@@ -1073,7 +1073,7 @@ __attribute__((__aligned__(16))) constexpr uint16_t shift100[] = {
OOR, OOR, OOR, OOR, OOR, OOR // 250 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, // 0-9
OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10
OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20
......
...@@ -102,7 +102,7 @@ struct MicroSpinLock { ...@@ -102,7 +102,7 @@ struct MicroSpinLock {
#define FOLLY_CACHE_LINE_SIZE 64 #define FOLLY_CACHE_LINE_SIZE 64
template <class T, size_t N> template <class T, size_t N>
struct SpinLockArray { struct FOLLY_ALIGNED_MAX SpinLockArray {
T& operator[](size_t i) { T& operator[](size_t i) {
return data_[i].lock; return data_[i].lock;
} }
...@@ -132,7 +132,7 @@ struct SpinLockArray { ...@@ -132,7 +132,7 @@ struct SpinLockArray {
char padding_[FOLLY_CACHE_LINE_SIZE]; char padding_[FOLLY_CACHE_LINE_SIZE];
std::array<PaddedSpinLock, N> data_; std::array<PaddedSpinLock, N> data_;
} __attribute__((__aligned__)); };
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
......
...@@ -63,6 +63,15 @@ struct MaxAlign { char c; } __attribute__((__aligned__)); ...@@ -63,6 +63,15 @@ struct MaxAlign { char c; } __attribute__((__aligned__));
// compiler specific attribute translation // compiler specific attribute translation
// msvc should come first, so if clang is in msvc mode it gets the right defines // 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 // NOTE: this will only do checking in msvc with versions that support /analyze
#if _MSC_VER #if _MSC_VER
# ifdef _USE_ATTRIBUTES_FOR_SAL # ifdef _USE_ATTRIBUTES_FOR_SAL
......
...@@ -127,7 +127,7 @@ struct CacheLocality { ...@@ -127,7 +127,7 @@ struct CacheLocality {
/// An attribute that will cause a variable or field to be aligned so that /// 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. /// 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), /// Holds a function pointer to the VDSO implementation of getcpu(2),
/// if available /// if available
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment