diff --git a/include/boost/uuid/uuid.hpp b/include/boost/uuid/uuid.hpp index acaa58de..2e1d0b74 100644 --- a/include/boost/uuid/uuid.hpp +++ b/include/boost/uuid/uuid.hpp @@ -38,6 +38,16 @@ struct uuid // data + // Align uuid objects to 16 bytes on 64-bit platforms for performance. + // On most 32-bit platforms, default stack and dynamic memory alignment is lower than 16 + // (typically 4 or 8), which makes it problematic to require uuid alignment of 16. +#if (defined(__SIZEOF_POINTER__) && (__SIZEOF_POINTER__ >= 8)) || \ + defined(__x86_64__) /* This covers x86-x32 */ || \ + defined(_M_AMD64) || defined(_M_ARM64) + BOOST_ALIGNMENT(16) +#else + BOOST_ALIGNMENT(4) +#endif std::uint8_t data[ 16 ]; public: diff --git a/test/test_alignment.cpp b/test/test_alignment.cpp index 4a8a5838..9d9b8af3 100644 --- a/test/test_alignment.cpp +++ b/test/test_alignment.cpp @@ -22,8 +22,8 @@ struct X2 int main() { - BOOST_TEST_EQ( offsetof(X1, b), 1 ); - BOOST_TEST_EQ( offsetof(X2, b), 2 ); + BOOST_TEST_EQ( offsetof(X1, b) % alignof(uuid), 0 ); + BOOST_TEST_EQ( offsetof(X2, b) % alignof(uuid), 0 ); return boost::report_errors(); }