(Wangle) Align lambdaBuf_
authorHannes Roth <hannesr@fb.com>
Tue, 11 Aug 2015 23:11:11 +0000 (16:11 -0700)
committerfacebook-github-bot-9 <folly-bot@fb.com>
Tue, 11 Aug 2015 23:22:05 +0000 (16:22 -0700)
Summary: Not aligning `lambdaBuf_` can lead to unaligned reads, which can be a problem on some platforms.

We use `lambdaBuf_` to store a type of up to `lambdaBufSize`, so aligning it to that should always be safe.

Reviewed By: @alexshap

Differential Revision: D2319347

folly/futures/detail/Core.h

index efbf2616ebf821e5bb32bb063d61be9f76f3330e..252af04a56071fae5b039d1c074b2a5c1e735d7d 100644 (file)
@@ -148,7 +148,7 @@ class Core {
 
       // Move the lambda into the Core if it fits
       if (sizeof(LambdaBufHelper<F>) <= lambdaBufSize) {
-        auto funcLoc = static_cast<LambdaBufHelper<F>*>((void*)lambdaBuf_);
+        auto funcLoc = reinterpret_cast<LambdaBufHelper<F>*>(&lambdaBuf_);
         new (funcLoc) LambdaBufHelper<F>(std::forward<F>(func));
         callback_ = std::ref(*funcLoc);
       } else {
@@ -366,7 +366,7 @@ class Core {
 
   // lambdaBuf occupies exactly one cache line
   static constexpr size_t lambdaBufSize = 8 * sizeof(void*);
-  char lambdaBuf_[lambdaBufSize];
+  typename std::aligned_storage<lambdaBufSize>::type lambdaBuf_;
   // place result_ next to increase the likelihood that the value will be
   // contained entirely in one cache line
   folly::Optional<Try<T>> result_;