Skip to content

Commit fb5850f

Browse files
Mike KleinSkia Commit-Bot
authored andcommitted
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>
1 parent 0c08849 commit fb5850f

File tree

5 files changed

+17
-9
lines changed

5 files changed

+17
-9
lines changed

bench/GrMemoryPoolBench.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@
1515
namespace {
1616

1717
// sizeof is a multiple of GrMemoryPool::kAlignment for 4, 8, or 16 byte alignment
18-
using Aligned = std::aligned_storage<32, GrMemoryPool::kAlignment>::type;
18+
struct alignas(GrMemoryPool::kAlignment) Aligned {
19+
char buf[32];
20+
};
1921
static_assert(sizeof(Aligned) == 32);
2022
static_assert(sizeof(Aligned) % GrMemoryPool::kAlignment == 0);
2123

2224
// sizeof is not a multiple of GrMemoryPool::kAlignment (will not be a multiple of max_align_t
2325
// if it's 4, 8, or 16, as desired).
24-
using Unaligned = std::aligned_storage<30, 2>::type;
26+
struct alignas(2) Unaligned {
27+
char buf[30];
28+
};
2529
static_assert(sizeof(Unaligned) == 30);
2630
static_assert(sizeof(Unaligned) % GrMemoryPool::kAlignment != 0);
2731

src/core/SkDescriptor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class SkAutoDescriptor {
100100
+ 32; // slop for occasional small extras
101101

102102
SkDescriptor* fDesc{nullptr};
103-
std::aligned_storage<kStorageSize, alignof(uint32_t)>::type fStorage;
103+
alignas(uint32_t) char fStorage[kStorageSize];
104104
};
105105

106106
#endif //SkDescriptor_DEFINED

src/core/SkTLazy.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ template <typename T> class SkTLazy {
111111
T* getMaybeNull() const { return fPtr; }
112112

113113
private:
114-
typename std::aligned_storage<sizeof(T), alignof(T)>::type fStorage;
115-
T* fPtr{nullptr}; // nullptr or fStorage
114+
alignas(T) char fStorage[sizeof(T)];
115+
T* fPtr{nullptr}; // nullptr or fStorage
116116
};
117117

118118
/**

src/shaders/SkImageShader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ std::unique_ptr<GrFragmentProcessor> SkImageShader::asFragmentProcessor(
337337
alignof(GrTextureAdjuster ),
338338
alignof(GrImageTextureMaker ),
339339
alignof(GrBitmapTextureMaker )});
340-
std::aligned_storage_t<kSize, kAlign> storage;
340+
alignas(kAlign) char storage[kSize];
341341
GrTextureProducer* producer = nullptr;
342342
SkScopeExit destroyProducer([&producer]{ if (producer) { producer->~GrTextureProducer(); } });
343343

tests/ArenaAllocTest.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ struct WithDtor {
6767
~WithDtor() { }
6868
};
6969

70+
struct alignas(8) OddAlignment {
71+
char buf[10];
72+
};
73+
7074
DEF_TEST(ArenaAlloc, r) {
7175

7276
{
@@ -90,7 +94,7 @@ DEF_TEST(ArenaAlloc, r) {
9094
REPORTER_ASSERT(r, fooArray[4].y == -3.0f);
9195
REPORTER_ASSERT(r, created == 11);
9296
REPORTER_ASSERT(r, destroyed == 0);
93-
arena.make<typename std::aligned_storage<10,8>::type>();
97+
arena.make<OddAlignment>();
9498
}
9599
REPORTER_ASSERT(r, created == 11);
96100
REPORTER_ASSERT(r, destroyed == 11);
@@ -116,7 +120,7 @@ DEF_TEST(ArenaAlloc, r) {
116120
REPORTER_ASSERT(r, fooArray[4].y == -3.0f);
117121
REPORTER_ASSERT(r, created == 11);
118122
REPORTER_ASSERT(r, destroyed == 0);
119-
arena.make<typename std::aligned_storage<10,8>::type>();
123+
arena.make<OddAlignment>();
120124
}
121125
REPORTER_ASSERT(r, created == 11);
122126
REPORTER_ASSERT(r, destroyed == 11);
@@ -143,7 +147,7 @@ DEF_TEST(ArenaAlloc, r) {
143147
REPORTER_ASSERT(r, fooArray[4].y == -3.0f);
144148
REPORTER_ASSERT(r, created == 11);
145149
REPORTER_ASSERT(r, destroyed == 0);
146-
arena.make<typename std::aligned_storage<10,8>::type>();
150+
arena.make<OddAlignment>();
147151
}
148152
REPORTER_ASSERT(r, created == 11);
149153
REPORTER_ASSERT(r, destroyed == 11);

0 commit comments

Comments
 (0)