Commit 9f705afd authored by Tudor Bosman's avatar Tudor Bosman

Properly align internal buffer in IOBuf.

Summary:
Yes, it's gcc specific, because alignas and max_align_t aren't in gcc
4.6.2.

Test Plan: iobuf_test, test added

Reviewed By: brianp@fb.com

FB internal diff: D491939
parent c838e280
...@@ -919,7 +919,7 @@ class IOBuf { ...@@ -919,7 +919,7 @@ class IOBuf {
}; };
struct InternalBuf { struct InternalBuf {
uint32_t capacity; uint32_t capacity;
uint8_t buf[]; uint8_t buf[] __attribute__((aligned));
}; };
// The maximum size for an IOBuf object, including any internal data buffer // The maximum size for an IOBuf object, including any internal data buffer
......
...@@ -599,6 +599,21 @@ TEST(IOBuf, takeOwnershipUniquePtr) { ...@@ -599,6 +599,21 @@ TEST(IOBuf, takeOwnershipUniquePtr) {
EXPECT_EQ(1, customDeleterCount); EXPECT_EQ(1, customDeleterCount);
} }
TEST(IOBuf, Alignment) {
// max_align_t doesn't exist in gcc 4.6.2
struct MaxAlign {
char c;
} __attribute__((aligned));
size_t alignment = alignof(MaxAlign);
std::vector<size_t> sizes {0, 1, 64, 256, 1024, 1 << 10};
for (size_t size : sizes) {
auto buf = IOBuf::create(size);
uintptr_t p = reinterpret_cast<uintptr_t>(buf->data());
EXPECT_EQ(0, p & (alignment - 1)) << "size=" << size;
}
}
int main(int argc, char** argv) { int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv); testing::InitGoogleTest(&argc, argv);
google::ParseCommandLineFlags(&argc, &argv, true); google::ParseCommandLineFlags(&argc, &argv, true);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment