summaryrefslogtreecommitdiffstats
path: root/llvm/unittests/Support/TrailingObjectsTest.cpp
diff options
context:
space:
mode:
authorJames Y Knight <jyknight@google.com>2015-12-29 04:00:43 +0000
committerJames Y Knight <jyknight@google.com>2015-12-29 04:00:43 +0000
commitd734aaa4ba07f6a56fd5e855d8c152756135e1eb (patch)
treee606e79cff6aedaf852f543ea5dd21ad92423ee9 /llvm/unittests/Support/TrailingObjectsTest.cpp
parent992904a0afce8131b6407ad3534a1f3b29838b75 (diff)
downloadbcm5719-llvm-d734aaa4ba07f6a56fd5e855d8c152756135e1eb.tar.gz
bcm5719-llvm-d734aaa4ba07f6a56fd5e855d8c152756135e1eb.zip
[TrailingObjects] Dynamically realign under-aligned trailing objects.
Previously, the code enforced non-decreasing alignment of each trailing type. However, it's easy enough to allow for realignment as needed, and thus avoid the developer having to think about the possiblilities for alignment requirements on all architectures. (E.g. on Linux/x86, a struct with an int64 member is 4-byte aligned, while on other 32-bit archs -- and even with other OSes on x86 -- it has 8-byte alignment. This sort of thing is irritating to have to manually deal with.) llvm-svn: 256533
Diffstat (limited to 'llvm/unittests/Support/TrailingObjectsTest.cpp')
-rw-r--r--llvm/unittests/Support/TrailingObjectsTest.cpp17
1 files changed, 17 insertions, 0 deletions
diff --git a/llvm/unittests/Support/TrailingObjectsTest.cpp b/llvm/unittests/Support/TrailingObjectsTest.cpp
index 4c05d665f98..866ff1e6e88 100644
--- a/llvm/unittests/Support/TrailingObjectsTest.cpp
+++ b/llvm/unittests/Support/TrailingObjectsTest.cpp
@@ -175,4 +175,21 @@ TEST(TrailingObjects, ThreeArg) {
reinterpret_cast<short *>(reinterpret_cast<double *>(C + 1) + 1) +
1));
}
+
+class Class4 final : public TrailingObjects<Class4, char, long> {
+ friend TrailingObjects;
+ size_t numTrailingObjects(OverloadToken<char>) const { return 1; }
+};
+
+TEST(TrailingObjects, Realignment) {
+ EXPECT_EQ((Class4::additionalSizeToAlloc<char, long>(1, 1)),
+ llvm::RoundUpToAlignment(sizeof(long) + 1, llvm::alignOf<long>()));
+ EXPECT_EQ(sizeof(Class4), llvm::RoundUpToAlignment(1, llvm::alignOf<long>()));
+ std::unique_ptr<char[]> P(new char[1000]);
+ Class4 *C = reinterpret_cast<Class4 *>(P.get());
+ EXPECT_EQ(C->getTrailingObjects<char>(), reinterpret_cast<char *>(C + 1));
+ EXPECT_EQ(C->getTrailingObjects<long>(),
+ reinterpret_cast<long *>(llvm::alignAddr(
+ reinterpret_cast<char *>(C + 1) + 1, llvm::alignOf<long>())));
+}
}
OpenPOWER on IntegriCloud