forked from boostorg/smart_ptr
Factor out alignment code into sp_align
This commit is contained in:
@@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
#include <boost/smart_ptr/detail/array_traits.hpp>
|
#include <boost/smart_ptr/detail/array_traits.hpp>
|
||||||
#include <boost/smart_ptr/detail/array_utility.hpp>
|
#include <boost/smart_ptr/detail/array_utility.hpp>
|
||||||
|
#include <boost/smart_ptr/detail/sp_align.hpp>
|
||||||
#include <boost/type_traits/alignment_of.hpp>
|
#include <boost/type_traits/alignment_of.hpp>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
@@ -139,19 +140,17 @@ namespace boost {
|
|||||||
enum {
|
enum {
|
||||||
M = boost::alignment_of<type>::value
|
M = boost::alignment_of<type>::value
|
||||||
};
|
};
|
||||||
std::size_t n1 = count * sizeof(value_type) + M - 1;
|
std::size_t n1 = count * sizeof(value_type);
|
||||||
std::size_t n2 = size * sizeof(type);
|
std::size_t n2 = size * sizeof(type) + M - 1;
|
||||||
CA ca(*this);
|
CA ca(*this);
|
||||||
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
|
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
|
||||||
void* p1 = CT::allocate(ca, n1 + n2, value);
|
void* p1 = CT::allocate(ca, n1 + n2, value);
|
||||||
#else
|
#else
|
||||||
void* p1 = ca.allocate(n1 + n2, value);
|
void* p1 = ca.allocate(n1 + n2, value);
|
||||||
#endif
|
#endif
|
||||||
char* p2 = static_cast<char*>(p1) + n1;
|
void* p2 = static_cast<char*>(p1) + n1;
|
||||||
while (std::size_t(p2) % M != 0) {
|
p2 = sp_align(M, p2);
|
||||||
p2--;
|
*data.result = static_cast<type*>(p2);
|
||||||
}
|
|
||||||
*data.result = reinterpret_cast<type*>(p2);
|
|
||||||
return static_cast<value_type*>(p1);
|
return static_cast<value_type*>(p1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -159,8 +158,8 @@ namespace boost {
|
|||||||
enum {
|
enum {
|
||||||
M = boost::alignment_of<type>::value
|
M = boost::alignment_of<type>::value
|
||||||
};
|
};
|
||||||
std::size_t n1 = count * sizeof(value_type) + M - 1;
|
std::size_t n1 = count * sizeof(value_type);
|
||||||
std::size_t n2 = size * sizeof(type);
|
std::size_t n2 = size * sizeof(type) + M - 1;
|
||||||
char* p1 = reinterpret_cast<char*>(memory);
|
char* p1 = reinterpret_cast<char*>(memory);
|
||||||
CA ca(*this);
|
CA ca(*this);
|
||||||
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
|
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
|
||||||
@@ -315,14 +314,12 @@ namespace boost {
|
|||||||
enum {
|
enum {
|
||||||
M = boost::alignment_of<type>::value
|
M = boost::alignment_of<type>::value
|
||||||
};
|
};
|
||||||
std::size_t n1 = count * sizeof(value_type) + M - 1;
|
std::size_t n1 = count * sizeof(value_type);
|
||||||
std::size_t n2 = size * sizeof(type);
|
std::size_t n2 = size * sizeof(type) + M - 1;
|
||||||
void* p1 = ::operator new(n1 + n2);
|
void* p1 = ::operator new(n1 + n2);
|
||||||
char* p2 = static_cast<char*>(p1) + n1;
|
void* p2 = static_cast<char*>(p1) + n1;
|
||||||
while (std::size_t(p2) % M != 0) {
|
p2 = sp_align(M, p2);
|
||||||
p2--;
|
*data.result = static_cast<type*>(p2);
|
||||||
}
|
|
||||||
*data.result = reinterpret_cast<type*>(p2);
|
|
||||||
return static_cast<value_type*>(p1);
|
return static_cast<value_type*>(p1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
37
include/boost/smart_ptr/detail/sp_align.hpp
Normal file
37
include/boost/smart_ptr/detail/sp_align.hpp
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2014 Glen Joseph Fernandes
|
||||||
|
* glenfe at live dot com
|
||||||
|
*
|
||||||
|
* Distributed under the Boost Software License,
|
||||||
|
* Version 1.0. (See accompanying file LICENSE_1_0.txt
|
||||||
|
* or copy at http://boost.org/LICENSE_1_0.txt)
|
||||||
|
*/
|
||||||
|
#ifndef BOOST_SMART_PTR_DETAIL_SP_ALIGN_HPP
|
||||||
|
#define BOOST_SMART_PTR_DETAIL_SP_ALIGN_HPP
|
||||||
|
|
||||||
|
#include <boost/config.hpp>
|
||||||
|
#if !defined(BOOST_NO_CXX11_STD_ALIGN)
|
||||||
|
#include <memory>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace boost {
|
||||||
|
namespace detail {
|
||||||
|
#if !defined(BOOST_NO_CXX11_STD_ALIGN)
|
||||||
|
inline void* sp_align(std::size_t alignment, void* ptr) {
|
||||||
|
std::size_t n1 = alignment - 1;
|
||||||
|
std::align(alignment, 0, ptr, n1);
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
inline void* sp_align(std::size_t alignment, void* ptr) {
|
||||||
|
std::size_t n1 = (std::size_t)ptr % alignment;
|
||||||
|
if (n1 != 0) {
|
||||||
|
ptr = (char*)ptr + alignment - n1;
|
||||||
|
}
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
Reference in New Issue
Block a user