Properly align internal buffer in IOBuf.
authorTudor Bosman <tudorb@fb.com>
Mon, 11 Jun 2012 23:18:22 +0000 (16:18 -0700)
committerTudor Bosman <tudorb@fb.com>
Tue, 12 Jun 2012 23:22:54 +0000 (16:22 -0700)
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

folly/experimental/io/IOBuf.h
folly/experimental/io/test/IOBufTest.cpp

index 784f9a7d46e57bb41124488738d34e05d5bfe839..03f2126f0a94c4b2b57c3c2d71da94fd79074f69 100644 (file)
@@ -919,7 +919,7 @@ class IOBuf {
   };
   struct InternalBuf {
     uint32_t capacity;
-    uint8_t buf[];
+    uint8_t buf[] __attribute__((aligned));
   };
 
   // The maximum size for an IOBuf object, including any internal data buffer
index b800e8b9d9696c5f270feb381b294dc4ac2e3262..5072810fc550882f9d5b5708646e8853e689e002 100644 (file)
@@ -599,6 +599,21 @@ TEST(IOBuf, takeOwnershipUniquePtr) {
   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) {
   testing::InitGoogleTest(&argc, argv);
   google::ParseCommandLineFlags(&argc, &argv, true);