Added FOLLY_ALIGNED(), to allow aligning on MSVC as well
authorOrvid King <blah38621@gmail.com>
Wed, 29 Jul 2015 18:44:32 +0000 (11:44 -0700)
committerfacebook-github-bot-4 <folly-bot@fb.com>
Wed, 29 Jul 2015 20:22:08 +0000 (13:22 -0700)
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
folly/Conv.h
folly/MicroSpinLock.h
folly/Portability.h
folly/detail/CacheLocality.h

index 00caa947abba291e2bdc07e1e3b7318ed7ff1bba..530e0505ed4933df75fd07367acbe021c6ce9490 100644 (file)
@@ -132,7 +132,7 @@ class Arena {
   typedef boost::intrusive::slist_member_hook<
     boost::intrusive::tag<Arena>> 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);
index 2991f02e4d13b61b673984df28f8cefaa430e245..1d1c056128072b67727e7d876e2b0119d5863bdc 100644 (file)
@@ -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
index cccba15d88b6a8523f115d42537c481d7678cdfc..6a7588f9416b842e15436b25c901ac03df774fbc 100644 (file)
@@ -102,7 +102,7 @@ struct MicroSpinLock {
 #define FOLLY_CACHE_LINE_SIZE 64
 
 template <class T, size_t N>
-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<PaddedSpinLock, N> data_;
-} __attribute__((__aligned__));
+};
 
 //////////////////////////////////////////////////////////////////////
 
index c81ce63816c162a91e5ec837ffadb02267c67b35..bce4dd1f06a7d8c5aabba792c741da6e90b63a42 100644 (file)
@@ -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
index 0273100bb5274de185b0e7559a139249ef92ef4f..40d554f484044e6eb978b02834c03bf6db4b87c7 100644 (file)
@@ -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