Use alignas to force alignment.
authorherb <herb@google.com>
Fri, 4 Nov 2016 15:41:01 +0000 (08:41 -0700)
committerCommit bot <commit-bot@chromium.org>
Fri, 4 Nov 2016 15:41:01 +0000 (08:41 -0700)
Using alignas reduces code and platform specific macros.

Use alignas instead of std::aligned_storage because it is unimplemneted
in MSVC 2015.

Here is the bug from MS:
https://connect.microsoft.com/VisualStudio/feedback/details/1559873/std-aligned-storage-cannot-align-type-with-16-byte

BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2468243002
TBR=reed@google.com

Review-Url: https://codereview.chromium.org/2473143002

include/core/SkPostConfig.h
src/core/SkLinearBitmapPipeline.h
src/core/SkSmallAllocator.h

index 1b1cb3e751e7273d984ef48212ce266159b85136..c34397cde64c13c64e3d3839c616f82b98501611 100644 (file)
 #  endif
 #endif
 
-// As usual, there are two ways to increase alignment... the MSVC way and the everyone-else way.
-#ifndef SK_STRUCT_ALIGN
-    #ifdef _MSC_VER
-        #define SK_STRUCT_ALIGN(N) __declspec(align(N))
-    #else
-        #define SK_STRUCT_ALIGN(N) __attribute__((aligned(N)))
-    #endif
-#endif
-
 #if defined(_MSC_VER) && SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2
     #define SK_VECTORCALL __vectorcall
 #elif defined(SK_CPU_ARM32) && defined(SK_ARM_HAS_NEON)
index 91b573df5d8c5a95de93ff3b31a5c82741633c57..3abdb8014aa134503d7694c7c9ba25705572d42e 100644 (file)
@@ -78,17 +78,14 @@ public:
         // the pipeline on a new sampler.
         Base* cloneStageTo(Next* next, Stage* cloneToStage) const;
 
-        Base* get() const { return reinterpret_cast<Base*>(&fSpace); }
+        Base* get() const { return reinterpret_cast<Base*>(fSpace); }
         Base* operator->() const { return this->get(); }
         Base& operator*() const { return *(this->get()); }
 
     private:
         std::function<void (Next*, void*)> fStageCloner;
-        struct SK_STRUCT_ALIGN(16) Space {
-            char space[kSize];
-        };
-        bool fIsInitialized;
-        mutable Space fSpace;
+        alignas(16) mutable char           fSpace[kSize];
+        bool                               fIsInitialized;
     };
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -111,16 +108,13 @@ public:
             fIsInitialized = true;
         }
 
-        Base* get() const { return reinterpret_cast<Base*>(&fSpace); }
+        Base* get() const { return reinterpret_cast<Base*>(fSpace); }
         Base* operator->() const { return this->get(); }
         Base& operator*() const { return *(this->get()); }
 
     private:
-        struct SK_STRUCT_ALIGN(16) Space {
-            char space[kSize];
-        };
-        mutable Space fSpace;
-        bool          fIsInitialized;
+        alignas(16) mutable char fSpace[kSize];
+        bool                     fIsInitialized;
 
     };
 
@@ -134,7 +128,7 @@ public:
     using MatrixStage  = Stage<PointProcessorInterface,    160, PointProcessorInterface>;
     using TileStage    = Stage<PointProcessorInterface,    160, SampleProcessorInterface>;
     using SampleStage  = Stage<SampleProcessorInterface,   160, BlendProcessorInterface>;
-    using BlenderStage = Stage<BlendProcessorInterface,     40>;
+    using BlenderStage = Stage<BlendProcessorInterface,     48>;
     using Accessor     = PolyMemory<PixelAccessorInterface, 64>;
 
 private:
index 9095fa57fc11fde0022c023f5e8341d7dae819dd..67afe756912bdd014945f0ba235dc9797cdc3aa7 100644 (file)
@@ -93,7 +93,7 @@ public:
             // There is space in fStorage.
             rec->fStorageSize = storageRequired;
             rec->fHeapStorage = nullptr;
-            rec->fObj = static_cast<void*>(fStorage.fBytes + fStorageUsed);
+            rec->fObj = static_cast<void*>(fStorage + fStorageUsed);
             fStorageUsed += storageRequired;
         }
         rec->fKillProc = DestroyT<T>;
@@ -129,17 +129,11 @@ private:
         static_cast<T*>(ptr)->~T();
     }
 
-    struct SK_STRUCT_ALIGN(16) Storage {
-        // we add kMaxObjects * 15 to account for the worst-case slop, where each allocation wasted
-        // 15 bytes (due to forcing each to be 16-byte aligned)
-        char    fBytes[kTotalBytes + kMaxObjects * 15];
-    };
-
-    Storage     fStorage;
+    alignas(16) char fStorage[kTotalBytes];
     // Number of bytes used so far.
-    size_t      fStorageUsed;
-    uint32_t    fNumObjects;
-    Rec         fRecs[kMaxObjects];
+    size_t   fStorageUsed;
+    uint32_t fNumObjects;
+    Rec      fRecs[kMaxObjects];
 };
 
 #endif // SkSmallAllocator_DEFINED