Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 38 additions & 4 deletions include/boost/archive/detail/iserializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ namespace std{

#if ! BOOST_SERIALIZATION_DONT_USE_HAS_NEW_OPERATOR
#include <boost/type_traits/has_new_operator.hpp>
#include <boost/type_traits/integral_constant.hpp>
#endif

#include <boost/serialization/serialization.hpp>
Expand Down Expand Up @@ -213,14 +214,47 @@ BOOST_DLLEXPORT void iserializer<Archive, T>::load_object_data(

template<class T>
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<T *>(operator new(sizeof(T)));
return static_cast<T *>(allocate());
}
static void invoke_delete(T *t){
(operator delete(t));
deallocate(t);
}
#else
// note: we presume that a true value for has_new_operator
Expand All @@ -244,11 +278,11 @@ struct heap_allocation {
};
struct doesnt_have_new_operator {
static T* invoke_new() {
return static_cast<T *>(operator new(sizeof(T)));
return static_cast<T *>(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() {
Expand Down
1 change: 1 addition & 0 deletions test/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
Expand Down
139 changes: 139 additions & 0 deletions test/test_aligned_pointer.cpp
Original file line number Diff line number Diff line change
@@ -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 <cstddef> // NULL, std::size_t
#include <cstdio> // remove
#include <fstream>
#include <memory>
#include <vector>

#include <boost/config.hpp>
#if defined(BOOST_NO_STDC_NAMESPACE)
namespace std{
using ::remove;
}
#endif

#include <boost/serialization/nvp.hpp>
#include <boost/serialization/shared_ptr.hpp>

#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<class Archive>
void serialize(Archive & ar, const unsigned int /* file_version */){
ar & BOOST_SERIALIZATION_NVP(m_value);
}
};

template<class T>
bool is_aligned(const T * t){
return reinterpret_cast<std::size_t>(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<Aligned *> 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<Aligned *> loaded(count, static_cast<Aligned *>(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<std::shared_ptr<Aligned> > saved;
for(int i = 0; i < count; ++i)
saved.push_back(std::make_shared<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<std::shared_ptr<Aligned> > 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