diff --git a/include/boost/archive/detail/iserializer.hpp b/include/boost/archive/detail/iserializer.hpp index a048d5a87..31d77ad5c 100644 --- a/include/boost/archive/detail/iserializer.hpp +++ b/include/boost/archive/detail/iserializer.hpp @@ -73,6 +73,7 @@ namespace std{ #if ! BOOST_SERIALIZATION_DONT_USE_HAS_NEW_OPERATOR #include +#include #endif #include @@ -213,14 +214,47 @@ BOOST_DLLEXPORT void iserializer::load_object_data( template struct heap_allocation { + // Raw storage for a T without a class specific operator new. Since C++17 + // a type aligned beyond __STDCPP_DEFAULT_NEW_ALIGNMENT__ has to be + // allocated with the aligned allocation functions, as new-expressions + // and delete-expressions do: ::operator new(std::size_t) only guarantees + // the default alignment, and the object is usually destroyed later by a + // delete-expression, which then calls the aligned operator delete. + #if defined(__cpp_aligned_new) + typedef boost::integral_constant< + bool, + (alignof(T) > __STDCPP_DEFAULT_NEW_ALIGNMENT__) + > is_over_aligned; + static void * allocate(boost::true_type) { + return operator new(sizeof(T), std::align_val_t(alignof(T))); + } + static void deallocate(void * p, boost::true_type) { + (operator delete)(p, std::align_val_t(alignof(T))); + } + #else + typedef boost::false_type is_over_aligned; + #endif + static void * allocate(boost::false_type) { + return operator new(sizeof(T)); + } + static void deallocate(void * p, boost::false_type) { + (operator delete)(p); + } + static void * allocate() { + return allocate(is_over_aligned()); + } + static void deallocate(void * p) { + deallocate(p, is_over_aligned()); + } + // boost::has_new_operator< T > doesn't work on these compilers #if BOOST_SERIALIZATION_DONT_USE_HAS_NEW_OPERATOR // This doesn't handle operator new overload for class T static T * invoke_new(){ - return static_cast(operator new(sizeof(T))); + return static_cast(allocate()); } static void invoke_delete(T *t){ - (operator delete(t)); + deallocate(t); } #else // note: we presume that a true value for has_new_operator @@ -244,11 +278,11 @@ struct heap_allocation { }; struct doesnt_have_new_operator { static T* invoke_new() { - return static_cast(operator new(sizeof(T))); + return static_cast(allocate()); } static void invoke_delete(T * t) { // Note: I'm reliance upon automatic conversion from T * to void * here - (operator delete)(t); + deallocate(t); } }; static T * invoke_new() { diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index b934441a0..bd72aa21a 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -66,6 +66,7 @@ lib dll_tracking ; test-suite "serialization" : + [ test-bsl-run_files test_aligned_pointer ] [ test-bsl-run_files test_array : A : : [ requires cxx11_hdr_array ] ] # BOOST_NO_CXX11_HDR_ARRAY [ test-bsl-run_files test_boost_array : A ] [ test-bsl-run_files test_native_array : A ] diff --git a/test/test_aligned_pointer.cpp b/test/test_aligned_pointer.cpp new file mode 100644 index 000000000..de3b68208 --- /dev/null +++ b/test/test_aligned_pointer.cpp @@ -0,0 +1,139 @@ +/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 +// test_aligned_pointer.cpp + +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// should pass compilation and execution + +// Objects of a type aligned beyond __STDCPP_DEFAULT_NEW_ALIGNMENT__ that are +// loaded through a pointer must be allocated with that alignment, as a C++17 +// new-expression would, and be destroyable with a delete-expression. + +#include // NULL, std::size_t +#include // remove +#include +#include +#include + +#include +#if defined(BOOST_NO_STDC_NAMESPACE) +namespace std{ + using ::remove; +} +#endif + +#include +#include + +#include "test_tools.hpp" + +#if defined(__cpp_aligned_new) + +struct alignas(64) Aligned { + int m_value; + explicit Aligned(int value = 0) : m_value(value) {} + template + void serialize(Archive & ar, const unsigned int /* file_version */){ + ar & BOOST_SERIALIZATION_NVP(m_value); + } +}; + +template +bool is_aligned(const T * t){ + return reinterpret_cast(t) % alignof(T) == 0; +} + +// Enough objects that a 64 byte aligned result can't happen by chance with +// an allocator that only guarantees the default alignment. +const int count = 100; + +int test_raw_pointers(){ + const char * testfile = boost::archive::tmpnam(NULL); + BOOST_REQUIRE(NULL != testfile); + + std::vector saved; + for(int i = 0; i < count; ++i) + saved.push_back(new Aligned(i)); + { + test_ostream os(testfile, TEST_STREAM_FLAGS); + test_oarchive oa(os, TEST_ARCHIVE_FLAGS); + for(int i = 0; i < count; ++i) + oa << boost::serialization::make_nvp("a", saved[i]); + } + std::vector loaded(count, static_cast(NULL)); + { + test_istream is(testfile, TEST_STREAM_FLAGS); + test_iarchive ia(is, TEST_ARCHIVE_FLAGS); + for(int i = 0; i < count; ++i) + ia >> boost::serialization::make_nvp("a", loaded[i]); + } + std::remove(testfile); + + int misaligned = 0; + for(int i = 0; i < count; ++i){ + BOOST_REQUIRE(NULL != loaded[i]); + if(! is_aligned(loaded[i])) + ++misaligned; + BOOST_CHECK_EQUAL(i, loaded[i]->m_value); + // a delete-expression calls the aligned operator delete + delete loaded[i]; + delete saved[i]; + } + BOOST_CHECK_EQUAL(0, misaligned); + return misaligned == 0 ? EXIT_SUCCESS : EXIT_FAILURE; +} + +int test_shared_pointers(){ + const char * testfile = boost::archive::tmpnam(NULL); + BOOST_REQUIRE(NULL != testfile); + + std::vector > saved; + for(int i = 0; i < count; ++i) + saved.push_back(std::make_shared(i)); + { + test_ostream os(testfile, TEST_STREAM_FLAGS); + test_oarchive oa(os, TEST_ARCHIVE_FLAGS); + for(int i = 0; i < count; ++i) + oa << boost::serialization::make_nvp("a", saved[i]); + } + std::vector > loaded(count); + { + test_istream is(testfile, TEST_STREAM_FLAGS); + test_iarchive ia(is, TEST_ARCHIVE_FLAGS); + for(int i = 0; i < count; ++i) + ia >> boost::serialization::make_nvp("a", loaded[i]); + } + std::remove(testfile); + + int misaligned = 0; + for(int i = 0; i < count; ++i){ + BOOST_REQUIRE(loaded[i]); + if(! is_aligned(loaded[i].get())) + ++misaligned; + BOOST_CHECK_EQUAL(i, loaded[i]->m_value); + } + BOOST_CHECK_EQUAL(0, misaligned); + return misaligned == 0 ? EXIT_SUCCESS : EXIT_FAILURE; +} + +int test_main( int /* argc */, char* /* argv */[] ){ + BOOST_STATIC_ASSERT(alignof(Aligned) > __STDCPP_DEFAULT_NEW_ALIGNMENT__); + if(EXIT_SUCCESS != test_raw_pointers()) + return EXIT_FAILURE; + if(EXIT_SUCCESS != test_shared_pointers()) + return EXIT_FAILURE; + return EXIT_SUCCESS; +} + +#else + +int test_main( int /* argc */, char* /* argv */[] ){ + // aligned allocation needs C++17 + return EXIT_SUCCESS; +} + +#endif + +// EOF