Factor out alignment code into sp_align

This commit is contained in:
Glen Fernandes
2014-02-16 11:02:37 -08:00
parent adc0cdddff
commit 5f1d4eae4f
2 changed files with 50 additions and 16 deletions

View File

@@ -11,6 +11,7 @@
#include <boost/smart_ptr/detail/array_traits.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 <algorithm>
@@ -139,19 +140,17 @@ namespace boost {
enum {
M = boost::alignment_of<type>::value
};
std::size_t n1 = count * sizeof(value_type) + M - 1;
std::size_t n2 = size * sizeof(type);
std::size_t n1 = count * sizeof(value_type);
std::size_t n2 = size * sizeof(type) + M - 1;
CA ca(*this);
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
void* p1 = CT::allocate(ca, n1 + n2, value);
#else
void* p1 = ca.allocate(n1 + n2, value);
#endif
char* p2 = static_cast<char*>(p1) + n1;
while (std::size_t(p2) % M != 0) {
p2--;
}
*data.result = reinterpret_cast<type*>(p2);
void* p2 = static_cast<char*>(p1) + n1;
p2 = sp_align(M, p2);
*data.result = static_cast<type*>(p2);
return static_cast<value_type*>(p1);
}
@@ -159,8 +158,8 @@ namespace boost {
enum {
M = boost::alignment_of<type>::value
};
std::size_t n1 = count * sizeof(value_type) + M - 1;
std::size_t n2 = size * sizeof(type);
std::size_t n1 = count * sizeof(value_type);
std::size_t n2 = size * sizeof(type) + M - 1;
char* p1 = reinterpret_cast<char*>(memory);
CA ca(*this);
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
@@ -315,14 +314,12 @@ namespace boost {
enum {
M = boost::alignment_of<type>::value
};
std::size_t n1 = count * sizeof(value_type) + M - 1;
std::size_t n2 = size * sizeof(type);
std::size_t n1 = count * sizeof(value_type);
std::size_t n2 = size * sizeof(type) + M - 1;
void* p1 = ::operator new(n1 + n2);
char* p2 = static_cast<char*>(p1) + n1;
while (std::size_t(p2) % M != 0) {
p2--;
}
*data.result = reinterpret_cast<type*>(p2);
void* p2 = static_cast<char*>(p1) + n1;
p2 = sp_align(M, p2);
*data.result = static_cast<type*>(p2);
return static_cast<value_type*>(p1);
}

View 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