replace std::aligned_storage

There's no reason to use std::aligned_storage when it's simpler to use
an array and alignas().  This way you don't have to remember whether the
template arguments are size-then-align or align-then-size, you don't
have to remember to use the _t variant or typename ... ::type, and
there's no risk to forgetting the alignment parameter entirely.

It doesn't look like this was deprecated, but I still think this paper
makes good arguments for why we shouldn't use it:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1413r1.pdf

Bug: skia:10921
Change-Id: Ia64a2e43c4cba9b4d64138a7474e353a8eaf01a6
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/333258
Reviewed-by: Herb Derby <herb@google.com>
Commit-Queue: Mike Klein <mtklein@google.com>
This commit is contained in:
Mike Klein 2020-11-09 15:50:37 -06:00 committed by Skia Commit-Bot
parent 0c0884991d
commit fb5850f410
5 changed files with 17 additions and 9 deletions

View File

@ -15,13 +15,17 @@
namespace {
// sizeof is a multiple of GrMemoryPool::kAlignment for 4, 8, or 16 byte alignment
using Aligned = std::aligned_storage<32, GrMemoryPool::kAlignment>::type;
struct alignas(GrMemoryPool::kAlignment) Aligned {
char buf[32];
};
static_assert(sizeof(Aligned) == 32);
static_assert(sizeof(Aligned) % GrMemoryPool::kAlignment == 0);
// sizeof is not a multiple of GrMemoryPool::kAlignment (will not be a multiple of max_align_t
// if it's 4, 8, or 16, as desired).
using Unaligned = std::aligned_storage<30, 2>::type;
struct alignas(2) Unaligned {
char buf[30];
};
static_assert(sizeof(Unaligned) == 30);
static_assert(sizeof(Unaligned) % GrMemoryPool::kAlignment != 0);

View File

@ -100,7 +100,7 @@ private:
+ 32; // slop for occasional small extras
SkDescriptor* fDesc{nullptr};
std::aligned_storage<kStorageSize, alignof(uint32_t)>::type fStorage;
alignas(uint32_t) char fStorage[kStorageSize];
};
#endif //SkDescriptor_DEFINED

View File

@ -111,8 +111,8 @@ public:
T* getMaybeNull() const { return fPtr; }
private:
typename std::aligned_storage<sizeof(T), alignof(T)>::type fStorage;
T* fPtr{nullptr}; // nullptr or fStorage
alignas(T) char fStorage[sizeof(T)];
T* fPtr{nullptr}; // nullptr or fStorage
};
/**

View File

@ -337,7 +337,7 @@ std::unique_ptr<GrFragmentProcessor> SkImageShader::asFragmentProcessor(
alignof(GrTextureAdjuster ),
alignof(GrImageTextureMaker ),
alignof(GrBitmapTextureMaker )});
std::aligned_storage_t<kSize, kAlign> storage;
alignas(kAlign) char storage[kSize];
GrTextureProducer* producer = nullptr;
SkScopeExit destroyProducer([&producer]{ if (producer) { producer->~GrTextureProducer(); } });

View File

@ -67,6 +67,10 @@ struct WithDtor {
~WithDtor() { }
};
struct alignas(8) OddAlignment {
char buf[10];
};
DEF_TEST(ArenaAlloc, r) {
{
@ -90,7 +94,7 @@ DEF_TEST(ArenaAlloc, r) {
REPORTER_ASSERT(r, fooArray[4].y == -3.0f);
REPORTER_ASSERT(r, created == 11);
REPORTER_ASSERT(r, destroyed == 0);
arena.make<typename std::aligned_storage<10,8>::type>();
arena.make<OddAlignment>();
}
REPORTER_ASSERT(r, created == 11);
REPORTER_ASSERT(r, destroyed == 11);
@ -116,7 +120,7 @@ DEF_TEST(ArenaAlloc, r) {
REPORTER_ASSERT(r, fooArray[4].y == -3.0f);
REPORTER_ASSERT(r, created == 11);
REPORTER_ASSERT(r, destroyed == 0);
arena.make<typename std::aligned_storage<10,8>::type>();
arena.make<OddAlignment>();
}
REPORTER_ASSERT(r, created == 11);
REPORTER_ASSERT(r, destroyed == 11);
@ -143,7 +147,7 @@ DEF_TEST(ArenaAlloc, r) {
REPORTER_ASSERT(r, fooArray[4].y == -3.0f);
REPORTER_ASSERT(r, created == 11);
REPORTER_ASSERT(r, destroyed == 0);
arena.make<typename std::aligned_storage<10,8>::type>();
arena.make<OddAlignment>();
}
REPORTER_ASSERT(r, created == 11);
REPORTER_ASSERT(r, destroyed == 11);